ApiUrl.tsx53 lines · main
1import { Link } from 'lucide-react'
2import { toast } from 'sonner'
3import { Badge, copyToClipboard } from 'ui'
4import {
5 useRegisterCommands,
6 useResetCommandMenu,
7 useSetCommandMenuOpen,
8} from 'ui-patterns/CommandMenu'
9
10import { COMMAND_MENU_SECTIONS } from './CommandMenu.utils'
11import { orderCommandSectionsByPriority } from './ordering'
12import { useProjectSettingsV2Query } from '@/data/config/project-settings-v2-query'
13import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
14
15export function useApiUrlCommand() {
16 const setIsOpen = useSetCommandMenuOpen()
17 const resetCommandMenu = useResetCommandMenu()
18
19 const { data: project } = useSelectedProjectQuery()
20 const { data: settings } = useProjectSettingsV2Query(
21 { projectRef: project?.ref },
22 { enabled: !!project }
23 )
24
25 const protocol = settings?.app_config?.protocol ?? 'https'
26 const endpoint = settings?.app_config?.endpoint
27 const apiUrl = endpoint ? `${protocol}://${endpoint}` : undefined
28
29 useRegisterCommands(
30 COMMAND_MENU_SECTIONS.ACTIONS,
31 [
32 {
33 id: 'api-url',
34 name: 'Copy API URL',
35 action: () => {
36 copyToClipboard(apiUrl ?? '', () => {
37 toast.success('API URL copied to clipboard')
38 })
39 setIsOpen(false)
40 resetCommandMenu()
41 },
42 icon: () => <Link />,
43 badge: () => <Badge>Project: {project?.name}</Badge>,
44 },
45 ],
46 {
47 enabled: !!project,
48 deps: [apiUrl, project],
49 orderSection: orderCommandSectionsByPriority,
50 sectionMeta: { priority: 3 },
51 }
52 )
53}