1- import { getServersForUser } from '@/services/server.service' ;
2- import { ServersForUser } from '@/types/dbtypes' ;
1+ import { useFlagUserOffline , useFlagUserOnline } from '@/lib/store' ;
32import { useSupabaseClient , useUser } from '@supabase/auth-helpers-react' ;
4- import { RealtimeChannel } from '@supabase/supabase-js' ;
53import {
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 = { } ;
229export const ChatCtx = createContext ( ChatCtxDefaultVal ) ;
2310
2411export 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- }
0 commit comments