2023-03-27 14:59:55 -07:00
|
|
|
import clsx from 'clsx';
|
2023-03-27 13:31:13 -07:00
|
|
|
import React, { useEffect, useRef, useState } from 'react';
|
2023-03-20 16:49:41 -07:00
|
|
|
import { FormattedMessage } from 'react-intl';
|
2023-03-20 15:45:52 -07:00
|
|
|
|
2023-03-20 16:49:41 -07:00
|
|
|
import { HStack, IconButton, Text } from 'soapbox/components/ui';
|
2023-03-20 15:45:52 -07:00
|
|
|
|
|
|
|
interface IAuthorizeRejectButtons {
|
2023-03-20 17:22:00 -07:00
|
|
|
onAuthorize(): Promise<unknown> | unknown
|
|
|
|
onReject(): Promise<unknown> | unknown
|
2023-03-27 13:31:13 -07:00
|
|
|
countdown?: number
|
2023-03-20 15:45:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Buttons to approve or reject a pending item, usually an account. */
|
2023-03-27 14:59:55 -07:00
|
|
|
const AuthorizeRejectButtons: React.FC<IAuthorizeRejectButtons> = ({ onAuthorize, onReject, countdown }) => {
|
2023-03-27 13:31:13 -07:00
|
|
|
const [state, setState] = useState<'authorizing' | 'rejecting' | 'authorized' | 'rejected' | 'pending'>('pending');
|
|
|
|
const timeout = useRef<NodeJS.Timeout>();
|
2023-03-20 15:45:52 -07:00
|
|
|
|
2023-03-27 13:36:55 -07:00
|
|
|
function handleAction(
|
|
|
|
present: 'authorizing' | 'rejecting',
|
|
|
|
past: 'authorized' | 'rejected',
|
|
|
|
action: () => Promise<unknown> | unknown,
|
|
|
|
): void {
|
2023-03-27 13:31:13 -07:00
|
|
|
if (state === present) {
|
|
|
|
if (timeout.current) {
|
|
|
|
clearTimeout(timeout.current);
|
|
|
|
}
|
|
|
|
setState('pending');
|
|
|
|
} else {
|
2023-03-27 15:08:24 -07:00
|
|
|
const doAction = async () => {
|
2023-03-27 13:31:13 -07:00
|
|
|
try {
|
2023-03-27 13:36:55 -07:00
|
|
|
await action();
|
2023-03-27 13:31:13 -07:00
|
|
|
setState(past);
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
2023-03-27 15:08:24 -07:00
|
|
|
};
|
|
|
|
if (typeof countdown === 'number') {
|
|
|
|
setState(present);
|
|
|
|
timeout.current = setTimeout(doAction, countdown);
|
|
|
|
} else {
|
|
|
|
doAction();
|
|
|
|
}
|
2023-03-20 17:22:00 -07:00
|
|
|
}
|
2023-03-20 15:45:52 -07:00
|
|
|
}
|
|
|
|
|
2023-03-27 13:36:55 -07:00
|
|
|
const handleAuthorize = async () => handleAction('authorizing', 'authorized', onAuthorize);
|
|
|
|
const handleReject = async () => handleAction('rejecting', 'rejected', onReject);
|
2023-03-27 13:31:13 -07:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
return () => {
|
|
|
|
if (timeout.current) {
|
|
|
|
clearTimeout(timeout.current);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}, []);
|
2023-03-20 15:45:52 -07:00
|
|
|
|
|
|
|
switch (state) {
|
2023-03-27 13:31:13 -07:00
|
|
|
case 'authorized':
|
|
|
|
return (
|
2023-03-27 15:03:19 -07:00
|
|
|
<ActionEmblem text={<FormattedMessage id='authorize.success' defaultMessage='Approved' />} />
|
2023-03-27 13:31:13 -07:00
|
|
|
);
|
|
|
|
case 'rejected':
|
|
|
|
return (
|
2023-03-27 15:03:19 -07:00
|
|
|
<ActionEmblem text={<FormattedMessage id='reject.success' defaultMessage='Rejected' />} />
|
2023-03-27 13:31:13 -07:00
|
|
|
);
|
|
|
|
default:
|
2023-03-20 15:45:52 -07:00
|
|
|
return (
|
2023-03-20 16:26:27 -07:00
|
|
|
<HStack space={3} alignItems='center'>
|
2023-03-27 14:59:55 -07:00
|
|
|
<AuthorizeRejectButton
|
|
|
|
theme='danger'
|
|
|
|
icon={require('@tabler/icons/x.svg')}
|
|
|
|
action={handleReject}
|
|
|
|
isLoading={state === 'rejecting'}
|
|
|
|
disabled={state === 'authorizing'}
|
|
|
|
/>
|
|
|
|
<AuthorizeRejectButton
|
|
|
|
theme='primary'
|
|
|
|
icon={require('@tabler/icons/check.svg')}
|
|
|
|
action={handleAuthorize}
|
|
|
|
isLoading={state === 'authorizing'}
|
|
|
|
disabled={state === 'rejecting'}
|
|
|
|
/>
|
2023-03-20 15:45:52 -07:00
|
|
|
</HStack>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-03-27 15:03:19 -07:00
|
|
|
interface IActionEmblem {
|
|
|
|
text: React.ReactNode
|
|
|
|
}
|
|
|
|
|
|
|
|
const ActionEmblem: React.FC<IActionEmblem> = ({ text }) => {
|
|
|
|
return (
|
|
|
|
<div className='rounded-full bg-gray-100 px-4 py-2 dark:bg-gray-800'>
|
|
|
|
<Text theme='muted' size='sm'>
|
|
|
|
{text}
|
|
|
|
</Text>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-03-27 14:59:55 -07:00
|
|
|
interface IAuthorizeRejectButton {
|
|
|
|
theme: 'primary' | 'danger'
|
|
|
|
icon: string
|
|
|
|
action(): void
|
|
|
|
isLoading?: boolean
|
|
|
|
disabled?: boolean
|
|
|
|
}
|
|
|
|
|
|
|
|
const AuthorizeRejectButton: React.FC<IAuthorizeRejectButton> = ({ theme, icon, action, isLoading, disabled }) => {
|
|
|
|
return (
|
|
|
|
<div className='relative'>
|
|
|
|
<IconButton
|
|
|
|
src={isLoading ? require('@tabler/icons/player-stop-filled.svg') : icon}
|
|
|
|
onClick={action}
|
|
|
|
theme='seamless'
|
|
|
|
className={clsx('h-10 w-10 items-center justify-center border-2', {
|
|
|
|
'border-primary-500/10 hover:border-primary-500': theme === 'primary',
|
|
|
|
'border-danger-600/10 hover:border-danger-600': theme === 'danger',
|
|
|
|
})}
|
|
|
|
iconClassName={clsx('h-6 w-6', {
|
|
|
|
'text-primary-500': theme === 'primary',
|
|
|
|
'text-danger-600': theme === 'danger',
|
|
|
|
})}
|
|
|
|
disabled={disabled}
|
|
|
|
/>
|
|
|
|
{(isLoading) && (
|
|
|
|
<div
|
|
|
|
className={clsx('pointer-events-none absolute inset-0 h-10 w-10 animate-spin rounded-full border-2 border-transparent', {
|
|
|
|
'border-t-primary-500': theme === 'primary',
|
|
|
|
'border-t-danger-600': theme === 'danger',
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-03-20 15:45:52 -07:00
|
|
|
export { AuthorizeRejectButtons };
|