From c345655c8a35b6cba5ec3d962072f918e39cc8a2 Mon Sep 17 00:00:00 2001 From: William Muli Date: Sun, 24 Sep 2023 13:47:33 +0300 Subject: [PATCH] FCP optimization in chat --- src/components/livekit-room.tsx | 68 +++++++++++++++++++++++++++++ src/components/new-chat-dialog.tsx | 2 +- src/pages/chats/[roomName].tsx | 69 ++++++------------------------ 3 files changed, 82 insertions(+), 57 deletions(-) create mode 100644 src/components/livekit-room.tsx diff --git a/src/components/livekit-room.tsx b/src/components/livekit-room.tsx new file mode 100644 index 0000000..980160b --- /dev/null +++ b/src/components/livekit-room.tsx @@ -0,0 +1,68 @@ +import React from 'react' +import { useRouter } from 'next/router' +import { ExternalE2EEKeyProvider, Room, RoomOptions } from 'livekit-client' +import { LiveKitRoom, useToken } from '@livekit/components-react' + +import { LiveKitChat } from '../components/livekit-chat' + +export default function LivekitRoom() { + const [isClient, setIsClient] = React.useState(false) + const router = useRouter() + const encryptedKey = router.asPath.split('#')[1] + const { roomName, username } = router.query as { + roomName: string + username: string + } + + const token = useToken('/api/chat/livekit_token', roomName, { + userInfo: { + identity: username, + name: username + } + }) + + const roomOptions = React.useMemo((): RoomOptions => { + if (!encryptedKey) return {} + const worker = isClient + ? new Worker(new URL('livekit-client/e2ee-worker', import.meta.url)) + : undefined + const keyProvider = new ExternalE2EEKeyProvider() + keyProvider.setKey(encryptedKey) + + if (!worker) return {} + return { + e2ee: { + keyProvider, + worker + } + } + }, [encryptedKey, isClient]) + + const room = React.useMemo(() => new Room(roomOptions), [roomOptions]) + + React.useEffect(() => { + setIsClient(true) + }, []) + + if (token === '') { + return
Getting token...
+ } + + return ( + <> + {token && ( + + + + )} + + ) +} diff --git a/src/components/new-chat-dialog.tsx b/src/components/new-chat-dialog.tsx index 10ed2cd..d3b6f76 100644 --- a/src/components/new-chat-dialog.tsx +++ b/src/components/new-chat-dialog.tsx @@ -17,7 +17,7 @@ import { generateRoomId, uint8ArrayToBase64UrlSafe } from '../shared/utils' import RandomGenerator from '../shared/random-generator' import Keychain from '../shared/keychain' -export const NewChatDialog = ({ open, onClose }: { open: boolean, onClose: () => void }) => { +export default function NewChatDialog ({ open, onClose }: { open: boolean, onClose: () => void }) { const router = useRouter() const { roomName } = router.query as { roomName: string | undefined } const form = useForm<{ diff --git a/src/pages/chats/[roomName].tsx b/src/pages/chats/[roomName].tsx index 90235b4..11d0c3f 100644 --- a/src/pages/chats/[roomName].tsx +++ b/src/pages/chats/[roomName].tsx @@ -1,82 +1,39 @@ import React from 'react' import dynamic from 'next/dynamic' -import { ExternalE2EEKeyProvider, Room, RoomOptions } from 'livekit-client' -import { LiveKitRoom, useToken } from '@livekit/components-react' import MainLayout from '../../layouts/main' - +import LivekitRoom from '../../components/livekit-room' import { useRouter } from 'next/router' -import { LiveKitChat } from '../../components/livekit-chat' -import { NewChatDialog } from '../../components/new-chat-dialog' -const HomeContent = dynamic(() => import('../../components/home-content'), { ssr: true }) - +const HomeContent = dynamic(() => import('../../components/home-content'), { + ssr: true +}) +const NewChatDialog = dynamic( + () => import('../../components/new-chat-dialog'), + { ssr: true } +) export default function ChatPage() { - const [isClient, setIsClient] = React.useState(false) const router = useRouter() - const encryptedKey = router.asPath.split('#')[1] - const { roomName, username } = router.query as { - roomName: string + const { username } = router.query as { username: string } - - const token = useToken('/api/chat/livekit_token', roomName, { - userInfo: { - identity: username, - name: username - } - }) - - const roomOptions = React.useMemo((): RoomOptions => { - if (!encryptedKey) return {} - const worker = isClient - ? new Worker(new URL('livekit-client/e2ee-worker', import.meta.url)) - : undefined - const keyProvider = new ExternalE2EEKeyProvider() - keyProvider.setKey(encryptedKey) - - if (!worker) return {} - return { - e2ee: { - keyProvider, - worker - } - } - }, [encryptedKey, isClient]) - - const room = React.useMemo(() => new Room(roomOptions), [roomOptions]) + const [isClient, setIsClient] = React.useState(false) React.useEffect(() => { setIsClient(true) }, []) - if (token === '') { - return
Getting token...
- } - return ( <> - {token && ( - - - - )} + - {isClient && ( + {isClient ? ( router.push('/', undefined, { shallow: true })} /> - )} + ) : null} )