Skip to content

Commit 70f69a3

Browse files
committed
Re-introduce connection mode
1 parent c039254 commit 70f69a3

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

src/components/PlaygroundConnect.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import { useConfig } from "@/hooks/useConfig";
22
import { CLOUD_ENABLED, CloudConnect } from "../cloud/CloudConnect";
33
import { Button } from "./button/Button";
44
import { useState } from "react";
5+
import { ConnectionMode } from "@/hooks/useConnection";
56

67
type PlaygroundConnectProps = {
78
accentColor: string;
8-
onConnectClicked: () => void;
9+
onConnectClicked: (mode: ConnectionMode) => void;
910
};
1011

1112
const ConnectTab = ({ active, onClick, children }: any) => {
@@ -57,7 +58,7 @@ const TokenConnect = ({
5758
newSettings.ws_url = url;
5859
newSettings.token = token;
5960
setUserSettings(newSettings);
60-
onConnectClicked();
61+
onConnectClicked("manual");
6162
}}
6263
>
6364
Connect

src/hooks/useConnection.tsx

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
"use client"
22

3-
import { CLOUD_ENABLED } from "@/cloud/CloudConnect";
43
import { useCloud } from "@/cloud/useCloud";
54
import React, { createContext, useState } from "react";
65
import { useCallback } from "react";
76
import { useConfig } from "./useConfig";
87

8+
export type ConnectionMode = "cloud" | "manual" | "env"
9+
910
type 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

1719
const 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
}}

src/pages/index.tsx

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@ import { PlaygroundConnect } from "@/components/PlaygroundConnect";
1212
import Playground from "@/components/playground/Playground";
1313
import { PlaygroundToast, ToastType } from "@/components/toast/PlaygroundToast";
1414
import { ConfigProvider, useConfig } from "@/hooks/useConfig";
15-
import { ConnectionProvider, useConnection } from "@/hooks/useConnection";
15+
import { ConnectionMode, ConnectionProvider, useConnection } from "@/hooks/useConnection";
1616
import { useMemo } from "react";
17-
import { CLOUD_ENABLED } from "@/cloud/CloudConnect";
18-
import { useCloud } from "@/cloud/useCloud";
1917

2018
const themeColors = [
2119
"cyan",
@@ -45,14 +43,14 @@ export function HomeInner() {
4543
message: string;
4644
type: ToastType;
4745
} | null>(null);
48-
const { shouldConnect, wsUrl, token, connect, disconnect } =
46+
const { shouldConnect, wsUrl, token, mode, connect, disconnect } =
4947
useConnection();
5048

5149
const {config} = useConfig();
5250

5351
const handleConnect = useCallback(
54-
async (c: boolean) => {
55-
c ? connect() : disconnect();
52+
async (c: boolean, mode: ConnectionMode) => {
53+
c ? connect(mode) : disconnect();
5654
},
5755
[connect, disconnect]
5856
);
@@ -119,7 +117,8 @@ export function HomeInner() {
119117
<Playground
120118
themeColors={themeColors}
121119
onConnect={(c) => {
122-
handleConnect(c);
120+
const m = process.env.NEXT_PUBLIC_LIVEKIT_URL ? "env" : mode;
121+
handleConnect(c, m);
123122
}}
124123
/>
125124
<RoomAudioRenderer />
@@ -128,8 +127,8 @@ export function HomeInner() {
128127
) : (
129128
<PlaygroundConnect
130129
accentColor={themeColors[0]}
131-
onConnectClicked={() => {
132-
handleConnect(true);
130+
onConnectClicked={(mode) => {
131+
handleConnect(true, mode);
133132
}}
134133
/>
135134
)}

0 commit comments

Comments
 (0)