Skip to content

Commit

Permalink
Refactoring dialog group
Browse files Browse the repository at this point in the history
Improve dialog onClose
  • Loading branch information
TURMEL Kevin committed Mar 14, 2022
1 parent d2ff6f8 commit 643d3b4
Show file tree
Hide file tree
Showing 11 changed files with 226 additions and 197 deletions.
11 changes: 8 additions & 3 deletions src/renderer/components/dialog/dialog-changelog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,13 @@ const DialogChangelog = () => {
setCheckUsingLastVersion(false)
}

useOnKeyUp('Escape', () => {
setShowChangelog(false)
})
useOnKeyUp(
'Escape',
() => {
setShowChangelog(false)
},
isShowChangelog,
)

return (
<>
Expand All @@ -109,6 +113,7 @@ const DialogChangelog = () => {
/>

<Dialog
id="user-note"
open={isUserShowNotes}
onClose={onCloseDialog}
actions={
Expand Down
1 change: 1 addition & 0 deletions src/renderer/components/dialog/dialog-recent-files.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ const DialogRecentFiles = ({ isOpen, onClose }: Props) => {

return (
<Dialog
id="recent-files"
open={isOpen}
title={t('page.compilation.actions.recentFiles')}
onClose={onDialogClose}
Expand Down
23 changes: 16 additions & 7 deletions src/renderer/components/dialog/dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type DialogProps = {
content?: string
child?: string
}
id: string
}

enum CloseReason {
Expand All @@ -40,26 +41,33 @@ const Dialog = ({
content: Content,
contentClassNames = {},
children,
id,
}: React.PropsWithChildren<DialogProps>) => {
const container = useRef<HTMLDivElement | null>(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(
() => (
Expand Down Expand Up @@ -111,6 +119,7 @@ const Dialog = ({
</>
) : null,
document.body,
id,
)
}

Expand Down
1 change: 1 addition & 0 deletions src/renderer/components/groups/groups-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ const GroupsDialog = ({

return (
<Dialog
id="group"
open={open}
onClose={onDialogClose}
actions={
Expand Down
1 change: 1 addition & 0 deletions src/renderer/components/open-compilation-logs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ const OpenCompilationLogs = () => {
</NavItem>

<Dialog
id="compilation-logs"
open={isDialogOpen}
onClose={onClickButtonCloseLogs}
actions={
Expand Down
1 change: 1 addition & 0 deletions src/renderer/components/open-documentation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const OpenDocumentation = () => {
return (
<>
<Dialog
id="documentation"
open={isDialogOpen}
maxWidth={70}
title={t('nav.help.title')}
Expand Down
8 changes: 2 additions & 6 deletions src/renderer/components/page-app-bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useFocus } from '../hooks/use-focus'

type Props = {
title?: string
actions?: ReactNode[]
actions?: ReactNode
}

const PageAppBar = ({ title, actions = [] }: Props) => {
Expand All @@ -26,11 +26,7 @@ const PageAppBar = ({ title, actions = [] }: Props) => {
>
<div className="flex h-16 items-center px-4">
<h2 className="font-nova text-3xl font-bold">{title}</h2>
<div className="ml-auto flex items-center gap-2">
{actions.map((action, index) => {
return <React.Fragment key={index}>{action}</React.Fragment>
})}
</div>
<div className="ml-auto flex items-center gap-2">{actions}</div>
</div>
</div>
)
Expand Down
108 changes: 108 additions & 0 deletions src/renderer/hooks/use-groups.ts
Original file line number Diff line number Diff line change
@@ -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,
}
}
15 changes: 12 additions & 3 deletions src/renderer/hooks/use-on-key-up.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type UseOnKeyUpOptions = {
export const useOnKeyUp = (
key: string,
action: () => void,
isActive: boolean,
options?: UseOnKeyUpOptions,
) => {
const onKeyUp = useCallback(
Expand Down Expand Up @@ -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])
}
Loading

0 comments on commit 643d3b4

Please sign in to comment.