import React, { useState } from 'react' import { useAccess } from './useAccess.hook' import classes from './Access.module.css' import { Select, Space, Title, Image, Button, Modal, PasswordInput, TextInput, PinInput } from '@mantine/core' import { useDisclosure } from '@mantine/hooks' import left from '../images/login.png' import { IconLock, IconUser, IconUsers, IconSettings, IconServer, IconKey } from '@tabler/icons-react' import { modals } from '@mantine/modals' export function Access() { const { state, actions } = useAccess() const [urlOpened, { open: urlOpen, close: urlClose }] = useDisclosure(false) const [otpOpened, { open: otpOpen, close: otpClose }] = useDisclosure(false) const [disabled, setDisabled] = useState(false) const login = async () => { if (!state.loading) { actions.setLoading(true) try { if (state.mode === 'account') { await actions.accountLogin() } else if (state.mode === 'create') { await actions.accountCreate() } else if (state.mode === 'reset') { await actions.accountAccess() } else if (state.mode === 'admin') { await actions.adminLogin() } otpClose() } catch (err) { const { message } = err as { message: string } console.log(message) if (message === '405' || message === '403' || message === '429') { if (message === '429') { setDisabled(true) } else { setDisabled(false) } otpOpen() } else { modals.openConfirmModal({ title: state.strings.operationFailed, withCloseButton: true, overlayProps: { backgroundOpacity: 0.55, blur: 3, }, children:
{state.strings.tryAgain}
, cancelProps: { display: 'none' }, confirmProps: { display: 'none' }, }) } } actions.setLoading(false) } } return (
{state.layout === 'large' && (
)} {state.layout != null && (
{state.mode !== 'admin' && } placeholder={state.strings.username} onChange={(event) => actions.setUsername(event.currentTarget.value)} /> } placeholder={state.strings.password} onChange={(event) => actions.setPassword(event.currentTarget.value)} onKeyDown={(ev) => { if (ev.code === 'Enter' && state.password && state.username) login() }} /> )} {state.mode === 'reset' && ( <> {state.strings.accessAccount} } placeholder={state.strings.accessCode} onChange={(event) => actions.setToken(event.currentTarget.value)} /> )} {state.mode === 'create' && ( <> {state.strings.createAccount} } placeholder={state.strings.accessCode} onChange={(event) => actions.setToken(event.currentTarget.value)} /> } rightSection={state.taken ? : null} placeholder={state.strings.username} onChange={(event) => actions.setUsername(event.currentTarget.value)} error={state.taken ? true : false} /> } placeholder={state.strings.password} onChange={(event) => actions.setPassword(event.currentTarget.value)} /> } placeholder={state.strings.confirmPassword} onChange={(event) => actions.setConfirm(event.currentTarget.value)} /> )} {state.mode === 'admin' && ( <> {state.strings.admin} } placeholder={state.strings.password} onChange={(event) => actions.setPassword(event.currentTarget.value)} /> )}
actions.setLanguage(language as string)} />
)} } placeholder={state.strings.host} value={state.host} onKeyDown={(ev) => { if (ev.code === 'Enter') urlClose() }} onChange={(event) => actions.setNode(event.currentTarget.value)} />
{state.strings.mfaTitle}
{state.strings.mfaEnter}
actions.setCode(event)} />
{disabled ? state.strings.mfaDisabled : ''}
) }