import { useState } from 'react'; import { RefreshCwIcon } from 'lucide-react'; import { renewSiteSignIn } from '../../service/auth/auth.service'; import { Button } from '../ui/button'; import { Callout } from '../ui/callout'; /** Where a reconnect stands: not tried, running, or tried without a new token. */ type ReconnectState = 'idle' | 'working' | 'failed'; /** Said while the site's token is refused and Reconnect has not been pressed. */ const REJECTED_TEXT = 'Recomaze turned down this site’s sign-in, so your account cannot load. Reconnect to sign the site in again.'; /** Said when signing the site in again did not give it a new token. */ const FAILED_TEXT = 'Reconnecting did not work. Write to support@recomaze.ai with this site’s address and we will connect it for you.'; /** * Said above every page when the Recomaze API refused this site's token, in * place of a page that never loads: Reconnect signs the site in again and * reloads the page with the new token. * * @param {object} props * @param {boolean} props.isRejected - Whether the last /me was refused for its token. * @returns {JSX.Element | null} The notice, or nothing while the token is accepted. */ export const SessionNotice = ({ isRejected, }: { isRejected: boolean; }): JSX.Element | null => { const [reconnectState, setReconnectState] = useState('idle'); if (!isRejected) return null; const isWorking: boolean = reconnectState === 'working'; const text: string = reconnectState === 'failed' ? FAILED_TEXT : REJECTED_TEXT; const iconClass: string | undefined = isWorking ? 'animate-spin' : undefined; const reconnect = async (): Promise => { setReconnectState('working'); const answer = await renewSiteSignIn().catch(() => null); if (answer?.renewed) { window.location.reload(); return; } setReconnectState('failed'); }; return (

{text}

); };