Repository navigation
Expand file tree
/
Copy pathDatabaseDocs.tsx
More file actions
264 lines (241 loc) · 9.92 KB
/
Copy pathDatabaseDocs.tsx
File metadata and controls
264 lines (241 loc) · 9.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
"use client";
import React, { useState } from "react";
import { FileText, Loader2, Search, Sparkles, Download } from "lucide-react";
import { cn } from "@/lib/utils";
import { TableSchema } from "@/lib/types";
import { renderInline } from "@/components/rich-text";
import { downloadText } from "@/lib/export/download";
interface DatabaseDocsProps {
schema: TableSchema[];
schemaContext: string;
databaseType?: string;
}
interface ParsedSchemaTable {
name: string;
rowCount?: number;
columns?: { name: string; type: string; isPrimary?: boolean; isNullable?: boolean }[];
}
export function DatabaseDocs({ schema, schemaContext, databaseType }: DatabaseDocsProps) {
const [search, setSearch] = useState("");
const [aiDocs, setAiDocs] = useState("");
const [isAiLoading, setIsAiLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const filteredSchema = schema.filter(
(t) =>
t.name.toLowerCase().includes(search.toLowerCase()) ||
t.columns?.some((c) => c.name.toLowerCase().includes(search.toLowerCase())),
);
const generateAiDocs = async () => {
setIsAiLoading(true);
setError(null);
setAiDocs("");
try {
let filteredSchemaStr = "";
if (schemaContext) {
try {
const tables = JSON.parse(schemaContext);
filteredSchemaStr = tables
.slice(0, 50)
.map((t: ParsedSchemaTable) => {
const cols =
t.columns
?.map(
(c) =>
`${c.name} (${c.type}${c.isPrimary ? ", PK" : ""}${c.isNullable === false ? ", NOT NULL" : ""})`,
)
.join(", ") || "";
return `Table: ${t.name} (${t.rowCount || 0} rows)\nColumns: ${cols}`;
})
.join("\n\n");
} catch {
filteredSchemaStr = schemaContext.substring(0, 5000);
}
}
const response = await fetch("/api/ai/describe-schema", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
schemaContext: filteredSchemaStr,
databaseType,
mode: "full",
}),
});
if (!response.ok) {
const err = await response.json();
throw new Error(err.error || "Documentation generation failed");
}
const reader = response.body?.getReader();
if (!reader) throw new Error("No reader");
let full = "";
while (true) {
const { done, value } = await reader.read();
if (done) break;
full += new TextDecoder().decode(value);
setAiDocs(full);
}
} catch (err) {
setError(err instanceof Error ? err.message : "Unknown error");
} finally {
setIsAiLoading(false);
}
};
const exportMarkdown = () => {
let md = `# Database Documentation\n\n`;
md += `**Type:** ${databaseType || "Unknown"}\n`;
md += `**Tables:** ${schema.length}\n\n`;
if (aiDocs) {
md += `## AI Analysis\n\n${aiDocs}\n\n---\n\n`;
}
md += `## Table Reference\n\n`;
for (const table of schema) {
md += `### ${table.name}\n\n`;
if (table.rowCount !== undefined) md += `Rows: ${table.rowCount.toLocaleString()}\n\n`;
if (table.columns && table.columns.length > 0) {
md += `| Column | Type | Primary | Nullable |\n|--------|------|---------|----------|\n`;
for (const col of table.columns) {
md += `| ${col.name} | ${col.type} | ${col.isPrimary ? "Yes" : ""} | ${col.nullable !== false ? "Yes" : "No"} |\n`;
}
md += "\n";
}
}
downloadText(md, "text/markdown", "database-docs.md");
};
// Simple markdown rendering for AI docs
const renderMarkdown = (text: string) => {
return text.split("\n").map((line, i) => {
if (line.startsWith("## "))
return (
<h2 key={i} className="text-xs font-medium text-fg mt-4 mb-2">
{line.slice(3)}
</h2>
);
if (line.startsWith("### "))
return (
<h3 key={i} className="text-xs font-medium text-fg-secondary mt-3 mb-1">
{line.slice(4)}
</h3>
);
if (line.startsWith("- ")) {
return (
<li key={i} className="text-xs text-fg-tertiary ml-4 leading-relaxed">
{renderInline(line.slice(2))}
</li>
);
}
if (line.match(/^\d+\.\s/)) {
return (
<li key={i} className="text-xs text-fg-tertiary ml-4 leading-relaxed list-decimal">
{renderInline(line)}
</li>
);
}
if (line.trim()) {
return (
<p key={i} className="text-xs text-fg-tertiary leading-relaxed">
{renderInline(line)}
</p>
);
}
return <div key={i} className="h-1.5" />;
});
};
return (
<div className="h-full flex flex-col bg-sunken">
<div className="flex items-center justify-between px-4 py-2 border-b border-hairline bg-surface">
<div className="flex items-center gap-2">
<div className="p-1 rounded bg-teal-500/10">
<FileText strokeWidth={1.5} className="w-3 h-3 text-teal-400" />
</div>
<span className="text-xs font-medium text-teal-400">Database Docs</span>
<span className="text-[0.625rem] text-fg-muted font-mono">{schema.length} tables</span>
</div>
<div className="flex items-center gap-1.5">
<button
onClick={generateAiDocs}
disabled={isAiLoading}
className={cn(
"flex items-center gap-1 px-2.5 py-1 rounded-lg text-xs font-medium transition-colors",
isAiLoading ? "bg-teal-600/20 text-teal-400 cursor-wait" : "bg-teal-600 hover:bg-teal-500 text-white",
)}
>
{isAiLoading && <Loader2 strokeWidth={1.5} className="w-3 h-3 animate-spin" />}
{!isAiLoading && <Sparkles strokeWidth={1.5} className="w-3 h-3" />}
{aiDocs ? "Regenerate" : "AI Describe"}
</button>
<button
onClick={exportMarkdown}
className="flex items-center gap-1 px-2.5 py-1 rounded-lg bg-fill text-fg-tertiary text-xs font-medium hover:bg-fill-strong transition-colors"
>
<Download strokeWidth={1.5} className="w-3 h-3" /> Export MD
</button>
</div>
</div>
<div className="px-4 py-2 border-b border-hairline bg-surface">
<div className="relative">
<Search strokeWidth={1.5} className="absolute left-2.5 top-1/2 -translate-y-1/2 w-3 h-3 text-fg-muted" />
<input
value={search}
onChange={(e) => setSearch(e.target.value)}
placeholder="Search tables or columns..."
className="w-full bg-overlay border border-hairline-strong rounded-lg pl-7 pr-3 py-1.5 text-xs text-fg placeholder:text-fg-subtle outline-none focus:border-teal-500/30"
/>
</div>
</div>
<div className="flex-1 overflow-auto p-4 space-y-3">
{error && (
<div className="bg-red-500/10 border border-red-500/20 rounded-lg p-3 text-xs text-red-400">{error}</div>
)}
{(aiDocs || isAiLoading) && (
<div className="bg-teal-500/5 border border-teal-500/10 rounded-lg p-4 mb-4">
<div className="flex items-center gap-2 mb-3">
<Sparkles strokeWidth={1.5} className="w-3 h-3 text-teal-400" />
<span className="text-xs font-medium text-teal-400">AI-Generated Documentation</span>
{isAiLoading && <Loader2 strokeWidth={1.5} className="w-3 h-3 animate-spin text-teal-400" />}
</div>
{aiDocs && <div className="prose prose-invert prose-xs max-w-none">{renderMarkdown(aiDocs)}</div>}
</div>
)}
<h3 className="text-xs font-medium text-fg-tertiary">Table Reference</h3>
{filteredSchema.map((table) => (
<div key={table.name} className="bg-surface border border-hairline rounded-lg overflow-hidden">
<div className="px-3 py-2 flex items-center justify-between">
<div className="flex items-center gap-2">
<span className="text-xs font-medium text-fg">{table.name}</span>
{table.rowCount !== undefined && (
<span className="text-xs text-fg-muted font-mono">{table.rowCount.toLocaleString()} rows</span>
)}
</div>
<span className="text-xs text-fg-subtle">{table.columns?.length || 0} columns</span>
</div>
{table.columns && table.columns.length > 0 && (
<div className="border-t border-hairline">
<table className="w-full text-xs">
<thead>
<tr className="text-fg-muted">
<th className="text-left px-3 py-1 font-normal">Column</th>
<th className="text-left px-3 py-1 font-normal">Type</th>
<th className="text-left px-3 py-1 font-normal">PK</th>
<th className="text-left px-3 py-1 font-normal">Nullable</th>
</tr>
</thead>
<tbody>
{table.columns.map((col) => (
<tr key={col.name} className="border-t border-hairline hover:bg-fill-subtle">
<td className="px-3 py-1 text-fg-secondary font-mono">{col.name}</td>
<td className="px-3 py-1 text-fg-muted font-mono">{col.type}</td>
<td className="px-3 py-1">
{col.isPrimary && <span className="text-amber-400 text-[0.625rem] font-medium">PK</span>}
</td>
<td className="px-3 py-1 text-fg-subtle">{col.nullable !== false ? "Yes" : "No"}</td>
</tr>
))}
</tbody>
</table>
</div>
)}
</div>
))}
</div>
</div>
);
}