From 88b45fd9520bd112f8add654eb50466826655e56 Mon Sep 17 00:00:00 2001 From: Carlos Valente <34649812+cpvalente@users.noreply.github.com> Date: Tue, 16 Apr 2024 21:21:01 +0200 Subject: [PATCH] fix: shutdown from menu (#893) --- apps/client/src/App.tsx | 26 ----- .../common/components/buttons/QuitIconBtn.tsx | 96 ------------------- .../panel/shutdown-panel/ShutdownPanel.tsx | 36 ++++++- apps/client/src/features/editors/Editor.tsx | 9 ++ 4 files changed, 43 insertions(+), 124 deletions(-) delete mode 100644 apps/client/src/common/components/buttons/QuitIconBtn.tsx diff --git a/apps/client/src/App.tsx b/apps/client/src/App.tsx index e72d6f4ff..710efd119 100644 --- a/apps/client/src/App.tsx +++ b/apps/client/src/App.tsx @@ -1,4 +1,3 @@ -import { useEffect } from 'react'; import { BrowserRouter } from 'react-router-dom'; import { ChakraProvider } from '@chakra-ui/react'; import { QueryClientProvider } from '@tanstack/react-query'; @@ -7,7 +6,6 @@ import { ReactQueryDevtools } from '@tanstack/react-query-devtools'; import { ContextMenu } from './common/components/context-menu/ContextMenu'; import ErrorBoundary from './common/components/error-boundary/ErrorBoundary'; import { AppContextProvider } from './common/context/AppContext'; -import useElectronEvent from './common/hooks/useElectronEvent'; import { ontimeQueryClient } from './common/queryClient'; import { socketClientName } from './common/stores/connectionName'; import { connectSocket } from './common/utils/socket'; @@ -23,30 +21,6 @@ const preferredClientName = socketClientName.getState().name; connectSocket(preferredClientName); function App() { - const { isElectron, sendToElectron } = useElectronEvent(); - - useEffect(() => { - const handleKeyPress = (event: KeyboardEvent) => { - // handle held key - if (event.repeat) return; - // check if the alt key is pressed - if (event.altKey) { - if (event.code === 'KeyT') { - // ask to see debug - sendToElectron('set-window', 'show-dev'); - } - } - }; - - if (isElectron) { - document.addEventListener('keydown', handleKeyPress); - } - return () => { - if (isElectron) { - document.removeEventListener('keydown', handleKeyPress); - } - }; - }, [isElectron, sendToElectron]); return ( diff --git a/apps/client/src/common/components/buttons/QuitIconBtn.tsx b/apps/client/src/common/components/buttons/QuitIconBtn.tsx deleted file mode 100644 index b99d24191..000000000 --- a/apps/client/src/common/components/buttons/QuitIconBtn.tsx +++ /dev/null @@ -1,96 +0,0 @@ -import { useCallback, useEffect, useRef, useState } from 'react'; -import { - AlertDialog, - AlertDialogBody, - AlertDialogContent, - AlertDialogFooter, - AlertDialogHeader, - AlertDialogOverlay, - Button, - IconButton, - Tooltip, -} from '@chakra-ui/react'; -import { IoPowerOutline } from '@react-icons/all-files/io5/IoPowerOutline'; - -import { useEmitLog } from '../../stores/logger'; - -interface QuitIconBtnProps { - clickHandler: () => void; - disabled?: boolean; -} - -const quitBtnStyle = { - color: '#D20300', // $red-700 - borderColor: '#D20300', // $red-700 - _focus: { boxShadow: 'none' }, - _hover: { - background: '#D20300', // $red-700 - color: 'white', - _disabled: { - color: '#D20300', // $red-700 - background: 'none', - }, - }, - _active: { - background: '#9A0000', // $red-1000 - color: 'white', - }, - variant: 'outline', - isRound: true, -}; - -export default function QuitIconBtn(props: QuitIconBtnProps) { - const { clickHandler, disabled, ...rest } = props; - const [isOpen, setIsOpen] = useState(false); - const { emitInfo } = useEmitLog(); - const onClose = () => setIsOpen(false); - const cancelRef = useRef(null); - - useEffect(() => { - if (window.process?.type === 'renderer') { - window.ipcRenderer.on('user-request-shutdown', () => { - emitInfo('Shutdown request'); - setIsOpen(true); - }); - } - }, [emitInfo]); - - const handleShutdown = useCallback(() => { - onClose(); - clickHandler(); - }, [clickHandler]); - - return ( - <> - - } - onClick={() => setIsOpen(true)} - isDisabled={disabled} - {...quitBtnStyle} - {...rest} - /> - - - - - - Ontime Shutdown - - This will shutdown the Ontime server. Are you sure? - - - - - - - - - ); -} diff --git a/apps/client/src/features/app-settings/panel/shutdown-panel/ShutdownPanel.tsx b/apps/client/src/features/app-settings/panel/shutdown-panel/ShutdownPanel.tsx index 15320c04f..788a9d5cc 100644 --- a/apps/client/src/features/app-settings/panel/shutdown-panel/ShutdownPanel.tsx +++ b/apps/client/src/features/app-settings/panel/shutdown-panel/ShutdownPanel.tsx @@ -1,10 +1,22 @@ -import { Button } from '@chakra-ui/react'; +import { useRef } from 'react'; +import { + AlertDialog, + AlertDialogBody, + AlertDialogContent, + AlertDialogFooter, + AlertDialogHeader, + AlertDialogOverlay, + Button, + useDisclosure, +} from '@chakra-ui/react'; import useElectronEvent from '../../../../common/hooks/useElectronEvent'; import * as Panel from '../PanelUtils'; export default function ShutdownPanel() { const { isElectron, sendToElectron } = useElectronEvent(); + const { isOpen, onOpen, onClose } = useDisclosure(); + const cancelRef = useRef(null); const sendShutdown = () => { sendToElectron('shutdown', 'now'); @@ -18,9 +30,29 @@ export default function ShutdownPanel() { This will shutdown the Ontime server.
The runtime state will be lost, but your project is kept for next time. - + + + + + Ontime Shutdown + + + This will shutdown the Ontime server.
Are you sure? +
+ + + + +
+
+
); diff --git a/apps/client/src/features/editors/Editor.tsx b/apps/client/src/features/editors/Editor.tsx index 1403e90f5..140b09268 100644 --- a/apps/client/src/features/editors/Editor.tsx +++ b/apps/client/src/features/editors/Editor.tsx @@ -62,6 +62,15 @@ export default function Editor() { useWindowTitle('Editor'); + // listen to shutdown request from electron process + useEffect(() => { + if (window.process?.type === 'renderer') { + window.ipcRenderer.on('user-request-shutdown', () => { + setLocation('shutdown'); + }); + } + }, [setLocation]); + return (