| 1 | // This file is part of HFS - Copyright 2021-2023, Massimo Melina <a@rejetto.com> - License https://www.gnu.org/licenses/gpl-3.0.txt |
| 2 | |
| 3 | import { createElement as h, Fragment } from "react" |
| 4 | import { Alert, Box } from '@mui/material' |
| 5 | import { apiCall, useApiEx } from './api' |
| 6 | import { alertDialog } from "./dialog" |
| 7 | import { useSnapState } from './state' |
| 8 | import { HTTP_UNAUTHORIZED } from './misc' |
| 9 | import { Logout, PowerSettingsNew } from '@mui/icons-material' |
| 10 | import { Btn } from './mui' |
| 11 | |
| 12 | export default function LogoutPage() { |
| 13 | const { element } = useApiEx('get_config', { only: [] }) // sort of noop, just to get the 'element' part |
| 14 | const { username } = useSnapState() |
| 15 | if (element) |
| 16 | return element |
| 17 | return h(Box, { sx: { display: 'flex', flexDirection:'column', alignItems: 'flex-start', gap: 2 } }, |
| 18 | !username ? h(Alert, { severity: 'info' }, "You are not logged in, because authentication is not required on localhost") |
| 19 | : h(Fragment, {}, |
| 20 | "You are logged in as: " + username, |
| 21 | h(Btn, { |
| 22 | icon: Logout, |
| 23 | size: 'large', |
| 24 | onClick: () => apiCall('logout').catch(err => // we expect 401 |
| 25 | err.code !== HTTP_UNAUTHORIZED && alertDialog(err)) |
| 26 | }, "I want to logout") |
| 27 | ), |
| 28 | h(Btn, { |
| 29 | icon: PowerSettingsNew, |
| 30 | size: 'large', |
| 31 | color: 'warning', |
| 32 | confirm: "Stopping the server, this interface won't respond anymore", |
| 33 | async onClick() { |
| 34 | await apiCall('quit') |
| 35 | await alertDialog("Good-bye", 'success') |
| 36 | location.reload() |
| 37 | }, |
| 38 | }, "Quit HFS") |
| 39 | ) |
| 40 | } |