From 097a5c55c6b5940cede5f1fe12be7fd62d3092b4 Mon Sep 17 00:00:00 2001 From: ItzCrazyKns <95534749+ItzCrazyKns@users.noreply.github.com> Date: Fri, 24 Oct 2025 22:57:56 +0530 Subject: [PATCH] feat(layout): add everything inside chat provider --- src/app/c/[chatId]/page.tsx | 7 +------ src/app/layout.tsx | 5 +++-- src/app/page.tsx | 10 +--------- src/components/ChatWindow.tsx | 5 ++--- src/lib/hooks/useChat.tsx | 22 +++++++++++++++++----- 5 files changed, 24 insertions(+), 25 deletions(-) diff --git a/src/app/c/[chatId]/page.tsx b/src/app/c/[chatId]/page.tsx index 672107a..c913b40 100644 --- a/src/app/c/[chatId]/page.tsx +++ b/src/app/c/[chatId]/page.tsx @@ -1,16 +1,11 @@ 'use client'; import ChatWindow from '@/components/ChatWindow'; -import { useParams } from 'next/navigation'; import React from 'react'; -import { ChatProvider } from '@/lib/hooks/useChat'; const Page = () => { - const { chatId }: { chatId: string } = useParams(); return ( - - - + ); }; diff --git a/src/app/layout.tsx b/src/app/layout.tsx index 830d842..e9fd8c7 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -9,6 +9,7 @@ import { Toaster } from 'sonner'; import ThemeProvider from '@/components/theme/Provider'; import configManager from '@/lib/config'; import SetupWizard from '@/components/Setup/SetupWizard'; +import { ChatProvider } from '@/lib/hooks/useChat'; const montserrat = Montserrat({ weight: ['300', '400', '500', '700'], @@ -36,7 +37,7 @@ export default function RootLayout({ {setupComplete ? ( - <> + {children} - + ) : ( )} diff --git a/src/app/page.tsx b/src/app/page.tsx index 25981b5..5fb86cf 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -1,7 +1,5 @@ import ChatWindow from '@/components/ChatWindow'; -import { ChatProvider } from '@/lib/hooks/useChat'; import { Metadata } from 'next'; -import { Suspense } from 'react'; export const metadata: Metadata = { title: 'Chat - Perplexica', @@ -10,13 +8,7 @@ export const metadata: Metadata = { const Home = () => { return ( -
- - - - - -
+ ); }; diff --git a/src/components/ChatWindow.tsx b/src/components/ChatWindow.tsx index 5c0a3e8..c04b4ea 100644 --- a/src/components/ChatWindow.tsx +++ b/src/components/ChatWindow.tsx @@ -9,6 +9,7 @@ import Link from 'next/link'; import NextError from 'next/error'; import { useChat } from '@/lib/hooks/useChat'; import Loader from './ui/Loader'; +import SettingsButtonMobile from './Settings/SettingsButtonMobile'; export interface BaseMessage { chatId: string; @@ -56,9 +57,7 @@ const ChatWindow = () => { return (
- - - +

diff --git a/src/lib/hooks/useChat.tsx b/src/lib/hooks/useChat.tsx index ab346ec..84225a5 100644 --- a/src/lib/hooks/useChat.tsx +++ b/src/lib/hooks/useChat.tsx @@ -17,7 +17,7 @@ import { useState, } from 'react'; import crypto from 'crypto'; -import { useSearchParams } from 'next/navigation'; +import { useParams, useSearchParams } from 'next/navigation'; import { toast } from 'sonner'; import { getSuggestions } from '../actions'; import { MinimalProvider } from '../models/types'; @@ -274,15 +274,14 @@ export const chatContext = createContext({ export const ChatProvider = ({ children, - id, }: { children: React.ReactNode; - id?: string; }) => { + const params: { chatId: string } = useParams() const searchParams = useSearchParams(); const initialMessage = searchParams.get('q'); - const [chatId, setChatId] = useState(id); + const [chatId, setChatId] = useState(params.chatId); const [newChatCreated, setNewChatCreated] = useState(false); const [loading, setLoading] = useState(false); @@ -451,6 +450,19 @@ export const ChatProvider = ({ // eslint-disable-next-line react-hooks/exhaustive-deps }, []); + useEffect(() => { + if (params.chatId && params.chatId !== chatId) { + setChatId(params.chatId); + setMessages([]); + setChatHistory([]); + setFiles([]); + setFileIds([]); + setIsMessagesLoaded(false); + setNotFound(false); + setNewChatCreated(false); + } + }, [params.chatId, chatId]); + useEffect(() => { if ( chatId && @@ -474,7 +486,7 @@ export const ChatProvider = ({ setChatId(crypto.randomBytes(20).toString('hex')); } // eslint-disable-next-line react-hooks/exhaustive-deps - }, []); + }, [chatId, isMessagesLoaded, newChatCreated, messages.length]); useEffect(() => { messagesRef.current = messages;