1
0
mirror of https://github.com/kamranahmedse/developer-roadmap.git synced 2025-09-03 06:12:53 +02:00
This commit is contained in:
Arik Chakma
2025-06-10 11:39:10 +06:00
parent 4a9a8f6e91
commit 71c23fca5c
2 changed files with 97 additions and 63 deletions

View File

@@ -5,7 +5,12 @@ import {
} from '../../queries/chat-history'; } from '../../queries/chat-history';
import { queryClient } from '../../stores/query-client'; import { queryClient } from '../../stores/query-client';
import { ChatHistoryItem } from './ChatHistoryItem'; import { ChatHistoryItem } from './ChatHistoryItem';
import { Loader2Icon, PlusIcon, SearchIcon } from 'lucide-react'; import {
AlertCircleIcon,
Loader2Icon,
PlusIcon,
SearchIcon,
} from 'lucide-react';
import { DateTime } from 'luxon'; import { DateTime } from 'luxon';
import { useEffect, useMemo, useState } from 'react'; import { useEffect, useMemo, useState } from 'react';
import { useDebounceValue } from '../../hooks/use-debounce'; import { useDebounceValue } from '../../hooks/use-debounce';
@@ -21,8 +26,16 @@ export function ListChatHistory(props: ListChatHistoryProps) {
const { activeChatHistoryId, onChatHistoryClick, onDelete } = props; const { activeChatHistoryId, onChatHistoryClick, onDelete } = props;
const [query, setQuery] = useState(''); const [query, setQuery] = useState('');
const { data, fetchNextPage, hasNextPage, isFetchingNextPage, isLoading } = const {
useInfiniteQuery(listChatHistoryOptions({ query }), queryClient); data,
fetchNextPage,
hasNextPage,
isFetchingNextPage,
isLoading,
isError,
error,
refetch,
} = useInfiniteQuery(listChatHistoryOptions({ query }), queryClient);
const groupedChatHistory = useMemo(() => { const groupedChatHistory = useMemo(() => {
const today = DateTime.now().startOf('day'); const today = DateTime.now().startOf('day');
@@ -65,72 +78,75 @@ export function ListChatHistory(props: ListChatHistoryProps) {
); );
}, [data?.pages]); }, [data?.pages]);
if (isLoading) {
return <ListChatHistorySkeleton />;
}
return ( return (
<div className="w-[255px] shrink-0 border-r border-gray-200 bg-white p-2"> <div className="w-[255px] shrink-0 border-r border-gray-200 bg-white p-2">
<button {isLoading && <ListChatHistorySkeleton />}
className="flex w-full items-center justify-center gap-2 rounded-lg bg-black p-2 text-sm text-white" {!isLoading && isError && <ErrorState error={error} />}
onClick={() => {
onChatHistoryClick(null);
}}
>
<PlusIcon className="h-4 w-4" />
<span className="text-sm">New Chat</span>
</button>
<SearchInput onSearch={setQuery} isLoading={isLoading} /> {!isLoading && !isError && (
<>
<div className="mt-6 space-y-4">
{Object.entries(groupedChatHistory ?? {}).map(([key, value]) => {
if (value.histories.length === 0) {
return null;
}
return (
<div key={key}>
<h2 className="ml-2 text-xs text-gray-500">{value.title}</h2>
<ul className="mt-1 space-y-0.5">
{value.histories.map((chatHistory) => {
return (
<ChatHistoryItem
key={chatHistory._id}
chatHistory={chatHistory}
isActive={activeChatHistoryId === chatHistory._id}
onChatHistoryClick={onChatHistoryClick}
onDelete={() => {
onDelete?.(chatHistory._id);
}}
/>
);
})}
</ul>
</div>
);
})}
</div>
{hasNextPage && (
<div className="mt-4">
<button <button
className="flex w-full items-center justify-center gap-2 text-sm text-gray-500 hover:text-black" className="flex w-full items-center justify-center gap-2 rounded-lg bg-black p-2 text-sm text-white"
onClick={() => { onClick={() => {
fetchNextPage(); onChatHistoryClick(null);
}} }}
disabled={isFetchingNextPage}
> >
{isFetchingNextPage && ( <PlusIcon className="h-4 w-4" />
<> <span className="text-sm">New Chat</span>
<Loader2Icon className="h-4 w-4 animate-spin" />
Loading more...
</>
)}
{!isFetchingNextPage && 'Load More'}
</button> </button>
</div>
<SearchInput onSearch={setQuery} isLoading={isLoading} />
<div className="mt-6 space-y-4">
{Object.entries(groupedChatHistory ?? {}).map(([key, value]) => {
if (value.histories.length === 0) {
return null;
}
return (
<div key={key}>
<h2 className="ml-2 text-xs text-gray-500">{value.title}</h2>
<ul className="mt-1 space-y-0.5">
{value.histories.map((chatHistory) => {
return (
<ChatHistoryItem
key={chatHistory._id}
chatHistory={chatHistory}
isActive={activeChatHistoryId === chatHistory._id}
onChatHistoryClick={onChatHistoryClick}
onDelete={() => {
onDelete?.(chatHistory._id);
}}
/>
);
})}
</ul>
</div>
);
})}
</div>
{hasNextPage && (
<div className="mt-4">
<button
className="flex w-full items-center justify-center gap-2 text-sm text-gray-500 hover:text-black"
onClick={() => {
fetchNextPage();
}}
disabled={isFetchingNextPage}
>
{isFetchingNextPage && (
<>
<Loader2Icon className="h-4 w-4 animate-spin" />
Loading more...
</>
)}
{!isFetchingNextPage && 'Load More'}
</button>
</div>
)}
</>
)} )}
</div> </div>
); );
@@ -180,3 +196,21 @@ function SearchInput(props: SearchInputProps) {
</form> </form>
); );
} }
type ErrorStateProps = {
error: Error | null;
};
function ErrorState(props: ErrorStateProps) {
const { error } = props;
return (
<div className="mt-10 flex flex-col items-center justify-center text-center">
<AlertCircleIcon className="h-8 w-8 text-red-500" />
<h3 className="mt-4 text-sm font-medium text-gray-900">
Something went wrong
</h3>
<p className="mt-0.5 text-xs text-gray-500">{error?.message}</p>
</div>
);
}

View File

@@ -1,6 +1,6 @@
export function ListChatHistorySkeleton() { export function ListChatHistorySkeleton() {
return ( return (
<div className="w-[255px] shrink-0 border-r border-gray-200 bg-white p-2"> <>
<div className="h-9 w-full animate-pulse rounded-lg bg-gray-200" /> <div className="h-9 w-full animate-pulse rounded-lg bg-gray-200" />
<div className="relative mt-2"> <div className="relative mt-2">
@@ -25,6 +25,6 @@ export function ListChatHistorySkeleton() {
</div> </div>
))} ))}
</div> </div>
</div> </>
); );
} }