@@ -15,16 +15,15 @@ import { Button, Field } from "@/app/components";
1515import { useAuth } from "@/app/lib/context/AuthContext" ;
1616import { useApp } from "@/app/lib/context/AppContext" ;
1717import { apiFetch } from "@/app/lib/apiClient" ;
18- import { Document } from "@/app/lib/types/document" ;
1918import {
20- Collection ,
2119 JobStatusData ,
2220 CollectionResponse ,
2321 DocumentResponse ,
2422 CreateCollectionResponse ,
2523 DeleteCollectionResponse ,
2624 DocumentDetailResponse ,
2725} from "@/app/lib/types/knowledgeBase" ;
26+ import { Document , Collection } from "@/app/lib/types/document" ;
2827
2928export default function KnowledgeBasePage ( ) {
3029 const { sidebarCollapsed } = useApp ( ) ;
@@ -757,6 +756,23 @@ export default function KnowledgeBasePage() {
757756 }
758757 } ;
759758
759+ // Fetch document details and set preview
760+ const fetchAndPreviewDoc = async ( doc : Document ) => {
761+ setPreviewDoc ( doc ) ;
762+ if ( isAuthenticated ) {
763+ try {
764+ const data = await apiFetch < DocumentDetailResponse & Document > (
765+ `/api/document/${ doc . id } ` ,
766+ apiKey ?. key ?? "" ,
767+ ) ;
768+ const documentDetails = ( data . data || data ) as Document ;
769+ setPreviewDoc ( documentDetails ) ;
770+ } catch ( err ) {
771+ console . error ( "Failed to fetch document details:" , err ) ;
772+ }
773+ }
774+ } ;
775+
760776 // Toggle document selection
761777 const toggleDocumentSelection = ( documentId : string ) => {
762778 const newSelection = new Set ( selectedDocuments ) ;
@@ -839,7 +855,7 @@ export default function KnowledgeBasePage() {
839855 { /* Left Panel - Collections List */ }
840856 < div className = "w-1/3 border-r border-border flex flex-col" >
841857 { /* Create Button */ }
842- < div className = "p-6 flex justify-end" >
858+ < div className = "px-6 py-4 flex justify-end" >
843859 < Button
844860 variant = "outline"
845861 size = "sm"
@@ -852,7 +868,6 @@ export default function KnowledgeBasePage() {
852868 </ Button >
853869 </ div >
854870
855- { /* Collections List */ }
856871 < div className = "flex-1 overflow-y-auto px-6 pb-6" >
857872 { isLoading && collections . length === 0 ? (
858873 < div className = "text-center py-8 text-text-secondary" >
@@ -983,7 +998,6 @@ export default function KnowledgeBasePage() {
983998 < ChevronRightIcon className = "w-4 h-4 text-text-secondary" />
984999 </ button >
9851000
986- { /* Show selected documents */ }
9871001 { selectedDocuments . size > 0 && (
9881002 < div className = "mt-3 space-y-2" >
9891003 { Array . from ( selectedDocuments ) . map ( ( docId ) => {
@@ -1012,7 +1026,6 @@ export default function KnowledgeBasePage() {
10121026 ) }
10131027 </ div >
10141028
1015- { /* Create Button */ }
10161029 < div className = "flex justify-end" >
10171030 < Button
10181031 onClick = { handleCreateClick }
@@ -1028,7 +1041,6 @@ export default function KnowledgeBasePage() {
10281041 </ div >
10291042 ) : selectedCollection ? (
10301043 < >
1031- { /* Preview Header */ }
10321044 < div className = "p-6 border-b border-border" >
10331045 < div className = "flex items-start justify-between" >
10341046 < div className = "flex-1" >
@@ -1093,7 +1105,6 @@ export default function KnowledgeBasePage() {
10931105 </ div >
10941106 </ div >
10951107
1096- { /* Documents in Collection */ }
10971108 < div className = "flex-1 overflow-y-auto px-6 pt-6 pb-4" >
10981109 < div className = "flex items-center justify-between mb-4" >
10991110 < h3 className = "text-sm font-semibold text-text-primary" >
@@ -1103,30 +1114,11 @@ export default function KnowledgeBasePage() {
11031114 { selectedCollection . documents &&
11041115 selectedCollection . documents . length > 0 && (
11051116 < button
1106- onClick = { async ( ) => {
1117+ onClick = { ( ) => {
11071118 setShowDocPreviewModal ( true ) ;
1108- // Fetch the first document with signed_url
1109- const firstDoc = selectedCollection . documents ! [ 0 ] ;
1110- setPreviewDoc ( firstDoc ) ;
1111-
1112- if ( isAuthenticated ) {
1113- try {
1114- const data = await apiFetch <
1115- DocumentDetailResponse & Document
1116- > (
1117- `/api/document/${ firstDoc . id } ` ,
1118- apiKey ?. key ?? "" ,
1119- ) ;
1120- const documentDetails = ( data . data ||
1121- data ) as Document ;
1122- setPreviewDoc ( documentDetails ) ;
1123- } catch ( err ) {
1124- console . error (
1125- "Failed to fetch document details for preview:" ,
1126- err ,
1127- ) ;
1128- }
1129- }
1119+ fetchAndPreviewDoc (
1120+ selectedCollection . documents ! [ 0 ] ,
1121+ ) ;
11301122 } }
11311123 className = "text-xs px-3 py-1.5 rounded-md border border-border bg-bg-secondary text-text-primary hover:bg-neutral-100 transition-colors"
11321124 >
@@ -1310,58 +1302,27 @@ export default function KnowledgeBasePage() {
13101302 </ div >
13111303 </ Modal >
13121304
1313- { showDocPreviewModal && ! ! selectedCollection ?. documents && (
1314- < div
1315- className = "fixed inset-0 bg-black/30 backdrop-blur-[2px] flex items-center justify-center z-50"
1316- onClick = { ( ) => {
1317- setShowDocPreviewModal ( false ) ;
1318- setPreviewDoc ( null ) ;
1319- } }
1320- >
1305+ < Modal
1306+ open = { showDocPreviewModal && ! ! selectedCollection ?. documents }
1307+ onClose = { ( ) => {
1308+ setShowDocPreviewModal ( false ) ;
1309+ setPreviewDoc ( null ) ;
1310+ } }
1311+ title = "Document Preview"
1312+ maxWidth = "max-w-5xl"
1313+ maxHeight = "h-[80vh]"
1314+ >
1315+ < div className = "flex flex-1 overflow-hidden h-full" >
13211316 < div
13221317 className = "bg-white rounded-2xl shadow-xl border border-border w-full max-w-5xl h-[85vh] flex flex-col"
13231318 onClick = { ( e ) => e . stopPropagation ( ) }
13241319 >
1325- < div className = "flex items-center justify-between px-6 py-4 shrink-0 border-b border-border" >
1326- < h2 className = "text-lg font-semibold text-text-primary" >
1327- Document Preview
1328- </ h2 >
1329- < button
1330- onClick = { ( ) => {
1331- setShowDocPreviewModal ( false ) ;
1332- setPreviewDoc ( null ) ;
1333- } }
1334- className = "p-1 rounded-md text-text-secondary transition-colors hover:bg-neutral-100 hover:text-text-primary cursor-pointer"
1335- >
1336- < CloseIcon className = "w-5 h-5" />
1337- </ button >
1338- </ div >
1339-
1340- { /* Two-pane body */ }
13411320 < div className = "flex flex-1 min-h-0" >
1342- { /* Left pane — doc list */ }
13431321 < div className = "w-56 shrink-0 border-r border-border overflow-y-auto" >
13441322 { selectedCollection ?. documents ?. map ( ( doc : Document ) => (
13451323 < button
13461324 key = { doc . id }
1347- onClick = { async ( ) => {
1348- setPreviewDoc ( doc ) ;
1349- if ( isAuthenticated ) {
1350- try {
1351- const data = await apiFetch <
1352- DocumentDetailResponse & Document
1353- > ( `/api/document/${ doc . id } ` , apiKey ?. key ?? "" ) ;
1354- const documentDetails = ( data . data ||
1355- data ) as Document ;
1356- setPreviewDoc ( documentDetails ) ;
1357- } catch ( err ) {
1358- console . error (
1359- "Failed to fetch document details:" ,
1360- err ,
1361- ) ;
1362- }
1363- }
1364- } }
1325+ onClick = { ( ) => fetchAndPreviewDoc ( doc ) }
13651326 className = { `w-full text-left px-4 py-3 border-b border-border transition-colors ${
13661327 previewDoc ?. id === doc . id
13671328 ? "bg-neutral-100"
@@ -1380,7 +1341,6 @@ export default function KnowledgeBasePage() {
13801341 ) ) }
13811342 </ div >
13821343
1383- { /* Right pane — preview */ }
13841344 < div className = "flex-1 min-h-0 bg-neutral-50" >
13851345 { previewDoc ?. signed_url ? (
13861346 < iframe
@@ -1402,7 +1362,7 @@ export default function KnowledgeBasePage() {
14021362 </ div >
14031363 </ div >
14041364 </ div >
1405- ) }
1365+ </ Modal >
14061366 </ div >
14071367 ) ;
14081368}
0 commit comments