Spaces:
Running
Running
File size: 6,201 Bytes
c2ea5ed |
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 |
/**
* Context Documents Section Component
*
* Main section for managing context documents in the TraceDetailsModal
*/
import React, { useState, useEffect } from "react";
import { Button } from "@/components/ui/button";
import { FileText, Plus } from "lucide-react";
import { useContextDocuments } from "@/hooks/useContextDocuments";
import { ContextDocumentCard } from "@/components/features/context/ContextDocumentCard";
import { ContextUploadDialog } from "@/components/features/context/ContextUploadDialog";
import { ContextDocumentModal } from "@/components/features/context/ContextDocumentModal";
import { ContextDocument } from "@/types/context";
interface ContextDocumentsSectionProps {
traceId: string;
showHeader?: boolean; // New prop to control header visibility
triggerAdd?: number; // Prop to trigger add dialog from external button
}
export function ContextDocumentsSection({
traceId,
showHeader = true, // Default to showing header for backward compatibility
triggerAdd,
}: ContextDocumentsSectionProps) {
const {
documents,
loading,
error,
loadDocuments,
deleteDocument,
updateDocument,
} = useContextDocuments();
const [isUploadDialogOpen, setIsUploadDialogOpen] = useState(false);
const [selectedDocument, setSelectedDocument] =
useState<ContextDocument | null>(null);
const [isModalOpen, setIsModalOpen] = useState(false);
// Load documents when component mounts or traceId changes
useEffect(() => {
if (traceId) {
loadDocuments(traceId);
}
}, [traceId, loadDocuments]);
// Trigger add dialog when external button is clicked
useEffect(() => {
if (triggerAdd && triggerAdd > 0) {
setIsUploadDialogOpen(true);
}
}, [triggerAdd]);
const handleAddContext = () => {
setIsUploadDialogOpen(true);
};
const handleDeleteDocument = async (contextId: string) => {
const success = await deleteDocument(traceId, contextId);
if (success) {
// Document has been removed from state by the hook
}
};
const handleViewEdit = (document: ContextDocument) => {
setSelectedDocument(document);
setIsModalOpen(true);
};
const handleModalClose = () => {
setIsModalOpen(false);
setSelectedDocument(null);
};
const handleDocumentSave = async (updates: any) => {
if (!selectedDocument) return false;
const updatedDocument = await updateDocument(
traceId,
selectedDocument.id,
updates
);
if (updatedDocument) {
setSelectedDocument(updatedDocument);
return true;
}
return false;
};
const handleDocumentDelete = async () => {
if (!selectedDocument) return false;
const success = await deleteDocument(traceId, selectedDocument.id);
if (success) {
setIsModalOpen(false);
setSelectedDocument(null);
}
return success;
};
if (error) {
return (
<div className="p-4 rounded-lg border border-destructive/20 bg-destructive/5">
<div className="flex items-center gap-2 text-destructive mb-2">
<FileText className="h-5 w-5" />
<span className="font-medium">Context Documents - Error</span>
</div>
<p className="text-sm text-destructive mb-2">{error}</p>
<Button
variant="outline"
size="sm"
onClick={() => loadDocuments(traceId)}
>
Retry
</Button>
</div>
);
}
return (
<>
<div className="space-y-4">
{showHeader && (
<div className="flex items-center justify-between">
<div className="flex items-center gap-2">
<FileText className="h-5 w-5" />
<span className="font-medium">
Context Documents ({documents.length})
</span>
</div>
<Button
onClick={handleAddContext}
disabled={loading}
size="sm"
className="bg-primary hover:bg-primary/90"
data-context-add-button
>
<Plus className="h-3 w-3 mr-2" />
Add Context
</Button>
</div>
)}
{loading && documents.length === 0 ? (
<div className="text-center py-8">
<div className="w-8 h-8 border-2 border-primary border-t-transparent rounded-full animate-spin mx-auto mb-2" />
<p className="text-sm text-muted-foreground">
Loading context documents...
</p>
</div>
) : documents.length === 0 ? (
<div className="text-center py-8 text-muted-foreground">
<FileText className="h-12 w-12 mx-auto mb-3 opacity-50" />
<p className="font-medium mb-2">No context documents yet</p>
<p className="text-sm mb-4">
Add domain knowledge, schemas, or guidelines to improve extraction
quality.
</p>
<Button
onClick={handleAddContext}
size="sm"
data-context-add-button
>
<Plus className="h-4 w-4 mr-2" />
Add Your First Context Document
</Button>
</div>
) : (
<div className="space-y-3">
{documents.map((document) => (
<ContextDocumentCard
key={document.id}
document={document}
onDelete={() => handleDeleteDocument(document.id)}
onViewEdit={() => handleViewEdit(document)}
/>
))}
</div>
)}
</div>
{/* Upload Dialog */}
<ContextUploadDialog
isOpen={isUploadDialogOpen}
onClose={() => setIsUploadDialogOpen(false)}
traceId={traceId}
onSuccess={() => {
setIsUploadDialogOpen(false);
loadDocuments(traceId);
}}
/>
{/* Context Document Modal */}
{selectedDocument && (
<ContextDocumentModal
document={selectedDocument}
isOpen={isModalOpen}
onClose={handleModalClose}
onSave={handleDocumentSave}
onDelete={handleDocumentDelete}
/>
)}
</>
);
}
|