useFilesBucketsShortcuts.ts53 lines · main
| 1 | import { PermissionAction } from '@supabase/shared-types/out/constants' |
| 2 | import { Dispatch, RefObject, SetStateAction } from 'react' |
| 3 | |
| 4 | import { STORAGE_BUCKET_SORT } from '../Storage.constants' |
| 5 | import { useAsyncCheckPermissions } from '@/hooks/misc/useCheckPermissions' |
| 6 | import { SHORTCUT_IDS } from '@/state/shortcuts/registry' |
| 7 | import { useShortcut } from '@/state/shortcuts/useShortcut' |
| 8 | |
| 9 | interface UseFilesBucketsShortcutsParams { |
| 10 | searchInputRef: RefObject<HTMLInputElement | null> |
| 11 | setFilterString: Dispatch<SetStateAction<string>> |
| 12 | sortBucket: STORAGE_BUCKET_SORT |
| 13 | setSortBucket: (value: STORAGE_BUCKET_SORT) => void |
| 14 | setCreateVisible: (value: boolean) => void |
| 15 | onRefresh: () => void |
| 16 | } |
| 17 | |
| 18 | export function useFilesBucketsShortcuts({ |
| 19 | searchInputRef, |
| 20 | setFilterString, |
| 21 | sortBucket, |
| 22 | setSortBucket, |
| 23 | setCreateVisible, |
| 24 | onRefresh, |
| 25 | }: UseFilesBucketsShortcutsParams) { |
| 26 | const { can: canCreateBuckets } = useAsyncCheckPermissions(PermissionAction.STORAGE_WRITE, '*') |
| 27 | |
| 28 | useShortcut( |
| 29 | SHORTCUT_IDS.LIST_PAGE_FOCUS_SEARCH, |
| 30 | () => { |
| 31 | searchInputRef.current?.focus() |
| 32 | searchInputRef.current?.select() |
| 33 | }, |
| 34 | { label: 'Search buckets' } |
| 35 | ) |
| 36 | |
| 37 | useShortcut(SHORTCUT_IDS.LIST_PAGE_NEW_ITEM, () => setCreateVisible(true), { |
| 38 | label: 'Create new bucket', |
| 39 | enabled: canCreateBuckets, |
| 40 | }) |
| 41 | |
| 42 | useShortcut(SHORTCUT_IDS.LIST_PAGE_RESET_FILTERS, () => { |
| 43 | setFilterString('') |
| 44 | }) |
| 45 | |
| 46 | useShortcut(SHORTCUT_IDS.STORAGE_BUCKETS_REFRESH, onRefresh) |
| 47 | |
| 48 | useShortcut( |
| 49 | SHORTCUT_IDS.STORAGE_BUCKETS_CLEAR_SORT, |
| 50 | () => setSortBucket(STORAGE_BUCKET_SORT.CREATED_AT), |
| 51 | { enabled: sortBucket !== STORAGE_BUCKET_SORT.CREATED_AT } |
| 52 | ) |
| 53 | } |