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