Re-introduce connection mode
This commit is contained in:
parent
c039254501
commit
70f69a3cff
@ -2,10 +2,11 @@ import { useConfig } from "@/hooks/useConfig";
|
|||||||
import { CLOUD_ENABLED, CloudConnect } from "../cloud/CloudConnect";
|
import { CLOUD_ENABLED, CloudConnect } from "../cloud/CloudConnect";
|
||||||
import { Button } from "./button/Button";
|
import { Button } from "./button/Button";
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
|
import { ConnectionMode } from "@/hooks/useConnection";
|
||||||
|
|
||||||
type PlaygroundConnectProps = {
|
type PlaygroundConnectProps = {
|
||||||
accentColor: string;
|
accentColor: string;
|
||||||
onConnectClicked: () => void;
|
onConnectClicked: (mode: ConnectionMode) => void;
|
||||||
};
|
};
|
||||||
|
|
||||||
const ConnectTab = ({ active, onClick, children }: any) => {
|
const ConnectTab = ({ active, onClick, children }: any) => {
|
||||||
@ -57,7 +58,7 @@ const TokenConnect = ({
|
|||||||
newSettings.ws_url = url;
|
newSettings.ws_url = url;
|
||||||
newSettings.token = token;
|
newSettings.token = token;
|
||||||
setUserSettings(newSettings);
|
setUserSettings(newSettings);
|
||||||
onConnectClicked();
|
onConnectClicked("manual");
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
Connect
|
Connect
|
||||||
|
|||||||
@ -1,17 +1,19 @@
|
|||||||
"use client"
|
"use client"
|
||||||
|
|
||||||
import { CLOUD_ENABLED } from "@/cloud/CloudConnect";
|
|
||||||
import { useCloud } from "@/cloud/useCloud";
|
import { useCloud } from "@/cloud/useCloud";
|
||||||
import React, { createContext, useState } from "react";
|
import React, { createContext, useState } from "react";
|
||||||
import { useCallback } from "react";
|
import { useCallback } from "react";
|
||||||
import { useConfig } from "./useConfig";
|
import { useConfig } from "./useConfig";
|
||||||
|
|
||||||
|
export type ConnectionMode = "cloud" | "manual" | "env"
|
||||||
|
|
||||||
type TokenGeneratorData = {
|
type TokenGeneratorData = {
|
||||||
shouldConnect: boolean;
|
shouldConnect: boolean;
|
||||||
wsUrl: string;
|
wsUrl: string;
|
||||||
token: string;
|
token: string;
|
||||||
|
mode: ConnectionMode;
|
||||||
disconnect: () => Promise<void>;
|
disconnect: () => Promise<void>;
|
||||||
connect: () => Promise<void>;
|
connect: (mode: ConnectionMode) => Promise<void>;
|
||||||
};
|
};
|
||||||
|
|
||||||
const ConnectionContext = createContext<TokenGeneratorData | undefined>(undefined);
|
const ConnectionContext = createContext<TokenGeneratorData | undefined>(undefined);
|
||||||
@ -26,16 +28,20 @@ export const ConnectionProvider = ({
|
|||||||
const [connectionDetails, setConnectionDetails] = useState<{
|
const [connectionDetails, setConnectionDetails] = useState<{
|
||||||
wsUrl: string;
|
wsUrl: string;
|
||||||
token: string;
|
token: string;
|
||||||
|
mode: ConnectionMode;
|
||||||
shouldConnect: boolean;
|
shouldConnect: boolean;
|
||||||
}>({ wsUrl: "", token: "", shouldConnect: false });
|
}>({ wsUrl: "", token: "", shouldConnect: false, mode: "manual" });
|
||||||
|
|
||||||
const connect = useCallback(async () => {
|
const connect = useCallback(async (mode: ConnectionMode) => {
|
||||||
let token = "";
|
let token = "";
|
||||||
let url = "";
|
let url = "";
|
||||||
if (CLOUD_ENABLED) {
|
if (mode === "cloud") {
|
||||||
token = await generateToken();
|
token = await generateToken();
|
||||||
url = cloudWSUrl;
|
url = cloudWSUrl;
|
||||||
} else if (process.env.NEXT_PUBLIC_LIVEKIT_URL) {
|
} else if (mode === "env") {
|
||||||
|
if(!process.env.NEXT_PUBLIC_LIVEKIT_URL) {
|
||||||
|
throw new Error("NEXT_PUBLIC_LIVEKIT_URL is not set");
|
||||||
|
}
|
||||||
url = process.env.NEXT_PUBLIC_LIVEKIT_URL;
|
url = process.env.NEXT_PUBLIC_LIVEKIT_URL;
|
||||||
const {accessToken} = await fetch("/api/token").then((res) => res.json());
|
const {accessToken} = await fetch("/api/token").then((res) => res.json());
|
||||||
token = accessToken;
|
token = accessToken;
|
||||||
@ -43,7 +49,7 @@ export const ConnectionProvider = ({
|
|||||||
token = config.settings.token;
|
token = config.settings.token;
|
||||||
url = config.settings.ws_url;
|
url = config.settings.ws_url;
|
||||||
}
|
}
|
||||||
setConnectionDetails({ wsUrl: url, token, shouldConnect: true });
|
setConnectionDetails({ wsUrl: url, token, shouldConnect: true, mode });
|
||||||
}, [
|
}, [
|
||||||
cloudWSUrl,
|
cloudWSUrl,
|
||||||
config.settings.token,
|
config.settings.token,
|
||||||
@ -61,6 +67,7 @@ export const ConnectionProvider = ({
|
|||||||
wsUrl: connectionDetails.wsUrl,
|
wsUrl: connectionDetails.wsUrl,
|
||||||
token: connectionDetails.token,
|
token: connectionDetails.token,
|
||||||
shouldConnect: connectionDetails.shouldConnect,
|
shouldConnect: connectionDetails.shouldConnect,
|
||||||
|
mode: connectionDetails.mode,
|
||||||
connect,
|
connect,
|
||||||
disconnect,
|
disconnect,
|
||||||
}}
|
}}
|
||||||
|
|||||||
@ -12,10 +12,8 @@ import { PlaygroundConnect } from "@/components/PlaygroundConnect";
|
|||||||
import Playground from "@/components/playground/Playground";
|
import Playground from "@/components/playground/Playground";
|
||||||
import { PlaygroundToast, ToastType } from "@/components/toast/PlaygroundToast";
|
import { PlaygroundToast, ToastType } from "@/components/toast/PlaygroundToast";
|
||||||
import { ConfigProvider, useConfig } from "@/hooks/useConfig";
|
import { ConfigProvider, useConfig } from "@/hooks/useConfig";
|
||||||
import { ConnectionProvider, useConnection } from "@/hooks/useConnection";
|
import { ConnectionMode, ConnectionProvider, useConnection } from "@/hooks/useConnection";
|
||||||
import { useMemo } from "react";
|
import { useMemo } from "react";
|
||||||
import { CLOUD_ENABLED } from "@/cloud/CloudConnect";
|
|
||||||
import { useCloud } from "@/cloud/useCloud";
|
|
||||||
|
|
||||||
const themeColors = [
|
const themeColors = [
|
||||||
"cyan",
|
"cyan",
|
||||||
@ -45,14 +43,14 @@ export function HomeInner() {
|
|||||||
message: string;
|
message: string;
|
||||||
type: ToastType;
|
type: ToastType;
|
||||||
} | null>(null);
|
} | null>(null);
|
||||||
const { shouldConnect, wsUrl, token, connect, disconnect } =
|
const { shouldConnect, wsUrl, token, mode, connect, disconnect } =
|
||||||
useConnection();
|
useConnection();
|
||||||
|
|
||||||
const {config} = useConfig();
|
const {config} = useConfig();
|
||||||
|
|
||||||
const handleConnect = useCallback(
|
const handleConnect = useCallback(
|
||||||
async (c: boolean) => {
|
async (c: boolean, mode: ConnectionMode) => {
|
||||||
c ? connect() : disconnect();
|
c ? connect(mode) : disconnect();
|
||||||
},
|
},
|
||||||
[connect, disconnect]
|
[connect, disconnect]
|
||||||
);
|
);
|
||||||
@ -119,7 +117,8 @@ export function HomeInner() {
|
|||||||
<Playground
|
<Playground
|
||||||
themeColors={themeColors}
|
themeColors={themeColors}
|
||||||
onConnect={(c) => {
|
onConnect={(c) => {
|
||||||
handleConnect(c);
|
const m = process.env.NEXT_PUBLIC_LIVEKIT_URL ? "env" : mode;
|
||||||
|
handleConnect(c, m);
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
<RoomAudioRenderer />
|
<RoomAudioRenderer />
|
||||||
@ -128,8 +127,8 @@ export function HomeInner() {
|
|||||||
) : (
|
) : (
|
||||||
<PlaygroundConnect
|
<PlaygroundConnect
|
||||||
accentColor={themeColors[0]}
|
accentColor={themeColors[0]}
|
||||||
onConnectClicked={() => {
|
onConnectClicked={(mode) => {
|
||||||
handleConnect(true);
|
handleConnect(true, mode);
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user