import React, { useState } from 'react'; import { faCircleExclamation } from '@fortawesome/free-solid-svg-icons/faCircleExclamation'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { Alert, Anchor, Center, Container, Divider, LoadingOverlay, Modal, Stack, Title } from '@mantine/core'; import { useAsync } from '../../hooks/useAsync'; import { LoginUtils } from '../../security/LoginUtils'; import { userSession } from '../../security/UserSession'; import { SessionWatcher } from '../../security/watcher'; import { useVisynAppContext } from '../VisynAppContext'; import { DefaultLoginForm, UserStoreUIMap } from './UserStoreUIMap'; export function VisynLoginMenu({ watch = false }: { watch?: boolean }) { const { appName, user } = useVisynAppContext(); const [show, setShow] = useState(false); const [error, setError] = useState(null); React.useEffect(() => { if (watch) { const watcher = SessionWatcher.startWatching(LoginUtils.logout); return () => { watcher?.stop(); }; } return undefined; }, [watch]); React.useEffect(() => { if (user) { setShow(false); } else { const loginTimeout = setTimeout(() => { // Check if we are now logged in, if not, show the dialog. LoginUtils.loggedInAs() .then((u) => { userSession.login(u); setShow(false); }) .catch(() => { setShow(true); }); }, 500); return () => { clearTimeout(loginTimeout); }; } return undefined; }, [user]); const { value: userStores, error: userStoreError, status: userStoreStatus, execute: retryGetStores } = useAsync(LoginUtils.getStores, []); const userStoresWithUI = userStores?.filter((store) => store.ui); const hasError = error && error !== 'not_reachable'; const isOffline = error === 'not_reachable' || userStoreStatus === 'error'; return ( null} title={null} data-testid="visyn-login-modal">
Welcome to {appName}
{isOffline ? ( } color="yellow" radius="md"> The server seems to be offline!{' '} { setError(null); retryGetStores(); }} size="sm" > Try again . ) : null} {hasError ? ( } color="red" radius="md"> {error} ) : null} {userStoreStatus === 'pending' ? : null} {!userStores || !userStoresWithUI || isOffline ? null : userStoresWithUI.length === 0 ? ( // Use the dummy store as default if no store is found ) : ( // Render all stores next to eachother userStoresWithUI.map((store, i, all) => { const ToRender = UserStoreUIMap.get(store.ui!); return ( {ToRender ? ( ) : ( No {store.ui} found for {store.id}. Contact the site administrator if this issue perists. )} {ToRender && i !== all.length - 1 ? : null} ); }) )}
); }