worried-petabyte-38885
03/02/2025, 5:38 AMgray-monkey-83525
03/02/2025, 5:38 AMvoice-assistant-frontend
project by modifying the UI components while retaining the existing functionality. To achieve this, you can start by adjusting the layout in the page.tsx
file, which is part of the Next.js app structure. You can use CSS to set the top 80% of the screen for your custom view and the bottom 20% for the control bar.
Here's a basic example of how you might structure your page.tsx
file:
'use client';
import { LiveKitRoom, RoomAudioRenderer, ControlBar } from '@livekit/components-react';
import '@livekit/components-styles';
import { useEffect, useState } from 'react';
export default function Page() {
const room = 'quickstart-room';
const name = 'quickstart-user';
const [token, setToken] = useState('');
useEffect(() => {
(async () => {
try {
const resp = await fetch(`/api/token?room=${room}&username=${name}`);
const data = await resp.json();
setToken(data.token);
} catch (e) {
console.error(e);
}
})();
}, []);
if (token === '') {
return <div>Getting token...</div>;
}
return (
<LiveKitRoom
video={true}
audio={true}
token={token}
serverUrl={process.env.LIVEKIT_URL}
data-lk-theme="default"
style={{ height: '100dvh' }}
>
<div style={{ height: '80%', backgroundColor: '#f0f0f0' }}>
{/* Your custom view goes here */}
</div>
<div style={{ height: '20%', backgroundColor: '#ccc' }}>
<ControlBar />
</div>
<RoomAudioRenderer />
</LiveKitRoom>
);
}
For the directory structure, you can follow the standard Next.js structure, with your components and styles organized in a way that suits your project. You might have a components
directory for reusable components and a styles
directory for CSS files.
Would you like more detailed guidance on any specific part of this process?
Sources:
- https://docs.livekit.io/home/egress/custom-template/ 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.worried-petabyte-38885
03/02/2025, 6:49 AMgray-monkey-83525
03/02/2025, 6:50 AMDisconnectButton
with your own custom button while maintaining the existing functionality, you can create a custom button component that calls the disconnect
method on the room context. Here's how you can do it:
1. Create a Custom Disconnect Button: Define a new component that uses the useRoomContext
hook to access the room instance and call the disconnect
method.
2. Replace the Existing Disconnect Button: Use your custom button in place of the existing DisconnectButton
in the ControlBar
component.
Here's an example of how you can implement this:
import { useRoomContext } from '@livekit/components-react';
import { CloseIcon } from '@/components/CloseIcon';
function CustomDisconnectButton() {
const room = useRoomContext();
const handleDisconnect = () => {
room.disconnect();
};
return (
<button onClick={handleDisconnect} className="custom-disconnect-button">
<CloseIcon />
Disconnect
</button>
);
}
function ControlBar(props: {
onConnectButtonClicked: () => void;
agentState: AgentState;
}) {
const krisp = useKrispNoiseFilter();
useEffect(() => {
krisp.setNoiseFilterEnabled(true);
}, []);
return (
<div className="relative h-[100px]">
<AnimatePresence>
{props.agentState === 'disconnected' && (
<button onClick={() => props.onConnectButtonClicked()}>
Start a conversation
</button>
)}
</AnimatePresence>
<AnimatePresence>
{props.agentState !== 'disconnected' && props.agentState !== 'connecting' && (
<motion.div
initial={{ opacity: 0, top: '10px' }}
animate={{ opacity: 1, top: 0 }}
exit={{ opacity: 0, top: '-10px' }}
transition={{ duration: 0.4, ease: [0.09, 1.04, 0.245, 1.055] }}
className="flex h-8 absolute left-1/2 -translate-x-1/2 justify-center"
>
<VoiceAssistantControlBar controls={{ leave: false }} />
<CustomDisconnectButton />
</motion.div>
)}
</AnimatePresence>
</div>
);
}
In this example, CustomDisconnectButton
uses the useRoomContext
hook to access the room instance and calls room.disconnect()
when clicked. This replaces the existing DisconnectButton
while maintaining the same functionality.
Would you like more detailed guidance on any specific part of this process?
Sources:
- useDisconnectButton 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.worried-petabyte-38885
03/02/2025, 6:58 AMgray-monkey-83525
03/02/2025, 6:58 AMuseRoomContext
hook, you need to ensure that your component is nested within a LiveKitRoom
component, as this component provides the necessary context. The useRoomContext
hook is part of the @livekit/components-react
package and allows you to access the Room
instance within your React components.
Here's how you can import and use the useRoomContext
hook:
1. Import the Hook: First, import the useRoomContext
from the @livekit/components-react
package.
2. Use the Hook in Your Component: Call useRoomContext
within your component to access the Room
instance.
Here's an example:
import { useRoomContext } from '@livekit/components-react';
function CustomComponent() {
const room = useRoomContext();
const handleDisconnect = () => {
room.disconnect();
};
return (
<button onClick={handleDisconnect}>
Disconnect
</button>
);
}
Make sure that CustomComponent
is used within a LiveKitRoom
component in your component tree, as shown below:
<LiveKitRoom
token={yourToken}
serverUrl={yourServerUrl}
connect={true}
>
<CustomComponent />
</LiveKitRoom>
This setup ensures that useRoomContext
can access the Room
instance provided by LiveKitRoom
.
Would you like more detailed guidance on any specific part of this process?
Sources:
- https://livekit-users.slack.com/archives/C07FY8WHGPM/p1731444545.555719
- https://docs.livekit.io/reference/components/react/concepts/contexts/ 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.worried-petabyte-38885
03/02/2025, 7:01 AMuseDisconnectButton
hook is used to implement the DisconnectButton
or your
* custom implementation of it. It adds onClick handler to the button to disconnect
* from the room.
*
* @example
* tsx
* const { buttonProps } = useDisconnectButton(buttonProps);
* return <button {...buttonProps}>Disconnect</button>;
*
* @public
*/
export function useDisconnectButton(props: DisconnectButtonProps) {
const room = useRoomContext();
const connectionState = useConnectionState(room);
const buttonProps = React.useMemo(() => {
const { className, disconnect } = setupDisconnectButton(room);
const mergedProps = mergeProps(props, {
className,
onClick: () => disconnect(props.stopTracks ?? true),
disabled: connectionState === ConnectionState.Disconnected,
});
return mergedProps;
}, [room, props, connectionState]);
return { buttonProps };
}. tell me how can use this in function CustomDisconnectButton() {
const room = useDisconnectButton()
const handleDisconnect = () => {
room.disconnect()
}
return (
<button onClick={handleDisconnect} className="custom-disconnect-button">
<CloseIcon />
Disconnect
</button>
)
} this code some where correctly @millions-hair-24956gray-monkey-83525
03/02/2025, 7:01 AM