a tool for shared writing and social publishing
1import { GetLeafletDataReturnType } from "app/api/rpc/[command]/get_leaflet_data";
2import { Json } from "supabase/database.types";
3
4/**
5 * Return type for publication metadata extraction.
6 * Note: `publications.record` and `documents.data` are raw JSON from the database.
7 * Consumers should use `normalizePublicationRecord()` and `normalizeDocumentRecord()`
8 * from `src/utils/normalizeRecords` to get properly typed data.
9 */
10export type PublicationMetadata = {
11 description: string;
12 title: string;
13 leaflet: string;
14 doc: string | null;
15 publications: {
16 identity_did: string;
17 name: string;
18 indexed_at: string;
19 /** Raw record - use normalizePublicationRecord() to get typed data */
20 record: Json | null;
21 uri: string;
22 } | null;
23 documents: {
24 /** Raw data - use normalizeDocumentRecord() to get typed data */
25 data: Json;
26 indexed_at: string;
27 uri: string;
28 } | null;
29} | null;
30
31export function getPublicationMetadataFromLeafletData(
32 data?: GetLeafletDataReturnType["result"]["data"],
33): PublicationMetadata {
34 if (!data) return null;
35
36 let pubData:
37 | NonNullable<PublicationMetadata>
38 | undefined
39 | null = data?.leaflets_in_publications?.[0];
40
41 // If not found, check for standalone documents
42 let standaloneDoc = data?.leaflets_to_documents?.[0];
43 if (!pubData && standaloneDoc) {
44 // Transform standalone document data to match the expected format
45 pubData = {
46 ...standaloneDoc,
47 publications: null, // No publication for standalone docs
48 doc: standaloneDoc.document,
49 };
50 }
51 return pubData || null;
52}