feat(chat-window): handle notFound errors

This commit is contained in:
ItzCrazyKns 2024-06-29 12:11:34 +05:30
parent f4b01a29bb
commit c62684407d
No known key found for this signature in database
GPG Key ID: 8162927C7CCE3065
1 changed files with 33 additions and 19 deletions

View File

@ -9,6 +9,7 @@ import crypto from 'crypto';
import { toast } from 'sonner';
import { useSearchParams } from 'next/navigation';
import { getSuggestions } from '@/lib/actions';
import Error from 'next/error';
export type Message = {
messageId: string;
@ -158,6 +159,7 @@ const loadMessages = async (
setIsMessagesLoaded: (loaded: boolean) => void,
setChatHistory: (history: [string, string][]) => void,
setFocusMode: (mode: string) => void,
setNotFound: (notFound: boolean) => void,
) => {
const res = await fetch(
`${process.env.NEXT_PUBLIC_API_URL}/chats/${chatId}`,
@ -169,6 +171,12 @@ const loadMessages = async (
},
);
if (res.status === 404) {
setNotFound(true);
setIsMessagesLoaded(true);
return;
}
const data = await res.json();
const messages = data.messages.map((msg: any) => {
@ -190,7 +198,6 @@ const loadMessages = async (
setChatHistory(history);
setFocusMode(data.chat.focusMode);
console.log(data);
setIsMessagesLoaded(true);
};
@ -221,6 +228,8 @@ const ChatWindow = ({ id }: { id?: string }) => {
const [isMessagesLoaded, setIsMessagesLoaded] = useState(false);
const [notFound, setNotFound] = useState(false);
useEffect(() => {
if (
chatId &&
@ -234,6 +243,7 @@ const ChatWindow = ({ id }: { id?: string }) => {
setIsMessagesLoaded,
setChatHistory,
setFocusMode,
setNotFound,
);
} else if (!chatId) {
setNewChatCreated(true);
@ -416,6 +426,9 @@ const ChatWindow = ({ id }: { id?: string }) => {
}
return isReady ? (
notFound ? (
<Error statusCode={404} />
) : (
<div>
{messages.length > 0 ? (
<>
@ -436,6 +449,7 @@ const ChatWindow = ({ id }: { id?: string }) => {
/>
)}
</div>
)
) : (
<div className="flex flex-row items-center justify-center min-h-screen">
<svg