|
| 1 | +import { TokenSource, TokenSourceBase, TokenSourceResponseObject } from 'livekit-client'; |
| 2 | +import { createContext, useContext, useMemo, useState } from 'react'; |
| 3 | +import { SessionProvider, useSession } from '@livekit/components-react'; |
| 4 | + |
| 5 | +// TODO: Add your Sandbox ID here |
| 6 | +const sandboxID = ''; |
| 7 | + |
| 8 | +// The name of the agent you wish to be dispatched. |
| 9 | +const agentName = undefined |
| 10 | + |
| 11 | +// NOTE: If you prefer not to use LiveKit Sandboxes for testing, you can generate your |
| 12 | +// tokens manually by visiting https://cloud.livekit.io/projects/p_/settings/keys |
| 13 | +// and using one of your API Keys to generate a token with custom TTL and permissions. |
| 14 | + |
| 15 | +// For use without a token server. |
| 16 | +const hardcodedUrl = ''; |
| 17 | +const hardcodedToken = ''; |
| 18 | + |
| 19 | +interface ConnectionContextType { |
| 20 | + isConnectionActive: boolean; |
| 21 | + connect: () => void; |
| 22 | + startDisconnectTransition: () => void; |
| 23 | + onDisconnectTransitionComplete: () => void; |
| 24 | +} |
| 25 | + |
| 26 | +const ConnectionContext = createContext<ConnectionContextType>({ |
| 27 | + isConnectionActive: false, |
| 28 | + connect: () => {}, |
| 29 | + startDisconnectTransition: () => {}, |
| 30 | + onDisconnectTransitionComplete: () => {}, |
| 31 | +}); |
| 32 | + |
| 33 | +export function useConnection() { |
| 34 | + const ctx = useContext(ConnectionContext); |
| 35 | + if (!ctx) { |
| 36 | + throw new Error('useConnection must be used within a ConnectionProvider'); |
| 37 | + } |
| 38 | + return ctx; |
| 39 | +} |
| 40 | + |
| 41 | +interface ConnectionProviderProps { |
| 42 | + children: React.ReactNode; |
| 43 | +} |
| 44 | + |
| 45 | +export function ConnectionProvider({ children }: ConnectionProviderProps) { |
| 46 | + const [isConnectionActive, setIsConnectionActive] = useState(false); |
| 47 | + |
| 48 | + const tokenSource = useMemo(() => { |
| 49 | + if (sandboxID) { |
| 50 | + return TokenSource.sandboxTokenServer(sandboxID) |
| 51 | + } else { |
| 52 | + return TokenSource.literal( |
| 53 | + { |
| 54 | + serverUrl: hardcodedUrl, |
| 55 | + participantToken: hardcodedToken, |
| 56 | + } satisfies TokenSourceResponseObject |
| 57 | + ) |
| 58 | + } |
| 59 | + }, [sandboxID, hardcodedUrl, hardcodedToken]) |
| 60 | + |
| 61 | + const session = useSession( |
| 62 | + tokenSource, |
| 63 | + agentName ? { agentName } : undefined |
| 64 | + ); |
| 65 | + |
| 66 | + const { start: startSession, end: endSession } = session; |
| 67 | + |
| 68 | + const value = useMemo(() => { |
| 69 | + return { |
| 70 | + isConnectionActive, |
| 71 | + connect: () => { |
| 72 | + setIsConnectionActive(true); |
| 73 | + startSession(); |
| 74 | + }, |
| 75 | + startDisconnectTransition: () => { |
| 76 | + setIsConnectionActive(false); |
| 77 | + }, |
| 78 | + onDisconnectTransitionComplete: () => { |
| 79 | + endSession(); |
| 80 | + }, |
| 81 | + }; |
| 82 | + }, [startSession, endSession, isConnectionActive]); |
| 83 | + |
| 84 | + return ( |
| 85 | + <SessionProvider session={session}> |
| 86 | + <ConnectionContext.Provider value={value}>{children}</ConnectionContext.Provider> |
| 87 | + </SessionProvider> |
| 88 | + ); |
| 89 | +} |
0 commit comments