diff --git a/app/api/rpc/[command]/get_leaflet_data.ts b/app/api/rpc/[command]/get_leaflet_data.ts index 6fd8c729..874018f5 100644 --- a/app/api/rpc/[command]/get_leaflet_data.ts +++ b/app/api/rpc/[command]/get_leaflet_data.ts @@ -5,23 +5,43 @@ import type { Env } from "./route"; export type GetLeafletDataReturnType = Awaited< ReturnType<(typeof get_leaflet_data)["handler"]> >; + +const leaflets_in_publications_query = `leaflets_in_publications(*, publications(*, documents_in_publications(count)), documents(*))`; export const get_leaflet_data = makeRoute({ route: "get_leaflet_data", input: z.object({ token_id: z.string(), }), + handler: async ({ token_id }, { supabase }: Pick) => { let res = await supabase .from("permission_tokens") .select( `*, - permission_token_rights(*), + permission_token_rights(*, entity_sets(permission_tokens(${leaflets_in_publications_query}))), custom_domain_routes!custom_domain_routes_edit_permission_token_fkey(*), - leaflets_in_publications(*, publications(*, documents_in_publications(count)), documents(*)) `, + ${leaflets_in_publications_query}`, ) .eq("id", token_id) .single(); - return { result: res }; + type t = typeof res; + type data = Exclude; + + //All of these shenanigans are to make entity_set optional so that we don't have to write it when we create this data elsewhere, mainly in LeafletList + return { + result: res as Omit & { + data: + | null + | (Omit & { + permission_token_rights: (Omit< + data["permission_token_rights"][0], + "entity_sets" + > & { + entity_sets?: data["permission_token_rights"][0]["entity_sets"]; + })[]; + }); + }, + }; }, }); diff --git a/components/PageSWRDataProvider.tsx b/components/PageSWRDataProvider.tsx index 32d1b8d8..cc03de90 100644 --- a/components/PageSWRDataProvider.tsx +++ b/components/PageSWRDataProvider.tsx @@ -67,7 +67,12 @@ let useLeafletData = () => { export function useLeafletPublicationData() { let { data, mutate } = useLeafletData(); return { - data: data?.leaflets_in_publications?.[0] || null, + data: + data?.leaflets_in_publications?.[0] || + data?.permission_token_rights[0].entity_sets?.permission_tokens?.find( + (p) => p.leaflets_in_publications.length, + )?.leaflets_in_publications?.[0] || + null, mutate, }; } diff --git a/components/Pages/PublicationMetadata.tsx b/components/Pages/PublicationMetadata.tsx index a7a9541d..00a981d1 100644 --- a/components/Pages/PublicationMetadata.tsx +++ b/components/Pages/PublicationMetadata.tsx @@ -17,6 +17,7 @@ import { getPublicationURL, } from "app/lish/createPub/getPublicationURL"; import { useSubscribe } from "src/replicache/useSubscribe"; +import { useEntitySetContext } from "components/EntitySetProvider"; export const PublicationMetadata = ({ cardBorderHidden, }: { @@ -28,6 +29,7 @@ export const PublicationMetadata = ({ let description = useSubscribe(rep, (tx) => tx.get("publication_description"), ); + let { permissions } = useEntitySetContext(); let record = pub?.documents?.data as PubLeafletDocument.Record | null; let publishedAt = record?.publishedAt; @@ -56,6 +58,7 @@ export const PublicationMetadata = ({ { @@ -67,6 +70,7 @@ export const PublicationMetadata = ({ placeholder="Untitled" />