useFunctionsDetailShortcuts.ts73 lines · main
1import { useRouter } from 'next/router'
2
3import { SHORTCUT_IDS } from '@/state/shortcuts/registry'
4import { useShortcut } from '@/state/shortcuts/useShortcut'
5
6interface UseFunctionsDetailShortcutsParams {
7 projectRef: string | undefined
8 functionSlug: string | undefined
9 canReadFunctions: boolean
10 isPlatform: boolean
11 onOpenTest: () => void
12 onOpenDownload: () => void
13 onCopyUrl: () => void
14}
15
16/**
17 * Registers shortcuts that apply across every tab of a single edge function:
18 * - 1..5: jump between Overview / Invocations / Logs / Code / Settings
19 * - Shift+T: open the test sheet
20 * - Shift+D: open the download popover
21 * - Shift+C: copy the function URL
22 *
23 * Should be mounted once at the EdgeFunctionDetailsLayout level.
24 */
25export function useFunctionsDetailShortcuts({
26 projectRef,
27 functionSlug,
28 canReadFunctions,
29 isPlatform,
30 onOpenTest,
31 onOpenDownload,
32 onCopyUrl,
33}: UseFunctionsDetailShortcutsParams) {
34 const router = useRouter()
35
36 const ready = Boolean(projectRef && functionSlug)
37 const shortcutsEnabled = ready && canReadFunctions
38 const base = `/project/${projectRef}/functions/${functionSlug}`
39
40 useShortcut(SHORTCUT_IDS.NAV_FUNCTION_DETAIL_OVERVIEW, () => router.push(base), {
41 enabled: shortcutsEnabled && isPlatform,
42 })
43
44 useShortcut(
45 SHORTCUT_IDS.NAV_FUNCTION_DETAIL_INVOCATIONS,
46 () => router.push(`${base}/invocations`),
47 { enabled: shortcutsEnabled && isPlatform }
48 )
49
50 useShortcut(SHORTCUT_IDS.NAV_FUNCTION_DETAIL_LOGS, () => router.push(`${base}/logs`), {
51 enabled: shortcutsEnabled && isPlatform,
52 })
53
54 useShortcut(SHORTCUT_IDS.NAV_FUNCTION_DETAIL_CODE, () => router.push(`${base}/code`), {
55 enabled: shortcutsEnabled,
56 })
57
58 useShortcut(SHORTCUT_IDS.NAV_FUNCTION_DETAIL_SETTINGS, () => router.push(`${base}/details`), {
59 enabled: shortcutsEnabled,
60 })
61
62 useShortcut(SHORTCUT_IDS.FUNCTION_DETAIL_OPEN_TEST, onOpenTest, {
63 enabled: shortcutsEnabled,
64 })
65
66 useShortcut(SHORTCUT_IDS.FUNCTION_DETAIL_OPEN_DOWNLOAD, onOpenDownload, {
67 enabled: shortcutsEnabled,
68 })
69
70 useShortcut(SHORTCUT_IDS.FUNCTION_DETAIL_COPY_URL, onCopyUrl, {
71 enabled: shortcutsEnabled,
72 })
73}