Skip to content

Commit b74614b

Browse files
committed
fetch from invite splash
1 parent 158ea61 commit b74614b

3 files changed

Lines changed: 41 additions & 22 deletions

File tree

hooks/useRealtimeStore.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export function useRealtimeStore(supabase: SupabaseClient<Database>) {
6464
const getAllServerRoles = useGetAllServerRoles();
6565
const getRolesForServer = useGetRolesForServer();
6666

67-
const getAllServerProfiles = useGetAllServerUserProfiles();
67+
const getAllServerProfilesForServer = useGetAllServerUserProfiles();
6868
const updateServerUserProfile = useUpateServerUserProfile();
6969
const updateServerUserProfileByServerUserId = useUpdateServerUserProfileByServerUserId();
7070
const stripServerUserAndRoles = useStripServerUserAndRoles();
@@ -92,7 +92,20 @@ export function useRealtimeStore(supabase: SupabaseClient<Database>) {
9292

9393
addServer(supabase, (payload.new as ServerUser).id);
9494
getRolesForServer(supabase, payload.new.server_id);
95-
updateServerUserProfileByServerUserId(supabase, payload.new.server_id);
95+
updateServerUserProfileByServerUserId(supabase, payload.new.id);
96+
}
97+
)
98+
.on<ServerUser>(
99+
'postgres_changes',
100+
{
101+
event: 'UPDATE',
102+
schema: 'public',
103+
table: 'server_users',
104+
filter: `profile_id=neq.${user?.id}`,
105+
},
106+
async (payload) => {
107+
console.log('Another user joined server');
108+
getAllServerProfilesForServer(supabase, payload.new.server_id);
96109
}
97110
)
98111
.on<ServerUser>(
@@ -104,8 +117,8 @@ export function useRealtimeStore(supabase: SupabaseClient<Database>) {
104117
filter: `profile_id=eq.${user?.id}`,
105118
},
106119
async (payload) => {
107-
console.log('update');
108-
updateServerUserProfileByServerUserId(supabase, payload.new.server_id);
120+
console.log('This user joined server');
121+
updateServerUserProfileByServerUserId(supabase, payload.new.id);
109122
}
110123
)
111124
.on<ServerUser>(
@@ -203,7 +216,7 @@ export function useRealtimeStore(supabase: SupabaseClient<Database>) {
203216
addDMChannel(supabase, payload.new.id);
204217

205218
// Fetch the profiles for the server
206-
getAllServerProfiles(supabase, payload.new.id);
219+
getAllServerProfilesForServer(supabase, payload.new.id);
207220
}
208221
)
209222
.on<Role>(
@@ -215,7 +228,7 @@ export function useRealtimeStore(supabase: SupabaseClient<Database>) {
215228
},
216229
async (payload) => {
217230
console.log('Server role insert event');
218-
getAllServerProfiles(supabase, payload.new.server_id);
231+
getAllServerProfilesForServer(supabase, payload.new.server_id);
219232
addRole(payload.new);
220233
}
221234
)
@@ -228,7 +241,7 @@ export function useRealtimeStore(supabase: SupabaseClient<Database>) {
228241
},
229242
async (payload) => {
230243
console.log('Server role update event');
231-
getAllServerProfiles(supabase, payload.new.server_id);
244+
getAllServerProfilesForServer(supabase, payload.new.server_id);
232245
updateRole(payload.new);
233246
}
234247
)
@@ -252,7 +265,7 @@ export function useRealtimeStore(supabase: SupabaseClient<Database>) {
252265
for (const role of profile.roles) {
253266
// As soon as we find the role that was deleted, we trigger a server-wide profile update
254267
if (role.id === payload.old.id) {
255-
getAllServerProfiles(supabase, server_id);
268+
getAllServerProfilesForServer(supabase, server_id);
256269
return;
257270
}
258271
}
@@ -337,7 +350,7 @@ export function useRealtimeStore(supabase: SupabaseClient<Database>) {
337350
deleteRole,
338351
getRolesForServer,
339352
updateServerUserProfileByServerUserId,
340-
getAllServerProfiles,
353+
getAllServerProfilesForServer,
341354
allServerProfiles,
342355
updateServerUserProfile,
343356
stripServerUserAndRoles,

pages/index.tsx

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
useConnectionRef,
1414
useUserSettings,
1515
useSetConnectionState,
16+
useGetAllServerUserProfiles,
1617
} from '@/lib/store';
1718
import { useSetChannel } from '@/lib/store';
1819
import { getChannelById } from '@/services/channels.service';
@@ -25,7 +26,7 @@ export default function Home() {
2526
const token = useTokenRef();
2627
const userSettings = useUserSettings();
2728
const setChannel = useSetChannel();
28-
const { c: channel_id } = router.query;
29+
const { c: channel_id, s: server_id } = router.query;
2930

3031
useRealtimeStore(supabase);
3132
const [isMobile, setIsMobile] = useState(false);
@@ -35,7 +36,7 @@ export default function Home() {
3536
const tryConnect = useConnectionRef();
3637
const setConnectionState = useSetConnectionState();
3738
const [livekitError, setLivekitError] = useState<Error | null>(null);
38-
39+
const getAllServerProfilesForServer = useGetAllServerUserProfiles();
3940
const modalRef = useRef<HTMLDialogElement>(null);
4041

4142
const checkMobile = useMediaQuery({ query: '(max-width: 940px)' });
@@ -65,23 +66,27 @@ export default function Home() {
6566
async function handleAsync() {
6667
// First, try parsing the channel_id as a number. If that fails, we're done
6768
const channel_id_as_number = parseInt(channel_id as string);
69+
const server_id_as_number = parseInt(server_id as string);
6870

69-
if (isNaN(channel_id_as_number)) {
70-
return;
71-
}
71+
if (!isNaN(channel_id_as_number)) {
72+
const { data: _channel } = await getChannelById(
73+
supabase,
74+
channel_id_as_number
75+
);
7276

73-
const { data: _channel } = await getChannelById(
74-
supabase,
75-
channel_id_as_number
76-
);
77+
if (_channel) {
78+
setChannel(_channel);
79+
}
80+
}
7781

78-
if (_channel) {
79-
setChannel(_channel);
82+
if (!isNaN(server_id_as_number)) {
83+
getAllServerProfilesForServer(supabase, server_id_as_number);
8084
}
85+
8186
}
8287

8388
handleAsync();
84-
}, [channel_id, setChannel, supabase]);
89+
}, [channel_id, setChannel, supabase, server_id, getAllServerProfilesForServer]);
8590

8691
return (
8792
<>

pages/invite/[inviteCode].tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { OverflowMarquee } from '@/components/home/OverflowMarquee';
1010
import { getServersForUser } from '@/services/server.service';
1111
import { useMediaQuery } from 'react-responsive';
1212
import { toast } from 'react-toastify';
13+
import { useRealtimeStore } from '@/hooks/useRealtimeStore';
1314

1415
export default function InviteSplash() {
1516
const user = useUser();
@@ -116,7 +117,7 @@ export default function InviteSplash() {
116117
return;
117118
}
118119

119-
router.push(`/?c=${invite.channel_id}`, '/');
120+
router.push(`/?c=${invite.channel_id}&s=${invite.server_id}`, '/');
120121
}}
121122
>
122123
{userInServer ? 'Joined' : 'Join'}

0 commit comments

Comments
 (0)