Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | 236x 236x 236x 236x 236x 236x 236x 236x 253x 253x 253x 12x 207x 207x 207x 207x 207x 4x 207x 1x 207x 201x 249x 249x 249x 249x 38x 207x 8x 2x 1x 1x 1x 1x 199x 1x 1x 198x 1x | import { Conversation } from "@/objects/Model";
import {
ChevronDown,
ChevronLeft,
ChevronRight,
Loader2,
MessageSquare,
Plus,
Search,
} from "lucide-react";
import React, { useId, useState } from "react";
import { parseServerDate } from "@/util/time";
// ─── Helpers ──────────────────────────────────────────────────────────────────
function formatDate(iso: string): string {
const date = parseServerDate(iso);
const now = new Date();
const diffMs = now.getTime() - date.getTime();
const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24));
Iif (diffDays === 0) {
return date.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" });
}
Iif (diffDays === 1) return "Yesterday";
Iif (diffDays < 7) {
return date.toLocaleDateString([], { weekday: "short" });
}
return date.toLocaleDateString([], { month: "short", day: "numeric" });
}
function getSnippet(conversation: Conversation): string {
const first = conversation.messages.find((m) => m.role === "user");
Iif (!first) return "Empty conversation";
return first.content.length > 52
? first.content.slice(0, 52) + "…"
: first.content;
}
// ─── Props ────────────────────────────────────────────────────────────────────
export interface ConversationSidebarProps {
conversations: Conversation[];
selectedId: string | null;
onSelect: (conversation: Conversation) => void;
onNew: () => void;
loading?: boolean;
/** Run the height of a stretched parent instead of capping the list at
* 60vh — `ChatLayout` beside the chat, on the page and in full screen. */
fill?: boolean;
/** Too little room beside the chat (`ChatLayout`): a bar above it — the
* list behind a disclosure, with New chat beside it. */
stacked?: boolean;
/** Conversations with an answer still being written (svc-core's running
* turns): marked, so a person who left mid-answer sees where it is. */
answering?: ReadonlySet<string>;
}
// ─── Component ────────────────────────────────────────────────────────────────
const ConversationSidebar: React.FC<ConversationSidebarProps> = ({
conversations,
selectedId,
onSelect,
onNew,
loading = false,
fill = false,
stacked = false,
answering,
}) => {
const [collapsed, setCollapsed] = useState(false);
// The stacked bar's list: closed until asked for, and closed again once a
// conversation is picked, so the chat is what the screen shows.
const [open, setOpen] = useState(false);
const [search, setSearch] = useState("");
const listId = useId();
const filtered = search.trim()
? conversations.filter((c) =>
getSnippet(c).toLowerCase().includes(search.toLowerCase()),
)
: conversations;
const searchBox = (
<div className="relative">
<Search className="absolute left-2.5 top-1/2 -translate-y-1/2 w-3.5 h-3.5 text-gray-400 pointer-events-none" />
<input
type="text"
value={search}
onChange={(e) => setSearch(e.target.value)}
placeholder="Search…"
aria-label="Search conversations"
className="w-full pl-8 pr-3 py-1.5 text-xs rounded-lg bg-gray-100 dark:bg-gray-800 border border-gray-200 dark:border-gray-700 text-gray-700 dark:text-gray-300 placeholder-gray-400 dark:placeholder-gray-500 focus:outline-none focus:ring-2 focus:ring-indigo-400 focus:border-transparent transition-colors"
/>
</div>
);
const renderList = (pick: (conversation: Conversation) => void) =>
loading ? (
<div className="flex justify-center mt-6">
<Loader2 className="w-4 h-4 animate-spin text-gray-400 dark:text-gray-500" />
</div>
) : filtered.length === 0 ? (
<p className="text-xs text-gray-400 dark:text-gray-500 text-center mt-4 px-2">
{search.trim() ? "No matches" : "No past conversations"}
</p>
) : (
filtered.map((conv) => {
const isActive = conv.id === selectedId;
const isAnswering = answering?.has(conv.id) ?? false;
const Icon = isAnswering ? Loader2 : MessageSquare;
return (
<button
key={conv.id}
onClick={() => pick(conv)}
className={`w-full text-left px-3 py-2.5 rounded-lg transition-colors group ${
isActive
? "bg-indigo-50 dark:bg-indigo-900/30 border border-indigo-200 dark:border-indigo-800"
: "hover:bg-gray-100 dark:hover:bg-gray-800 border border-transparent"
}`}
>
{/* min-w-0: every button is a flex container (index.css), so
this row is a flex item that would otherwise refuse to
shrink below its text — the snippet ran past the sidebar
and scrolled it sideways instead of truncating. */}
<div className="flex w-full min-w-0 items-start gap-2">
<Icon
aria-hidden="true"
className={`w-3.5 h-3.5 shrink-0 mt-0.5 ${
isAnswering ? "animate-spin " : ""
}${
isActive
? "text-indigo-500"
: "text-gray-400 dark:text-gray-500 group-hover:text-gray-500 dark:group-hover:text-gray-400"
}`}
/>
<div className="flex-1 min-w-0">
<p
className={`text-xs leading-snug truncate ${
isActive
? "text-indigo-700 dark:text-indigo-300 font-medium"
: "text-gray-700 dark:text-gray-300"
}`}
>
{getSnippet(conv)}
</p>
<p className="text-[10px] text-gray-400 dark:text-gray-500 mt-0.5">
{isAnswering ? (
<span className="text-indigo-500 dark:text-indigo-400">
Answering…
</span>
) : (
formatDate(conv.created_at)
)}
</p>
</div>
</div>
</button>
);
})
);
if (stacked) {
return (
<div className="w-full flex flex-col gap-2">
{/* A container: on a bar under 20rem, New chat drops its label so
the list's own label is not the one cut short. */}
<div className="@container flex items-center gap-2">
<button
type="button"
onClick={() => setOpen((v) => !v)}
aria-expanded={open}
aria-controls={listId}
className="flex flex-1 min-w-0 items-center gap-2 px-3 py-2 rounded-lg border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-900 text-sm font-medium text-gray-700 dark:text-gray-200 hover:bg-gray-50 dark:hover:bg-gray-800 transition-colors"
>
<MessageSquare className="w-4 h-4 shrink-0 text-gray-400 dark:text-gray-500" />
<span className="truncate">Conversations</span>
<span className="ml-auto shrink-0 rounded-full bg-gray-100 dark:bg-gray-800 px-1.5 text-[10px] font-semibold text-gray-500 dark:text-gray-400">
{conversations.length}
</span>
<ChevronDown
className={`w-4 h-4 shrink-0 text-gray-400 transition-transform ${
open ? "rotate-180" : ""
}`}
/>
</button>
<button
type="button"
onClick={() => {
setOpen(false);
onNew();
}}
aria-label="New chat"
title="New chat"
className="shrink-0 flex items-center gap-1.5 px-3 py-2 rounded-lg text-sm font-medium bg-indigo-500 hover:bg-indigo-600 text-white transition-colors shadow-sm"
>
<Plus className="w-4 h-4 shrink-0" />
<span className="hidden @xs:inline">New chat</span>
</button>
</div>
{open && (
<div
id={listId}
className="flex flex-col gap-2 rounded-lg border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-900 p-2"
>
{searchBox}
<div
className="overflow-y-auto overflow-x-hidden space-y-0.5"
style={{ maxHeight: "40vh" }}
>
{renderList((conv) => {
setOpen(false);
onSelect(conv);
})}
</div>
</div>
)}
</div>
);
}
if (collapsed) {
return (
<div className="shrink-0 flex flex-col items-center gap-2 w-10">
<button
onClick={() => setCollapsed(false)}
title="Expand sidebar"
className="p-2 rounded-lg text-gray-400 hover:text-gray-600 dark:hover:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-800 transition-colors"
>
<ChevronRight className="w-4 h-4" />
</button>
<button
onClick={onNew}
title="New chat"
className="p-2 rounded-lg text-indigo-500 hover:text-indigo-600 hover:bg-indigo-50 dark:hover:bg-indigo-900/30 transition-colors"
>
<Plus className="w-4 h-4" />
</button>
</div>
);
}
return (
<div className={`w-60 shrink-0 flex flex-col gap-3 ${fill ? "min-h-0" : ""}`}>
{/* Header */}
<div className="flex items-center justify-between">
<span className="text-xs font-semibold uppercase tracking-wider text-gray-400 dark:text-gray-500">
Conversations
</span>
<button
onClick={() => setCollapsed(true)}
title="Collapse sidebar"
className="p-1 rounded-md text-gray-400 hover:text-gray-600 dark:hover:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-800 transition-colors"
>
<ChevronLeft className="w-3.5 h-3.5" />
</button>
</div>
{/* New chat button */}
<button
onClick={onNew}
className="flex items-center gap-2 w-full px-3 py-2 rounded-lg text-sm font-medium bg-indigo-500 hover:bg-indigo-600 text-white transition-colors shadow-sm"
>
<Plus className="w-4 h-4 shrink-0" />
New chat
</button>
{/* Search */}
{searchBox}
{/* List */}
<div
className="flex-1 min-h-0 overflow-y-auto overflow-x-hidden space-y-0.5"
style={fill ? undefined : { maxHeight: "60vh" }}
>
{renderList(onSelect)}
</div>
</div>
);
};
export default ConversationSidebar;
|