import React, { useMemo } from "react";
import { ContentMediaProvider } from "../media/ContentMediaContext";
import { ScreenError, ScreenLoading } from "../components/ContentScreenState";
import { useContentDetailScreen } from "../hooks/use-content-detail-screen";
import { GenericDetailRenderer } from "../renderers/GenericDetailRenderer";
import { resolveDetailRenderer } from "../renderers/registry";
import { RendererFallbackBoundary } from "../renderers/shared/RendererFallbackBoundary";
import { DetailPageShell } from "../renderers/shared/DetailPageShell";
import { applyPresentationContractToData } from "../utils/attribute-presentation";
export interface ContentDetailScreenProps {
moduleCode: string;
templateCode: string;
contentId: string;
}
function normalizeRequired(value: string, name: string): string {
const normalized = value.trim();
if (!normalized) throw new Error(`${name} is required.`);
return normalized;
}
export function ContentDetailScreen({
moduleCode,
templateCode,
contentId,
}: ContentDetailScreenProps) {
let normalizedModuleCode: string;
let normalizedTemplateCode: string;
let normalizedContentId: string;
try {
normalizedModuleCode = normalizeRequired(moduleCode, "moduleCode");
normalizedTemplateCode = normalizeRequired(templateCode, "templateCode");
normalizedContentId = normalizeRequired(contentId, "contentId");
} catch (error) {
return (
);
}
return (
);
}
function ContentDetailScreenResolved({
moduleCode,
templateCode,
contentId,
}: ContentDetailScreenProps) {
const screen = useContentDetailScreen({ moduleCode, templateCode, contentId });
const rendererCode = screen.contractQuery.data?.presentation.renderers.detail;
const Renderer = useMemo(
() => resolveDetailRenderer(rendererCode, templateCode, moduleCode),
[rendererCode, templateCode, moduleCode],
);
if (screen.isLoading) {
return ;
}
if (screen.moduleMismatch) {
return (
);
}
if (screen.contractQuery.isError || screen.contentQuery.isError || screen.definitionsQuery.isError) {
return (
void screen.refetch()}
/>
);
}
const response = screen.contentQuery.data;
if (!response) {
return ;
}
const presentationAttributes = screen.contractQuery.data?.presentation.attributes ?? [];
const presentedData = applyPresentationContractToData(response.data, presentationAttributes);
const pointName = typeof response.data.point_name === "string" ? response.data.point_name.trim() : "";
const name = typeof response.data.name === "string" ? response.data.name.trim() : "";
const displayTitle = pointName || name || response.content.title;
const alternateNames = [response.data.alt_name_1, response.data.alt_name_2]
.filter((value): value is string => typeof value === "string" && value.trim().length > 0)
.map((value) => value.trim())
.join(" · ");
return (
}
>
);
}