11"use client"
22
3- import { CLOUD_ENABLED } from "@/cloud/CloudConnect" ;
43import { useCloud } from "@/cloud/useCloud" ;
54import React , { createContext , useState } from "react" ;
65import { useCallback } from "react" ;
76import { useConfig } from "./useConfig" ;
87
8+ export type ConnectionMode = "cloud" | "manual" | "env"
9+
910type TokenGeneratorData = {
1011 shouldConnect : boolean ;
1112 wsUrl : string ;
1213 token : string ;
14+ mode : ConnectionMode ;
1315 disconnect : ( ) => Promise < void > ;
14- connect : ( ) => Promise < void > ;
16+ connect : ( mode : ConnectionMode ) => Promise < void > ;
1517} ;
1618
1719const ConnectionContext = createContext < TokenGeneratorData | undefined > ( undefined ) ;
@@ -26,24 +28,28 @@ export const ConnectionProvider = ({
2628 const [ connectionDetails , setConnectionDetails ] = useState < {
2729 wsUrl : string ;
2830 token : string ;
31+ mode : ConnectionMode ;
2932 shouldConnect : boolean ;
30- } > ( { wsUrl : "" , token : "" , shouldConnect : false } ) ;
33+ } > ( { wsUrl : "" , token : "" , shouldConnect : false , mode : "manual" } ) ;
3134
32- const connect = useCallback ( async ( ) => {
35+ const connect = useCallback ( async ( mode : ConnectionMode ) => {
3336 let token = "" ;
3437 let url = "" ;
35- if ( CLOUD_ENABLED ) {
38+ if ( mode === "cloud" ) {
3639 token = await generateToken ( ) ;
3740 url = cloudWSUrl ;
38- } else if ( process . env . NEXT_PUBLIC_LIVEKIT_URL ) {
41+ } else if ( mode === "env" ) {
42+ if ( ! process . env . NEXT_PUBLIC_LIVEKIT_URL ) {
43+ throw new Error ( "NEXT_PUBLIC_LIVEKIT_URL is not set" ) ;
44+ }
3945 url = process . env . NEXT_PUBLIC_LIVEKIT_URL ;
4046 const { accessToken} = await fetch ( "/api/token" ) . then ( ( res ) => res . json ( ) ) ;
4147 token = accessToken ;
4248 } else {
4349 token = config . settings . token ;
4450 url = config . settings . ws_url ;
4551 }
46- setConnectionDetails ( { wsUrl : url , token, shouldConnect : true } ) ;
52+ setConnectionDetails ( { wsUrl : url , token, shouldConnect : true , mode } ) ;
4753 } , [
4854 cloudWSUrl ,
4955 config . settings . token ,
@@ -61,6 +67,7 @@ export const ConnectionProvider = ({
6167 wsUrl : connectionDetails . wsUrl ,
6268 token : connectionDetails . token ,
6369 shouldConnect : connectionDetails . shouldConnect ,
70+ mode : connectionDetails . mode ,
6471 connect,
6572 disconnect,
6673 } }
0 commit comments