Skip to content

Commit 56fa89d

Browse files
rename useAppSession to useConnection
1 parent 5020567 commit 56fa89d

File tree

7 files changed

+50
-50
lines changed

7 files changed

+50
-50
lines changed

app/ui/layout.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { headers } from 'next/headers';
2-
import { AppSessionProvider } from '@/hooks/useAppSession';
2+
import { ConnectionProvider } from '@/hooks/useConnection';
33
import { getAppConfig } from '@/lib/utils';
44

55
interface LayoutProps {
@@ -11,7 +11,7 @@ export default async function Layout({ children }: LayoutProps) {
1111
const appConfig = await getAppConfig(hdrs);
1212

1313
return (
14-
<AppSessionProvider appConfig={appConfig}>
14+
<ConnectionProvider appConfig={appConfig}>
1515
<div className="bg-muted/20 min-h-svh p-8">
1616
<div className="mx-auto max-w-3xl space-y-8">
1717
<header className="space-y-2">
@@ -40,6 +40,6 @@ export default async function Layout({ children }: LayoutProps) {
4040
<main className="space-y-20">{children}</main>
4141
</div>
4242
</div>
43-
</AppSessionProvider>
43+
</ConnectionProvider>
4444
);
4545
}

components/app/app.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ import { RoomAudioRenderer, StartAudio } from '@livekit/components-react';
44
import type { AppConfig } from '@/app-config';
55
import { ViewController } from '@/components/app/view-controller';
66
import { Toaster } from '@/components/livekit/toaster';
7-
import { AppSessionProvider } from '@/hooks/useAppSession';
7+
import { ConnectionProvider } from '@/hooks/useConnection';
88

99
interface AppProps {
1010
appConfig: AppConfig;
1111
}
1212

1313
export function App({ appConfig }: AppProps) {
1414
return (
15-
<AppSessionProvider appConfig={appConfig}>
15+
<ConnectionProvider appConfig={appConfig}>
1616
<main className="grid h-svh grid-cols-1 place-content-center">
1717
<ViewController appConfig={appConfig} />
1818
</main>
1919
<StartAudio label="Start Audio" />
2020
<RoomAudioRenderer />
2121
<Toaster />
22-
</AppSessionProvider>
22+
</ConnectionProvider>
2323
);
2424
}

components/app/session-view.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use client';
22

3-
import React, { useEffect, useRef, useState } from 'react';
3+
import React, { useCallback, useEffect, useRef, useState } from 'react';
44
import { motion } from 'motion/react';
5-
import { useSessionMessages } from '@livekit/components-react';
5+
import { useSessionContext, useSessionMessages } from '@livekit/components-react';
66
import type { AppConfig } from '@/app-config';
77
import { ChatTranscript } from '@/components/app/chat-transcript';
88
import { PreConnectMessage } from '@/components/app/preconnect-message';
@@ -11,7 +11,7 @@ import {
1111
AgentControlBar,
1212
type ControlBarControls,
1313
} from '@/components/livekit/agent-control-bar/agent-control-bar';
14-
import { useAppSession } from '@/hooks/useAppSession';
14+
import { useConnection } from '@/hooks/useConnection';
1515
import { cn } from '@/lib/utils';
1616
import { ScrollArea } from '../livekit/scroll-area/scroll-area';
1717

@@ -65,9 +65,10 @@ export const SessionView = ({
6565
appConfig,
6666
...props
6767
}: React.ComponentProps<'section'> & SessionViewProps) => {
68-
const { session, isSessionActive, endSession } = useAppSession();
68+
const session = useSessionContext();
6969
const { messages } = useSessionMessages(session);
7070
const [chatOpen, setChatOpen] = useState(false);
71+
const { isConnectionActive, disconnect } = useConnection();
7172
const scrollAreaRef = useRef<HTMLDivElement>(null);
7273

7374
const controls: ControlBarControls = {
@@ -87,10 +88,9 @@ export const SessionView = ({
8788
}
8889
}, [messages]);
8990

90-
const handleDisconnect = () => {
91-
// pass false so we can manually end the session when the exit animation completes
92-
endSession(false);
93-
};
91+
const handleDisconnect = useCallback(() => {
92+
disconnect(false);
93+
}, [disconnect]);
9494

9595
return (
9696
<section className="bg-background relative z-10 h-full w-full overflow-hidden" {...props}>
@@ -126,7 +126,7 @@ export const SessionView = ({
126126
<Fade bottom className="absolute inset-x-0 top-0 h-4 -translate-y-full" />
127127
<AgentControlBar
128128
controls={controls}
129-
isSessionActive={isSessionActive}
129+
isConnectionActive={isConnectionActive}
130130
onDisconnect={handleDisconnect}
131131
onChatOpenChange={setChatOpen}
132132
/>

components/app/view-controller.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { AppConfig } from '@/app-config';
77
import { SessionView } from '@/components/app/session-view';
88
import { WelcomeView } from '@/components/app/welcome-view';
99
import { useAgentErrors } from '@/hooks/useAgentErrors';
10-
import { useAppSession } from '@/hooks/useAppSession';
10+
import { useConnection } from '@/hooks/useConnection';
1111
import { useDebugMode } from '@/hooks/useDebug';
1212

1313
const IN_DEVELOPMENT = process.env.NODE_ENV !== 'production';
@@ -39,7 +39,7 @@ interface ViewControllerProps {
3939

4040
export function ViewController({ appConfig }: ViewControllerProps) {
4141
const session = useSessionContext();
42-
const { isSessionActive, startSession } = useAppSession();
42+
const { isConnectionActive, connect } = useConnection();
4343

4444
useDebugMode({ enabled: IN_DEVELOPMENT });
4545
useAgentErrors();
@@ -56,17 +56,17 @@ export function ViewController({ appConfig }: ViewControllerProps) {
5656

5757
return (
5858
<AnimatePresence mode="wait">
59-
{/* Welcome screen */}
60-
{!isSessionActive && (
59+
{/* Welcome view */}
60+
{!isConnectionActive && (
6161
<MotionWelcomeView
6262
key="welcome"
6363
{...VIEW_MOTION_PROPS}
6464
startButtonText={appConfig?.startButtonText ?? ''}
65-
onStartCall={startSession}
65+
onStartCall={connect}
6666
/>
6767
)}
6868
{/* Session view */}
69-
{isSessionActive && (
69+
{isConnectionActive && (
7070
<MotionSessionView
7171
key="session-view"
7272
{...VIEW_MOTION_PROPS}

components/livekit/agent-control-bar/agent-control-bar.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export interface ControlBarControls {
2222
}
2323

2424
export interface AgentControlBarProps extends UseInputControlsProps {
25-
isSessionActive?: boolean;
25+
isConnectionActive?: boolean;
2626
controls?: ControlBarControls;
2727
onChatOpenChange?: (open: boolean) => void;
2828
onDeviceError?: (error: { source: Track.Source; error: Error }) => void;
@@ -35,7 +35,7 @@ export function AgentControlBar({
3535
controls,
3636
saveUserChoices = true,
3737
className,
38-
isSessionActive = false,
38+
isConnectionActive = false,
3939
onDisconnect,
4040
onDeviceError,
4141
onChatOpenChange,
@@ -158,7 +158,7 @@ export function AgentControlBar({
158158
<Button
159159
variant="destructive"
160160
onClick={onDisconnect}
161-
disabled={!isSessionActive}
161+
disabled={!isConnectionActive}
162162
className="font-mono"
163163
>
164164
<PhoneDisconnectIcon weight="bold" />

hooks/useAgentErrors.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { useEffect } from 'react';
22
import { useAgent } from '@livekit/components-react';
33
import { toastAlert } from '@/components/livekit/alert-toast';
4-
import { useAppSession } from './useAppSession';
4+
import { useConnection } from './useConnection';
55

66
export function useAgentErrors() {
77
const agent = useAgent();
8-
const { isSessionActive, endSession } = useAppSession();
8+
const { isConnectionActive, disconnect } = useConnection();
99

1010
useEffect(() => {
11-
if (isSessionActive && agent.state === 'failed') {
11+
if (isConnectionActive && agent.state === 'failed') {
1212
const reasons = agent.failureReasons;
1313

1414
toastAlert({
@@ -38,7 +38,7 @@ export function useAgentErrors() {
3838
),
3939
});
4040

41-
endSession();
41+
disconnect();
4242
}
43-
}, [agent, isSessionActive, endSession]);
43+
}, [agent, isConnectionActive, disconnect]);
4444
}
Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,35 @@ import { TokenSource } from 'livekit-client';
55
import { SessionProvider, type UseSessionReturn, useSession } from '@livekit/components-react';
66
import type { AppConfig } from '@/app-config';
77

8-
interface AppSessionContextType {
8+
interface ConnectionContextType {
99
session?: UseSessionReturn;
10-
isSessionActive: boolean;
11-
startSession: (startSession?: boolean) => void;
12-
endSession: (endSession?: boolean) => void;
10+
isConnectionActive: boolean;
11+
connect: (startSession?: boolean) => void;
12+
disconnect: (endSession?: boolean) => void;
1313
}
1414

15-
const AppSessionContext = createContext<AppSessionContextType>({
15+
const ConnectionContext = createContext<ConnectionContextType>({
1616
session: undefined,
17-
isSessionActive: false,
18-
startSession: () => {},
19-
endSession: () => {},
17+
isConnectionActive: false,
18+
connect: () => {},
19+
disconnect: () => {},
2020
});
2121

22-
export function useAppSession() {
23-
const ctx = useContext(AppSessionContext);
22+
export function useConnection() {
23+
const ctx = useContext(ConnectionContext);
2424
if (!ctx) {
25-
throw new Error('useAppSession must be used within a AppSessionProvider');
25+
throw new Error('useConnection must be used within a ConnectionProvider');
2626
}
2727
return ctx;
2828
}
2929

30-
interface AppSessionProviderProps {
30+
interface ConnectionProviderProps {
3131
appConfig: AppConfig;
3232
children: React.ReactNode;
3333
}
3434

35-
export function AppSessionProvider({ appConfig, children }: AppSessionProviderProps) {
36-
const [isSessionActive, setIsSessionActive] = useState(false);
35+
export function ConnectionProvider({ appConfig, children }: ConnectionProviderProps) {
36+
const [isConnectionActive, setIsConnectionActive] = useState(false);
3737

3838
const tokenSource = useMemo(() => {
3939
if (typeof process.env.NEXT_PUBLIC_CONN_DETAILS_ENDPOINT === 'string') {
@@ -75,15 +75,15 @@ export function AppSessionProvider({ appConfig, children }: AppSessionProviderPr
7575

7676
const value = useMemo(
7777
() => ({
78-
isSessionActive,
78+
isConnectionActive,
7979
/**
8080
* Start the application session and optionally start the agent session.
8181
*
8282
* @param startSession - Whether to start the agent session automatically. Default is true.
8383
* If startSession is passed in asfalse, you are opting to manually start the session.
8484
*/
85-
startSession: (startSession = true) => {
86-
setIsSessionActive(true);
85+
connect: (startSession = true) => {
86+
setIsConnectionActive(true);
8787
if (startSession) {
8888
session.start();
8989
}
@@ -94,8 +94,8 @@ export function AppSessionProvider({ appConfig, children }: AppSessionProviderPr
9494
* @param endSession - Whether to end the agent session automatically. Default is true.
9595
* If endSession is passed in as false, you are opting to manually end the session.
9696
*/
97-
endSession: (endSession = true) => {
98-
setIsSessionActive(false);
97+
disconnect: (endSession = true) => {
98+
setIsConnectionActive(false);
9999
if (endSession) {
100100
session.end();
101101
}
@@ -104,12 +104,12 @@ export function AppSessionProvider({ appConfig, children }: AppSessionProviderPr
104104
// session object is not a stable reference
105105
// TODO: add session object to dependencies
106106
/* eslint-disable-next-line react-hooks/exhaustive-deps */
107-
[isSessionActive]
107+
[isConnectionActive]
108108
);
109109

110110
return (
111111
<SessionProvider session={session}>
112-
<AppSessionContext.Provider value={value}>{children}</AppSessionContext.Provider>
112+
<ConnectionContext.Provider value={value}>{children}</ConnectionContext.Provider>
113113
</SessionProvider>
114114
);
115115
}

0 commit comments

Comments
 (0)