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 = 0 }) => { 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 { setState(present); timeout.current = setTimeout(async () => { try { await action(); setState(past); } catch (e) { console.error(e); } }, countdown); } } 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 (
{(state === 'rejecting') && (
)}
{(state === 'authorizing') && (
)}
); } }; export { AuthorizeRejectButtons };