DatabaseDiffPanel.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { CircleAlert, Database, Download, Loader2, Wind } from 'lucide-react'
  2. import Link from 'next/link'
  3. import { toast } from 'sonner'
  4. import { Button, Card, CardContent, CardHeader, CardTitle, Skeleton } from 'ui'
  5. import { DiffEditor } from '@/components/ui/DiffEditor'
  6. interface DatabaseDiffPanelProps {
  7. diffContent?: string
  8. isLoading: boolean
  9. error?: any
  10. showRefreshButton?: boolean
  11. currentBranchRef?: string
  12. }
  13. export const DatabaseDiffPanel = ({
  14. diffContent,
  15. isLoading,
  16. error,
  17. currentBranchRef,
  18. }: DatabaseDiffPanelProps) => {
  19. if (isLoading) {
  20. return (
  21. <div className="flex flex-1 min-h-0 flex-col">
  22. <div className="flex flex-1 min-h-[400px] flex-col rounded-md border border-border bg-surface-100">
  23. <div className="flex shrink-0 items-center gap-2 border-b border-border px-4 py-3">
  24. <Loader2
  25. size={16}
  26. strokeWidth={1.5}
  27. className="animate-spin text-foreground-muted"
  28. aria-hidden
  29. />
  30. <span className="text-sm text-foreground-light">Loading database diff…</span>
  31. </div>
  32. <div className="min-h-0 flex-1 p-4">
  33. <Skeleton className="h-full w-full rounded-sm" />
  34. </div>
  35. </div>
  36. </div>
  37. )
  38. }
  39. if (error)
  40. return (
  41. <div className="p-6 text-center">
  42. <CircleAlert size={32} strokeWidth={1.5} className="text-foreground-muted mx-auto mb-8" />
  43. <h3 className="mb-1">Error loading branch diff</h3>
  44. <p className="text-sm text-foreground-light">
  45. Please try again in a few minutes and contact support if the problem persists.
  46. </p>
  47. </div>
  48. )
  49. if (!diffContent || diffContent.trim() === '') {
  50. return (
  51. <div className="p-6 text-center">
  52. <Wind size={32} strokeWidth={1.5} className="text-foreground-muted mx-auto mb-8" />
  53. <h3 className="mb-1">No changes detected between branches</h3>
  54. <p className="text-sm text-foreground-light">
  55. Any changes to your database schema will be shown here for review
  56. </p>
  57. </div>
  58. )
  59. }
  60. return (
  61. <Card className="flex flex-1 min-h-0 flex-col">
  62. <CardHeader className="flex shrink-0 flex-row items-center justify-between space-y-0 py-3">
  63. <CardTitle>
  64. <Link
  65. href={`/project/${currentBranchRef}/database/schema`}
  66. className="flex items-center gap-2"
  67. >
  68. <Database strokeWidth={1.5} size={16} className="text-foreground-muted" />
  69. Schema Changes
  70. </Link>
  71. </CardTitle>
  72. <Button
  73. type="default"
  74. size="tiny"
  75. icon={<Download strokeWidth={1.5} size={14} className="text-foreground-light" />}
  76. className="mt-0"
  77. onClick={() => {
  78. if (!diffContent) return
  79. const now = new Date()
  80. const pad = (n: number) => n.toString().padStart(2, '0')
  81. const timestamp =
  82. now.getFullYear().toString() +
  83. pad(now.getMonth() + 1) +
  84. pad(now.getDate()) +
  85. pad(now.getHours()) +
  86. pad(now.getMinutes()) +
  87. pad(now.getSeconds())
  88. const filename = `${timestamp}_migration.sql`
  89. const blob = new Blob([diffContent], { type: 'text/plain;charset=utf-8;' })
  90. const url = window.URL.createObjectURL(blob)
  91. const a = document.createElement('a')
  92. a.href = url
  93. a.setAttribute('download', filename)
  94. document.body.appendChild(a)
  95. a.click()
  96. document.body.removeChild(a)
  97. window.URL.revokeObjectURL(url)
  98. toast.success('Migration file downloaded!')
  99. }}
  100. >
  101. Download as migration
  102. </Button>
  103. </CardHeader>
  104. <CardContent className="flex min-h-0 flex-1 flex-col p-0">
  105. <div className="min-h-0 flex-1">
  106. <DiffEditor
  107. language="sql"
  108. original=""
  109. modified={diffContent}
  110. options={{ readOnly: true }}
  111. />
  112. </div>
  113. </CardContent>
  114. </Card>
  115. )
  116. }