@@ -1,5 +1,5 @@
11 "use client";
2-import { useRef, useState, useEffect } from "react";
2+import { useRef, useMemo, useCallback } from "react";
33 import Sidebar from "@/src/components/Sidebar";
44 import MessageInput, { type MessageInputRef } from "@/src/components/MessageInput";
55 import MessageList from "@/src/components/MessageList";
@@ -9,15 +9,16 @@ import { useThreads } from "@/src/hooks/useThreads";
99 import { useMessages } from "@/src/hooks/useMessages";
1010 import { useThreadSelection } from "@/src/hooks/useThreadSelection";
1111 import { useLocalStorage } from "@/src/hooks/useLocalStorage";
12-import { useModels, isValidModel } from "@/src/hooks/useModels";
12+import { useModelValidation } from "@/src/hooks/useModelValidation";
13+import { useErrorWithAutoDismiss } from "@/src/hooks/useErrorWithAutoDismiss";
14+import { useThreadManagement } from "@/src/hooks/useThreadManagement";
1315 import { getErrorMessage } from "@/src/lib/errors";
1416 import "./highlight-theme.css";
1517
1618 export default function Home() {
17- const [error, setError] = useState<string | null>(null);
19+ const [error, setError] = useErrorWithAutoDismiss();
1820 const { themeMode, handleThemeChange } = useTheme();
1921 const [selectedModel, setSelectedModel] = useLocalStorage<string>("selectedModel", "gpt-oss");
20- const { models } = useModels();
2122 const [maxPromptLength, setMaxPromptLength] = useLocalStorage<"none" | 1024 | 4096>(
2223 "maxPromptLength",
2324 "none"
@@ -26,6 +27,9 @@ export default function Home() {
2627 "newThreadUserPrompt",
2728 ""
2829 );
30+
31+ useModelValidation(selectedModel, setSelectedModel);
32+
2933 const {
3034 threads,
3135 totalThreadCount,
@@ -53,117 +57,99 @@ export default function Home() {
5357 });
5458 const messageInputRef = useRef<MessageInputRef>(null);
5559
56- // Validate and initialize model from localStorage or fetch first available model
57- useEffect(() => {
58- if (models.length === 0) return;
59-
60- // Validate that the saved model still exists
61- if (selectedModel && isValidModel(selectedModel, models)) {
62- return;
63- }
64-
65- // If no saved model or it doesn't exist, get first available
66- if (models.length > 0) {
67- setSelectedModel(models[0].model);
68- }
69- }, [models, selectedModel, setSelectedModel]);
70-
71- const handleModelChange = (model: string) => {
72- setSelectedModel(model);
73- };
74-
75- const handleMaxPromptLengthChange = (value: "none" | 1024 | 4096) => {
76- setMaxPromptLength(value);
77- };
60+ const { handleThreadSelect, handleThreadUpdate, handleThreadDelete } = useThreadManagement(
61+ updateThread,
62+ deleteThread,
63+ loadThreads,
64+ selectThread,
65+ deselectThread,
66+ clearMessages,
67+ currentThreadId,
68+ setError
69+ );
7870
79- const handleNewThreadUserPromptChange = (userPrompt: string) => {
80- setNewThreadUserPrompt(userPrompt);
81- };
71+ const handleModelChange = useCallback(
72+ (model: string) => {
73+ setSelectedModel(model);
74+ },
75+ [setSelectedModel]
76+ );
8277
83- const handleDeleteMessage = (messageId: number, threadId: number) => {
84- deleteMessageInternal(messageId, threadId, setError);
85- };
78+ const handleMaxPromptLengthChange = useCallback(
79+ (value: "none" | 1024 | 4096) => {
80+ setMaxPromptLength(value);
81+ },
82+ [setMaxPromptLength]
83+ );
8684
87- // Auto-dismiss error after 3 seconds
88- useEffect(() => {
89- if (error) {
90- const timer = setTimeout(() => {
91- setError(null);
92- }, 3000);
93- return () => clearTimeout(timer);
94- }
95- }, [error]);
85+ const handleNewThreadUserPromptChange = useCallback(
86+ (userPrompt: string) => {
87+ setNewThreadUserPrompt(userPrompt);
88+ },
89+ [setNewThreadUserPrompt]
90+ );
9691
97- const handleThreadSelect = (threadId: number) => {
98- setError(null);
99- selectThread(threadId);
100- };
92+ const handleDeleteMessage = useCallback(
93+ (messageId: number, threadId: number) => {
94+ deleteMessageInternal(messageId, threadId, setError);
95+ },
96+ [deleteMessageInternal, setError]
97+ );
10198
102- const handleCreateNewThread = () => {
99+ const handleCreateNewThread = useCallback(() => {
103100 deselectThread();
104101 clearMessages();
105102 setError(null);
106- // Focus the input after a short delay to ensure DOM has updated
107103 setTimeout(() => {
108104 messageInputRef.current?.focus();
109105 }, 0);
110- };
111-
112- const handleThreadUpdate = async (
113- threadId: number,
114- updates: {
115- model?: string;
116- maxPromptLength?: "none" | 1024 | 4096 | null;
117- userPrompt?: string | null;
118- }
119- ) => {
120- setError(null);
121- try {
122- await updateThread(threadId, updates, setError);
123- // Reload threads to ensure UI is in sync
124- await loadThreads(8, setError);
125- } catch (error) {
126- setError(getErrorMessage(error));
127- }
128- };
106+ }, [deselectThread, clearMessages, setError]);
129107
130- const handleThreadDelete = async (threadId: number) => {
131- setError(null);
132- try {
133- await deleteThread(threadId, setError);
134- // If the deleted thread was the current thread, deselect it
135- if (currentThreadId === threadId) {
136- deselectThread();
137- clearMessages();
108+ const handleSendMessage = useCallback(
109+ async (message: string) => {
110+ setError(null);
111+ try {
112+ await sendMessage(
113+ message,
114+ currentThreadId,
115+ (title, firstMessage) =>
116+ createThread(
117+ selectedModel,
118+ maxPromptLength,
119+ title,
120+ firstMessage,
121+ newThreadUserPrompt,
122+ setError
123+ ),
124+ selectThread,
125+ () => loadThreads(8, setError),
126+ setError
127+ );
128+ } catch (error) {
129+ setError(getErrorMessage(error));
138130 }
139- } catch (error) {
140- setError(getErrorMessage(error));
141- }
142- };
131+ },
132+ [
133+ sendMessage,
134+ currentThreadId,
135+ createThread,
136+ selectedModel,
137+ maxPromptLength,
138+ newThreadUserPrompt,
139+ selectThread,
140+ loadThreads,
141+ setError,
142+ ]
143+ );
143144
144- const handleSendMessage = async (message: string) => {
145- setError(null);
146- try {
147- await sendMessage(
148- message,
149- currentThreadId,
150- (title, firstMessage) =>
151- createThread(
152- selectedModel,
153- maxPromptLength,
154- title,
155- firstMessage,
156- newThreadUserPrompt,
157- setError
158- ),
159- selectThread,
160- () => loadThreads(8, setError),
161- setError
162- );
163- } catch (error) {
164- setError(getErrorMessage(error));
165- }
166- };
145+ const currentThread = useMemo(
146+ () => (currentThreadId !== null ? threads.find((t) => t.id === currentThreadId) || null : null),
147+ [currentThreadId, threads]
148+ );
149+
150+ const handleLoadMore = useCallback(() => {
151+ void loadMoreThreads(8, setError);
152+ }, [loadMoreThreads, setError]);
167153
168154 return (
169155 <div className="flex h-screen bg-neutral-100 text-neutral-800 dark:bg-neutral-950 dark:text-neutral-200">
@@ -198,9 +184,7 @@ export default function Home() {
198184 onStop={stopGeneration}
199185 error={error}
200186 currentThreadId={currentThreadId}
201- currentThread={
202- currentThreadId !== null ? threads.find((t) => t.id === currentThreadId) || null : null
203- }
187+ currentThread={currentThread}
204188 onThreadUpdate={handleThreadUpdate}
205189 onThreadDelete={handleThreadDelete}
206190 onError={setError}
@@ -219,9 +203,7 @@ export default function Home() {
219203 onCreateNewThread={handleCreateNewThread}
220204 onThreadSelect={handleThreadSelect}
221205 onThemeChange={handleThemeChange}
222- onLoadMore={() => {
223- void loadMoreThreads(8, setError);
224- }}
206+ onLoadMore={handleLoadMore}
225207 hasMoreThreads={hasMoreThreads}
226208 totalThreadCount={totalThreadCount}
227209 />