148 lines
5.1 KiB
TypeScript
148 lines
5.1 KiB
TypeScript
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 (
|
|
<ScreenError
|
|
title="Invalid content route"
|
|
message={error instanceof Error ? error.message : "Required route parameters are missing."}
|
|
/>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<ContentDetailScreenResolved
|
|
moduleCode={normalizedModuleCode}
|
|
templateCode={normalizedTemplateCode}
|
|
contentId={normalizedContentId}
|
|
/>
|
|
);
|
|
}
|
|
|
|
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 <ScreenLoading message="Loading content…" />;
|
|
}
|
|
|
|
if (screen.moduleMismatch) {
|
|
return (
|
|
<ScreenError
|
|
title="Template/module mismatch"
|
|
message={`Template ${templateCode} is not configured for module ${moduleCode}.`}
|
|
/>
|
|
);
|
|
}
|
|
|
|
if (screen.contractQuery.isError || screen.contentQuery.isError || screen.definitionsQuery.isError) {
|
|
return (
|
|
<ScreenError
|
|
title="Unable to load content"
|
|
message="The template contract or content detail request failed."
|
|
onRetry={() => void screen.refetch()}
|
|
/>
|
|
);
|
|
}
|
|
|
|
const response = screen.contentQuery.data;
|
|
if (!response) {
|
|
return <ScreenError title="Content unavailable" message="The content detail response was empty." />;
|
|
}
|
|
|
|
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 (
|
|
<DetailPageShell title={displayTitle} subtitle={alternateNames || undefined}>
|
|
<ContentMediaProvider media={response.media}>
|
|
<RendererFallbackBoundary
|
|
rendererKind="detail"
|
|
rendererCode={rendererCode}
|
|
fallback={
|
|
<GenericDetailRenderer
|
|
moduleCode={moduleCode}
|
|
templateCode={templateCode}
|
|
contentId={response.content.id}
|
|
content={{
|
|
id: response.content.id,
|
|
title: response.content.title,
|
|
slug: response.content.slug,
|
|
summary: response.content.summary,
|
|
}}
|
|
data={presentedData}
|
|
definitions={screen.definitionsQuery.data?.attributes ?? []}
|
|
presentationAttributes={presentationAttributes}
|
|
/>
|
|
}
|
|
>
|
|
<Renderer
|
|
moduleCode={moduleCode}
|
|
templateCode={templateCode}
|
|
contentId={response.content.id}
|
|
content={{
|
|
id: response.content.id,
|
|
title: response.content.title,
|
|
slug: response.content.slug,
|
|
summary: response.content.summary,
|
|
}}
|
|
data={presentedData}
|
|
definitions={screen.definitionsQuery.data?.attributes ?? []}
|
|
presentationAttributes={presentationAttributes}
|
|
/>
|
|
</RendererFallbackBoundary>
|
|
</ContentMediaProvider>
|
|
</DetailPageShell>
|
|
);
|
|
}
|