useFunctionsListShortcuts.ts51 lines · main
| 1 | import { Dispatch, RefObject, SetStateAction } from 'react' |
| 2 | |
| 3 | import type { EdgeFunctionsSort } from '@/components/interfaces/EdgeFunctions/EdgeFunctionsSortDropdown' |
| 4 | import { SHORTCUT_IDS } from '@/state/shortcuts/registry' |
| 5 | import { useShortcut } from '@/state/shortcuts/useShortcut' |
| 6 | |
| 7 | const DEFAULT_SORT: EdgeFunctionsSort = 'name:asc' |
| 8 | |
| 9 | interface 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 | |
| 19 | export 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 | } |