useFunctionsListShortcuts.ts51 lines · main
1import { Dispatch, RefObject, SetStateAction } from 'react'
2
3import type { EdgeFunctionsSort } from '@/components/interfaces/EdgeFunctions/EdgeFunctionsSortDropdown'
4import { SHORTCUT_IDS } from '@/state/shortcuts/registry'
5import { useShortcut } from '@/state/shortcuts/useShortcut'
6
7const DEFAULT_SORT: EdgeFunctionsSort = 'name:asc'
8
9interface UseFunctionsListShortcutsParams {
10 searchInputRef: RefObject<HTMLInputElement | null>
11 setSearch: Dispatch<SetStateAction<string>> | ((value: string) => void)
12 sort: EdgeFunctionsSort
13 setSort: (value: EdgeFunctionsSort) => void
14 canCreateNew: boolean
15 onCreateNew: () => void
16 onRefresh: () => void
17}
18
19export function useFunctionsListShortcuts({
20 searchInputRef,
21 setSearch,
22 sort,
23 setSort,
24 canCreateNew,
25 onCreateNew,
26 onRefresh,
27}: UseFunctionsListShortcutsParams) {
28 useShortcut(
29 SHORTCUT_IDS.LIST_PAGE_FOCUS_SEARCH,
30 () => {
31 searchInputRef.current?.focus()
32 searchInputRef.current?.select()
33 },
34 { label: 'Search functions' }
35 )
36
37 useShortcut(SHORTCUT_IDS.LIST_PAGE_NEW_ITEM, onCreateNew, {
38 enabled: canCreateNew,
39 label: 'Deploy a new function',
40 })
41
42 useShortcut(SHORTCUT_IDS.LIST_PAGE_RESET_FILTERS, () => {
43 setSearch('')
44 })
45
46 useShortcut(SHORTCUT_IDS.FUNCTIONS_LIST_REFRESH, onRefresh)
47
48 useShortcut(SHORTCUT_IDS.FUNCTIONS_LIST_CLEAR_SORT, () => setSort(DEFAULT_SORT), {
49 enabled: sort !== DEFAULT_SORT,
50 })
51}