import clsx from 'clsx'; import React, { useEffect, useRef, useState } from 'react'; import { FormattedMessage } from 'react-intl'; import { HStack, IconButton, Text } from 'soapbox/components/ui'; interface IAuthorizeRejectButtons { onAuthorize(): Promise | unknown onReject(): Promise | unknown countdown?: number } /** Buttons to approve or reject a pending item, usually an account. */ const AuthorizeRejectButtons: React.FC = ({ onAuthorize, onReject, countdown }) => { const [state, setState] = useState<'authorizing' | 'rejecting' | 'authorized' | 'rejected' | 'pending'>('pending'); const timeout = useRef(); function handleAction( present: 'authorizing' | 'rejecting', past: 'authorized' | 'rejected', action: () => Promise | unknown, ): void { if (state === present) { if (timeout.current) { clearTimeout(timeout.current); } setState('pending'); } else { const doAction = async () => { try { await action(); setState(past); } catch (e) { console.error(e); } }; if (typeof countdown === 'number') { setState(present); timeout.current = setTimeout(doAction, countdown); } else { doAction(); } } } const handleAuthorize = async () => handleAction('authorizing', 'authorized', onAuthorize); const handleReject = async () => handleAction('rejecting', 'rejected', onReject); useEffect(() => { return () => { if (timeout.current) { clearTimeout(timeout.current); } }; }, []); switch (state) { case 'authorized': return ( } /> ); case 'rejected': return ( } /> ); default: return ( ); } }; interface IActionEmblem { text: React.ReactNode } const ActionEmblem: React.FC = ({ text }) => { return (
{text}
); }; interface IAuthorizeRejectButton { theme: 'primary' | 'danger' icon: string action(): void isLoading?: boolean disabled?: boolean } const AuthorizeRejectButton: React.FC = ({ theme, icon, action, isLoading, disabled }) => { return (
{(isLoading) && (
)}
); }; export { AuthorizeRejectButtons };