feat: Add component strucutre for calls and super basic livekit implementation
This commit is contained in:
parent
cdb0fdbf76
commit
b7266da61f
20 changed files with 284 additions and 45 deletions
|
|
@ -6,7 +6,8 @@
|
|||
"exports": {
|
||||
"./context": "./src/context.tsx",
|
||||
"./screen": "./src/screen.tsx",
|
||||
"./utils": "./src/utils.ts"
|
||||
"./utils": "./src/utils.ts",
|
||||
"./sidebarBox": "./src/components/sidebarBox.tsx"
|
||||
},
|
||||
"scripts": {
|
||||
"format": "bunx prettier --write .",
|
||||
|
|
@ -14,16 +15,18 @@
|
|||
"build": "tsc -p tsconfig.json --noEmit"
|
||||
},
|
||||
"dependencies": {
|
||||
"@livekit/components-react": "^2.9.20",
|
||||
"@tanstack/react-query": "^5.0.0",
|
||||
"@tanstack/react-router": "^1.0.0",
|
||||
"@tanstack/react-virtual": "^3.0.0",
|
||||
"@tensamin/crypto": "workspace:*",
|
||||
"@tensamin/ttp": "workspace:*",
|
||||
"@tensamin/storage": "workspace:*",
|
||||
"@tensamin/user": "workspace:*",
|
||||
"@tensamin/markdown": "workspace:*",
|
||||
"@tensamin/shared": "workspace:*",
|
||||
"@tensamin/storage": "workspace:*",
|
||||
"@tensamin/ttp": "workspace:*",
|
||||
"@tensamin/ui": "workspace:*",
|
||||
"@tensamin/user": "workspace:*",
|
||||
"livekit-client": "^2.18.1",
|
||||
"lucide-react": "^0.564.0",
|
||||
"react": "^19.2.0",
|
||||
"react-dom": "^19.2.0",
|
||||
|
|
|
|||
3
packages/call/src/components/actions.tsx
Normal file
3
packages/call/src/components/actions.tsx
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
export default function Actions() {
|
||||
return <div>Actions</div>;
|
||||
}
|
||||
3
packages/call/src/components/modals/big/user.tsx
Normal file
3
packages/call/src/components/modals/big/user.tsx
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
export default function BigUser() {
|
||||
return <div>Big User</div>;
|
||||
}
|
||||
3
packages/call/src/components/modals/big/video.tsx
Normal file
3
packages/call/src/components/modals/big/video.tsx
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
export default function BigVideo() {
|
||||
return <div>Big Video</div>;
|
||||
}
|
||||
3
packages/call/src/components/modals/small/user.tsx
Normal file
3
packages/call/src/components/modals/small/user.tsx
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
export default function SmallUser() {
|
||||
return <div>Small User</div>;
|
||||
}
|
||||
3
packages/call/src/components/modals/small/video.tsx
Normal file
3
packages/call/src/components/modals/small/video.tsx
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
export default function SmallVideo() {
|
||||
return <div>Small Video</div>;
|
||||
}
|
||||
7
packages/call/src/components/sidebarBox.tsx
Normal file
7
packages/call/src/components/sidebarBox.tsx
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
import { useCall } from "../context";
|
||||
|
||||
export default function SidebarBox() {
|
||||
const { state } = useCall();
|
||||
|
||||
return state === "closed" ? null : <div>Sidebar</div>;
|
||||
}
|
||||
|
|
@ -1,20 +1,25 @@
|
|||
import { createContext, useContext, useEffect, useState } from "react";
|
||||
import { log } from "@tensamin/shared/log";
|
||||
import { log, toast } from "@tensamin/shared/log";
|
||||
import { useNavigate } from "@tanstack/react-router";
|
||||
import { useTTP } from "@tensamin/ttp/context";
|
||||
import z from "zod";
|
||||
import { ttp } from "@tensamin/shared/data";
|
||||
|
||||
import { LiveKitRoom } from "@livekit/components-react";
|
||||
|
||||
export const context = createContext<contextType | undefined>(undefined);
|
||||
|
||||
export default function Provider(props: { children: React.ReactNode }) {
|
||||
const navigate = useNavigate();
|
||||
const { send } = useTTP();
|
||||
|
||||
const [state, setState] = useState<"closed" | "connecting" | "open">(
|
||||
"closed",
|
||||
);
|
||||
const [state, setState] = useState<
|
||||
"closed" | "closing" | "connecting" | "open"
|
||||
>("closed");
|
||||
|
||||
const [callId, setCallId] = useState<string | null>(null);
|
||||
const [callSecret, setCallSecret] = useState<string | null>(null);
|
||||
|
||||
console.log(callId, callSecret);
|
||||
const [livekitToken, setLivekitToken] = useState<string | null>(null);
|
||||
|
||||
function connect(callId: string, callSecret: string) {
|
||||
setState("connecting");
|
||||
|
|
@ -25,21 +30,23 @@ export default function Provider(props: { children: React.ReactNode }) {
|
|||
|
||||
// Master reset function
|
||||
function disconnect() {
|
||||
setState("closed");
|
||||
setState("closing");
|
||||
setCallId(null);
|
||||
setCallSecret(null);
|
||||
setLivekitToken(null);
|
||||
}
|
||||
|
||||
// Utils
|
||||
function joinCall(userId: number, callId?: string) {
|
||||
// get/generate e2ee secret
|
||||
// go into convs and find call id
|
||||
// generate shared secret and decrypt enc call secret
|
||||
/// go into convs and find call id
|
||||
/// generate shared secret and decrypt enc call secret
|
||||
|
||||
// check if user is already in call
|
||||
|
||||
// connect to call
|
||||
connect("", "");
|
||||
setView("grid");
|
||||
|
||||
// navigate to call page
|
||||
navigate({ to: "/call", search: { userId: userId, callId: callId } });
|
||||
|
|
@ -48,25 +55,135 @@ export default function Provider(props: { children: React.ReactNode }) {
|
|||
// Event listener for incoming calls
|
||||
useEffect(() => {}, []);
|
||||
|
||||
/**
|
||||
* All of UI
|
||||
*/
|
||||
const [view, setView] = useState<"preview" | "focused" | "grid">("preview");
|
||||
const [currentCallData, setCurrentCallData] = useState<z.infer<
|
||||
typeof ttp.get_call_data.response
|
||||
> | null>(null);
|
||||
|
||||
// Get current call information for preview view
|
||||
useEffect(() => {
|
||||
if (view !== "preview" || !callId) return;
|
||||
|
||||
send("get_call_data", { call_id: callId })
|
||||
.then((data) => {
|
||||
setCurrentCallData(data.data);
|
||||
})
|
||||
.catch((err) => {
|
||||
log(1, "Call", "red", "Failed to get call data", {
|
||||
callId,
|
||||
error: err,
|
||||
});
|
||||
setCurrentCallData({
|
||||
someInfo: "Failed",
|
||||
});
|
||||
});
|
||||
}, [callId, send, view]);
|
||||
|
||||
return (
|
||||
<context.Provider
|
||||
value={{
|
||||
state,
|
||||
view,
|
||||
setView,
|
||||
|
||||
connect,
|
||||
disconnect,
|
||||
joinCall,
|
||||
currentCallData,
|
||||
}}
|
||||
>
|
||||
{props.children}
|
||||
<LiveKitRoom
|
||||
serverUrl="wss://call.tensamin.net"
|
||||
token={livekitToken || ""}
|
||||
connect={state !== "closed"}
|
||||
options={{
|
||||
encryption: {
|
||||
e2eeManager: {
|
||||
on(event, callback) {
|
||||
void event;
|
||||
void callback;
|
||||
return this;
|
||||
},
|
||||
isEnabled: callSecret !== null,
|
||||
isDataChannelEncryptionEnabled: true,
|
||||
setup: async (room) => {
|
||||
void room;
|
||||
},
|
||||
setupEngine: async (engine) => {
|
||||
void engine;
|
||||
},
|
||||
setParticipantCryptorEnabled: async (
|
||||
enabled: boolean,
|
||||
participantIdentity: string,
|
||||
) => {
|
||||
console.log(
|
||||
`Set participant ${participantIdentity} cryptor enabled: ${enabled}`,
|
||||
);
|
||||
},
|
||||
setSifTrailer: async (trailer: Uint8Array) => {
|
||||
void trailer;
|
||||
},
|
||||
|
||||
// Encryption
|
||||
encryptData: async (data: Uint8Array) => {
|
||||
console.log(callSecret);
|
||||
return {
|
||||
uuid: "",
|
||||
payload: data,
|
||||
iv: new Uint8Array(),
|
||||
keyIndex: 0,
|
||||
};
|
||||
},
|
||||
handleEncryptedData: async (
|
||||
payload: Uint8Array,
|
||||
iv: Uint8Array,
|
||||
participantIdentity: string,
|
||||
keyIndex: number,
|
||||
) => {
|
||||
void iv;
|
||||
void participantIdentity;
|
||||
void keyIndex;
|
||||
console.log(callSecret);
|
||||
return {
|
||||
uuid: crypto.randomUUID(),
|
||||
payload,
|
||||
};
|
||||
},
|
||||
},
|
||||
},
|
||||
}}
|
||||
onConnected={() => setState("open")}
|
||||
onDisconnected={() => setState("closed")}
|
||||
onError={(error) => log(1, "Call", "red", error.message)}
|
||||
onMediaDeviceFailure={(failure, kind) => {
|
||||
log(1, "call", "red", "Media device failure", { failure, kind });
|
||||
toast("error", "Media device failure. See console for details.");
|
||||
}}
|
||||
onEncryptionError={(error) => {
|
||||
log(1, "call", "red", "Encryption error", { error });
|
||||
toast(
|
||||
"error",
|
||||
"Error during call encryption. See console for details.",
|
||||
);
|
||||
}}
|
||||
>
|
||||
{props.children}
|
||||
</LiveKitRoom>
|
||||
</context.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
type contextType = {
|
||||
state: "closed" | "connecting" | "open";
|
||||
state: "closed" | "closing" | "connecting" | "open";
|
||||
view: "preview" | "focused" | "grid";
|
||||
setView: (view: "preview" | "focused" | "grid") => void;
|
||||
connect: (callId: string, callSecret: string) => void;
|
||||
disconnect: () => void;
|
||||
joinCall: (userId: number, callId?: string) => void;
|
||||
currentCallData: z.infer<typeof ttp.get_call_data.response> | null;
|
||||
};
|
||||
|
||||
export function useCall(): contextType {
|
||||
|
|
|
|||
|
|
@ -1,3 +1,16 @@
|
|||
import { useCall } from "./context";
|
||||
|
||||
import MainLayout from "./views/main/layout";
|
||||
import MainGrid from "./views/main/grid";
|
||||
import MainFocused from "./views/main/focused";
|
||||
import Preview from "./views/preview";
|
||||
|
||||
export default function Screen() {
|
||||
return <div></div>;
|
||||
const { view } = useCall();
|
||||
|
||||
return view === "preview" ? (
|
||||
<Preview />
|
||||
) : (
|
||||
<MainLayout>{view === "grid" ? <MainGrid /> : <MainFocused />}</MainLayout>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
3
packages/call/src/views/main/focused.tsx
Normal file
3
packages/call/src/views/main/focused.tsx
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
export default function View() {
|
||||
return <div>Focused</div>;
|
||||
}
|
||||
3
packages/call/src/views/main/grid.tsx
Normal file
3
packages/call/src/views/main/grid.tsx
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
export default function View() {
|
||||
return <div>Grid</div>;
|
||||
}
|
||||
8
packages/call/src/views/main/layout.tsx
Normal file
8
packages/call/src/views/main/layout.tsx
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
export default function Layout({ children }: { children: React.ReactNode }) {
|
||||
return (
|
||||
<div>
|
||||
Layout
|
||||
{children}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
7
packages/call/src/views/preview.tsx
Normal file
7
packages/call/src/views/preview.tsx
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
import { useCall } from "../context";
|
||||
|
||||
export default function Preview() {
|
||||
const { currentCallData } = useCall();
|
||||
|
||||
return <div>Preview View {currentCallData?.someInfo}</div>;
|
||||
}
|
||||
|
|
@ -153,6 +153,16 @@ export const ttp = {
|
|||
timestamp: z.number(),
|
||||
}),
|
||||
},
|
||||
|
||||
// Calls
|
||||
get_call_data: {
|
||||
request: z.object({
|
||||
call_id: z.string(),
|
||||
}),
|
||||
response: z.object({
|
||||
someInfo: z.string(),
|
||||
}),
|
||||
},
|
||||
} satisfies Record<string, { request: z.ZodType; response: z.ZodType }>;
|
||||
|
||||
export type TTP = typeof ttp;
|
||||
|
|
|
|||
Loading…
Reference in a new issue