- Add POST /api/knowledge/import endpoint for .md and .zip files - Add GET /api/knowledge/export endpoint with markdown/json formats - Import parses frontmatter (title, tags, status, visibility) - Export includes frontmatter in markdown format - Add ImportExportActions component with drag-and-drop UI - Add import progress dialog with success/error summary - Add export dropdown with format selection - Include comprehensive test suite - Support bulk import with detailed error reporting
This commit is contained in:
@@ -4,6 +4,7 @@ import { useState, useMemo } from "react";
|
||||
import { EntryStatus } from "@mosaic/shared";
|
||||
import { EntryList } from "@/components/knowledge/EntryList";
|
||||
import { EntryFilters } from "@/components/knowledge/EntryFilters";
|
||||
import { ImportExportActions } from "@/components/knowledge";
|
||||
import { mockEntries, mockTags } from "@/lib/api/knowledge";
|
||||
import Link from "next/link";
|
||||
import { Plus } from "lucide-react";
|
||||
@@ -99,22 +100,35 @@ export default function KnowledgePage() {
|
||||
return (
|
||||
<main className="container mx-auto px-4 py-8 max-w-5xl">
|
||||
{/* Header */}
|
||||
<div className="mb-8 flex items-center justify-between">
|
||||
<div>
|
||||
<h1 className="text-3xl font-bold text-gray-900">Knowledge Base</h1>
|
||||
<p className="text-gray-600 mt-2">
|
||||
Documentation, guides, and knowledge entries
|
||||
</p>
|
||||
<div className="mb-8">
|
||||
<div className="flex items-center justify-between mb-4">
|
||||
<div>
|
||||
<h1 className="text-3xl font-bold text-gray-900">Knowledge Base</h1>
|
||||
<p className="text-gray-600 mt-2">
|
||||
Documentation, guides, and knowledge entries
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* Create button */}
|
||||
<Link
|
||||
href="/knowledge/new"
|
||||
className="inline-flex items-center gap-2 px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition-colors shadow-sm"
|
||||
>
|
||||
<Plus className="w-5 h-5" />
|
||||
<span>Create Entry</span>
|
||||
</Link>
|
||||
</div>
|
||||
|
||||
{/* Create button */}
|
||||
<Link
|
||||
href="/knowledge/new"
|
||||
className="inline-flex items-center gap-2 px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition-colors shadow-sm"
|
||||
>
|
||||
<Plus className="w-5 h-5" />
|
||||
<span>Create Entry</span>
|
||||
</Link>
|
||||
{/* Import/Export Actions */}
|
||||
<div className="flex justify-end">
|
||||
<ImportExportActions
|
||||
onImportComplete={() => {
|
||||
// TODO: Refresh the entry list when real API is connected
|
||||
// For now, this would trigger a refetch of the entries
|
||||
window.location.reload();
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Filters */}
|
||||
|
||||
316
apps/web/src/components/knowledge/ImportExportActions.tsx
Normal file
316
apps/web/src/components/knowledge/ImportExportActions.tsx
Normal file
@@ -0,0 +1,316 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useRef } from "react";
|
||||
import { Upload, Download, Loader2, CheckCircle2, XCircle } from "lucide-react";
|
||||
|
||||
interface ImportResult {
|
||||
filename: string;
|
||||
success: boolean;
|
||||
entryId?: string;
|
||||
slug?: string;
|
||||
title?: string;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
interface ImportResponse {
|
||||
success: boolean;
|
||||
totalFiles: number;
|
||||
imported: number;
|
||||
failed: number;
|
||||
results: ImportResult[];
|
||||
}
|
||||
|
||||
interface ImportExportActionsProps {
|
||||
selectedEntryIds?: string[];
|
||||
onImportComplete?: () => void;
|
||||
}
|
||||
|
||||
export function ImportExportActions({
|
||||
selectedEntryIds = [],
|
||||
onImportComplete,
|
||||
}: ImportExportActionsProps) {
|
||||
const [isImporting, setIsImporting] = useState(false);
|
||||
const [isExporting, setIsExporting] = useState(false);
|
||||
const [importResult, setImportResult] = useState<ImportResponse | null>(null);
|
||||
const [showImportDialog, setShowImportDialog] = useState(false);
|
||||
const fileInputRef = useRef<HTMLInputElement>(null);
|
||||
|
||||
/**
|
||||
* Handle import file selection
|
||||
*/
|
||||
const handleImportClick = () => {
|
||||
fileInputRef.current?.click();
|
||||
};
|
||||
|
||||
/**
|
||||
* Handle file upload and import
|
||||
*/
|
||||
const handleFileChange = async (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const file = e.target.files?.[0];
|
||||
if (!file) return;
|
||||
|
||||
// Validate file type
|
||||
if (!file.name.endsWith(".md") && !file.name.endsWith(".zip")) {
|
||||
alert("Please upload a .md or .zip file");
|
||||
return;
|
||||
}
|
||||
|
||||
setIsImporting(true);
|
||||
setShowImportDialog(true);
|
||||
setImportResult(null);
|
||||
|
||||
try {
|
||||
const formData = new FormData();
|
||||
formData.append("file", file);
|
||||
|
||||
const response = await fetch("/api/knowledge/import", {
|
||||
method: "POST",
|
||||
body: formData,
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const error = await response.json();
|
||||
throw new Error(error.message || "Import failed");
|
||||
}
|
||||
|
||||
const result: ImportResponse = await response.json();
|
||||
setImportResult(result);
|
||||
|
||||
// Notify parent component
|
||||
if (result.imported > 0 && onImportComplete) {
|
||||
onImportComplete();
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Import error:", error);
|
||||
alert(error instanceof Error ? error.message : "Failed to import file");
|
||||
setShowImportDialog(false);
|
||||
} finally {
|
||||
setIsImporting(false);
|
||||
// Reset file input
|
||||
if (fileInputRef.current) {
|
||||
fileInputRef.current.value = "";
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Handle export
|
||||
*/
|
||||
const handleExport = async (format: "markdown" | "json" = "markdown") => {
|
||||
setIsExporting(true);
|
||||
|
||||
try {
|
||||
// Build query params
|
||||
const params = new URLSearchParams({
|
||||
format,
|
||||
});
|
||||
|
||||
// Add selected entry IDs if any
|
||||
if (selectedEntryIds.length > 0) {
|
||||
selectedEntryIds.forEach((id) => params.append("entryIds", id));
|
||||
}
|
||||
|
||||
const response = await fetch(`/api/knowledge/export?${params.toString()}`, {
|
||||
method: "GET",
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error("Export failed");
|
||||
}
|
||||
|
||||
// Get filename from Content-Disposition header
|
||||
const contentDisposition = response.headers.get("Content-Disposition");
|
||||
const filenameMatch = contentDisposition?.match(/filename="(.+)"/);
|
||||
const filename = filenameMatch?.[1] || `knowledge-export-${format}.zip`;
|
||||
|
||||
// Download file
|
||||
const blob = await response.blob();
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
const a = document.createElement("a");
|
||||
a.href = url;
|
||||
a.download = filename;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
window.URL.revokeObjectURL(url);
|
||||
document.body.removeChild(a);
|
||||
} catch (error) {
|
||||
console.error("Export error:", error);
|
||||
alert("Failed to export entries");
|
||||
} finally {
|
||||
setIsExporting(false);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Close import dialog
|
||||
*/
|
||||
const handleCloseImportDialog = () => {
|
||||
setShowImportDialog(false);
|
||||
setImportResult(null);
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
{/* Action Buttons */}
|
||||
<div className="flex items-center gap-2">
|
||||
{/* Import Button */}
|
||||
<button
|
||||
onClick={handleImportClick}
|
||||
disabled={isImporting}
|
||||
className="inline-flex items-center gap-2 px-4 py-2 bg-gray-100 text-gray-700 rounded-lg hover:bg-gray-200 transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
|
||||
title="Import entries from .md or .zip file"
|
||||
>
|
||||
{isImporting ? (
|
||||
<Loader2 className="w-5 h-5 animate-spin" />
|
||||
) : (
|
||||
<Upload className="w-5 h-5" />
|
||||
)}
|
||||
<span>Import</span>
|
||||
</button>
|
||||
|
||||
{/* Export Dropdown */}
|
||||
<div className="relative group">
|
||||
<button
|
||||
disabled={isExporting}
|
||||
className="inline-flex items-center gap-2 px-4 py-2 bg-gray-100 text-gray-700 rounded-lg hover:bg-gray-200 transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
|
||||
title="Export entries"
|
||||
>
|
||||
{isExporting ? (
|
||||
<Loader2 className="w-5 h-5 animate-spin" />
|
||||
) : (
|
||||
<Download className="w-5 h-5" />
|
||||
)}
|
||||
<span>Export</span>
|
||||
</button>
|
||||
|
||||
{/* Dropdown Menu */}
|
||||
<div className="absolute right-0 mt-1 w-48 bg-white rounded-lg shadow-lg border border-gray-200 opacity-0 invisible group-hover:opacity-100 group-hover:visible transition-all z-10">
|
||||
<div className="py-1">
|
||||
<button
|
||||
onClick={() => handleExport("markdown")}
|
||||
className="w-full text-left px-4 py-2 hover:bg-gray-100 text-sm text-gray-700"
|
||||
>
|
||||
Export as Markdown
|
||||
</button>
|
||||
<button
|
||||
onClick={() => handleExport("json")}
|
||||
className="w-full text-left px-4 py-2 hover:bg-gray-100 text-sm text-gray-700"
|
||||
>
|
||||
Export as JSON
|
||||
</button>
|
||||
{selectedEntryIds.length > 0 && (
|
||||
<div className="border-t border-gray-200 mt-1 pt-1 px-4 py-2 text-xs text-gray-500">
|
||||
{selectedEntryIds.length} selected
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Hidden File Input */}
|
||||
<input
|
||||
ref={fileInputRef}
|
||||
type="file"
|
||||
accept=".md,.zip"
|
||||
onChange={handleFileChange}
|
||||
className="hidden"
|
||||
/>
|
||||
|
||||
{/* Import Result Dialog */}
|
||||
{showImportDialog && (
|
||||
<div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50">
|
||||
<div className="bg-white rounded-lg shadow-xl max-w-2xl w-full mx-4 max-h-[80vh] flex flex-col">
|
||||
{/* Header */}
|
||||
<div className="px-6 py-4 border-b border-gray-200">
|
||||
<h2 className="text-xl font-semibold text-gray-900">
|
||||
{isImporting ? "Importing..." : "Import Results"}
|
||||
</h2>
|
||||
</div>
|
||||
|
||||
{/* Content */}
|
||||
<div className="px-6 py-4 overflow-y-auto flex-1">
|
||||
{isImporting && (
|
||||
<div className="flex items-center justify-center py-8">
|
||||
<Loader2 className="w-8 h-8 animate-spin text-blue-600" />
|
||||
<span className="ml-3 text-gray-600">Processing file...</span>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{importResult && (
|
||||
<div>
|
||||
{/* Summary */}
|
||||
<div className="grid grid-cols-3 gap-4 mb-6">
|
||||
<div className="bg-gray-50 rounded-lg p-4">
|
||||
<div className="text-sm text-gray-600">Total Files</div>
|
||||
<div className="text-2xl font-semibold text-gray-900">
|
||||
{importResult.totalFiles}
|
||||
</div>
|
||||
</div>
|
||||
<div className="bg-green-50 rounded-lg p-4">
|
||||
<div className="text-sm text-green-600">Imported</div>
|
||||
<div className="text-2xl font-semibold text-green-700">
|
||||
{importResult.imported}
|
||||
</div>
|
||||
</div>
|
||||
<div className="bg-red-50 rounded-lg p-4">
|
||||
<div className="text-sm text-red-600">Failed</div>
|
||||
<div className="text-2xl font-semibold text-red-700">
|
||||
{importResult.failed}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Results List */}
|
||||
{importResult.results.length > 0 && (
|
||||
<div className="space-y-2">
|
||||
<h3 className="font-medium text-gray-900 mb-2">Details</h3>
|
||||
{importResult.results.map((result, index) => (
|
||||
<div
|
||||
key={index}
|
||||
className={`flex items-start gap-3 p-3 rounded-lg ${
|
||||
result.success ? "bg-green-50" : "bg-red-50"
|
||||
}`}
|
||||
>
|
||||
{result.success ? (
|
||||
<CheckCircle2 className="w-5 h-5 text-green-600 flex-shrink-0 mt-0.5" />
|
||||
) : (
|
||||
<XCircle className="w-5 h-5 text-red-600 flex-shrink-0 mt-0.5" />
|
||||
)}
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="font-medium text-sm text-gray-900 truncate">
|
||||
{result.title || result.filename}
|
||||
</div>
|
||||
{result.success ? (
|
||||
<div className="text-xs text-gray-600">
|
||||
{result.slug && `Slug: ${result.slug}`}
|
||||
</div>
|
||||
) : (
|
||||
<div className="text-xs text-red-600">{result.error}</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Footer */}
|
||||
{!isImporting && (
|
||||
<div className="px-6 py-4 border-t border-gray-200 flex justify-end">
|
||||
<button
|
||||
onClick={handleCloseImportDialog}
|
||||
className="px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition-colors"
|
||||
>
|
||||
Close
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -6,3 +6,4 @@ export { EntryViewer } from "./EntryViewer";
|
||||
export { EntryEditor } from "./EntryEditor";
|
||||
export { EntryMetadata } from "./EntryMetadata";
|
||||
export { VersionHistory } from "./VersionHistory";
|
||||
export { ImportExportActions } from "./ImportExportActions";
|
||||
|
||||
Reference in New Issue
Block a user