Skip to content

Commit

Permalink
Move search state out of useFilteredCommands.
Browse files Browse the repository at this point in the history
  • Loading branch information
raineorshine committed Oct 10, 2024
1 parent 6d15236 commit 3324846
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
3 changes: 2 additions & 1 deletion src/components/CommandPalette.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,8 @@ const CommandPalette: FC = () => {
const fontSize = useSelector(state => state.fontSize)
const unmounted = useRef(false)
const [recentCommands, setRecentCommands] = useState(storageModel.get('recentCommands'))
const { search, setSearch, shortcuts } = useFilteredCommands({
const [search, setSearch] = useState('')
const shortcuts = useFilteredCommands(search, {
recentCommands,
sortActiveCommandsFirst: true,
})
Expand Down
5 changes: 3 additions & 2 deletions src/components/ShortcutTable.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FC } from 'react'
import { FC, useState } from 'react'
import { modalText } from '../../styled-system/recipes'
import Shortcut from '../@types/Shortcut'
import ShortcutId from '../@types/ShortcutId'
Expand Down Expand Up @@ -178,7 +178,8 @@ const ShortcutTable = ({
onSelect?: (shortcut: Shortcut | null) => void
selectedShortcut?: Shortcut
}) => {
const { setSearch, search, shortcuts } = useFilteredCommands({ platformShortcutsOnly: true })
const [search, setSearch] = useState('')
const shortcuts = useFilteredCommands(search, { platformShortcutsOnly: true })

return (
<div>
Expand Down
34 changes: 16 additions & 18 deletions src/hooks/useFilteredCommands.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _ from 'lodash'
import { useMemo, useState } from 'react'
import { useMemo } from 'react'
import { useStore } from 'react-redux'
import Shortcut from '../@types/Shortcut'
import ShortcutId from '../@types/ShortcutId'
Expand All @@ -15,18 +15,21 @@ const isExecutable = (state: State, shortcut: Shortcut) =>
(!shortcut.canExecute || shortcut.canExecute(() => state)) && (shortcut.allowExecuteFromModal || !state.showModal)

/** A hook that filters and sorts commands based on a search or the current gesture or keyboard input. */
const useFilteredCommands = ({
platformShortcutsOnly,
recentCommands,
sortActiveCommandsFirst,
}: {
/** Only include commands that have shortcuts on the current platform (keyboard on desktop/gestures on mobile). */
platformShortcutsOnly?: boolean
recentCommands?: ShortcutId[]
sortActiveCommandsFirst?: boolean
} = {}) => {
const useFilteredCommands = (
search: string,
{
platformShortcutsOnly,
recentCommands,
sortActiveCommandsFirst,
}: {
/** Only include commands that have shortcuts on the current platform (keyboard on desktop/gestures on mobile). */
platformShortcutsOnly?: boolean
recentCommands?: ShortcutId[]
/** The search term to filter commands that match. */
sortActiveCommandsFirst?: boolean
},
) => {
const gestureInProgress = gestureStore.useState()
const [search, setSearch] = useState('')
const store = useStore()

const possibleShortcutsSorted = useMemo(() => {
Expand Down Expand Up @@ -113,12 +116,7 @@ const useFilteredCommands = ({
return sorted
}, [gestureInProgress, sortActiveCommandsFirst, search, recentCommands, store, platformShortcutsOnly])

return {
shortcuts: possibleShortcutsSorted,
search,
setSearch,
recentCommands,
}
return possibleShortcutsSorted
}

export default useFilteredCommands

0 comments on commit 3324846

Please sign in to comment.