-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
61857ed
commit 67f88c6
Showing
7 changed files
with
171 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { | ||
createContext, | ||
useContext, | ||
useEffect, | ||
useMemo, | ||
type PropsWithChildren | ||
} from 'react'; | ||
import RealtimeClient from '@u22n/realtime/client'; | ||
import { useGlobalStore } from './global-store-provider'; | ||
import { env } from 'next-runtime-env'; | ||
import { toast } from 'sonner'; | ||
|
||
const realtimeContext = createContext<RealtimeClient | null>(null); | ||
|
||
const appKey = env('NEXT_PUBLIC_REALTIME_APP_KEY')!; | ||
const host = env('NEXT_PUBLIC_REALTIME_HOST')!; | ||
const port = Number(env('NEXT_PUBLIC_REALTIME_PORT')!); | ||
const PLATFORM_URL = env('NEXT_PUBLIC_PLATFORM_URL')!; | ||
|
||
export function RealtimeProvider({ children }: PropsWithChildren) { | ||
const orgShortCode = useGlobalStore((state) => state.currentOrg.shortCode); | ||
|
||
const client = useMemo( | ||
() => | ||
new RealtimeClient({ | ||
appKey, | ||
host, | ||
port, | ||
authEndpoint: `${PLATFORM_URL}/realtime/auth` | ||
}), | ||
[] | ||
); | ||
|
||
useEffect(() => { | ||
void client.connect({ orgShortCode }).catch(() => { | ||
toast.error( | ||
'Uninbox encountered an error while trying to connect to the realtime server' | ||
); | ||
}); | ||
return () => { | ||
client.disconnect(); | ||
}; | ||
}, [client, orgShortCode]); | ||
|
||
return ( | ||
<realtimeContext.Provider value={client}> | ||
{children} | ||
</realtimeContext.Provider> | ||
); | ||
} | ||
|
||
export function useRealtime() { | ||
const client = useContext(realtimeContext); | ||
if (!client) { | ||
throw new Error('useRealtime must be used within RealtimeProvider'); | ||
} | ||
return client; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.