|
| 1 | +/* |
| 2 | + * Project: Frieren Framework |
| 3 | + * Copyright (C) 2026 DSR! <xchwarze@gmail.com> |
| 4 | + * SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0 |
| 5 | + * More info at: https://github.com/xchwarze/frieren |
| 6 | + */ |
| 7 | +import { useState, useEffect, useCallback } from 'react'; |
| 8 | +import { Modal, Spinner } from 'react-bootstrap'; |
| 9 | +import { useQueryClient } from '@tanstack/react-query'; |
| 10 | +import { useSetAtom } from 'jotai'; |
| 11 | +import { useLocation } from 'wouter'; |
| 12 | +import PropTypes from 'prop-types'; |
| 13 | + |
| 14 | +import authAtom from '@src/atoms/authAtom.js'; |
| 15 | +import Icon from '@src/components/Icon'; |
| 16 | +import { fetchPost } from '@src/services/fetchService.js'; |
| 17 | + |
| 18 | +const REBOOT_POLL_INTERVAL = 20000; |
| 19 | +const SHUTDOWN_DISCONNECT_DELAY = 30000; |
| 20 | + |
| 21 | +/** |
| 22 | + * Checks whether a fetch error indicates the device is unreachable (network failure or timeout). |
| 23 | + * |
| 24 | + * @param {Error} error - The error thrown by fetchPost. |
| 25 | + * @return {boolean} True if the device could not be reached at all. |
| 26 | + */ |
| 27 | +const isDeviceUnreachable = (error) => |
| 28 | + error instanceof TypeError || error.message === 'Request timed out'; |
| 29 | + |
| 30 | +/** |
| 31 | + * Modal that displays device status during restart or shutdown operations. |
| 32 | + * On restart: polls the server every 20 seconds and redirects to login when the device responds. |
| 33 | + * On shutdown: shows a progress message, then a disconnect prompt after 30 seconds. |
| 34 | + * |
| 35 | + * @param {string|null} action - Current action: 'restart', 'shutdown', or null. |
| 36 | + * @returns {ReactElement|null} The status modal or null when no action is active. |
| 37 | + */ |
| 38 | +const SystemStatusModal = ({ action }) => { |
| 39 | + const setAuth = useSetAtom(authAtom); |
| 40 | + const [, setLocation] = useLocation(); |
| 41 | + const queryClient = useQueryClient(); |
| 42 | + const [canDisconnect, setCanDisconnect] = useState(false); |
| 43 | + |
| 44 | + const redirectToLogin = useCallback(() => { |
| 45 | + setAuth(false); |
| 46 | + setLocation('/'); |
| 47 | + queryClient.clear(); |
| 48 | + }, [setAuth, setLocation, queryClient]); |
| 49 | + |
| 50 | + useEffect(() => { |
| 51 | + if (action !== 'restart') return; |
| 52 | + |
| 53 | + let timeoutId; |
| 54 | + let cancelled = false; |
| 55 | + |
| 56 | + const poll = async () => { |
| 57 | + try { |
| 58 | + await fetchPost({ module: 'header', action: 'serverPing' }); |
| 59 | + if (!cancelled) redirectToLogin(); |
| 60 | + } catch (error) { |
| 61 | + if (!cancelled && !isDeviceUnreachable(error)) { |
| 62 | + redirectToLogin(); |
| 63 | + return; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + if (!cancelled) { |
| 68 | + timeoutId = setTimeout(poll, REBOOT_POLL_INTERVAL); |
| 69 | + } |
| 70 | + }; |
| 71 | + |
| 72 | + timeoutId = setTimeout(poll, REBOOT_POLL_INTERVAL); |
| 73 | + |
| 74 | + return () => { |
| 75 | + cancelled = true; |
| 76 | + clearTimeout(timeoutId); |
| 77 | + }; |
| 78 | + }, [action, redirectToLogin]); |
| 79 | + |
| 80 | + useEffect(() => { |
| 81 | + if (action !== 'shutdown') return; |
| 82 | + |
| 83 | + const timeoutId = setTimeout(() => { |
| 84 | + setCanDisconnect(true); |
| 85 | + }, SHUTDOWN_DISCONNECT_DELAY); |
| 86 | + |
| 87 | + return () => clearTimeout(timeoutId); |
| 88 | + }, [action]); |
| 89 | + |
| 90 | + if (!action) { |
| 91 | + return null; |
| 92 | + } |
| 93 | + |
| 94 | + if (action === 'restart') { |
| 95 | + return ( |
| 96 | + <Modal show backdrop={'static'} keyboard={false} centered> |
| 97 | + <Modal.Body className={'text-center py-5'}> |
| 98 | + <Spinner animation={'border'} className={'mb-3'} /> |
| 99 | + <h5>Restarting device...</h5> |
| 100 | + <p className={'text-muted mb-0'}>Checking connectivity every 20 seconds</p> |
| 101 | + </Modal.Body> |
| 102 | + </Modal> |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + return ( |
| 107 | + <Modal show backdrop={'static'} keyboard={false} centered> |
| 108 | + <Modal.Body className={'text-center py-5'}> |
| 109 | + {canDisconnect ? ( |
| 110 | + <> |
| 111 | + <div className={'mb-3'}> |
| 112 | + <Icon name={'check-circle'} style={{ fontSize: '2.5rem' }} /> |
| 113 | + </div> |
| 114 | + <h5>You can now disconnect the device</h5> |
| 115 | + </> |
| 116 | + ) : ( |
| 117 | + <> |
| 118 | + <Spinner animation={'border'} className={'mb-3'} /> |
| 119 | + <h5>Shutting down device...</h5> |
| 120 | + </> |
| 121 | + )} |
| 122 | + </Modal.Body> |
| 123 | + </Modal> |
| 124 | + ); |
| 125 | +}; |
| 126 | + |
| 127 | +SystemStatusModal.propTypes = { |
| 128 | + action: PropTypes.oneOf(['restart', 'shutdown']), |
| 129 | +}; |
| 130 | + |
| 131 | +export default SystemStatusModal; |
0 commit comments