1- "use client" ;
2-
3- import { createClient } from "~/utils/supabase/client" ;
1+ import { createClient } from "~/utils/supabase/server" ;
42import { getSessionUserData } from "~/utils/supabase/account" ;
53import Link from "next/link" ;
6- import { useState , useEffect } from "react" ;
74import { Tables } from "@repo/database/dbTypes" ;
8- import useInternalError from "~/utils/internalError " ;
5+ import internalError from "~/utils/internalErrorSsr " ;
96
107type 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 ) : (
0 commit comments