acupedia-mobile-ui/src/features/content/screens/SingleTemplateScreen.tsx

103 lines
3.6 KiB
TypeScript

import React, { createContext, useMemo, useState } from "react";
import { ContentMediaProvider } from "../media/ContentMediaContext";
import { ScreenEmpty, ScreenError, ScreenLoading } from "../components/ContentScreenState";
import { useSingleTemplateContentQuery } from "../hooks/use-single-template-content-query";
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";
import type { TemplateContractResponse } from "../types/presentation.types";
export interface BackActionContextType {
setCustomBackAction: (action: (() => void) | null) => void;
}
export const BackActionContext = createContext<BackActionContextType>({
setCustomBackAction: () => {},
});
export function SingleTemplateScreen({
moduleCode,
templateCode,
contract,
}: {
moduleCode: string;
templateCode: string;
contract: TemplateContractResponse;
}) {
const [customBack, setCustomBack] = useState<(() => void) | null>(null);
const query = useSingleTemplateContentQuery({ moduleCode, templateCode });
const rendererCode = contract.presentation.renderers.detail;
const Renderer = useMemo(
() => resolveDetailRenderer(rendererCode, templateCode, moduleCode),
[rendererCode, templateCode, moduleCode],
);
if (query.resolution.status === "loading") {
return <ScreenLoading message="Loading content…" />;
}
if (query.resolution.status === "error") {
return (
<ScreenError
title="Unable to load content"
message="The single-template content request failed."
onRetry={() => void query.refetch()}
/>
);
}
if (query.resolution.status === "empty") {
return <ScreenEmpty message={`No published content is available for ${templateCode}.`} />;
}
if (query.resolution.status === "invalid") {
return (
<ScreenError
title="Single-template invariant violated"
message={`Template ${templateCode} is configured as single but returned ${query.resolution.count} content items.`}
/>
);
}
const { content, data, media } = query.resolution;
const presentationAttributes = contract.presentation.attributes ?? [];
const presentedData = applyPresentationContractToData(data, presentationAttributes);
return (
<DetailPageShell title={content.title || moduleCode} onBack={customBack ?? undefined}>
<BackActionContext.Provider value={{ setCustomBackAction: setCustomBack }}>
<ContentMediaProvider media={media}>
<RendererFallbackBoundary
rendererKind="detail"
rendererCode={rendererCode}
fallback={
<GenericDetailRenderer
moduleCode={moduleCode}
templateCode={templateCode}
contentId={content.id}
content={content}
data={presentedData}
definitions={[]}
presentationAttributes={presentationAttributes}
/>
}
>
<Renderer
moduleCode={moduleCode}
templateCode={templateCode}
contentId={content.id}
content={content}
data={presentedData}
definitions={[]}
presentationAttributes={presentationAttributes}
/>
</RendererFallbackBoundary>
</ContentMediaProvider>
</BackActionContext.Provider>
</DetailPageShell>
);
}