Merge branch 'add-animation-to-auth-rej-buttons' into 'develop'

Add animation to AuthorizeRejectButton comp

See merge request soapbox-pub/soapbox!2396
This commit is contained in:
Chewbacca 2023-04-03 18:07:06 +00:00
commit f5ec14e9ba

View file

@ -14,6 +14,23 @@ interface IAuthorizeRejectButtons {
const AuthorizeRejectButtons: React.FC<IAuthorizeRejectButtons> = ({ onAuthorize, onReject, countdown }) => { const AuthorizeRejectButtons: React.FC<IAuthorizeRejectButtons> = ({ onAuthorize, onReject, countdown }) => {
const [state, setState] = useState<'authorizing' | 'rejecting' | 'authorized' | 'rejected' | 'pending'>('pending'); const [state, setState] = useState<'authorizing' | 'rejecting' | 'authorized' | 'rejected' | 'pending'>('pending');
const timeout = useRef<NodeJS.Timeout>(); const timeout = useRef<NodeJS.Timeout>();
const interval = useRef<ReturnType<typeof setInterval>>();
const [progress, setProgress] = useState<number>(0);
const startProgressInterval = () => {
let startValue = 1;
interval.current = setInterval(() => {
startValue++;
const newValue = startValue * 3.6; // get to 360 (deg)
setProgress(newValue);
if (newValue >= 360) {
clearInterval(interval.current as NodeJS.Timeout);
setProgress(0);
}
}, (countdown as number) / 100);
};
function handleAction( function handleAction(
present: 'authorizing' | 'rejecting', present: 'authorizing' | 'rejecting',
@ -21,6 +38,9 @@ const AuthorizeRejectButtons: React.FC<IAuthorizeRejectButtons> = ({ onAuthorize
action: () => Promise<unknown> | unknown, action: () => Promise<unknown> | unknown,
): void { ): void {
if (state === present) { if (state === present) {
if (interval.current) {
clearInterval(interval.current);
}
if (timeout.current) { if (timeout.current) {
clearTimeout(timeout.current); clearTimeout(timeout.current);
} }
@ -37,6 +57,7 @@ const AuthorizeRejectButtons: React.FC<IAuthorizeRejectButtons> = ({ onAuthorize
if (typeof countdown === 'number') { if (typeof countdown === 'number') {
setState(present); setState(present);
timeout.current = setTimeout(doAction, countdown); timeout.current = setTimeout(doAction, countdown);
startProgressInterval();
} else { } else {
doAction(); doAction();
} }
@ -46,11 +67,28 @@ const AuthorizeRejectButtons: React.FC<IAuthorizeRejectButtons> = ({ onAuthorize
const handleAuthorize = async () => handleAction('authorizing', 'authorized', onAuthorize); const handleAuthorize = async () => handleAction('authorizing', 'authorized', onAuthorize);
const handleReject = async () => handleAction('rejecting', 'rejected', onReject); const handleReject = async () => handleAction('rejecting', 'rejected', onReject);
const renderStyle = (selectedState: typeof state) => {
if (state === 'authorizing' && selectedState === 'authorizing') {
return {
background: `conic-gradient(rgb(var(--color-primary-500)) ${progress}deg, rgb(var(--color-primary-500) / 0.1) 0deg)`,
};
} else if (state === 'rejecting' && selectedState === 'rejecting') {
return {
background: `conic-gradient(rgb(var(--color-danger-600)) ${progress}deg, rgb(var(--color-danger-600) / 0.1) 0deg)`,
};
}
return {};
};
useEffect(() => { useEffect(() => {
return () => { return () => {
if (timeout.current) { if (timeout.current) {
clearTimeout(timeout.current); clearTimeout(timeout.current);
} }
if (interval.current) {
clearInterval(interval.current);
}
}; };
}, []); }, []);
@ -72,6 +110,7 @@ const AuthorizeRejectButtons: React.FC<IAuthorizeRejectButtons> = ({ onAuthorize
action={handleReject} action={handleReject}
isLoading={state === 'rejecting'} isLoading={state === 'rejecting'}
disabled={state === 'authorizing'} disabled={state === 'authorizing'}
style={renderStyle('rejecting')}
/> />
<AuthorizeRejectButton <AuthorizeRejectButton
theme='primary' theme='primary'
@ -79,6 +118,7 @@ const AuthorizeRejectButtons: React.FC<IAuthorizeRejectButtons> = ({ onAuthorize
action={handleAuthorize} action={handleAuthorize}
isLoading={state === 'authorizing'} isLoading={state === 'authorizing'}
disabled={state === 'rejecting'} disabled={state === 'rejecting'}
style={renderStyle('authorizing')}
/> />
</HStack> </HStack>
); );
@ -105,33 +145,34 @@ interface IAuthorizeRejectButton {
action(): void action(): void
isLoading?: boolean isLoading?: boolean
disabled?: boolean disabled?: boolean
style: React.CSSProperties
} }
const AuthorizeRejectButton: React.FC<IAuthorizeRejectButton> = ({ theme, icon, action, isLoading, disabled }) => { const AuthorizeRejectButton: React.FC<IAuthorizeRejectButton> = ({ theme, icon, action, isLoading, style, disabled }) => {
return ( return (
<div className='relative'> <div className='relative'>
<IconButton <div
src={isLoading ? require('@tabler/icons/player-stop-filled.svg') : icon} style={style}
onClick={action} className={
theme='seamless' clsx({
className={clsx('h-10 w-10 items-center justify-center border-2', { 'flex h-11 w-11 items-center justify-center rounded-full': true,
'border-primary-500/10 hover:border-primary-500': theme === 'primary', 'bg-danger-600/10': theme === 'danger',
'border-danger-600/10 hover:border-danger-600': theme === 'danger', 'bg-primary-500/10': theme === 'primary',
})} })
iconClassName={clsx('h-6 w-6', { }
'text-primary-500': theme === 'primary', >
'text-danger-600': theme === 'danger', <IconButton
})} src={isLoading ? require('@tabler/icons/player-stop-filled.svg') : icon}
disabled={disabled} onClick={action}
/> theme='seamless'
{(isLoading) && ( className='h-10 w-10 items-center justify-center bg-gray-900'
<div iconClassName={clsx('h-6 w-6', {
className={clsx('pointer-events-none absolute inset-0 h-10 w-10 animate-spin rounded-full border-2 border-transparent', { 'text-primary-500': theme === 'primary',
'border-t-primary-500': theme === 'primary', 'text-danger-600': theme === 'danger',
'border-t-danger-600': theme === 'danger',
})} })}
disabled={disabled}
/> />
)} </div>
</div> </div>
); );
}; };