Skip to content

Commit 15db8ad

Browse files
committed
only give the group link if admin
1 parent 0998402 commit 15db8ad

2 files changed

Lines changed: 37 additions & 11 deletions

File tree

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

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ type GroupData = Tables<"my_groups">;
99

1010
export const ListGroups = () => {
1111
const [groupData, setGroupData] = useState<GroupData[] | null>(null);
12+
const [adminData, setAdminData] = useState<Record<string, boolean>>({});
1213
const [userName, setUserName] = useState<string | null>(null);
1314
const [error, setError] = useState<string | null>(null);
1415

@@ -21,16 +22,32 @@ export const ListGroups = () => {
2122
setError("Not logged in");
2223
return;
2324
}
24-
const { name, type } = userData;
25+
const { name, type, id } = userData;
2526
if (type === "anonymous") setUserName("Space " + name);
2627
else if (type === "group") setUserName("group " + name);
2728
else if (type === "person") setUserName(name);
28-
const response = await client.from("my_groups").select();
29-
if (response.error) {
30-
setError(response.error.message);
29+
const groupResponse = await client.from("my_groups").select();
30+
if (groupResponse.error) {
31+
setError(groupResponse.error.message);
3132
return;
3233
}
33-
setGroupData(response.data);
34+
setGroupData(groupResponse.data);
35+
const membershipReq = await client
36+
.from("group_membership")
37+
.select("group_id,admin")
38+
.eq("member_id", id);
39+
if (membershipReq.error) {
40+
setError(membershipReq.error.message);
41+
return;
42+
}
43+
setAdminData(
44+
Object.fromEntries(
45+
membershipReq.data.map(({ group_id, admin }) => [
46+
group_id,
47+
admin || false,
48+
]),
49+
),
50+
);
3451
} catch (error) {
3552
setError(
3653
error instanceof Error ? error.message : "Unknown error occurred",
@@ -56,7 +73,11 @@ export const ListGroups = () => {
5673
<ul>
5774
{groupData.map((d) => (
5875
<li key={d.id}>
59-
<a href={"group/" + d.id!}>{d.name}</a>
76+
{adminData[d.id || ""] ? (
77+
<a href={"group/" + d.id!}>{d.name}</a>
78+
) : (
79+
d.name
80+
)}
6081
</li>
6182
))}
6283
</ul>

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -485,10 +485,15 @@ export const processAndInsertBatch = async <
485485

486486
export const getSessionUserData = async (
487487
client: DGSupabaseClient,
488-
): Promise<{ name: string; type: AgentType; email?: string } | null> => {
488+
): Promise<{
489+
id: string;
490+
name: string;
491+
type: AgentType;
492+
email?: string;
493+
} | null> => {
489494
const session = await client.auth.getSession();
490495
if (!session?.data?.session?.user) return null;
491-
const email = session.data.session.user.email;
496+
const { id, email } = session.data.session.user;
492497
if (email) {
493498
const [name, host] = email.split("@") as [string, string];
494499
if (host === "database.discoursegraphs.com" && name.endsWith("-anon")) {
@@ -502,10 +507,10 @@ export const getSessionUserData = async (
502507
if (spaceReq.error || !spaceReq.data) {
503508
return null;
504509
}
505-
return { name: spaceReq.data.name, type: "anonymous", email };
510+
return { name: spaceReq.data.name, id, type: "anonymous", email };
506511
}
507512
if (host === "groups.discoursegraphs.com") {
508-
return { name, email, type: "group" };
513+
return { name, id, email, type: "group" };
509514
}
510515
}
511516
const accountReq = await client
@@ -517,5 +522,5 @@ export const getSessionUserData = async (
517522
if (accountReq.error || !accountReq.data) {
518523
return null;
519524
}
520-
return { name: accountReq.data.name, type: "person", email };
525+
return { id, name: accountReq.data.name, type: "person", email };
521526
};

0 commit comments

Comments
 (0)