xonecas.io
53129
Pa

PAT / commit 0b50b14

feat: better buttons

xonecas2025-12-16 0b50b14571f9d1418d064dbb3d98bb503d1915af parent de1bf61

7 files changed

TODO.md+1−2
@@ -2,8 +2,7 @@
22
33 ## BUGS:
44
5-- Improve websearch, currently not working at all. Use this as a template: https://github.com/meirm/ollama-tools/blob/main/sample_functions.py#L5
6-- Markdown typography, it could be much better.
5+- FIND MORE
76
87 ## New features:
98
app/api/threads/route.ts+13−4
@@ -1,7 +1,7 @@
11 import { NextResponse } from "next/server";
22 import { db } from "@/src/lib/db";
33 import { threads } from "@/src/lib/db/schema";
4-import { desc } from "drizzle-orm";
4+import { desc, count } from "drizzle-orm";
55
66 export async function POST(request: Request) {
77 try {
@@ -22,15 +22,24 @@ export async function POST(request: Request) {
2222 }
2323 }
2424
25-export async function GET() {
25+export async function GET(request: Request) {
2626 try {
27+ const { searchParams } = new URL(request.url);
28+ const offset = parseInt(searchParams.get("offset") || "0", 10);
29+ const limit = parseInt(searchParams.get("limit") || "8", 10);
30+
2731 const threadsList = await db
2832 .select()
2933 .from(threads)
3034 .orderBy(desc(threads.updatedAt))
31- .limit(10);
35+ .limit(limit)
36+ .offset(offset);
37+
38+ const totalCountResult = await db.select({ count: count() }).from(threads);
39+ const totalCount = totalCountResult[0]?.count || 0;
40+ const hasMore = offset + limit < totalCount;
3241
33- return NextResponse.json({ threads: threadsList });
42+ return NextResponse.json({ threads: threadsList, hasMore });
3443 } catch (error) {
3544 console.error("Get threads error", error);
3645 return NextResponse.json({ error: "Failed to get threads" }, { status: 500 });
app/page.tsx+30−5
@@ -16,6 +16,7 @@ export default function Home() {
1616 const [currentThreadId, setCurrentThreadId] = useState<number | null>(null);
1717 const [threads, setThreads] = useState<Thread[]>([]);
1818 const [isLoading, setIsLoading] = useState(false);
19+ const [hasMoreThreads, setHasMoreThreads] = useState(true);
1920 const messagesContainerRef = useRef<HTMLDivElement>(null);
2021 const messageRefs = useRef<Map<number, HTMLDivElement>>(new Map());
2122 const prevMessagesLengthRef = useRef<number>(0);
@@ -25,10 +26,11 @@ export default function Home() {
2526 useEffect(() => {
2627 const loadThreads = async () => {
2728 try {
28- const res = await fetch("/api/threads");
29+ const res = await fetch("/api/threads?limit=8");
2930 const data = await res.json();
3031 const threadsList = data.threads || [];
3132 setThreads(threadsList);
33+ setHasMoreThreads(data.hasMore || false);
3234 } catch (error) {
3335 console.error("Failed to load threads", error);
3436 }
@@ -83,9 +85,11 @@ export default function Home() {
8385 setMessages([]);
8486 prevMessagesLengthRef.current = 0;
8587 // Reload threads to get the full list
86- const threadsRes = await fetch("/api/threads");
88+ const threadsRes = await fetch("/api/threads?limit=8");
8789 const threadsData = await threadsRes.json();
88- setThreads(threadsData.threads || []);
90+ const threadsList = threadsData.threads || [];
91+ setThreads(threadsList);
92+ setHasMoreThreads(threadsData.hasMore || false);
8993 return newThread.id;
9094 } catch (error) {
9195 console.error("Failed to create thread", error);
@@ -108,6 +112,23 @@ export default function Home() {
108112 }, 0);
109113 };
110114
115+ const loadMoreThreads = async () => {
116+ try {
117+ const offset = threads.length;
118+ const res = await fetch(`/api/threads?offset=${offset}&limit=8`);
119+ const data = await res.json();
120+ const newThreads = data.threads || [];
121+ if (newThreads.length > 0) {
122+ setThreads((prev) => [...prev, ...newThreads]);
123+ setHasMoreThreads(data.hasMore || false);
124+ } else {
125+ setHasMoreThreads(false);
126+ }
127+ } catch (error) {
128+ console.error("Failed to load more threads", error);
129+ }
130+ };
131+
111132 const sendMessage = async (message: string) => {
112133 if (!message.trim() || isLoading) return;
113134
@@ -155,9 +176,11 @@ export default function Home() {
155176 await loadMessages(threadId!);
156177
157178 // Reload threads to update the order
158- const threadsRes = await fetch("/api/threads");
179+ const threadsRes = await fetch("/api/threads?limit=8");
159180 const threadsData = await threadsRes.json();
160- setThreads(threadsData.threads || []);
181+ const threadsList = threadsData.threads || [];
182+ setThreads(threadsList);
183+ setHasMoreThreads(threadsData.hasMore || false);
161184 } catch (error) {
162185 console.error("Failed to send message", error);
163186 } finally {
@@ -234,6 +257,8 @@ export default function Home() {
234257 onCreateNewThread={handleCreateNewThread}
235258 onThreadSelect={handleThreadSelect}
236259 onThemeChange={handleThemeChange}
260+ onLoadMore={loadMoreThreads}
261+ hasMoreThreads={hasMoreThreads}
237262 />
238263 </div>
239264 );
src/components/NoThreadSelected.tsx+3−3
@@ -30,16 +30,16 @@ export default function NoThreadSelected({ threadCount }: NoThreadSelectedProps)
3030
3131 return (
3232 <div className="min-w-0 max-w-4xl mx-auto">
33- <div className="p-1 bg-neutral-200 dark:bg-neutral-900 prose prose-neutral dark:prose-invert">
33+ <div className="p-8 bg-neutral-200 dark:bg-neutral-900 prose prose-neutral dark:prose-invert">
3434 <ReactMarkdown remarkPlugins={[remarkGfm]} rehypePlugins={[rehypeHighlight]}>
3535 {`# Welcome to PAT 👋
3636
37-**PAT** (Personal Assistant Thing) is your personal assistant. To get started, click the **"New Thread"** button in the sidebar or start typing below to create your a conversation thread.
37+**PAT** (Personal Assistant Thing) is your personal assistant. Start typing below to start new a conversation thread or pick a previous conversation thread from the sidebar.
3838
3939 ---
4040
4141 ${timeDisplay ? `*Current time: ${timeDisplay}*` : ""}
42-*${threadCount} ${threadCount === 1 ? "thread" : "threads"} available*`}
42+*${threadCount} ${threadCount === 1 ? "thread" : "threads"} created*`}
4343 </ReactMarkdown>
4444 </div>
4545 </div>
src/components/Sidebar.tsx+22−6
@@ -1,6 +1,8 @@
11 "use client";
22
33 import type { Thread } from "@/src/types";
4+import PlusIcon from "@/src/components/icons/PlusIcon";
5+import ChevronDownIcon from "@/src/components/icons/ChevronDownIcon";
46
57 type SidebarProps = {
68 threads: Thread[];
@@ -9,6 +11,8 @@ type SidebarProps = {
911 onCreateNewThread: () => void;
1012 onThreadSelect: (threadId: number) => void;
1113 onThemeChange: (mode: "device" | "dark" | "light") => void;
14+ onLoadMore: () => void;
15+ hasMoreThreads: boolean;
1216 };
1317
1418 export default function Sidebar({
@@ -18,19 +22,22 @@ export default function Sidebar({
1822 onCreateNewThread,
1923 onThreadSelect,
2024 onThemeChange,
25+ onLoadMore,
26+ hasMoreThreads,
2127 }: SidebarProps) {
2228 return (
2329 <div className="w-64 bg-neutral-200 p-1 dark:bg-neutral-900">
2430 <div className="flex flex-col gap-2 p-4">
25- <button
26- onClick={() => onCreateNewThread()}
27- className="bg-green-900 px-1 py-1 text-neutral-100 hover:bg-green-800 dark:bg-green-500 dark:text-neutral-900 dark:hover:bg-green-600"
28- >
29- New Thread
30- </button>
3131 <div className="flex flex-col gap-2">
3232 <label className="text-sm font-semibold">Threads</label>
3333 <div className="flex flex-col gap-1">
34+ <button
35+ onClick={() => onCreateNewThread()}
36+ className="flex items-center justify-center gap-1 bg-indigo-200 px-1 py-1 text-indigo-900 hover:bg-indigo-300 dark:bg-indigo-950 dark:text-indigo-200 dark:hover:bg-indigo-700"
37+ >
38+ <PlusIcon className="h-4 w-4" />
39+ New Thread
40+ </button>
3441 {threads.length === 0 ? (
3542 <div className="px-3 py-2 text-sm text-neutral-600 dark:text-neutral-400">
3643 No threads yet
@@ -53,6 +60,15 @@ export default function Sidebar({
5360 ))
5461 )}
5562 </div>
63+ {hasMoreThreads && (
64+ <button
65+ onClick={() => onLoadMore()}
66+ className="flex items-center justify-center gap-1 bg-indigo-200 px-1 py-1 text-indigo-900 hover:bg-indigo-300 dark:bg-indigo-950 dark:text-indigo-200 dark:hover:bg-indigo-700"
67+ >
68+ <ChevronDownIcon className="h-4 w-4" />
69+ Load More
70+ </button>
71+ )}
5672 </div>
5773 <div className="flex flex-col gap-2">
5874 <label className="text-sm font-semibold">Theme</label>
src/components/icons/ChevronDownIcon.tsx+17−0
@@ -0,0 +1,17 @@
1+export default function ChevronDownIcon({ 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="m6 9 6 6 6-6" />
14+ </svg>
15+ );
16+}
17+
src/components/icons/PlusIcon.tsx+18−0
@@ -0,0 +1,18 @@
1+export default function PlusIcon({ 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 5v14" />
14+ <path d="M5 12h14" />
15+ </svg>
16+ );
17+}
18+