From b9e7c6310aef04752ce01a41c7b72b95c1bb24fb Mon Sep 17 00:00:00 2001 From: Amit Salunke Date: Sat, 12 Sep 2026 20:30:33 +0530 Subject: [PATCH] Updated module ui --- .../content/renderers/GenericCardRenderer.tsx | 52 +++++- .../renderers/GenericCollectionRenderer.tsx | 122 ++++++++++++++- .../renderers/GenericDetailRenderer.tsx | 25 ++- src/features/content/renderers/registry.ts | 43 ++++- .../shared/RendererFallbackBoundary.tsx | 55 +++++++ .../screens/CollectionTemplateScreen.tsx | 148 +++++++++--------- .../content/screens/ContentDetailScreen.tsx | 79 +++++----- .../content/screens/SingleTemplateScreen.tsx | 65 ++++---- 8 files changed, 433 insertions(+), 156 deletions(-) create mode 100644 src/features/content/renderers/shared/RendererFallbackBoundary.tsx diff --git a/src/features/content/renderers/GenericCardRenderer.tsx b/src/features/content/renderers/GenericCardRenderer.tsx index 790b5d9..f704ddf 100644 --- a/src/features/content/renderers/GenericCardRenderer.tsx +++ b/src/features/content/renderers/GenericCardRenderer.tsx @@ -1,6 +1,52 @@ import React from "react"; -import { Pressable, Text, View } from "react-native"; -import { CollectionItemRendererProps } from "../types/renderer.types"; +import { Pressable, StyleSheet, Text, View } from "react-native"; + +import { Colors } from "@/theme"; +import type { CollectionItemRendererProps } from "../types/renderer.types"; + +/** Safe, schema-agnostic fallback for collection items. */ export function GenericCardRenderer({ item, onPress }: CollectionItemRendererProps) { - return {item.title}{item.summary ? {item.summary} : null}; + return ( + [styles.pressable, pressed && styles.pressed]} + > + + {item.title || "Untitled content"} + {item.summary ? {item.summary} : null} + + + ); } + +const styles = StyleSheet.create({ + pressable: { + marginHorizontal: 16, + marginVertical: 5, + }, + pressed: { + opacity: 0.72, + }, + card: { + paddingHorizontal: 16, + paddingVertical: 14, + borderRadius: 14, + borderWidth: 1, + borderColor: "#DDE7E1", + backgroundColor: "#FFFFFF", + }, + title: { + fontSize: 17, + lineHeight: 23, + fontWeight: "700", + color: Colors.textPrimary, + }, + summary: { + marginTop: 5, + fontSize: 14, + lineHeight: 20, + color: Colors.textSecondary, + }, +}); diff --git a/src/features/content/renderers/GenericCollectionRenderer.tsx b/src/features/content/renderers/GenericCollectionRenderer.tsx index fc390ff..9758147 100644 --- a/src/features/content/renderers/GenericCollectionRenderer.tsx +++ b/src/features/content/renderers/GenericCollectionRenderer.tsx @@ -1,7 +1,119 @@ import React from "react"; -import { FlatList, Text, View } from "react-native"; -import { CollectionRendererProps } from "../types/renderer.types"; -export function GenericCollectionRenderer({ result, isLoading, isFetchingNextPage, onLoadMore, renderItem }: CollectionRendererProps) { - if (isLoading) return Loading...; - return item.id} renderItem={({item})=>renderItem({item,onPress:()=>undefined})} onEndReached={onLoadMore} onEndReachedThreshold={0.4} ListFooterComponent={isFetchingNextPage?Loading more...:null}/>; +import { + ActivityIndicator, + FlatList, + RefreshControl, + StyleSheet, + Text, + View, +} from "react-native"; + +import { Colors } from "@/theme"; +import type { CollectionRendererProps } from "../types/renderer.types"; + +/** + * Renderer-independent collection fallback. + * + * This component deliberately depends only on the common collection contract, + * so content remains navigable when a template references a renderer that is + * not implemented in the installed mobile build. + */ +export function GenericCollectionRenderer({ + items, + total, + isLoading, + isRefreshing, + isFetchingNextPage, + onRefresh, + onLoadMore, + onItemPress, + renderItem, +}: CollectionRendererProps) { + if (isLoading && items.length === 0) { + return ( + + + Loading content… + + ); + } + + return ( + item.id} + renderItem={({ item }) => + renderItem({ + item, + onPress: () => onItemPress(item), + }) + } + contentContainerStyle={items.length === 0 ? styles.emptyContainer : styles.content} + refreshControl={ + + } + onEndReached={onLoadMore} + onEndReachedThreshold={0.4} + ListEmptyComponent={ + + No content available + + There are no items to display for this collection. + + + } + ListFooterComponent={ + isFetchingNextPage ? ( + + + + ) : items.length > 0 ? ( + + {total === 1 ? "1 item" : `${total} items`} + + ) : null + } + showsVerticalScrollIndicator={false} + /> + ); } + +const styles = StyleSheet.create({ + content: { + paddingVertical: 8, + }, + emptyContainer: { + flexGrow: 1, + }, + centered: { + flex: 1, + minHeight: 220, + alignItems: "center", + justifyContent: "center", + paddingHorizontal: 28, + gap: 8, + }, + emptyTitle: { + fontSize: 17, + lineHeight: 23, + fontWeight: "700", + color: Colors.textPrimary, + textAlign: "center", + }, + statusText: { + fontSize: 14, + lineHeight: 20, + color: Colors.textSecondary, + textAlign: "center", + }, + footer: { + paddingVertical: 18, + alignItems: "center", + }, + countText: { + paddingVertical: 18, + textAlign: "center", + fontSize: 12, + color: Colors.textSecondary, + }, +}); diff --git a/src/features/content/renderers/GenericDetailRenderer.tsx b/src/features/content/renderers/GenericDetailRenderer.tsx index 789cc1f..298172d 100644 --- a/src/features/content/renderers/GenericDetailRenderer.tsx +++ b/src/features/content/renderers/GenericDetailRenderer.tsx @@ -1,12 +1,21 @@ import React from "react"; import { ScrollView } from "react-native"; -import type { DetailRendererProps } from "../types/renderer.types"; -import { Hero } from "./shared/Hero"; -import { SectionCard } from "./shared/SectionCard"; -import { ImageGallery } from "./shared/ImageGallery"; -import { ContentValue } from "./shared/ContentValue"; -import { humanizeKey } from "./helpers/value"; +import type { DetailRendererProps } from "../types/renderer.types"; +import { humanizeKey } from "./helpers/value"; +import { ContentValue } from "./shared/ContentValue"; +import { Hero } from "./shared/Hero"; +import { ImageGallery } from "./shared/ImageGallery"; +import { SectionCard } from "./shared/SectionCard"; + +const RESERVED_KEYS = new Set(["code"]); + +/** + * Renderer-independent detail fallback. + * + * `data` has already been filtered/ordered through the presentation contract by + * ContentDetailScreen, so object insertion order is the intended display order. + */ export function GenericDetailRenderer({ content, data }: DetailRendererProps) { return ( - + {Object.entries(data) - .filter(([key]) => key !== "code") + .filter(([key]) => !RESERVED_KEYS.has(key)) .map(([key, value]) => ( diff --git a/src/features/content/renderers/registry.ts b/src/features/content/renderers/registry.ts index b5d6feb..a93a7cd 100644 --- a/src/features/content/renderers/registry.ts +++ b/src/features/content/renderers/registry.ts @@ -1,4 +1,7 @@ import type { CollectionItemRendererComponent, CollectionRendererComponent, DetailRendererComponent } from "../types/renderer.types"; +import { GenericCollectionRenderer } from "./GenericCollectionRenderer"; +import { GenericCardRenderer } from "./GenericCardRenderer"; +import { GenericDetailRenderer } from "./GenericDetailRenderer"; import { ModuleCatalogCollectionRenderer, ModuleCatalogCardRenderer } from "./collections/ModuleCatalogRenderers"; import { AcuPointCollectionRenderer, AcuPointIndexCollectionRenderer, AcuPointCardRenderer, AcuPointIndexCardRenderer } from "./collections/PointRenderers"; import { MeridianCollectionRenderer, MeridianIndexCollectionRenderer, MeridianCardRenderer, MeridianIndexCardRenderer } from "./collections/MeridianRenderers"; @@ -80,6 +83,40 @@ export const detailRenderers: Readonly> ear_acupuncture_catalog_detail: EarAcupunctureCatalogDetailRenderer, ear_acupuncture_detail: EarAcupunctureDetailRenderer, }); -export function resolveCollectionRenderer(code:string):CollectionRendererComponent { const r=collectionRenderers[code]; if(!r) throw new Error(`Unknown collection renderer: ${code}`); return r; } -export function resolveCollectionItemRenderer(code:string):CollectionItemRendererComponent { const r=collectionItemRenderers[code]; if(!r) throw new Error(`Unknown collection item renderer: ${code}`); return r; } -export function resolveDetailRenderer(code:string):DetailRendererComponent { const r=detailRenderers[code]; if(!r) throw new Error(`Unknown detail renderer: ${code}`); return r; } +function reportMissingRenderer(kind: string, code?: string | null) { + if (__DEV__) { + console.warn( + `[content] No registered ${kind} renderer for ${code || ""}; using generic fallback.`, + ); + } +} + +export function hasCollectionRenderer(code?: string | null): boolean { + return Boolean(code && collectionRenderers[code]); +} + +export function hasCollectionItemRenderer(code?: string | null): boolean { + return Boolean(code && collectionItemRenderers[code]); +} + +export function hasDetailRenderer(code?: string | null): boolean { + return Boolean(code && detailRenderers[code]); +} + +export function resolveCollectionRenderer(code?: string | null): CollectionRendererComponent { + if (code && collectionRenderers[code]) return collectionRenderers[code]; + reportMissingRenderer("collection", code); + return GenericCollectionRenderer; +} + +export function resolveCollectionItemRenderer(code?: string | null): CollectionItemRendererComponent { + if (code && collectionItemRenderers[code]) return collectionItemRenderers[code]; + reportMissingRenderer("collection-item", code); + return GenericCardRenderer; +} + +export function resolveDetailRenderer(code?: string | null): DetailRendererComponent { + if (code && detailRenderers[code]) return detailRenderers[code]; + reportMissingRenderer("detail", code); + return GenericDetailRenderer; +} diff --git a/src/features/content/renderers/shared/RendererFallbackBoundary.tsx b/src/features/content/renderers/shared/RendererFallbackBoundary.tsx new file mode 100644 index 0000000..b04abe4 --- /dev/null +++ b/src/features/content/renderers/shared/RendererFallbackBoundary.tsx @@ -0,0 +1,55 @@ +import React from "react"; + +interface RendererFallbackBoundaryProps { + children: React.ReactNode; + fallback: React.ReactNode; + rendererCode?: string | null; + rendererKind: "collection" | "collection_item" | "detail"; +} + +interface RendererFallbackBoundaryState { + failed: boolean; +} + +/** + * Last-resort containment for renderer implementation failures. + * + * Missing renderer registrations are handled by the registry itself; this + * boundary additionally protects the content screen from runtime exceptions in + * an implemented renderer caused by an unexpected but valid content shape. + */ +export class RendererFallbackBoundary extends React.Component< + RendererFallbackBoundaryProps, + RendererFallbackBoundaryState +> { + state: RendererFallbackBoundaryState = { failed: false }; + + static getDerivedStateFromError(): RendererFallbackBoundaryState { + return { failed: true }; + } + + componentDidCatch(error: Error, info: React.ErrorInfo) { + console.error( + `[content] ${this.props.rendererKind} renderer failed; using generic fallback`, + { + rendererCode: this.props.rendererCode ?? null, + error, + componentStack: info.componentStack, + }, + ); + } + + componentDidUpdate(previousProps: RendererFallbackBoundaryProps) { + if ( + this.state.failed && + (previousProps.rendererCode !== this.props.rendererCode || + previousProps.rendererKind !== this.props.rendererKind) + ) { + this.setState({ failed: false }); + } + } + + render() { + return this.state.failed ? this.props.fallback : this.props.children; + } +} diff --git a/src/features/content/screens/CollectionTemplateScreen.tsx b/src/features/content/screens/CollectionTemplateScreen.tsx index 5ee8539..cc7fb97 100644 --- a/src/features/content/screens/CollectionTemplateScreen.tsx +++ b/src/features/content/screens/CollectionTemplateScreen.tsx @@ -10,10 +10,13 @@ import { ContractSortModal } from "../components/search/ContractSortModal"; import { useContentSearchInfiniteQuery } from "../hooks/use-content-search-infinite-query"; import { useModuleDisplayName } from "../hooks/use-module-display-name"; import { useTemplateSearchController } from "../hooks/use-template-search-controller"; +import { GenericCardRenderer } from "../renderers/GenericCardRenderer"; +import { GenericCollectionRenderer } from "../renderers/GenericCollectionRenderer"; import { resolveCollectionItemRenderer, resolveCollectionRenderer, } from "../renderers/registry"; +import { RendererFallbackBoundary } from "../renderers/shared/RendererFallbackBoundary"; import type { TemplateContractResponse } from "../types/presentation.types"; import { buildContentReferenceRoute, getNavigableContentReferences } from "../utils/content-reference"; @@ -58,48 +61,16 @@ export function CollectionTemplateScreen({ }; }, [query.data]); - const rendererResolution = useMemo(() => { - try { - const collectionCode = contract.presentation.renderers.collection; - const itemCode = contract.presentation.renderers.collection_item; - if (!collectionCode || !itemCode) { - throw new Error( - `Collection template ${templateCode} must configure both collection and collection_item renderers.`, - ); - } - return { - CollectionRenderer: resolveCollectionRenderer(collectionCode), - ItemRenderer: resolveCollectionItemRenderer(itemCode), - error: null as Error | null, - }; - } catch (error) { - return { - CollectionRenderer: null, - ItemRenderer: null, - error: - error instanceof Error - ? error - : new Error("Renderer resolution failed."), - }; - } - }, [ - contract.presentation.renderers.collection, - contract.presentation.renderers.collection_item, - templateCode, - ]); - - if ( - rendererResolution.error || - !rendererResolution.CollectionRenderer || - !rendererResolution.ItemRenderer - ) { - return ( - - ); - } + const collectionRendererCode = contract.presentation.renderers.collection; + const itemRendererCode = contract.presentation.renderers.collection_item; + const CollectionRenderer = useMemo( + () => resolveCollectionRenderer(collectionRendererCode), + [collectionRendererCode], + ); + const ItemRenderer = useMemo( + () => resolveCollectionItemRenderer(itemRendererCode), + [itemRendererCode], + ); if (query.isError) { return ( @@ -111,8 +82,6 @@ export function CollectionTemplateScreen({ ); } - const CollectionRenderer = rendererResolution.CollectionRenderer; - const ItemRenderer = rendererResolution.ItemRenderer; const hasSort = contract.search.sort_fields.length > 0; const hasCustomSort = JSON.stringify(search.sort) !== JSON.stringify(contract.search.default_sort); @@ -169,32 +138,71 @@ export function CollectionTemplateScreen({ right={headerActions} /> - void query.refetch()} - onLoadMore={() => { - if (query.hasNextPage && !query.isFetchingNextPage) { - void query.fetchNextPage(); - } - }} - onItemPress={(item) => { - const references = Object.values(item.attributes).flatMap((value) => - getNavigableContentReferences(value), - ); - if (references.length === 1) { - router.push(buildContentReferenceRoute(references[0]) as never); - return; - } - onContentPress(item.id); - }} - renderItem={({ item, onPress }) => ( - - )} - /> + void query.refetch()} + onLoadMore={() => { + if (query.hasNextPage && !query.isFetchingNextPage) { + void query.fetchNextPage(); + } + }} + onItemPress={(item) => { + const references = Object.values(item.attributes).flatMap((value) => + getNavigableContentReferences(value), + ); + if (references.length === 1) { + router.push(buildContentReferenceRoute(references[0]) as never); + return; + } + onContentPress(item.id); + }} + renderItem={({ item, onPress }) => ( + + )} + /> + } + > + void query.refetch()} + onLoadMore={() => { + if (query.hasNextPage && !query.isFetchingNextPage) { + void query.fetchNextPage(); + } + }} + onItemPress={(item) => { + const references = Object.values(item.attributes).flatMap((value) => + getNavigableContentReferences(value), + ); + if (references.length === 1) { + router.push(buildContentReferenceRoute(references[0]) as never); + return; + } + onContentPress(item.id); + }} + renderItem={({ item, onPress }) => ( + } + > + + + )} + /> + {hasSort ? ( { - const code = screen.contractQuery.data?.presentation.renderers.detail; - if (!code) return { renderer: null, error: null as Error | null }; - - try { - return { renderer: resolveDetailRenderer(code), error: null as Error | null }; - } catch (error) { - return { - renderer: null, - error: error instanceof Error ? error : new Error("Unknown content renderer."), - }; - } - }, [screen.contractQuery.data?.presentation.renderers.detail]); + const rendererCode = screen.contractQuery.data?.presentation.renderers.detail; + const Renderer = useMemo( + () => resolveDetailRenderer(rendererCode), + [rendererCode], + ); if (screen.isLoading) { return ; @@ -93,15 +87,6 @@ function ContentDetailScreenResolved({ ); } - if (rendererResolution.error || !rendererResolution.renderer) { - return ( - - ); - } - const response = screen.contentQuery.data; if (!response) { return ; @@ -110,7 +95,6 @@ function ContentDetailScreenResolved({ const presentationAttributes = screen.contractQuery.data?.presentation.attributes ?? []; const presentedData = applyPresentationContractToData(response.data, presentationAttributes); - const Renderer = rendererResolution.renderer; 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; @@ -122,20 +106,41 @@ function ContentDetailScreenResolved({ return ( - + + } + > + + ); diff --git a/src/features/content/screens/SingleTemplateScreen.tsx b/src/features/content/screens/SingleTemplateScreen.tsx index f4e2871..8cc5752 100644 --- a/src/features/content/screens/SingleTemplateScreen.tsx +++ b/src/features/content/screens/SingleTemplateScreen.tsx @@ -2,7 +2,10 @@ import React, { useMemo } 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 { applyPresentationContractToData } from "../utils/attribute-presentation"; import type { TemplateContractResponse } from "../types/presentation.types"; export function SingleTemplateScreen({ @@ -16,19 +19,11 @@ export function SingleTemplateScreen({ }) { const query = useSingleTemplateContentQuery({ moduleCode, templateCode }); - const rendererResolution = useMemo(() => { - try { - return { - Renderer: resolveDetailRenderer(contract.presentation.renderers.detail), - error: null as Error | null, - }; - } catch (error) { - return { - Renderer: null, - error: error instanceof Error ? error : new Error("Renderer resolution failed."), - }; - } - }, [contract.presentation.renderers.detail]); + const rendererCode = contract.presentation.renderers.detail; + const Renderer = useMemo( + () => resolveDetailRenderer(rendererCode), + [rendererCode], + ); if (query.resolution.status === "loading") { return ; @@ -57,27 +52,37 @@ export function SingleTemplateScreen({ ); } - if (rendererResolution.error || !rendererResolution.Renderer) { - return ( - - ); - } - - const Renderer = rendererResolution.Renderer; const { content, data, media } = query.resolution; + const presentationAttributes = contract.presentation.attributes ?? []; + const presentedData = applyPresentationContractToData(data, presentationAttributes); return ( - + + } + > + + ); }