import { snakeCase } from 'lodash' import { ExternalLink } from 'lucide-react' import Link from 'next/link' import { useState } from 'react' import { Button, cn, Dialog, DialogContent, DialogHeader, DialogSection, DialogSectionSeparator, DialogTitle, Select, SelectContent, SelectItem, SelectTrigger, SelectValue, type DialogProps, } from 'ui' import { CodeBlock } from 'ui-patterns/CodeBlock' import { generateFileCliCommand, generateMigrationCliCommand, generateSeedCliCommand, } from './SQLEditor.utils' import { TwoOptionToggle } from '@/components/ui/TwoOptionToggle' import { DOCS_URL } from '@/lib/constants' import { useSqlEditorV2StateSnapshot } from '@/state/sql-editor/sql-editor-state' const CLI_DOCS_URL = `${DOCS_URL}/guides/cli/local-development` export interface DownloadSnippetModalProps extends DialogProps { id: string } type DownloadFormat = 'migration' | 'seed' | 'sql' export const DownloadSnippetModal = ({ id, ...props }: DownloadSnippetModalProps) => { const snapV2 = useSqlEditorV2StateSnapshot() const snippet = snapV2.snippets[id]?.snippet const migrationName = snakeCase(snippet?.name) const [downloadFormat, setDownloadFormat] = useState('migration') const [selectedView, setSelectedView] = useState<'CLI' | 'NPX'>('CLI') const SNIPPETS = [ { id: 'migration' as const, label: 'Migration', caption: ( <> Run this command from your project directory to download the snippet in a new migration named {migrationName}. ), docLink: { label: 'About migrations', href: `${DOCS_URL}/guides/deployment/database-migrations`, }, cli: generateMigrationCliCommand(id, migrationName), npx: generateMigrationCliCommand(id, migrationName, true), }, { id: 'seed' as const, label: 'Seed file', caption: ( <> Run this command from your project directory to download the snippet. If your query consists of sample data, append it to the end of{' '} supabase/seed.sql. ), docLink: { label: 'About seeding', href: `${DOCS_URL}/guides/local-development/seeding-your-database`, }, cli: generateSeedCliCommand(id), npx: generateSeedCliCommand(id, true), }, { id: 'sql' as const, label: 'SQL file', caption: ( <> Run this command from your project directory to download the snippet into a new SQL file named {migrationName}.sql. ), cli: generateFileCliCommand(id, migrationName), npx: generateFileCliCommand(id, migrationName, true), }, ] const selectedSnippet = SNIPPETS.find((s) => s.id === downloadFormat) ?? SNIPPETS[0] const commandValue = selectedView === 'CLI' ? selectedSnippet.cli : selectedSnippet.npx return ( Export query

Export as

Run with

selectedView === 'CLI' ? setSelectedView('NPX') : setSelectedView('CLI') } />

{selectedSnippet.caption}

{selectedSnippet.docLink && ( )}
) }