tangled
alpha
login
or
join now
leaflet.pub
/
leaflet
289
fork
atom
a tool for shared writing and social publishing
289
fork
atom
overview
issues
28
pulls
pipelines
move getting home leaflets to rpc from server action
awarm.space
1 year ago
aa8d9b26
c500383e
+66
-70
6 changed files
expand all
collapse all
unified
split
actions
getLeafletData.ts
app
api
rpc
[command]
getFactsFromHomeLeaflets.ts
route.ts
lib.ts
home
LeafletList.tsx
page.tsx
-31
actions/getLeafletData.ts
···
1
1
-
"use server";
2
2
-
3
3
-
import { createServerClient } from "@supabase/ssr";
4
4
-
import { Fact } from "src/replicache";
5
5
-
import { Attributes } from "src/replicache/attributes";
6
6
-
import { Database } from "supabase/database.types";
7
7
-
8
8
-
let supabase = createServerClient<Database>(
9
9
-
process.env.NEXT_PUBLIC_SUPABASE_API_URL as string,
10
10
-
process.env.SUPABASE_SERVICE_ROLE_KEY as string,
11
11
-
{ cookies: {} },
12
12
-
);
13
13
-
export async function getLeafletData(tokens: string[]) {
14
14
-
//Eventually check permission tokens in here somehow!
15
15
-
let all_facts = await supabase.rpc("get_facts_for_roots", {
16
16
-
max_depth: 3,
17
17
-
roots: tokens,
18
18
-
});
19
19
-
if (all_facts.data)
20
20
-
return all_facts.data.reduce(
21
21
-
(acc, fact) => {
22
22
-
if (!acc[fact.root_id]) acc[fact.root_id] = [];
23
23
-
acc[fact.root_id].push(
24
24
-
fact as unknown as Fact<keyof typeof Attributes>,
25
25
-
);
26
26
-
return acc;
27
27
-
},
28
28
-
{} as { [key: string]: Fact<keyof typeof Attributes>[] },
29
29
-
);
30
30
-
return {};
31
31
-
}
+35
app/api/rpc/[command]/getFactsFromHomeLeaflets.ts
···
1
1
+
import { z } from "zod";
2
2
+
import { Fact } from "src/replicache";
3
3
+
import { Attributes } from "src/replicache/attributes";
4
4
+
import { makeRoute } from "../lib";
5
5
+
import { Env } from "./route";
6
6
+
7
7
+
export const getFactsFromHomeLeaflets = makeRoute({
8
8
+
route: "getFactsFromHomeLeaflets",
9
9
+
input: z.object({
10
10
+
tokens: z.array(z.string()),
11
11
+
}),
12
12
+
handler: async ({ tokens }, { supabase }: Pick<Env, "supabase">) => {
13
13
+
let all_facts = await supabase.rpc("get_facts_for_roots", {
14
14
+
max_depth: 3,
15
15
+
roots: tokens,
16
16
+
});
17
17
+
18
18
+
if (all_facts.data) {
19
19
+
return {
20
20
+
result: all_facts.data.reduce(
21
21
+
(acc, fact) => {
22
22
+
if (!acc[fact.root_id]) acc[fact.root_id] = [];
23
23
+
acc[fact.root_id].push(
24
24
+
fact as unknown as Fact<keyof typeof Attributes>,
25
25
+
);
26
26
+
return acc;
27
27
+
},
28
28
+
{} as { [key: string]: Fact<keyof typeof Attributes>[] },
29
29
+
),
30
30
+
};
31
31
+
}
32
32
+
33
33
+
return { result: {} };
34
34
+
},
35
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
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
22
-
let Routes = [push, pull];
23
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
11
-
handler: (msg: z.infer<Input>, env: Env, request: Request) => Promise<Result>;
11
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
62
-
result = (await handler.handler(
63
63
-
msg.data as any,
64
64
-
env,
65
65
-
request,
66
66
-
)) as object;
62
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
10
-
import { getLeafletData } from "actions/getLeafletData";
11
10
import { getIdentityData } from "actions/getIdentityData";
11
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
24
-
() => {
25
25
-
if (identity)
26
26
-
return getLeafletData(
27
27
-
identity.permission_token_on_homepage.map(
24
24
+
async () => {
25
25
+
if (identity) {
26
26
+
let { result } = await callRPC("getFactsFromHomeLeaflets", {
27
27
+
tokens: identity.permission_token_on_homepage.map(
28
28
(ptrh) => ptrh.permission_tokens.root_entity,
29
29
),
30
30
-
);
30
30
+
});
31
31
+
return result;
32
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
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
69
-
let { data } = await supabase.rpc("get_facts", {
70
70
-
root: permission_token.root_entity,
71
71
-
});
72
72
-
let initialFacts = (data as unknown as Fact<keyof typeof Attributes>[]) || [];
70
70
+
let [homeLeafletFacts, allLeafletFacts] = await Promise.all([
71
71
+
supabase.rpc("get_facts", {
72
72
+
root: permission_token.root_entity,
73
73
+
}),
74
74
+
auth_res
75
75
+
? getFactsFromHomeLeaflets.handler(
76
76
+
{
77
77
+
tokens: auth_res.permission_token_on_homepage.map(
78
78
+
(r) => r.permission_tokens.root_entity,
79
79
+
),
80
80
+
},
81
81
+
{ supabase },
82
82
+
)
83
83
+
: undefined,
84
84
+
]);
85
85
+
let initialFacts =
86
86
+
(homeLeafletFacts.data as unknown as Fact<keyof typeof Attributes>[]) || [];
73
87
74
88
let root_entity = permission_token.root_entity;
75
75
-
let home_docs_initialFacts: {
76
76
-
[root_entity: string]: Fact<keyof typeof Attributes>[];
77
77
-
} = {};
78
78
-
if (auth_res) {
79
79
-
let all_facts = await supabase.rpc("get_facts_for_roots", {
80
80
-
max_depth: 3,
81
81
-
roots: auth_res.permission_token_on_homepage.map(
82
82
-
(r) => r.permission_tokens.root_entity,
83
83
-
),
84
84
-
});
85
85
-
if (all_facts.data)
86
86
-
home_docs_initialFacts = all_facts.data.reduce(
87
87
-
(acc, fact) => {
88
88
-
if (!acc[fact.root_id]) acc[fact.root_id] = [];
89
89
-
acc[fact.root_id].push(
90
90
-
fact as unknown as Fact<keyof typeof Attributes>,
91
91
-
);
92
92
-
return acc;
93
93
-
},
94
94
-
{} as { [key: string]: Fact<keyof typeof Attributes>[] },
95
95
-
);
96
96
-
}
89
89
+
let home_docs_initialFacts = allLeafletFacts?.result || {};
97
90
return (
98
91
<ReplicacheProvider
99
92
rootEntity={root_entity}