DeployEdgeFunctionButton.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // @ts-nocheck
  2. import { useParams } from 'common'
  3. import { ChevronDown, Code, Terminal } from 'lucide-react'
  4. import { useRouter } from 'next/router'
  5. import { parseAsString, useQueryState } from 'nuqs'
  6. import {
  7. AiIconAnimation,
  8. DropdownMenu,
  9. DropdownMenuContent,
  10. DropdownMenuItem,
  11. DropdownMenuTrigger,
  12. } from 'ui'
  13. import { SIDEBAR_KEYS } from '@/components/layouts/ProjectLayout/LayoutSidebar/LayoutSidebarProvider'
  14. import { ButtonTooltip } from '@/components/ui/ButtonTooltip'
  15. import { useSendEventMutation } from '@/data/telemetry/send-event-mutation'
  16. import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization'
  17. import { useIsProjectActive } from '@/hooks/misc/useSelectedProject'
  18. import { useAiAssistantStateSnapshot } from '@/state/ai-assistant-state'
  19. import { useSidebarManagerSnapshot } from '@/state/sidebar-manager-state'
  20. export const DeployEdgeFunctionButton = () => {
  21. const router = useRouter()
  22. const { ref } = useParams()
  23. const { data: org } = useSelectedOrganizationQuery()
  24. const snap = useAiAssistantStateSnapshot()
  25. const { openSidebar } = useSidebarManagerSnapshot()
  26. const { mutate: sendEvent } = useSendEventMutation()
  27. const [, setCreateMethod] = useQueryState('create', parseAsString)
  28. const isProjectActive = useIsProjectActive()
  29. return (
  30. <DropdownMenu>
  31. <DropdownMenuTrigger asChild disabled={!isProjectActive}>
  32. <ButtonTooltip
  33. type="primary"
  34. disabled={!isProjectActive}
  35. iconRight={<ChevronDown className="w-4 h-4" strokeWidth={1.5} />}
  36. tooltip={{
  37. content: {
  38. side: 'bottom',
  39. text: !isProjectActive
  40. ? 'Unable to deploy function as project is inactive'
  41. : undefined,
  42. },
  43. }}
  44. >
  45. Deploy a new function
  46. </ButtonTooltip>
  47. </DropdownMenuTrigger>
  48. <DropdownMenuContent align="end" className="w-80">
  49. <DropdownMenuItem
  50. onSelect={() => {
  51. router.push(`/project/${ref}/functions/new`)
  52. sendEvent({
  53. action: 'edge_function_via_editor_button_clicked',
  54. properties: { origin: 'secondary_action' },
  55. groups: { project: ref ?? 'Unknown', organization: org?.slug ?? 'Unknown' },
  56. })
  57. }}
  58. className="gap-4"
  59. >
  60. <Code className="shrink-0" size={16} strokeWidth={1.5} />
  61. <div>
  62. <span className="text-foreground">Via Editor</span>
  63. <p>Write and deploy in the browser</p>
  64. </div>
  65. </DropdownMenuItem>
  66. <DropdownMenuItem
  67. className="gap-4"
  68. onSelect={() => {
  69. setCreateMethod('cli')
  70. sendEvent({
  71. action: 'edge_function_via_cli_button_clicked',
  72. properties: { origin: 'secondary_action' },
  73. groups: { project: ref ?? 'Unknown', organization: org?.slug ?? 'Unknown' },
  74. })
  75. }}
  76. >
  77. <Terminal className="shrink-0" size={16} strokeWidth={1.5} />
  78. <div>
  79. <span className="text-foreground">Via CLI</span>
  80. <p>Write locally, deploy with the CLI</p>
  81. </div>
  82. </DropdownMenuItem>
  83. <DropdownMenuItem
  84. className="gap-4"
  85. onSelect={() => {
  86. openSidebar(SIDEBAR_KEYS.AI_ASSISTANT)
  87. snap.newChat({
  88. name: 'Create new edge function',
  89. initialInput: `Create a new edge function that ...`,
  90. suggestions: {
  91. title:
  92. 'I can help you create a new edge function. Here are a few example prompts to get you started:',
  93. prompts: [
  94. {
  95. label: 'Stripe Payments',
  96. description: 'Create a new edge function that processes payments with Stripe',
  97. },
  98. {
  99. label: 'Email with Resend',
  100. description: 'Create a new edge function that sends emails with Resend',
  101. },
  102. {
  103. label: 'PDF Generator',
  104. description:
  105. 'Create a new edge function that generates PDFs from HTML templates',
  106. },
  107. ],
  108. },
  109. })
  110. sendEvent({
  111. action: 'edge_function_ai_assistant_button_clicked',
  112. properties: { origin: 'secondary_action' },
  113. groups: { project: ref ?? 'Unknown', organization: org?.slug ?? 'Unknown' },
  114. })
  115. }}
  116. >
  117. <AiIconAnimation className="shrink-0" size={16} />
  118. <div>
  119. <span className="text-foreground">Via AI Assistant</span>
  120. <p>Let the Assistant write and deploy for you</p>
  121. </div>
  122. </DropdownMenuItem>
  123. </DropdownMenuContent>
  124. </DropdownMenu>
  125. )
  126. }