Skip to content

Commit

Permalink
Merge pull request #1343 from JeevaRamanathan/enhancement/conversatio…
Browse files Browse the repository at this point in the history
…n-loading-state

Enhancement: Added loading state for conversation list
  • Loading branch information
dartpain authored Oct 30, 2024
2 parents d9787e8 + 1e88c86 commit 3be74b1
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 12 deletions.
25 changes: 21 additions & 4 deletions frontend/src/Navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import {
setSelectedDocs,
setSourceDocs,
} from './preferences/preferenceSlice';
import Spinner from './assets/spinner.svg';
import SpinnerDark from './assets/spinner-dark.svg';
import { selectQueries } from './conversation/conversationSlice';
import Upload from './upload/Upload';
import Help from './components/Help';
Expand Down Expand Up @@ -70,6 +72,7 @@ export default function Navigation({ navOpen, setNavOpen }: NavigationProps) {
const conversations = useSelector(selectConversations);
const modalStateDeleteConv = useSelector(selectModalStateDeleteConv);
const conversationId = useSelector(selectConversationId);
const [isDeletingConversation, setIsDeletingConversation] = useState(false);

const { isMobile } = useMediaQuery();
const [isDarkTheme] = useDarkTheme();
Expand All @@ -91,25 +94,28 @@ export default function Navigation({ navOpen, setNavOpen }: NavigationProps) {
const navigate = useNavigate();

useEffect(() => {
if (!conversations) {
if (!conversations?.data) {
fetchConversations();
}
if (queries.length === 0) {
resetConversation();
}
}, [conversations, dispatch]);
}, [conversations?.data, dispatch]);

async function fetchConversations() {
dispatch(setConversations({ ...conversations, 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 }));
});
}

const handleDeleteAllConversations = () => {
setIsDeletingConversation(true);
conversationService
.deleteAll()
.then(() => {
Expand All @@ -119,6 +125,7 @@ export default function Navigation({ navOpen, setNavOpen }: NavigationProps) {
};

const handleDeleteConversation = (id: string) => {
setIsDeletingConversation(true);
conversationService
.delete(id, {})
.then(() => {
Expand Down Expand Up @@ -205,6 +212,7 @@ export default function Navigation({ navOpen, setNavOpen }: NavigationProps) {
setNavOpen(!isMobile);
}, [isMobile]);
useDefaultDocument();

return (
<>
{!navOpen && (
Expand Down Expand Up @@ -306,13 +314,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 && !isDeletingConversation && (
<div className="absolute top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2">
<img
src={isDarkTheme ? SpinnerDark : Spinner}
className="animate-spin cursor-pointer bg-transparent"
alt="Loading..."
/>
</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}
Expand Down
11 changes: 6 additions & 5 deletions frontend/src/preferences/preferenceApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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 };
}
}

Expand Down
10 changes: 8 additions & 2 deletions frontend/src/preferences/preferenceSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -34,7 +37,10 @@ const initialState: Preference = {
retriever: 'classic',
} as Doc,
sourceDocs: null,
conversations: null,
conversations: {
data: null,
loading: false,
},
modalState: 'INACTIVE',
};

Expand Down
5 changes: 4 additions & 1 deletion frontend/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ const preloadedState: { preference: Preference } = {
chunks: JSON.parse(chunks ?? '2').toString(),
token_limit: token_limit ? parseInt(token_limit) : 2000,
selectedDocs: doc !== null ? JSON.parse(doc) : null,
conversations: null,
conversations: {
data: null,
loading: false,
},
sourceDocs: [
{
name: 'default',
Expand Down

0 comments on commit 3be74b1

Please sign in to comment.