Skip to content

Commit

Permalink
Add NoAgentNotification component to display a notification when no a…
Browse files Browse the repository at this point in the history
…gent connects to the room
  • Loading branch information
Ocupe committed Sep 16, 2024
1 parent bbe44c2 commit 32dd03d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
12 changes: 12 additions & 0 deletions frontend/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { useCallback, useState } from "react";
import { MediaDeviceFailure } from "livekit-client";
import type { ConnectionDetails } from "./api/connection-details/route";
import { NoAgentNotification } from "@/components/NoAgentNotification";

export default function Page() {
const [connectionDetails, updateConnectionDetails] = useState<
Expand Down Expand Up @@ -67,6 +68,17 @@ function SimpleVoiceAssistant() {
return (
<div className="h-80">
<BarVisualizer state={state} barCount={7} trackRef={audioTrack} />
<p className="font-mono text-center">{state}</p>
<NoAgentNotification state={state}>
<p className="font-bold mb-1">No agent</p>
<p>
No agent has joined the room yet. Please ensure you&#39;ve followed
the setup instructions and started your agent on your machine.{" "}
<a href="#" className="underline">
Learn more
</a>
</p>
</NoAgentNotification>
</div>
);
}
Expand Down
44 changes: 44 additions & 0 deletions frontend/components/NoAgentNotification.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { VoiceAssistantState } from "@livekit/components-react";
import React from "react";
interface NoAgentNotificationProps extends React.PropsWithChildren<object> {
state: VoiceAssistantState;
}

/**
* Renders some user info when no agent connects to the room after a certain time.
*/
export function NoAgentNotification(props: NoAgentNotificationProps) {
const timeToWaitMs = 6000;
const timeoutRef = React.useRef<number | null>(null);
const [showNotification, setShowNotification] = React.useState(false);

React.useEffect(() => {
if (props.state === "connecting") {
timeoutRef.current = window.setTimeout(() => {
if (props.state === "connecting") {
setShowNotification(true);
}
}, timeToWaitMs);
} else {
setShowNotification(false);
}

return () => {
if (timeoutRef.current) {
window.clearTimeout(timeoutRef.current);
}
};
}, [props.state]);

React.useEffect(() => {}, [props.state]);

return (
<>
{showNotification ? (
<div className="fixed text-sm bottom-[var(--lk-control-bar-height)] border border-cyan-500 font-mono m-4 w-96 max-w-[80vw] px-4 py-3 rounded left-0 bg-[var(--lk-bg)]">
{props.children}
</div>
) : null}
</>
);
}

0 comments on commit 32dd03d

Please sign in to comment.