Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace reconnect button with close if reconnect fails #26

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions src/views/ErrorScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ type ErrorScreenProps = {
errorType: 'socket_error' | 'audio_error' | 'mic_error' | 'unknown';
errorReason: string;
onConnect: () => void;
onClose: () => void;
ableToReconnect: boolean;
isConnecting: boolean;
};

export const ErrorScreen: FC<ErrorScreenProps> = ({
errorType,
errorReason,
onConnect,
onClose,
isConnecting,
ableToReconnect,
}) => {
return (
<div className="mt-12 flex w-full flex-col items-center justify-center gap-2">
Expand Down Expand Up @@ -71,13 +75,17 @@ export const ErrorScreen: FC<ErrorScreenProps> = ({
})}

<div className="pt-2">
<Button
onClick={onConnect}
isLoading={isConnecting}
loadingText={'Connecting...'}
>
Reconnect
</Button>
{ableToReconnect ? (
<Button
onClick={onConnect}
isLoading={isConnecting}
loadingText={'Connecting...'}
>
Reconnect
</Button>
) : (
<Button onClick={onClose}>Close</Button>
)}
</div>
</div>
);
Expand Down
25 changes: 21 additions & 4 deletions src/views/Views.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ConversationScreen } from '@/views/ConversationScreen';
import { ErrorScreen } from '@/views/ErrorScreen';
import { IntroScreen } from '@/views/IntroScreen';
import { useVoice } from '@humeai/voice-react';
import { FC } from 'react';
import { FC, useState } from 'react';
import { match } from 'ts-pattern';

export type ViewsProps = Record<never, never>;
Expand All @@ -17,6 +17,8 @@ export const Views: FC<ViewsProps> = () => {

const { connect, disconnect, status, error } = useVoice();

const [reconnectError, setReconnectError] = useState<string | null>(null);

if (layoutState === LayoutState.CLOSED) {
return (
<>
Expand All @@ -31,10 +33,14 @@ export const Views: FC<ViewsProps> = () => {
}

const onConnect = () => {
void connect()
.then(() => {})
setReconnectError(null);
return connect()
.then(() => {
return { success: true } as const;
})
.catch((e) => {
console.error(e);
return {success: false} as const;
});
};

Expand All @@ -51,8 +57,19 @@ export const Views: FC<ViewsProps> = () => {
<ErrorScreen
errorType={error?.type ?? ('unknown' as const)}
errorReason={error?.message ?? 'Unknown'}
onConnect={onConnect}
onConnect={() => {
onConnect()
.then(res => {
if(res.success === false){
setReconnectError('Failed to reconnect')
}
})
}}
onClose={() => {
close();
}}
isConnecting={status.value === 'connecting'}
ableToReconnect={reconnectError !== null}
/>
);
})
Expand Down