1
1
"use client"
2
2
3
- import { CLOUD_ENABLED } from "@/cloud/CloudConnect" ;
4
3
import { useCloud } from "@/cloud/useCloud" ;
5
4
import React , { createContext , useState } from "react" ;
6
5
import { useCallback } from "react" ;
7
6
import { useConfig } from "./useConfig" ;
8
7
8
+ export type ConnectionMode = "cloud" | "manual" | "env"
9
+
9
10
type TokenGeneratorData = {
10
11
shouldConnect : boolean ;
11
12
wsUrl : string ;
12
13
token : string ;
14
+ mode : ConnectionMode ;
13
15
disconnect : ( ) => Promise < void > ;
14
- connect : ( ) => Promise < void > ;
16
+ connect : ( mode : ConnectionMode ) => Promise < void > ;
15
17
} ;
16
18
17
19
const ConnectionContext = createContext < TokenGeneratorData | undefined > ( undefined ) ;
@@ -26,24 +28,28 @@ export const ConnectionProvider = ({
26
28
const [ connectionDetails , setConnectionDetails ] = useState < {
27
29
wsUrl : string ;
28
30
token : string ;
31
+ mode : ConnectionMode ;
29
32
shouldConnect : boolean ;
30
- } > ( { wsUrl : "" , token : "" , shouldConnect : false } ) ;
33
+ } > ( { wsUrl : "" , token : "" , shouldConnect : false , mode : "manual" } ) ;
31
34
32
- const connect = useCallback ( async ( ) => {
35
+ const connect = useCallback ( async ( mode : ConnectionMode ) => {
33
36
let token = "" ;
34
37
let url = "" ;
35
- if ( CLOUD_ENABLED ) {
38
+ if ( mode === "cloud" ) {
36
39
token = await generateToken ( ) ;
37
40
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
+ }
39
45
url = process . env . NEXT_PUBLIC_LIVEKIT_URL ;
40
46
const { accessToken} = await fetch ( "/api/token" ) . then ( ( res ) => res . json ( ) ) ;
41
47
token = accessToken ;
42
48
} else {
43
49
token = config . settings . token ;
44
50
url = config . settings . ws_url ;
45
51
}
46
- setConnectionDetails ( { wsUrl : url , token, shouldConnect : true } ) ;
52
+ setConnectionDetails ( { wsUrl : url , token, shouldConnect : true , mode } ) ;
47
53
} , [
48
54
cloudWSUrl ,
49
55
config . settings . token ,
@@ -61,6 +67,7 @@ export const ConnectionProvider = ({
61
67
wsUrl : connectionDetails . wsUrl ,
62
68
token : connectionDetails . token ,
63
69
shouldConnect : connectionDetails . shouldConnect ,
70
+ mode : connectionDetails . mode ,
64
71
connect,
65
72
disconnect,
66
73
} }
0 commit comments