Skip to content

Commit 3f859a2

Browse files
committed
use store for realtime to fix bugs + more use
1 parent 8800856 commit 3f859a2

6 files changed

Lines changed: 59 additions & 38 deletions

File tree

components/home/MessageHeader.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ export function MessageHeader({
2727
const currentUser = useUser();
2828
const supabase = useSupabaseClient();
2929
const relation = useRelations().find((relation) => relation.target_profile.id === profile.id);
30-
console.table(relation);
3130

3231
return (
3332
<ContextMenu.Root>

components/home/PresenceIndicator.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { useOnlinePresenceRef } from '@/context/ChatCtx';
1+
import { useOnlineUsers } from '@/lib/store';
22

33
export function PresenceIndicator({ userId }: { userId: string }) {
4-
const channel = useOnlinePresenceRef()!;
4+
const onlineUsers = useOnlineUsers();
55

66
return (
77
<div
@@ -12,7 +12,7 @@ export function PresenceIndicator({ userId }: { userId: string }) {
1212
h-4
1313
rounded-full
1414
z-10
15-
${channel.presenceState()[userId] ? 'bg-green-600' : 'bg-slate-500'}
15+
${onlineUsers[userId] ? 'bg-green-600' : 'bg-slate-500'}
1616
border-2
1717
-mt-3
1818
mr-2

components/home/Server.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import VerticalSettingsIcon from '@/components/icons/VerticalSettingsIcon';
22
import ChannelMessageIcon from '../icons/ChannelMessageIcon';
33
import { SyntheticEvent, useEffect, useState } from 'react';
4-
import { useOnlinePresenceRef } from '@/context/ChatCtx';
54
import { getChannelsInServer } from '@/services/channels.service';
65
import ServersIcon from '../icons/ServersIcon';
76
import { useSupabaseClient } from '@supabase/auth-helpers-react';

components/home/ServerMemberStats.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useOnlinePresenceRef } from '@/context/ChatCtx';
1+
import { useOnlineUsers } from '@/lib/store';
22
import { getServerMemberCount, getUsersInServer } from '@/services/server.service';
33
import styles from '@/styles/Servers.module.css';
44
import { Server } from '@/types/dbtypes';
@@ -8,8 +8,8 @@ import { useEffect, useState } from 'react';
88
export function ServerMemberStats({ server, flexStyle }: { server: Server, flexStyle?: string }) {
99
const [ memberCount, setMemberCount ] = useState(0);
1010
const [ onlineCount, setOnlineCount ] = useState(0);
11-
const onlinePresenceChannel = useOnlinePresenceRef();
1211
const supabase = useSupabaseClient();
12+
const onlineUsers = useOnlineUsers();
1313

1414
useEffect(() => {
1515
async function handleAsync() {
@@ -19,19 +19,19 @@ export function ServerMemberStats({ server, flexStyle }: { server: Server, flexS
1919
// Now we need to get the online count
2020
const { data: onlineData, error } = await getUsersInServer(supabase, server.id);
2121

22-
let onlineUsers = 0;
22+
let amtOnlineUsers = 0;
2323
if (!error) {
2424
for (const profile of onlineData) {
25-
if (onlinePresenceChannel.presenceState()[profile.id] !== undefined) {
26-
onlineUsers++;
25+
if (onlineUsers[profile.id] !== undefined) {
26+
amtOnlineUsers++;
2727
}
2828
}
29-
setOnlineCount(onlineUsers);
29+
setOnlineCount(amtOnlineUsers);
3030
}
3131
}
3232

3333
handleAsync();
34-
}, [supabase, onlinePresenceChannel, server.id]);
34+
}, [supabase, onlineUsers, server.id]);
3535

3636
return (
3737
<div className={`text-xs tracking-wide text-grey-300 ${flexStyle}`}>

context/ChatCtx.tsx

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,18 @@
1-
import { getServersForUser } from '@/services/server.service';
2-
import { ServersForUser } from '@/types/dbtypes';
1+
import { useFlagUserOffline, useFlagUserOnline } from '@/lib/store';
32
import { useSupabaseClient, useUser } from '@supabase/auth-helpers-react';
4-
import { RealtimeChannel } from '@supabase/supabase-js';
53
import {
64
createContext,
7-
SetStateAction,
8-
useContext,
9-
useState,
10-
Dispatch,
11-
useEffect,
125
} from 'react';
136

14-
type ChatCtxValue = {
15-
onlinePresenceRef: RealtimeChannel | null;
16-
};
17-
18-
export const ChatCtxDefaultVal: ChatCtxValue = {
19-
onlinePresenceRef: null,
20-
};
21-
7+
type ChatCtxValue = { };
8+
export const ChatCtxDefaultVal: ChatCtxValue = { };
229
export const ChatCtx = createContext(ChatCtxDefaultVal);
2310

2411
export function ChatCtxProvider({ children }: { children: React.ReactNode }) {
2512
const supabase = useSupabaseClient();
2613
const user = useUser();
14+
const flagUserOnline = useFlagUserOnline();
15+
const flagUserOffline = useFlagUserOffline();
2716

2817
const onlinePresenceRef = supabase.channel('online-users', {
2918
config: {
@@ -33,6 +22,14 @@ export function ChatCtxProvider({ children }: { children: React.ReactNode }) {
3322
},
3423
});
3524

25+
onlinePresenceRef
26+
.on('presence', { event: 'join' }, ({ key, newPresences }) => {
27+
flagUserOnline(key, newPresences);
28+
})
29+
.on('presence', { event: 'leave' }, ({ key }) => {
30+
flagUserOffline(key);
31+
});
32+
3633
onlinePresenceRef.subscribe(async (status) => {
3734
if (status === 'SUBSCRIBED') {
3835
const status = await onlinePresenceRef.track({
@@ -42,17 +39,8 @@ export function ChatCtxProvider({ children }: { children: React.ReactNode }) {
4239
});
4340

4441
return (
45-
<ChatCtx.Provider
46-
value={{
47-
onlinePresenceRef,
48-
}}
49-
>
42+
<ChatCtx.Provider value={ChatCtxDefaultVal}>
5043
{children}
5144
</ChatCtx.Provider>
5245
);
5346
}
54-
55-
export function useOnlinePresenceRef() {
56-
const { onlinePresenceRef } = useContext(ChatCtx);
57-
return onlinePresenceRef!;
58-
}

lib/store.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,38 @@ const useRelationsStore = create<RelationsState>()((set) => ({
241241
}
242242
}));
243243

244+
type OnlineUser = {
245+
[key: string]: any;
246+
}
247+
export interface OnlineState {
248+
onlineUsers: OnlineUser;
249+
250+
flagUserOnline: (userId: string, presenceInfo: any) => void;
251+
flagUserOffline: (userId: string) => void;
252+
}
253+
254+
const useOnlineStore = create<OnlineState>()((set) => ({
255+
onlineUsers: {},
256+
257+
flagUserOnline: (userId, presenceInfo) => {
258+
set((state) => ({
259+
onlineUsers: {
260+
...state.onlineUsers,
261+
[userId]: presenceInfo
262+
}
263+
}));
264+
},
265+
266+
flagUserOffline: (userId) => {
267+
set((state) => {
268+
const { [userId]: _, ...rest } = state.onlineUsers;
269+
return {
270+
onlineUsers: rest
271+
};
272+
});
273+
},
274+
}));
275+
244276
export const useServers = () => useServerStore((state) => state.servers);
245277
export const useAddServer = () => useServerStore((state) => state.addServer);
246278
export const useRemoveServer = () =>
@@ -269,3 +301,6 @@ export const useRemoveRelation = () =>
269301
useRelationsStore((state) => state.removeRelation);
270302
export const useGetRelations = () =>
271303
useRelationsStore((state) => state.getRelations);
304+
export const useOnlineUsers = () => useOnlineStore((state) => state.onlineUsers);
305+
export const useFlagUserOnline = () => useOnlineStore((state) => state.flagUserOnline);
306+
export const useFlagUserOffline = () => useOnlineStore((state) => state.flagUserOffline);

0 commit comments

Comments
 (0)