DownloadSnippetModal.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import type { ModalProps } from '@ui/components/Modal/Modal'
  2. import { snakeCase } from 'lodash'
  3. import { ExternalLink } from 'lucide-react'
  4. import Link from 'next/link'
  5. import { useState } from 'react'
  6. import { Button, Modal, Tabs } from 'ui'
  7. import { CodeBlock } from 'ui-patterns/CodeBlock'
  8. import { Markdown } from '../Markdown'
  9. import {
  10. generateFileCliCommand,
  11. generateMigrationCliCommand,
  12. generateSeedCliCommand,
  13. } from './SQLEditor.utils'
  14. import { TwoOptionToggle } from '@/components/ui/TwoOptionToggle'
  15. import { DOCS_URL } from '@/lib/constants'
  16. import { useSqlEditorV2StateSnapshot } from '@/state/sql-editor-v2'
  17. export interface DownloadSnippetModalProps extends ModalProps {
  18. id: string
  19. }
  20. const DownloadSnippetModal = ({ id, ...props }: DownloadSnippetModalProps) => {
  21. const snapV2 = useSqlEditorV2StateSnapshot()
  22. const snippet = snapV2.snippets[id]?.snippet
  23. const migrationName = snakeCase(snippet?.name)
  24. const [selectedView, setSelectedView] = useState<'CLI' | 'NPM'>('CLI')
  25. const SNIPPETS = [
  26. {
  27. id: 'migration',
  28. label: 'Migration',
  29. title: 'Download as migration',
  30. description: `Download the snippet in a new migration named \`${migrationName}\``,
  31. cli: generateMigrationCliCommand(id, migrationName),
  32. npm: generateMigrationCliCommand(id, migrationName, true),
  33. },
  34. {
  35. id: 'seed',
  36. label: 'Seed file',
  37. title: 'Download as seed file',
  38. description:
  39. 'If your query consists of sample data, append the snippet to the end of `briven/seed.sql`',
  40. cli: generateSeedCliCommand(id),
  41. npm: generateSeedCliCommand(id, true),
  42. },
  43. {
  44. id: 'sql',
  45. label: 'SQL file',
  46. title: 'Download as SQL file',
  47. description: `Download the snippet directly into a new SQL file named \`${migrationName}.sql\``,
  48. cli: generateFileCliCommand(id, migrationName),
  49. npm: generateFileCliCommand(id, migrationName, true),
  50. },
  51. ]
  52. return (
  53. <Modal
  54. hideFooter
  55. showCloseButton
  56. size="xlarge"
  57. header={<p>Download snippet as local migration file via the Briven CLI.</p>}
  58. {...props}
  59. >
  60. <div className="flex flex-col items-start justify-between gap-4 relative pt-2">
  61. <Tabs type="underlined" listClassNames="pl-5">
  62. {SNIPPETS.map((snippet) => {
  63. return (
  64. <Tabs.Panel key={snippet.id} id={snippet.id} label={snippet.label}>
  65. <Modal.Content className="py-0!">
  66. <div className="flex items-center justify-between mb-3">
  67. <div className="flex flex-col gap-y-1">
  68. <p className="text-base">{snippet.title}</p>
  69. <Markdown
  70. className="text-sm text-scale-1000 [&>p>code]:break-normal!"
  71. content={snippet.description}
  72. />
  73. </div>
  74. <TwoOptionToggle
  75. width={50}
  76. options={['CLI', 'NPM']}
  77. activeOption={selectedView}
  78. borderOverride="border-muted"
  79. onClickOption={() =>
  80. selectedView === 'CLI' ? setSelectedView('NPM') : setSelectedView('CLI')
  81. }
  82. />
  83. </div>
  84. <pre>
  85. <CodeBlock
  86. language="bash"
  87. className="language-bash prose dark:prose-dark max-w-none"
  88. >
  89. {selectedView === 'CLI' ? snippet.cli : snippet.npm}
  90. </CodeBlock>
  91. </pre>
  92. </Modal.Content>
  93. </Tabs.Panel>
  94. )
  95. })}
  96. </Tabs>
  97. <Modal.Content className="w-full flex items-center justify-between pt-0">
  98. <p className="text-xs text-lighter">Run this command from your project directory</p>
  99. <div className="flex justify-between items-center gap-x-2">
  100. <Button asChild type="default" icon={<ExternalLink strokeWidth={1.5} />}>
  101. <Link
  102. href={`${DOCS_URL}/guides/deployment/database-migrations`}
  103. target="_blank"
  104. rel="noreferrer"
  105. >
  106. About migrations
  107. </Link>
  108. </Button>
  109. <Button asChild type="default" icon={<ExternalLink strokeWidth={1.5} />}>
  110. <Link
  111. href={`${DOCS_URL}/guides/cli/local-development`}
  112. target="_blank"
  113. rel="noreferrer"
  114. >
  115. About CLI
  116. </Link>
  117. </Button>
  118. </div>
  119. </Modal.Content>
  120. </div>
  121. </Modal>
  122. )
  123. }
  124. export default DownloadSnippetModal