a tool for shared writing and social publishing

move getting home leaflets to rpc from server action

+66 -70
-31
actions/getLeafletData.ts
··· 1 - "use server"; 2 - 3 - import { createServerClient } from "@supabase/ssr"; 4 - import { Fact } from "src/replicache"; 5 - import { Attributes } from "src/replicache/attributes"; 6 - import { Database } from "supabase/database.types"; 7 - 8 - let supabase = createServerClient<Database>( 9 - process.env.NEXT_PUBLIC_SUPABASE_API_URL as string, 10 - process.env.SUPABASE_SERVICE_ROLE_KEY as string, 11 - { cookies: {} }, 12 - ); 13 - export async function getLeafletData(tokens: string[]) { 14 - //Eventually check permission tokens in here somehow! 15 - let all_facts = await supabase.rpc("get_facts_for_roots", { 16 - max_depth: 3, 17 - roots: tokens, 18 - }); 19 - if (all_facts.data) 20 - return all_facts.data.reduce( 21 - (acc, fact) => { 22 - if (!acc[fact.root_id]) acc[fact.root_id] = []; 23 - acc[fact.root_id].push( 24 - fact as unknown as Fact<keyof typeof Attributes>, 25 - ); 26 - return acc; 27 - }, 28 - {} as { [key: string]: Fact<keyof typeof Attributes>[] }, 29 - ); 30 - return {}; 31 - }
+35
app/api/rpc/[command]/getFactsFromHomeLeaflets.ts
··· 1 + import { z } from "zod"; 2 + import { Fact } from "src/replicache"; 3 + import { Attributes } from "src/replicache/attributes"; 4 + import { makeRoute } from "../lib"; 5 + import { Env } from "./route"; 6 + 7 + export const getFactsFromHomeLeaflets = makeRoute({ 8 + route: "getFactsFromHomeLeaflets", 9 + input: z.object({ 10 + tokens: z.array(z.string()), 11 + }), 12 + handler: async ({ tokens }, { supabase }: Pick<Env, "supabase">) => { 13 + let all_facts = await supabase.rpc("get_facts_for_roots", { 14 + max_depth: 3, 15 + roots: tokens, 16 + }); 17 + 18 + if (all_facts.data) { 19 + return { 20 + result: all_facts.data.reduce( 21 + (acc, fact) => { 22 + if (!acc[fact.root_id]) acc[fact.root_id] = []; 23 + acc[fact.root_id].push( 24 + fact as unknown as Fact<keyof typeof Attributes>, 25 + ); 26 + return acc; 27 + }, 28 + {} as { [key: string]: Fact<keyof typeof Attributes>[] }, 29 + ), 30 + }; 31 + } 32 + 33 + return { result: {} }; 34 + }, 35 + });
+2 -1
app/api/rpc/[command]/route.ts
··· 5 5 import { createClient } from "@supabase/supabase-js"; 6 6 import { Database } from "supabase/database.types"; 7 7 import { pull } from "./pull"; 8 + import { getFactsFromHomeLeaflets } from "./getFactsFromHomeLeaflets"; 8 9 9 10 const client = postgres(process.env.DB_URL as string, { idle_timeout: 5 }); 10 11 let supabase = createClient<Database>( ··· 19 20 }; 20 21 export type Env = typeof Env; 21 22 export type Routes = typeof Routes; 22 - let Routes = [push, pull]; 23 + let Routes = [push, pull, getFactsFromHomeLeaflets]; 23 24 export async function POST( 24 25 req: Request, 25 26 { params }: { params: { command: string } },
+2 -6
app/api/rpc/lib.ts
··· 8 8 > = { 9 9 route: Cmd; 10 10 input: Input; 11 - handler: (msg: z.infer<Input>, env: Env, request: Request) => Promise<Result>; 11 + handler: (msg: z.infer<Input>, env: Env) => Promise<Result>; 12 12 }; 13 13 14 14 type Routes<Env extends {}> = Route<string, any, any, Env>[]; ··· 59 59 break; 60 60 } 61 61 try { 62 - result = (await handler.handler( 63 - msg.data as any, 64 - env, 65 - request, 66 - )) as object; 62 + result = (await handler.handler(msg.data as any, env)) as object; 67 63 break; 68 64 } catch (e) { 69 65 console.log(e);
+8 -6
app/home/LeafletList.tsx
··· 7 7 import { LeafletPreview } from "./LeafletPreview"; 8 8 import { useIdentityData } from "components/IdentityProvider"; 9 9 import { Attributes } from "src/replicache/attributes"; 10 - import { getLeafletData } from "actions/getLeafletData"; 11 10 import { getIdentityData } from "actions/getIdentityData"; 11 + import { callRPC } from "app/api/rpc/client"; 12 12 13 13 export function LeafletList(props: { 14 14 initialFacts: { ··· 21 21 let { identity } = useIdentityData(); 22 22 let { data: initialFacts, mutate } = useSWR( 23 23 "home-leaflet-data", 24 - () => { 25 - if (identity) 26 - return getLeafletData( 27 - identity.permission_token_on_homepage.map( 24 + async () => { 25 + if (identity) { 26 + let { result } = await callRPC("getFactsFromHomeLeaflets", { 27 + tokens: identity.permission_token_on_homepage.map( 28 28 (ptrh) => ptrh.permission_tokens.root_entity, 29 29 ), 30 - ); 30 + }); 31 + return result; 32 + } 31 33 }, 32 34 { fallbackData: props.initialFacts }, 33 35 );
+19 -26
app/home/page.tsx
··· 21 21 import { HelpPopover } from "components/HelpPopover"; 22 22 import { AccountSettings } from "./AccountSettings"; 23 23 import { LoggedOutWarning } from "./LoggedOutWarning"; 24 + import { getFactsFromHomeLeaflets } from "app/api/rpc/[command]/getFactsFromHomeLeaflets"; 24 25 25 26 let supabase = createServerClient<Database>( 26 27 process.env.NEXT_PUBLIC_SUPABASE_API_URL as string, ··· 66 67 } 67 68 68 69 if (!permission_token) return <div>no home page wierdly</div>; 69 - let { data } = await supabase.rpc("get_facts", { 70 - root: permission_token.root_entity, 71 - }); 72 - let initialFacts = (data as unknown as Fact<keyof typeof Attributes>[]) || []; 70 + let [homeLeafletFacts, allLeafletFacts] = await Promise.all([ 71 + supabase.rpc("get_facts", { 72 + root: permission_token.root_entity, 73 + }), 74 + auth_res 75 + ? getFactsFromHomeLeaflets.handler( 76 + { 77 + tokens: auth_res.permission_token_on_homepage.map( 78 + (r) => r.permission_tokens.root_entity, 79 + ), 80 + }, 81 + { supabase }, 82 + ) 83 + : undefined, 84 + ]); 85 + let initialFacts = 86 + (homeLeafletFacts.data as unknown as Fact<keyof typeof Attributes>[]) || []; 73 87 74 88 let root_entity = permission_token.root_entity; 75 - let home_docs_initialFacts: { 76 - [root_entity: string]: Fact<keyof typeof Attributes>[]; 77 - } = {}; 78 - if (auth_res) { 79 - let all_facts = await supabase.rpc("get_facts_for_roots", { 80 - max_depth: 3, 81 - roots: auth_res.permission_token_on_homepage.map( 82 - (r) => r.permission_tokens.root_entity, 83 - ), 84 - }); 85 - if (all_facts.data) 86 - home_docs_initialFacts = all_facts.data.reduce( 87 - (acc, fact) => { 88 - if (!acc[fact.root_id]) acc[fact.root_id] = []; 89 - acc[fact.root_id].push( 90 - fact as unknown as Fact<keyof typeof Attributes>, 91 - ); 92 - return acc; 93 - }, 94 - {} as { [key: string]: Fact<keyof typeof Attributes>[] }, 95 - ); 96 - } 89 + let home_docs_initialFacts = allLeafletFacts?.result || {}; 97 90 return ( 98 91 <ReplicacheProvider 99 92 rootEntity={root_entity}