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
4export function getPublicationMetadataFromLeafletData(
5 data?: GetLeafletDataReturnType["result"]["data"],
6) {
7 if (!data) return null;
8
9 let pubData:
10 | {
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 record: Json | null;
20 uri: string;
21 } | null;
22 documents: {
23 data: Json;
24 indexed_at: string;
25 uri: string;
26 } | null;
27 }
28 | undefined
29 | null =
30 data?.leaflets_in_publications?.[0] ||
31 data?.permission_token_rights[0].entity_sets?.permission_tokens?.find(
32 (p) => p.leaflets_in_publications?.length,
33 )?.leaflets_in_publications?.[0];
34
35 // If not found, check for standalone documents
36 let standaloneDoc =
37 data?.leaflets_to_documents?.[0] ||
38 data?.permission_token_rights[0].entity_sets?.permission_tokens.find(
39 (p) => p.leaflets_to_documents?.length,
40 )?.leaflets_to_documents?.[0];
41 if (!pubData && standaloneDoc) {
42 // Transform standalone document data to match the expected format
43 pubData = {
44 ...standaloneDoc,
45 publications: null, // No publication for standalone docs
46 doc: standaloneDoc.document,
47 };
48 }
49 return pubData;
50}