useFunctionsDetailShortcuts.ts 2.1 KB

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