Skip to content

Commit

Permalink
update spinner and add basic error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
breeg554 committed Oct 4, 2024
1 parent 0572494 commit 556cc23
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
10 changes: 7 additions & 3 deletions apps/web-remix/app/components/chat/Webchat.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { Trash, Upload } from 'lucide-react';
import { LoaderCircle, Trash, Upload } from 'lucide-react';

import {
ChatGeneratingAnimation,
Expand Down Expand Up @@ -85,6 +85,7 @@ export const Webchat = ({
latestAiMessage,
isLoading,
isError,
error: chatError,
pipeline,
} = useChat({
organizationId: organizationId as unknown as number,
Expand Down Expand Up @@ -120,8 +121,11 @@ export const Webchat = ({
};
}, []);

if (isLoading) return <p>LOADING...</p>;
if (isError || !pipeline) return <p>ERROR</p>;
if (isLoading)
return <LoaderCircle className="animate-spin ease-in-out duration-300" />;
if (chatError) return <p className="text-foreground text-sm">{chatError}</p>;
if (isError || !pipeline)
return <p className="text-foreground text-sm">Something went wrong</p>;

return (
<ChatWrapper className={cn('h-full py-3 lg:py-4 relative', rest.className)}>
Expand Down
12 changes: 11 additions & 1 deletion apps/web-remix/app/components/chat/chat.reducer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import cloneDeep from 'lodash.clonedeep';
import startCase from 'lodash.startcase';
import { v4 as uuidv4 } from 'uuid';

import type {
Expand All @@ -17,6 +18,9 @@ type Action =
}
| {
type: 'ERROR';
payload: {
error: string | null;
};
}
| {
type: 'SEND_MESSAGE';
Expand Down Expand Up @@ -47,6 +51,7 @@ export type ChatStatus =
export type ChatOutputStatus = 'idle' | 'generating';

export type ChatReducerState = {
error: null | string;
status: ChatStatus;
pipelineConfig: WebchatPipelineConfig | null;
outputsStatus: Record<string, ChatOutputStatus>;
Expand All @@ -61,6 +66,7 @@ export const chatReducer = (
case 'CONNECT':
return {
...state,
error: null,
pipelineConfig: action.payload.data,
status: 'connected',
outputsStatus: toOutputStatus(
Expand All @@ -70,6 +76,7 @@ export const chatReducer = (
case 'ERROR':
return {
...state,
error: action.payload.error,
status: 'errored',
outputsStatus: setEveryOutputStatus(state.outputsStatus, 'idle'),
};
Expand Down Expand Up @@ -132,9 +139,12 @@ export const connect = (config: WebchatPipelineConfig) => {
} as const;
};

export const error = () => {
export const error = (err: string | null = null) => {
return {
type: 'ERROR',
payload: {
error: err ? startCase(err) : null,
},
} as const;
};

Expand Down
11 changes: 7 additions & 4 deletions apps/web-remix/app/components/chat/useChat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export const useChat = ({
const [state, dispatch] = useReducer(chatReducer, {
status: 'loading',
pipelineConfig: null,
error: null,
messages: [],
outputsStatus: {},
});
Expand Down Expand Up @@ -78,12 +79,13 @@ export const useChat = ({
}
};

const onBlockError = () => {
dispatch(error());
const onBlockError = (blockId: string, errors: string[]) => {
console.error(blockId, errors);
// dispatch(error());
};

const onError = () => {
dispatch(error());
const onError = (err: string) => {
dispatch(error(err));
};

const onConnect = (
Expand Down Expand Up @@ -171,6 +173,7 @@ export const useChat = ({
return {
push,
isError,
error: state.error,
stopRun,
startRun,
joinRun,
Expand Down

0 comments on commit 556cc23

Please sign in to comment.