Observability.Commands.tsx92 lines · main
| 1 | import { useParams } from 'common' |
| 2 | import { useMemo } from 'react' |
| 3 | import type { CommandOptions, ICommand } from 'ui-patterns/CommandMenu' |
| 4 | import { orderSectionFirst, useQuery, useRegisterCommands } from 'ui-patterns/CommandMenu' |
| 5 | |
| 6 | import { COMMAND_MENU_SECTIONS } from '@/components/interfaces/App/CommandMenu/CommandMenu.utils' |
| 7 | import { useIsFeatureEnabled } from '@/hooks/misc/useIsFeatureEnabled' |
| 8 | |
| 9 | const QUERY_PERFORMANCE_COMMAND_ID = 'nav-reports-query-performance' |
| 10 | |
| 11 | export function useReportsGotoCommands(options?: CommandOptions) { |
| 12 | let { ref } = useParams() |
| 13 | ref ||= '_' |
| 14 | |
| 15 | const { reportsAll } = useIsFeatureEnabled(['reports:all']) |
| 16 | |
| 17 | const commandQuery = useQuery()?.toLowerCase() ?? '' |
| 18 | const prioritizeQueryPerformance = commandQuery.includes('query') |
| 19 | |
| 20 | const orderQueryPerformanceCommand = useMemo(() => { |
| 21 | if (!prioritizeQueryPerformance) return undefined |
| 22 | |
| 23 | return (existingCommands: ICommand[], incomingCommands: ICommand[]) => { |
| 24 | const filteredExisting = existingCommands.filter( |
| 25 | (command) => command.id !== QUERY_PERFORMANCE_COMMAND_ID |
| 26 | ) |
| 27 | |
| 28 | return [...incomingCommands, ...filteredExisting] |
| 29 | } |
| 30 | }, [prioritizeQueryPerformance]) |
| 31 | |
| 32 | const orderNavigateSection = useMemo<CommandOptions['orderSection'] | undefined>(() => { |
| 33 | return prioritizeQueryPerformance ? orderSectionFirst : options?.orderSection |
| 34 | }, [options?.orderSection, prioritizeQueryPerformance]) |
| 35 | |
| 36 | useRegisterCommands( |
| 37 | COMMAND_MENU_SECTIONS.NAVIGATE, |
| 38 | reportsAll |
| 39 | ? [ |
| 40 | { |
| 41 | id: 'nav-reports', |
| 42 | name: 'Reports', |
| 43 | route: `/project/${ref}/observability`, |
| 44 | defaultHidden: true, |
| 45 | }, |
| 46 | { |
| 47 | id: 'nav-reports-api', |
| 48 | name: 'API Reports', |
| 49 | route: `/project/${ref}/observability/api-overview`, |
| 50 | defaultHidden: true, |
| 51 | }, |
| 52 | { |
| 53 | id: 'nav-reports-storage', |
| 54 | name: 'Storage Reports', |
| 55 | route: `/project/${ref}/observability/storage`, |
| 56 | defaultHidden: true, |
| 57 | }, |
| 58 | { |
| 59 | id: 'nav-reports-database', |
| 60 | name: 'Database Reports', |
| 61 | route: `/project/${ref}/observability/database`, |
| 62 | defaultHidden: true, |
| 63 | }, |
| 64 | ] |
| 65 | : [], |
| 66 | { |
| 67 | ...options, |
| 68 | orderSection: orderNavigateSection, |
| 69 | deps: [ref, orderNavigateSection, ...(options?.deps ?? [])], |
| 70 | } |
| 71 | ) |
| 72 | |
| 73 | useRegisterCommands( |
| 74 | COMMAND_MENU_SECTIONS.NAVIGATE, |
| 75 | reportsAll |
| 76 | ? [ |
| 77 | { |
| 78 | id: QUERY_PERFORMANCE_COMMAND_ID, |
| 79 | name: 'Query Performance Reports', |
| 80 | route: `/project/${ref}/observability/query-performance`, |
| 81 | defaultHidden: true, |
| 82 | }, |
| 83 | ] |
| 84 | : [], |
| 85 | { |
| 86 | ...options, |
| 87 | orderCommands: orderQueryPerformanceCommand ?? options?.orderCommands, |
| 88 | orderSection: orderNavigateSection, |
| 89 | deps: [ref, orderQueryPerformanceCommand, orderNavigateSection, ...(options?.deps ?? [])], |
| 90 | } |
| 91 | ) |
| 92 | } |