Skip to content

Commit 8cc9d0f

Browse files
authored
Merge pull request #3171 from liam-hq/refact/add-command-palette-unsafe-hook
♻️ refact(command palette): add unsafe hook to easy to use
2 parents 4db00fb + 7f39d12 commit 8cc9d0f

File tree

6 files changed

+34
-25
lines changed

6 files changed

+34
-25
lines changed

frontend/packages/erd-core/src/features/erd/components/ERDRenderer/CommandPalette/CommandPalette.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,13 @@ import {
1111
import { type FC, useEffect } from 'react'
1212
import styles from './CommandPalette.module.css'
1313
import { CommandPaletteContent } from './CommandPaletteContent'
14-
import { useCommandPalette } from './CommandPaletteProvider'
14+
import { useCommandPaletteOrThrow } from './CommandPaletteProvider'
1515
import { useSubscribeCommands } from './hooks'
1616

1717
export const CommandPalette: FC = () => {
1818
useSubscribeCommands()
1919

20-
const commandPaletteResult = useCommandPalette()
21-
if (commandPaletteResult.isErr()) {
22-
throw commandPaletteResult.error
23-
}
24-
const { open, setOpen, toggleOpen } = commandPaletteResult.value
20+
const { open, setOpen, toggleOpen } = useCommandPaletteOrThrow()
2521

2622
// Toggle the menu when ⌘K is pressed
2723
useEffect(() => {

frontend/packages/erd-core/src/features/erd/components/ERDRenderer/CommandPalette/CommandPaletteOptions/CommandOptions.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
import { Command } from 'cmdk'
1010
import type { FC } from 'react'
1111
import { useUserEditingOrThrow } from '@/stores'
12-
import { useCommandPalette } from '../CommandPaletteProvider'
12+
import { useCommandPaletteOrThrow } from '../CommandPaletteProvider'
1313
import { useCopyLink } from '../hooks/useCopyLink'
1414
import { useFitScreen } from '../hooks/useFitScreen'
1515
import { getSuggestionText } from '../utils'
@@ -20,8 +20,7 @@ export const CommandPaletteCommandOptions: FC = () => {
2020
const { zoomToFit, tidyUp } = useFitScreen()
2121
const { setShowMode } = useUserEditingOrThrow()
2222

23-
const result = useCommandPalette()
24-
const setOpen = result.isOk() ? result.value.setOpen : () => {}
23+
const { setOpen } = useCommandPaletteOrThrow()
2524

2625
return (
2726
<Command.Group heading="Commands">

frontend/packages/erd-core/src/features/erd/components/ERDRenderer/CommandPalette/CommandPaletteOptions/TableOptions.test.tsx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,28 @@ import {
1313
UserEditingProvider,
1414
} from '@/stores'
1515
import { CommandPaletteProvider } from '../CommandPaletteProvider'
16+
import * as UseCommandPalette from '../CommandPaletteProvider/hooks'
1617
import { TableOptions } from './TableOptions'
1718

1819
afterEach(() => {
1920
vi.clearAllMocks()
2021
})
2122

23+
const mockSetCommandPaletteDialogOpen = vi.fn()
2224
const mockSelectTable = vi.fn()
2325
const mockWindowOpen = vi.fn()
2426

27+
const originalUseCommandPaletteOrThrow =
28+
UseCommandPalette.useCommandPaletteOrThrow
29+
vi.spyOn(UseCommandPalette, 'useCommandPaletteOrThrow').mockImplementation(
30+
() => {
31+
const original = originalUseCommandPaletteOrThrow()
32+
return {
33+
...original,
34+
setOpen: mockSetCommandPaletteDialogOpen,
35+
}
36+
},
37+
)
2538
const originalUseTableSelection = UseTableSelection.useTableSelection
2639
vi.spyOn(UseTableSelection, 'useTableSelection').mockImplementation(() => {
2740
const original = originalUseTableSelection()
@@ -78,7 +91,7 @@ describe('mouse interactions', () => {
7891
await user.click(screen.getByRole('link', { name: 'follows' }))
7992

8093
expect(mockSelectTable).toHaveBeenCalled()
81-
// TODO: check if the dialog is closed
94+
expect(mockSetCommandPaletteDialogOpen).toHaveBeenCalledWith(false)
8295
})
8396

8497
it('does nothing with ⌘ + click (default browser action: open in new tab)', async () => {
@@ -90,6 +103,7 @@ describe('mouse interactions', () => {
90103
await user.keyboard('{/Meta}')
91104

92105
expect(mockSelectTable).not.toHaveBeenCalled()
106+
expect(mockSetCommandPaletteDialogOpen).not.toHaveBeenCalled()
93107
})
94108
})
95109

@@ -106,7 +120,7 @@ describe('keyboard interactions', () => {
106120
displayArea: 'main',
107121
tableId: 'users',
108122
})
109-
// TODO: check if the dialog is closed
123+
expect(mockSetCommandPaletteDialogOpen).toHaveBeenCalledWith(false)
110124

111125
// other functions are not called
112126
expect(mockWindowOpen).not.toHaveBeenCalled()
@@ -124,6 +138,7 @@ describe('keyboard interactions', () => {
124138

125139
// other functions are not called
126140
expect(mockSelectTable).not.toHaveBeenCalled()
141+
expect(mockSetCommandPaletteDialogOpen).not.toHaveBeenCalled()
127142
})
128143

129144
it('does nothing on Enter when suggestion is not table', async () => {
@@ -137,6 +152,6 @@ describe('keyboard interactions', () => {
137152

138153
expect(mockWindowOpen).not.toHaveBeenCalled()
139154
expect(mockSelectTable).not.toHaveBeenCalled()
140-
// TODO: check if the dialog is NOT closed
155+
expect(mockSetCommandPaletteDialogOpen).not.toHaveBeenCalled()
141156
})
142157
})

frontend/packages/erd-core/src/features/erd/components/ERDRenderer/CommandPalette/CommandPaletteOptions/TableOptions.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Command } from 'cmdk'
33
import { type FC, useCallback, useEffect } from 'react'
44
import { useTableSelection } from '@/features/erd/hooks'
55
import { useSchemaOrThrow } from '@/stores'
6-
import { useCommandPalette } from '../CommandPaletteProvider'
6+
import { useCommandPaletteOrThrow } from '../CommandPaletteProvider'
77
import type { CommandPaletteSuggestion } from '../types'
88
import { getSuggestionText } from '../utils'
99
import styles from './CommandPaletteOptions.module.css'
@@ -19,8 +19,7 @@ type Props = {
1919
}
2020

2121
export const TableOptions: FC<Props> = ({ suggestion }) => {
22-
const result = useCommandPalette()
23-
const setOpen = result.isOk() ? result.value.setOpen : () => {}
22+
const { setOpen } = useCommandPaletteOrThrow()
2423

2524
const schema = useSchemaOrThrow()
2625
const { selectTable } = useTableSelection()

frontend/packages/erd-core/src/features/erd/components/ERDRenderer/CommandPalette/CommandPaletteProvider/hooks.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ import {
55
type CommandPaletteContextValue,
66
} from './context'
77

8-
export const useCommandPalette = (): Result<
9-
CommandPaletteContextValue,
10-
Error
11-
> => {
8+
const useCommandPalette = (): Result<CommandPaletteContextValue, Error> => {
129
const commandPaletteValue = useContext(CommandPaletteContext)
1310
if (!commandPaletteValue)
1411
return err(
@@ -17,3 +14,10 @@ export const useCommandPalette = (): Result<
1714

1815
return ok(commandPaletteValue)
1916
}
17+
18+
export const useCommandPaletteOrThrow = (): CommandPaletteContextValue => {
19+
const result = useCommandPalette()
20+
if (result.isErr()) throw result.error
21+
22+
return result.value
23+
}

frontend/packages/erd-core/src/features/erd/components/ERDRenderer/CommandPalette/CommandPaletteTriggerButton/CommandPaletteTriggerButton.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
import { Search } from '@liam-hq/ui'
22
import type { FC } from 'react'
3-
import { useCommandPalette } from '../CommandPaletteProvider'
3+
import { useCommandPaletteOrThrow } from '../CommandPaletteProvider'
44
import styles from './CommandPaletteTriggerButton.module.css'
55

66
export const CommandPaletteTriggerButton: FC = () => {
7-
const commandPaletteResult = useCommandPalette()
8-
if (commandPaletteResult.isErr()) {
9-
throw commandPaletteResult.error
10-
}
11-
const { setOpen } = commandPaletteResult.value
7+
const { setOpen } = useCommandPaletteOrThrow()
128

139
return (
1410
<button

0 commit comments

Comments
 (0)