import React, { useEffect, useMemo, useState } from 'react';
import DocumentSummarySection from './DocumentSummarySection';
import { buildDocumentMetadataItems, extractDocumentMetadataPayload } from './documentMetadata';
const DocumentInfoPanel = ({
document,
summaryProps = {},
metadataItems: metadataItemsProp,
metadataPayload: metadataPayloadProp,
metadataTabLabel = 'Metadata',
detailsTabLabel = 'Details',
contentConfig: contentConfigProp = null,
activeTab: controlledActiveTab,
onTabChange,
defaultTabId = 'details',
resetKey = null,
classNamePrefix = 'document-info',
hideTabNavWhenSingle = true,
}) => {
const base = classNamePrefix;
const metadataItems = useMemo(() => {
if (Array.isArray(metadataItemsProp) && metadataItemsProp.length) {
return metadataItemsProp;
}
return buildDocumentMetadataItems(document);
}, [metadataItemsProp, document]);
const metadataPayload = useMemo(() => {
if (metadataPayloadProp !== undefined) {
return metadataPayloadProp;
}
return extractDocumentMetadataPayload(document);
}, [metadataPayloadProp, document]);
const contentConfig = contentConfigProp || null;
const contentEnabled = Boolean(contentConfig && (contentConfig.enabled ?? true));
const showContentTab = Boolean(contentConfig && ((contentConfig.forceDisplay ?? contentEnabled)));
const [contentState, setContentState] = useState(() => {
if (!contentConfig) {
return null;
}
if (!contentEnabled || typeof contentConfig.loadContent !== 'function') {
return { status: contentEnabled ? 'idle' : 'unavailable', data: null, error: null };
}
return { status: 'idle', data: null, error: null };
});
useEffect(() => {
if (!contentConfig || !showContentTab) {
setContentState(null);
return undefined;
}
if (!contentEnabled || typeof contentConfig.loadContent !== 'function') {
setContentState({ status: contentEnabled ? 'idle' : 'unavailable', data: null, error: null });
return undefined;
}
let cancelled = false;
const controller = new AbortController();
setContentState({ status: 'loading', data: null, error: null });
Promise.resolve(contentConfig.loadContent({ signal: controller.signal }))
.then((result) => {
if (cancelled) {
return;
}
if (result && result.length) {
setContentState({ status: 'loaded', data: result, error: null });
} else {
setContentState({ status: 'empty', data: '', error: null });
}
})
.catch((error) => {
if (cancelled || error?.name === 'AbortError') {
return;
}
setContentState({
status: 'error',
data: null,
error,
});
});
return () => {
cancelled = true;
controller.abort();
contentConfig.onCancel?.();
};
}, [contentConfig, contentEnabled, showContentTab, document?.id, resetKey]);
const visibleTabs = useMemo(() => {
const tabsList = [];
tabsList.push({
id: 'details',
label: detailsTabLabel,
render: () => (
No details available.
{metadataItems.map(({ label, value }) => (
) : (
{contentState.data}
);
case 'unavailable':
return (
{JSON.stringify(metadataPayload, null, 2)}