ApiUrl.tsx 1.6 KB

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