xonecas.io
53129
Pa

PAT / commit 3242dfe

feat: settings modal

xonecas2025-12-17 3242dfe2f28593f8b46e08230cd337fd97acf9ab parent 94077a5

11 files changed

app/api/chat/route.ts+10−8
@@ -48,27 +48,29 @@ export async function POST(request: Request) {
4848 const stream = new ReadableStream({
4949 async start(controller) {
5050 try {
51- const onChunk = (chunk: {
52- content?: string;
53- thinking?: string;
54- toolCalls?: any[];
55- }) => {
51+ const onChunk = (chunk: { content?: string; thinking?: string; toolCalls?: any[] }) => {
5652 if (chunk.content) {
5753 accumulatedContent += chunk.content;
5854 controller.enqueue(
59- encoder.encode(`data: ${JSON.stringify({ type: "content", content: chunk.content })}\n\n`)
55+ encoder.encode(
56+ `data: ${JSON.stringify({ type: "content", content: chunk.content })}\n\n`
57+ )
6058 );
6159 }
6260 if (chunk.thinking) {
6361 accumulatedThinking += chunk.thinking;
6462 controller.enqueue(
65- encoder.encode(`data: ${JSON.stringify({ type: "thinking", thinking: chunk.thinking })}\n\n`)
63+ encoder.encode(
64+ `data: ${JSON.stringify({ type: "thinking", thinking: chunk.thinking })}\n\n`
65+ )
6666 );
6767 }
6868 if (chunk.toolCalls) {
6969 allToolCalls.push(...chunk.toolCalls);
7070 controller.enqueue(
71- encoder.encode(`data: ${JSON.stringify({ type: "toolCalls", toolCalls: chunk.toolCalls })}\n\n`)
71+ encoder.encode(
72+ `data: ${JSON.stringify({ type: "toolCalls", toolCalls: chunk.toolCalls })}\n\n`
73+ )
7274 );
7375 }
7476 };
src/components/Modal.tsx+49−0
@@ -0,0 +1,49 @@
1+"use client";
2+
3+import { useEffect, type ReactNode } from "react";
4+import XIcon from "@/src/components/icons/XIcon";
5+
6+type ModalProps = {
7+ isOpen: boolean;
8+ onClose: () => void;
9+ children: ReactNode;
10+};
11+
12+export default function Modal({ isOpen, onClose, children }: ModalProps) {
13+ useEffect(() => {
14+ if (!isOpen) return;
15+
16+ const handleEscape = (e: KeyboardEvent) => {
17+ if (e.key === "Escape") {
18+ onClose();
19+ }
20+ };
21+
22+ document.addEventListener("keydown", handleEscape);
23+ return () => {
24+ document.removeEventListener("keydown", handleEscape);
25+ };
26+ }, [isOpen, onClose]);
27+
28+ if (!isOpen) return null;
29+
30+ return (
31+ <div className="fixed inset-0 z-50 flex items-center justify-center">
32+ {/* Overlay */}
33+ <div className="absolute inset-0 bg-black/80" onClick={onClose} aria-hidden="true" />
34+ {/* Modal */}
35+ <div className="relative z-10 w-full max-w-lg rounded-lg bg-neutral-50 p-6 shadow-lg dark:bg-neutral-900">
36+ {/* Close button */}
37+ <button
38+ onClick={onClose}
39+ className="absolute right-4 top-4 rounded p-1 text-neutral-600 hover:bg-neutral-200 hover:text-neutral-800 dark:text-neutral-400 dark:hover:bg-neutral-800 dark:hover:text-neutral-200"
40+ aria-label="Close modal"
41+ >
42+ <XIcon className="h-5 w-5" />
43+ </button>
44+ {/* Content */}
45+ <div className="pr-8">{children}</div>
46+ </div>
47+ </div>
48+ );
49+}
src/components/SettingsModal.tsx+37−0
@@ -0,0 +1,37 @@
1+"use client";
2+
3+import Modal from "@/src/components/Modal";
4+
5+type SettingsModalProps = {
6+ isOpen: boolean;
7+ onClose: () => void;
8+ themeMode: "device" | "dark" | "light";
9+ onThemeChange: (mode: "device" | "dark" | "light") => void;
10+};
11+
12+export default function SettingsModal({
13+ isOpen,
14+ onClose,
15+ themeMode,
16+ onThemeChange,
17+}: SettingsModalProps) {
18+ return (
19+ <Modal isOpen={isOpen} onClose={onClose}>
20+ <h2 className="mb-4 text-xl font-semibold text-neutral-800 dark:text-neutral-200">
21+ Settings
22+ </h2>
23+ <div className="flex flex-col gap-2">
24+ <label className="text-neutral-600 dark:text-neutral-400">Theme</label>
25+ <select
26+ value={themeMode}
27+ onChange={(e) => onThemeChange(e.target.value as "device" | "dark" | "light")}
28+ className="bg-neutral-100 px-3 py-1 text-neutral-800 focus:outline-none dark:bg-neutral-900 dark:text-neutral-200"
29+ >
30+ <option value="device">Device</option>
31+ <option value="dark">Dark</option>
32+ <option value="light">Light</option>
33+ </select>
34+ </div>
35+ </Modal>
36+ );
37+}
src/components/Sidebar.tsx+69−58
@@ -1,10 +1,13 @@
11 "use client";
22
3+import { useState } from "react";
34 import type { Thread } from "@/src/lib/db/schema";
45 import PlusIcon from "@/src/components/icons/PlusIcon";
56 import ChevronDownIcon from "@/src/components/icons/ChevronDownIcon";
67 import BowtieIcon from "@/src/components/icons/BowtieIcon";
7-import Anchor from "@/src/components/Anchor";
8+import GearIcon from "@/src/components/icons/GearIcon";
9+import SidebarButton from "@/src/components/SidebarButton";
10+import SettingsModal from "@/src/components/SettingsModal";
811
912 type SidebarProps = {
1013 threads: Thread[];
@@ -27,16 +30,17 @@ export default function Sidebar({
2730 onLoadMore,
2831 hasMoreThreads,
2932 }: SidebarProps) {
33+ const [isSettingsOpen, setIsSettingsOpen] = useState(false);
34+
3035 return (
31- <div className="h-full w-64 overflow-y-auto bg-neutral-50 p-1 text-neutral-800 dark:bg-neutral-950 dark:text-neutral-200">
32- <div className="flex flex-col gap-2 p-4">
33- <div className="flex justify-center pb-4">
34- <BowtieIcon className="h-12 w-12" />
35- </div>
36- <div className="flex flex-col gap-2">
37- <label className="text-neutral-500">Threads</label>
36+ <>
37+ <div className="h-full w-64 overflow-y-auto bg-neutral-50 p-1 text-neutral-800 dark:bg-neutral-950 dark:text-neutral-200">
38+ <div className="flex flex-col gap-2 p-4">
39+ <div className="flex justify-center pb-4">
40+ <BowtieIcon className="h-12 w-12" />
41+ </div>
3842 <div className="flex flex-col gap-2">
39- <Anchor
43+ <SidebarButton
4044 href="#"
4145 onClick={(e) => {
4246 e.preventDefault();
@@ -45,61 +49,68 @@ export default function Sidebar({
4549 >
4650 <PlusIcon className="h-4 w-4" />
4751 New Thread
48- </Anchor>
49- <div className="flex flex-col">
50- {threads.length === 0 ? (
51- <div className="px-3 py-2 text-neutral-600 dark:text-neutral-400">
52- No threads yet
53- </div>
54- ) : (
55- threads.map((thread) => {
56- const isSelected = currentThreadId === thread.id;
57- return (
58- <Anchor
59- key={thread.id}
60- href={`#thread-${thread.id}`}
61- onClick={(e) => {
62- e.preventDefault();
63- onThreadSelect(thread.id);
64- }}
65- isSelected={isSelected}
66- >
67- <span className="min-w-0 truncate">
68- {thread.title || `Thread ${thread.id}`}
69- </span>
70- </Anchor>
71- );
72- })
73- )}
74- </div>
75- </div>
76- {hasMoreThreads && (
77- <Anchor
52+ </SidebarButton>
53+ <SidebarButton
7854 href="#"
79- className="text-xs"
8055 onClick={(e) => {
8156 e.preventDefault();
82- onLoadMore();
57+ setIsSettingsOpen(true);
8358 }}
8459 >
85- <ChevronDownIcon className="mt-0.5 h-3 w-3" />
86- Load More
87- </Anchor>
88- )}
89- </div>
90- <div className="flex flex-col gap-2">
91- <label className="text-neutral-500">Theme</label>
92- <select
93- value={themeMode}
94- onChange={(e) => onThemeChange(e.target.value as "device" | "dark" | "light")}
95- className="bg-neutral-100 px-3 py-1 text-neutral-800 focus:outline-none dark:bg-neutral-900 dark:text-neutral-200"
96- >
97- <option value="device">Device</option>
98- <option value="dark">Dark</option>
99- <option value="light">Light</option>
100- </select>
60+ <GearIcon className="h-4 w-4" />
61+ Settings
62+ </SidebarButton>
63+ <label className="text-neutral-500">Threads</label>
64+ <div className="flex flex-col gap-2">
65+ <div className="flex flex-col">
66+ {threads.length === 0 ? (
67+ <div className="px-3 py-2 text-neutral-600 dark:text-neutral-400">
68+ No threads yet
69+ </div>
70+ ) : (
71+ threads.map((thread) => {
72+ const isSelected = currentThreadId === thread.id;
73+ return (
74+ <SidebarButton
75+ key={thread.id}
76+ href={`#thread-${thread.id}`}
77+ onClick={(e) => {
78+ e.preventDefault();
79+ onThreadSelect(thread.id);
80+ }}
81+ isSelected={isSelected}
82+ >
83+ <span className="min-w-0 truncate">
84+ {thread.title || `Thread ${thread.id}`}
85+ </span>
86+ </SidebarButton>
87+ );
88+ })
89+ )}
90+ </div>
91+ </div>
92+ {hasMoreThreads && (
93+ <SidebarButton
94+ href="#"
95+ className="text-xs"
96+ onClick={(e) => {
97+ e.preventDefault();
98+ onLoadMore();
99+ }}
100+ >
101+ <ChevronDownIcon className="mt-0.5 h-3 w-3" />
102+ Load More
103+ </SidebarButton>
104+ )}
105+ </div>
101106 </div>
102107 </div>
103- </div>
108+ <SettingsModal
109+ isOpen={isSettingsOpen}
110+ onClose={() => setIsSettingsOpen(false)}
111+ themeMode={themeMode}
112+ onThemeChange={onThemeChange}
113+ />
114+ </>
104115 );
105116 }
src/components/SidebarButton.tsx+3−3
@@ -8,10 +8,10 @@ type AnchorProps = AnchorHTMLAttributes<HTMLAnchorElement> & {
88 isSelected?: boolean;
99 };
1010
11-// Anchor: A reusable anchor component with neutral hover and selection states.
11+// SidebarButton: A reusable button component with neutral hover and selection states.
1212 // When selected (isSelected=true), displays a neutral-800 background (dark) or neutral-200 (light).
1313 // On hover, displays a neutral-700 background (dark) or neutral-300 (light).
14-export default function Anchor({
14+export default function SidebarButton({
1515 children,
1616 isSelected = false,
1717 className = "",
@@ -27,7 +27,7 @@ export default function Anchor({
2727
2828 return (
2929 <a
30- className={`flex min-w-0 items-center gap-1 rounded px-2 py-1 ${
30+ className={`flex min-w-0 items-center gap-2 rounded px-2 py-1 ${
3131 isSelected
3232 ? "bg-neutral-200 dark:bg-neutral-800"
3333 : "hover:bg-neutral-300 dark:hover:bg-neutral-700"
src/components/icons/GearIcon.tsx+17−0
@@ -0,0 +1,17 @@
1+export default function GearIcon({ className }: { className?: string }) {
2+ return (
3+ <svg
4+ xmlns="http://www.w3.org/2000/svg"
5+ viewBox="0 0 24 24"
6+ fill="none"
7+ stroke="currentColor"
8+ strokeWidth="2"
9+ strokeLinecap="round"
10+ strokeLinejoin="round"
11+ className={className}
12+ >
13+ <path d="M12.22 2h-.44a2 2 0 0 0-2 2v.18a2 2 0 0 1-1 1.73l-.43.25a2 2 0 0 1-2 0l-.15-.08a2 2 0 0 0-2.73.73l-.22.38a2 2 0 0 0 .73 2.73l.15.1a2 2 0 0 1 1 1.72v.51a2 2 0 0 1-1 1.74l-.15.09a2 2 0 0 0-.73 2.73l.22.38a2 2 0 0 0 2.73.73l.15-.08a2 2 0 0 1 2 0l.43.25a2 2 0 0 1 1 1.73V20a2 2 0 0 0 2 2h.44a2 2 0 0 0 2-2v-.18a2 2 0 0 1 1-1.73l.43-.25a2 2 0 0 1 2 0l.15.08a2 2 0 0 0 2.73-.73l.22-.39a2 2 0 0 0-.73-2.73l-.15-.08a2 2 0 0 1-1-1.74v-.5a2 2 0 0 1 1-1.74l.15-.09a2 2 0 0 0 .73-2.73l-.22-.38a2 2 …
14+ <circle cx="12" cy="12" r="3" />
15+ </svg>
16+ );
17+}
src/components/icons/XIcon.tsx+17−0
@@ -0,0 +1,17 @@
1+export default function XIcon({ className }: { className?: string }) {
2+ return (
3+ <svg
4+ xmlns="http://www.w3.org/2000/svg"
5+ viewBox="0 0 24 24"
6+ fill="none"
7+ stroke="currentColor"
8+ strokeWidth="2"
9+ strokeLinecap="round"
10+ strokeLinejoin="round"
11+ className={className}
12+ >
13+ <path d="M18 6L6 18" />
14+ <path d="M6 6l12 12" />
15+ </svg>
16+ );
17+}
src/hooks/useMessages.ts+1−3
@@ -122,9 +122,7 @@ export function useMessages(threadId: number | null) {
122122 accumulatedContent += data.content || "";
123123 setMessages((prev) =>
124124 prev.map((msg) =>
125- msg.id === assistantMsgId
126- ? { ...msg, content: accumulatedContent }
127- : msg
125+ msg.id === assistantMsgId ? { ...msg, content: accumulatedContent } : msg
128126 )
129127 );
130128 } else if (data.type === "done") {
src/lib/cache/index.ts+0−1
@@ -54,4 +54,3 @@ export async function getCache<T>(key: string): Promise<T | null> {
5454 return null;
5555 }
5656 }
57-
src/lib/db/schema.ts+3−9
@@ -5,12 +5,8 @@ import { relations } from "drizzle-orm";
55 export const threads = pgTable("threads", {
66 id: serial("id").primaryKey(),
77 title: text("title"),
8- createdAt: timestamp("created_at", { withTimezone: true })
9- .notNull()
10- .defaultNow(),
11- updatedAt: timestamp("updated_at", { withTimezone: true })
12- .notNull()
13- .defaultNow(),
8+ createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
9+ updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow(),
1410 });
1511
1612 export const messages = pgTable("messages", {
@@ -20,9 +16,7 @@ export const messages = pgTable("messages", {
2016 .references(() => threads.id, { onDelete: "cascade" }),
2117 role: text("role").notNull(), // "user" or "assistant"
2218 content: text("content").notNull(),
23- createdAt: timestamp("created_at", { withTimezone: true })
24- .notNull()
25- .defaultNow(),
19+ createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
2620 generationTimeMs: integer("generation_time_ms"), // Time taken to generate response (for assistant messages)
2721 toolCalls: text("tool_calls"), // JSON string of tool calls made to generate this message (for assistant messages)
2822 });
src/lib/ollama/index.ts+1−3
@@ -150,9 +150,7 @@ export async function fetchOllamaResponse(
150150 totalDuration += iterationDuration;
151151 const iterationDurationMs = Math.round(iterationDuration / 1_000_000);
152152
153- debug(
154- `[Ollama] Response received (${iterationDurationMs}ms), tool_calls: ${toolCalls.length}`
155- );
153+ debug(`[Ollama] Response received (${iterationDurationMs}ms), tool_calls: ${toolCalls.length}`);
156154
157155 // Append accumulated fields to messages
158156 if (thinking || content || toolCalls.length) {