enhancement: added loading state for conversation

This commit is contained in:
JeevaRamanathan M
2024-10-19 04:24:14 +00:00
parent 7bf79675c1
commit 2366c2cd94
3 changed files with 32 additions and 12 deletions

View File

@@ -37,11 +37,12 @@ import {
setSelectedDocs,
setSourceDocs,
} from './preferences/preferenceSlice';
import Spinner from './assets/spinner.svg';
import SpinnerDark from './assets/spinner-dark.svg';
import Upload from './upload/Upload';
import ShareButton from './components/ShareButton';
import Help from './components/Help';
interface NavigationProps {
navOpen: boolean;
setNavOpen: React.Dispatch<React.SetStateAction<boolean>>;
@@ -89,18 +90,20 @@ export default function Navigation({ navOpen, setNavOpen }: NavigationProps) {
const navigate = useNavigate();
useEffect(() => {
if (!conversations) {
if (!conversations?.data) {
fetchConversations();
}
}, [conversations, dispatch]);
}, [conversations?.data, dispatch]);
async function fetchConversations() {
dispatch(setConversations({ data: null, loading: true }));
return await getConversations()
.then((fetchedConversations) => {
dispatch(setConversations(fetchedConversations));
})
.catch((error) => {
console.error('Failed to fetch conversations: ', error);
dispatch(setConversations({ data: null, loading: false }));
});
}
@@ -196,6 +199,7 @@ export default function Navigation({ navOpen, setNavOpen }: NavigationProps) {
setNavOpen(!isMobile);
}, [isMobile]);
useDefaultDocument();
return (
<>
{!navOpen && (
@@ -278,13 +282,22 @@ export default function Navigation({ navOpen, setNavOpen }: NavigationProps) {
id="conversationsMainDiv"
className="mb-auto h-[78vh] overflow-y-auto overflow-x-hidden dark:text-white"
>
{conversations && conversations.length > 0 ? (
{conversations?.loading && (
<div className="my-auto mx-4 mt-2 flex h-6 items-center justify-center">
<img
src={isDarkTheme ? SpinnerDark : Spinner}
className="relative animate-spin cursor-pointer self-end bg-transparent"
></img>
</div>
)}
{conversations?.data && conversations.data.length > 0 ? (
<div>
<div className=" my-auto mx-4 mt-2 flex h-6 items-center justify-between gap-4 rounded-3xl">
<p className="mt-1 ml-4 text-sm font-semibold">{t('chats')}</p>
</div>
<div className="conversations-container">
{conversations?.map((conversation) => (
{conversations.data?.map((conversation) => (
<ConversationTile
key={conversation.id}
conversation={conversation}

View File

@@ -21,9 +21,10 @@ export async function getDocs(): Promise<Doc[] | null> {
}
}
export async function getConversations(): Promise<
{ name: string; id: string }[] | null
> {
export async function getConversations(): Promise<{
data: { name: string; id: string }[] | null;
loading: boolean;
}> {
try {
const response = await conversationService.getConversations();
const data = await response.json();
@@ -34,10 +35,10 @@ export async function getConversations(): Promise<
conversations.push(conversation as { name: string; id: string });
});
return conversations;
return { data: conversations, loading: false };
} catch (error) {
console.log(error);
return null;
return { data: null, loading: false };
}
}

View File

@@ -15,7 +15,10 @@ export interface Preference {
token_limit: number;
selectedDocs: Doc | null;
sourceDocs: Doc[] | null;
conversations: { name: string; id: string }[] | null;
conversations: {
data: { name: string; id: string }[] | null;
loading: boolean;
};
modalState: ActiveState;
}
@@ -34,7 +37,10 @@ const initialState: Preference = {
retriever: 'classic',
} as Doc,
sourceDocs: null,
conversations: null,
conversations: {
data: null,
loading: false,
},
modalState: 'INACTIVE',
};