Skip to content

Commit dea6c28

Browse files
committed
Rewritten as SSR
1 parent 71e6005 commit dea6c28

3 files changed

Lines changed: 53 additions & 75 deletions

File tree

apps/website/app/(home)/auth/group/page.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import { ListGroups } from "~/components/auth/ListGroups";
2-
import { Suspense } from "react";
32

43
const Page = () => (
54
<main>
65
<div className="mx-auto max-w-6xl space-y-12 px-6 py-12">
7-
<Suspense fallback={<>Loading</>}>
8-
<ListGroups />
9-
</Suspense>
6+
<ListGroups />
107
</div>
118
</main>
129
);

apps/website/app/components/auth/ListGroups.tsx

Lines changed: 46 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,55 @@
1-
"use client";
2-
3-
import { createClient } from "~/utils/supabase/client";
1+
import { createClient } from "~/utils/supabase/server";
42
import { getSessionUserData } from "~/utils/supabase/account";
53
import Link from "next/link";
6-
import { useState, useEffect } from "react";
74
import { Tables } from "@repo/database/dbTypes";
8-
import useInternalError from "~/utils/internalError";
5+
import internalError from "~/utils/internalErrorSsr";
96

107
type GroupData = Tables<"my_groups">;
118

12-
export const ListGroups = () => {
13-
const [groupData, setGroupData] = useState<GroupData[] | null>(null);
14-
const [adminData, setAdminData] = useState<Record<string, boolean>>({});
15-
const [userName, setUserName] = useState<string | null>(null);
16-
const [error, setError] = useState<string | null>(null);
17-
const internalError = useInternalError();
9+
export const ListGroups = async () => {
10+
let groupData: GroupData[] | null = null;
11+
let adminData: Record<string, boolean> = {};
12+
let userName: string | undefined;
13+
let error: string | undefined;
1814

19-
useEffect(() => {
20-
const getGroups = async () => {
21-
try {
22-
const client = createClient();
23-
const userData = await getSessionUserData(client);
24-
if (!userData) {
25-
const userMessage = "Not logged in.\nPlease log in from application.";
26-
setError(userMessage);
27-
return;
28-
}
29-
const { name, type, id } = userData;
30-
if (type === "anonymous") setUserName("Space " + name);
31-
else if (type === "group") setUserName("group " + name);
32-
else if (type === "person") setUserName(name);
33-
const groupResponse = await client.from("my_groups").select();
34-
if (groupResponse.error) {
35-
const userMessage = "Could not access DiscourseGraphs";
36-
setError(userMessage);
37-
internalError({
38-
error: groupResponse.error,
39-
});
40-
return;
41-
}
42-
setGroupData(groupResponse.data);
43-
const membershipReq = await client
44-
.from("group_membership")
45-
.select("group_id,admin")
46-
.eq("member_id", id);
47-
if (membershipReq.error) {
48-
const userMessage = "Could not access Discourse Graphs";
49-
setError(userMessage);
50-
internalError({
51-
error: membershipReq.error,
52-
});
53-
return;
54-
}
55-
setAdminData(
56-
Object.fromEntries(
57-
// eslint-disable-next-line @typescript-eslint/naming-convention
58-
membershipReq.data.map(({ group_id, admin }) => [
59-
group_id,
60-
admin || false,
61-
]),
62-
),
63-
);
64-
} catch (error) {
65-
const userMessage = "Unknown error occurred";
66-
setError(userMessage);
67-
internalError({
68-
error,
69-
});
70-
}
71-
};
72-
void getGroups();
73-
}, [internalError]);
15+
try {
16+
const client = await createClient();
17+
const userData = await getSessionUserData(client);
18+
if (!userData) {
19+
throw new Error("Not logged in.\nPlease log in from application.");
20+
}
21+
const { name, type, id } = userData;
22+
if (type === "anonymous") userName = "Space " + name;
23+
else if (type === "group") userName = "group " + name;
24+
else if (type === "person") userName = name;
25+
const groupResponse = await client.from("my_groups").select();
26+
if (groupResponse.error) {
27+
internalError({
28+
error: groupResponse.error,
29+
});
30+
throw new Error("Could not access DiscourseGraphs");
31+
}
32+
groupData = groupResponse.data;
33+
const membershipReq = await client
34+
.from("group_membership")
35+
.select("group_id,admin")
36+
.eq("member_id", id);
37+
if (membershipReq.error) {
38+
internalError({
39+
error: membershipReq.error,
40+
});
41+
throw new Error("Could not access Discourse Graphs");
42+
}
43+
adminData = Object.fromEntries(
44+
// eslint-disable-next-line @typescript-eslint/naming-convention
45+
membershipReq.data.map(({ group_id, admin }) => [
46+
group_id,
47+
admin || false,
48+
]),
49+
);
50+
} catch (error) {
51+
error = error instanceof Error ? error.message : "An unknown error occured";
52+
}
7453

7554
return (
7655
<>
@@ -81,7 +60,7 @@ export const ListGroups = () => {
8160
{error ? (
8261
"Error: " + error
8362
) : groupData === null ? (
84-
"loading"
63+
"Error" // we should have had an error in that case
8564
) : groupData.length === 0 ? (
8665
<p>You are not part of any group.</p>
8766
) : (

apps/website/app/utils/supabase/account.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ export const getSessionUserData = async (
1111
type: AgentType;
1212
email?: string;
1313
} | null> => {
14-
const session = await client.auth.getSession();
15-
if (!session?.data?.session?.user) return null;
16-
const { id, email } = session.data.session.user;
14+
const { data, error } = await client.auth.getUser();
15+
if (error || !data?.user) return null;
16+
const userData = data.user;
17+
if (typeof userData.id !== "string") return null;
18+
const { id, email }: { id: string; email?: string } = userData;
1719
if (email) {
1820
const [name, host] = email.split("@") as [string, string];
1921
if (host === "database.discoursegraphs.com" && name.endsWith("-anon")) {
@@ -37,7 +39,7 @@ export const getSessionUserData = async (
3739
const accountReq = await client
3840
.from("PlatformAccount")
3941
.select("name")
40-
.eq("dg_account", session.data.session.user.id)
42+
.eq("dg_account", id)
4143
.eq("agent_type", "person")
4244
.maybeSingle();
4345
if (accountReq.error || !accountReq.data) {

0 commit comments

Comments
 (0)