diff --git a/src/renderer/components/dialog/dialog-changelog.tsx b/src/renderer/components/dialog/dialog-changelog.tsx index 40255d11..ff7e3e17 100644 --- a/src/renderer/components/dialog/dialog-changelog.tsx +++ b/src/renderer/components/dialog/dialog-changelog.tsx @@ -82,9 +82,13 @@ const DialogChangelog = () => { setCheckUsingLastVersion(false) } - useOnKeyUp('Escape', () => { - setShowChangelog(false) - }) + useOnKeyUp( + 'Escape', + () => { + setShowChangelog(false) + }, + isShowChangelog, + ) return ( <> @@ -109,6 +113,7 @@ const DialogChangelog = () => { /> { return ( ) => { const container = useRef(null) useDocumentClick( () => { - onClose?.(CloseReason.outside) + if (open) { + onClose?.(CloseReason.outside) + } }, clicked => clicked === container.current, ) const onEscape = useCallback(() => { - onClose?.(CloseReason.escape) - }, [onClose]) + if (open) { + onClose?.(CloseReason.escape) + } + }, [onClose, open]) const onEnter = useCallback(() => { - onClose?.(CloseReason.enter) - }, [onClose]) + if (open) { + onClose?.(CloseReason.enter) + } + }, [onClose, open]) - useOnKeyUp('Escape', onEscape) - useOnKeyUp('Enter', onEnter) + useOnKeyUp('Escape', onEscape, open) + useOnKeyUp('Enter', onEnter, open) const dialogContent = useMemo( () => ( @@ -111,6 +119,7 @@ const Dialog = ({ ) : null, document.body, + id, ) } diff --git a/src/renderer/components/groups/groups-dialog.tsx b/src/renderer/components/groups/groups-dialog.tsx index 321547cd..93c1f0bd 100644 --- a/src/renderer/components/groups/groups-dialog.tsx +++ b/src/renderer/components/groups/groups-dialog.tsx @@ -137,6 +137,7 @@ const GroupsDialog = ({ return ( { { return ( <> { @@ -26,11 +26,7 @@ const PageAppBar = ({ title, actions = [] }: Props) => { >

{title}

-
- {actions.map((action, index) => { - return {action} - })} -
+
{actions}
) diff --git a/src/renderer/hooks/use-groups.ts b/src/renderer/hooks/use-groups.ts new file mode 100644 index 00000000..a1466837 --- /dev/null +++ b/src/renderer/hooks/use-groups.ts @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2022 Kiyozz~WK~WushuLate. + * + * All rights reserved. + */ + +import is from '@sindresorhus/is' + +import { TelemetryEvent } from '../../common/telemetry-event' +import { Group, GroupRenderer } from '../types' +import { useApp } from './use-app' +import { useTelemetry } from './use-telemetry' + +type EditGroupParams = { + group: GroupRenderer + lastGroupName: string +} + +type UseGroupsReturns = { + add: (group: GroupRenderer) => void + edit: (params: EditGroupParams) => void + remove: (group: GroupRenderer) => void +} + +export const useGroups = (): UseGroupsReturns => { + const { groups, setConfig } = useApp() + const { send } = useTelemetry() + + const addGroup = (group: GroupRenderer) => { + if ( + !is.undefined( + groups.find( + g => g.name.trim().toLowerCase() === group.name.trim().toLowerCase(), + ), + ) + ) { + return + } + + send(TelemetryEvent.groupCreated, { scripts: group.scripts.length }) + setConfig({ + groups: [ + ...groups, + { + name: group.name, + scripts: group.scripts.map(s => ({ name: s.name, path: s.path })), + }, + ], + }) + } + + const editGroup = ({ group, lastGroupName }: EditGroupParams) => { + if ( + group.name.trim().toLowerCase() !== lastGroupName.trim().toLowerCase() && + !is.undefined( + groups.find( + g => g.name.trim().toLowerCase() === group.name.trim().toLowerCase(), + ), + ) + ) { + return + } + + send(TelemetryEvent.groupEdited, { scripts: group.scripts.length }) + setConfig({ + groups: groups.map((g: Group) => { + if (g.name === lastGroupName) { + return { + scripts: group.scripts.map(s => ({ name: s.name, path: s.path })), + name: group.name, + } + } + + return { + ...g, + scripts: g.scripts.map(s => ({ name: s.name, path: s.path })), + } + }), + }) + } + + const removeGroup = (group: GroupRenderer) => { + if ( + is.undefined( + groups.find( + g => g.name.trim().toLowerCase() === group.name.trim().toLowerCase(), + ), + ) + ) { + return + } + + setConfig( + { + groups: groups + .map(g => ({ name: g.name, scripts: g.scripts })) + .filter(g => g.name !== group.name), + }, + true, + ) + } + + return { + add: addGroup, + edit: editGroup, + remove: removeGroup, + } +} diff --git a/src/renderer/hooks/use-on-key-up.ts b/src/renderer/hooks/use-on-key-up.ts index b562d5ff..0090df48 100644 --- a/src/renderer/hooks/use-on-key-up.ts +++ b/src/renderer/hooks/use-on-key-up.ts @@ -15,6 +15,7 @@ type UseOnKeyUpOptions = { export const useOnKeyUp = ( key: string, action: () => void, + isActive: boolean, options?: UseOnKeyUpOptions, ) => { const onKeyUp = useCallback( @@ -43,10 +44,18 @@ export const useOnKeyUp = ( ) useEffect(() => { - document.body.addEventListener('keyup', onKeyUp) + const unSub = () => { + document.body.removeEventListener('keyup', onKeyUp) + } + + if (isActive) { + document.body.addEventListener('keyup', onKeyUp) + } else { + unSub() + } return () => { - document.body.removeEventListener('keyup', onKeyUp) + unSub() } - }, [onKeyUp]) + }, [onKeyUp, isActive]) } diff --git a/src/renderer/pages/groups/groups.tsx b/src/renderer/pages/groups/groups.tsx index 8c3272b5..5af1dbb3 100644 --- a/src/renderer/pages/groups/groups.tsx +++ b/src/renderer/pages/groups/groups.tsx @@ -5,204 +5,101 @@ */ import CreateIcon from '@mui/icons-material/Create' -import is from '@sindresorhus/is' -import React, { useCallback, useState } from 'react' +import React, { useState } from 'react' import { useTranslation } from 'react-i18next' -import { TelemetryEvent } from '../../../common/telemetry-event' import GroupsDialog from '../../components/groups/groups-dialog' import Page from '../../components/page' import PageAppBar from '../../components/page-app-bar' import { useApp } from '../../hooks/use-app' -import { useTelemetry } from '../../hooks/use-telemetry' -import { Group, GroupRenderer } from '../../types' +import { useGroups } from '../../hooks/use-groups' +import { GroupRenderer } from '../../types' import GroupsListItem from './groups-list-item' -type EditGroupParams = { - group: GroupRenderer - lastGroupName: string -} - const Groups = () => { const { t } = useTranslation() - const { groups, setConfig } = useApp() - const { send } = useTelemetry() + const { groups } = useApp() + const { add, edit, remove } = useGroups() const [showAddPopup, setShowPopup] = useState(false) const [editingGroup, setEditingGroup] = useState() - const addGroup = useCallback( - (group: GroupRenderer) => { - if ( - !is.undefined( - groups.find( - g => - g.name.trim().toLowerCase() === group.name.trim().toLowerCase(), - ), - ) - ) { - return - } - - send(TelemetryEvent.groupCreated, { scripts: group.scripts.length }) - setConfig({ - groups: [ - ...groups, - { - name: group.name, - scripts: group.scripts.map(s => ({ name: s.name, path: s.path })), - }, - ], - }) - }, - [setConfig, groups, send], - ) - const editGroup = useCallback( - ({ group, lastGroupName }: EditGroupParams) => { - if ( - group.name.trim().toLowerCase() !== - lastGroupName.trim().toLowerCase() && - !is.undefined( - groups.find( - g => - g.name.trim().toLowerCase() === group.name.trim().toLowerCase(), - ), - ) - ) { - return - } - - send(TelemetryEvent.groupEdited, { scripts: group.scripts.length }) - setConfig({ - groups: groups.map((g: Group) => { - if (g.name === lastGroupName) { - return { - scripts: group.scripts.map(s => ({ name: s.name, path: s.path })), - name: group.name, - } - } - - return { - ...g, - scripts: g.scripts.map(s => ({ name: s.name, path: s.path })), - } - }), - }) - }, - [groups, setConfig, send], - ) - const removeGroup = useCallback( - (group: GroupRenderer) => { - if ( - is.undefined( - groups.find( - g => - g.name.trim().toLowerCase() === group.name.trim().toLowerCase(), - ), - ) - ) { - return - } - - setConfig( - { - groups: groups - .map(g => ({ name: g.name, scripts: g.scripts })) - .filter(g => g.name !== group.name), - }, - true, - ) - }, - [groups, setConfig], - ) - - const onClickRemoveGroup = useCallback( - (group: GroupRenderer) => { - return () => { - removeGroup(group) - } - }, - [removeGroup], - ) + const onClickRemoveGroup = (group: GroupRenderer) => { + return () => { + remove(group) + } + } - const onClickEditGroup = useCallback((group: GroupRenderer) => { + const onClickEditGroup = (group: GroupRenderer) => { return () => { setEditingGroup(group) setShowPopup(true) } - }, []) + } - const onClickAddButton = useCallback(() => { + const onClickAddButton = () => { setEditingGroup(undefined) setShowPopup(true) - }, []) + } - const onGroupAdd = useCallback( - (group: Partial) => { - setShowPopup(false) - addGroup(group as GroupRenderer) - }, - [setShowPopup, addGroup], - ) + const onGroupAdd = (group: GroupRenderer) => { + setShowPopup(false) + add(group) + } - const onGroupEdit = useCallback( - (lastGroupName: string, group: GroupRenderer) => { - setShowPopup(false) - editGroup({ group, lastGroupName }) - }, - [setShowPopup, editGroup], - ) + const onGroupEdit = (lastGroupName: string, group: GroupRenderer) => { + setShowPopup(false) + edit({ group, lastGroupName }) + } - const onClosePopup = useCallback(() => { + const onClosePopup = () => { setShowPopup(false) - }, []) + } return ( <> + actions={ + , - ]} + + } /> - <> - - - {groups.length > 0 ? ( -
- {groups.map(group => { - return ( - - ) - })} -
- ) : ( -
-

- {t('page.groups.createGroupText')} -

-

{t('page.groups.whatIsAGroup')}

-
- )} - + + + {groups.length > 0 ? ( +
+ {groups.map(group => { + return ( + + ) + })} +
+ ) : ( +
+

+ {t('page.groups.createGroupText')} +

+

{t('page.groups.whatIsAGroup')}

+
+ )}
) diff --git a/src/renderer/pages/settings/settings.tsx b/src/renderer/pages/settings/settings.tsx index fd834bf5..4f427ece 100644 --- a/src/renderer/pages/settings/settings.tsx +++ b/src/renderer/pages/settings/settings.tsx @@ -182,24 +182,25 @@ const Settings = () => { <> openDocumentation('settings-app-bar')} - > - - - - {t('common.documentation')} - , - , - ]} + actions={ + <> + + + + } />