import React from "react" ; import type { Awareness , PresenceProviderProps , User } from "~types" ; export const PresenceContext = React . createContext < Awareness | null > ( null ) ; export function PresenceProvider ( { awareness , children } ) { React . useEffect ( ( ) => { const currUser = awareness . getLocalState ( ) ; if ( ! currUser || Object . keys ( currUser ) . length === 0 ) { const user = { id : awareness . clientID } ; awareness . setLocalState ( user ) ; } } , [ awareness ] ) ; React . useEffect ( ( ) => { function handleUnload ( ) { awareness . setLocalState ( null ) ; } window . addEventListener ( "unload" , handleUnload ) ; return ( ) => { window . removeEventListener ( "unload" , handleUnload ) ; } ; } ) ; return ( < PresenceContext . Provider value = { awareness } > { children } < /PresenceContext.Provider>); }