-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathListGroups.tsx
More file actions
85 lines (81 loc) · 2.59 KB
/
Copy pathListGroups.tsx
File metadata and controls
85 lines (81 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { createClient } from "~/utils/supabase/server";
import { getSessionUserData } from "~/utils/supabase/account";
import Link from "next/link";
import { Tables } from "@repo/database/dbTypes";
import internalError from "~/utils/internalErrorSsr";
type GroupData = Tables<"my_groups">;
export const ListGroups = async () => {
let groupData: GroupData[] | null = null;
let adminData: Record<string, boolean> = {};
let userName: string | undefined;
let error: string | undefined;
try {
const client = await createClient();
const userData = await getSessionUserData(client);
if (!userData) {
throw new Error("Not logged in.\nPlease log in from application.");
}
const { name, type, id } = userData;
if (type === "anonymous") userName = "Space " + name;
else if (type === "group") userName = "group " + name;
else if (type === "person") userName = name;
const groupResponse = await client.from("my_groups").select();
if (groupResponse.error) {
internalError({
error: groupResponse.error,
});
throw new Error("Could not access Discourse Graphs");
}
groupData = groupResponse.data;
const membershipReq = await client
.from("group_membership")
.select("group_id,admin")
.eq("member_id", id);
if (membershipReq.error) {
internalError({
error: membershipReq.error,
});
throw new Error("Could not access Discourse Graphs");
}
adminData = Object.fromEntries(
// eslint-disable-next-line @typescript-eslint/naming-convention
membershipReq.data.map(({ group_id, admin }) => [
group_id,
admin || false,
]),
);
} catch (e) {
error = e instanceof Error ? e.message : "An unknown error occured";
}
return (
<>
<div className="text-right text-sm">
{userName ? <p>Logged in as {userName}</p> : ""}
</div>
<div>
{error ? (
"Error: " + error
) : groupData === null ? (
"Error" // we should have had an error in that case
) : groupData.length === 0 ? (
<p>You are not part of any group.</p>
) : (
<>
<p>Your groups:</p>
<ul className="list-inside list-disc space-y-2">
{groupData.map((d) => (
<li key={d.id}>
{adminData[d.id || ""] ? (
<Link href={"/auth/group/" + d.id!}>{d.name}</Link>
) : (
d.name
)}
</li>
))}
</ul>
</>
)}
</div>
</>
);
};