Spaces:
Running
Running
File size: 10,932 Bytes
c0ddd13 e286e6f c0ddd13 | 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 | import { useState } from "react";
import { motion, AnimatePresence } from "motion/react";
import {
Plus,
MessageSquare,
Trash2,
ChevronLeft,
ChevronRight,
Settings,
LogOut,
} from "lucide-react";
const maintivalogoUrl = "/maintiva-logo.png";
import type { ChatSession, StoredUser } from "./types";
interface SidebarProps {
sessions: ChatSession[];
activeSessionId: string | null;
isLoadingSessions: boolean;
user: StoredUser | null;
onNewChat: () => void;
onSelectSession: (id: string) => void;
onDeleteSession: (id: string) => void;
onLogout: () => void;
mobileOpen: boolean;
onMobileClose: () => void;
}
function formatTime(dateStr: string | null | undefined): string {
if (!dateStr) return "";
const d = new Date(dateStr);
const now = new Date();
const diffMs = now.getTime() - d.getTime();
const diffDays = Math.floor(diffMs / 86400000);
if (diffDays === 0) {
return d.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" });
}
if (diffDays === 1) return "Yesterday";
if (diffDays < 7) return `${diffDays}d ago`;
return d.toLocaleDateString([], { month: "short", day: "numeric" });
}
export default function Sidebar({
sessions,
activeSessionId,
isLoadingSessions,
user,
onNewChat,
onSelectSession,
onDeleteSession,
onLogout,
mobileOpen,
onMobileClose,
}: SidebarProps) {
const [collapsed, setCollapsed] = useState(false);
const [hoveredSession, setHoveredSession] = useState<string | null>(null);
return (
<>
{/* Mobile backdrop */}
<AnimatePresence>
{mobileOpen && (
<motion.div
className="fixed inset-0 bg-black/40 z-40 md:hidden"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
transition={{ duration: 0.2 }}
onClick={onMobileClose}
/>
)}
</AnimatePresence>
<motion.div
className={[
"flex flex-col h-full flex-shrink-0 bg-white border-r border-neutral-100 overflow-hidden",
// Mobile: fixed overlay drawer; desktop: in-flow
"fixed inset-y-0 left-0 z-50 md:relative md:inset-auto md:z-auto",
// Mobile slide in/out; always visible on desktop
mobileOpen ? "translate-x-0" : "-translate-x-full",
"md:translate-x-0",
// CSS transition for mobile only; framer-motion handles desktop width
"transition-transform duration-300 ease-in-out md:transition-none",
// Never overflow phone screen
"max-w-[85vw] md:max-w-none",
].join(" ")}
animate={{ width: collapsed ? 72 : 280 }}
transition={{ duration: 0.3, ease: "easeInOut" }}
>
{/* Collapse toggle — desktop only */}
<button
onClick={() => setCollapsed((v) => !v)}
className="hidden md:flex absolute -right-3 top-1/2 -translate-y-1/2 z-20 w-6 h-6 rounded-full bg-white border border-neutral-200 shadow-sm text-neutral-500 hover:text-brand-green hover:border-brand-green transition-all items-center justify-center"
aria-label={collapsed ? "Expand sidebar" : "Collapse sidebar"}
>
{collapsed ? (
<ChevronRight className="h-3.5 w-3.5" />
) : (
<ChevronLeft className="h-3.5 w-3.5" />
)}
</button>
{/* Header */}
<div className="flex items-center gap-3 px-4 py-4 border-b border-neutral-100">
<motion.div
className="w-9 h-9 rounded-xl overflow-hidden flex-shrink-0 shadow-md"
whileHover={{ scale: 1.05 }}
>
<img src={maintivalogoUrl} alt="Maintiva" className="w-full h-full object-cover" />
</motion.div>
<AnimatePresence>
{!collapsed && (
<motion.div
initial={{ opacity: 0, width: 0 }}
animate={{ opacity: 1, width: "auto" }}
exit={{ opacity: 0, width: 0 }}
transition={{ duration: 0.2 }}
className="overflow-hidden"
>
<p className="font-bold text-neutral-900 text-base whitespace-nowrap">Maintiva Agent</p>
<p className="text-xs text-neutral-400 whitespace-nowrap">AI Data Assistant</p>
</motion.div>
)}
</AnimatePresence>
</div>
{/* New Chat button */}
<div className="px-3 py-3">
<button
onClick={() => { onNewChat(); onMobileClose(); }}
className={`w-full flex items-center gap-3 rounded-xl px-3 py-2.5 bg-gradient-to-br from-brand-green-light to-brand-green text-white text-sm font-semibold shadow-md shadow-brand-green/20 hover:shadow-lg hover:shadow-brand-green/25 hover:brightness-105 transition-all ${
collapsed ? "justify-center px-0" : ""
}`}
>
<Plus className="h-4 w-4 flex-shrink-0" />
<AnimatePresence>
{!collapsed && (
<motion.span
initial={{ opacity: 0, width: 0 }}
animate={{ opacity: 1, width: "auto" }}
exit={{ opacity: 0, width: 0 }}
transition={{ duration: 0.2 }}
className="overflow-hidden whitespace-nowrap"
>
New Chat
</motion.span>
)}
</AnimatePresence>
</button>
</div>
{/* Sessions */}
<div className="flex-1 overflow-y-auto">
{!collapsed && sessions.length > 0 && (
<p className="text-xs font-semibold text-neutral-400 uppercase tracking-wider px-4 pt-2 pb-1">
Recent
</p>
)}
{isLoadingSessions ? (
<div className="flex justify-center py-4">
<div className="w-4 h-4 rounded-full border-2 border-brand-green border-t-transparent animate-spin" />
</div>
) : (
<div className="px-2 space-y-0.5">
{sessions.map((session) => {
const isActive = session.id === activeSessionId;
return (
<div
key={session.id}
className={`relative flex items-center gap-3 rounded-xl px-3 py-2.5 cursor-pointer transition-all duration-150 ${
isActive
? "bg-brand-green-50 text-brand-green"
: "text-neutral-700 hover:bg-neutral-50"
}`}
onClick={() => { onSelectSession(session.id); onMobileClose(); }}
onMouseEnter={() => setHoveredSession(session.id)}
onMouseLeave={() => setHoveredSession(null)}
>
<MessageSquare
className={`h-4 w-4 flex-shrink-0 ${
isActive ? "text-brand-green" : "text-neutral-400"
}`}
/>
<AnimatePresence>
{!collapsed && (
<motion.div
initial={{ opacity: 0, width: 0 }}
animate={{ opacity: 1, width: "auto" }}
exit={{ opacity: 0, width: 0 }}
transition={{ duration: 0.2 }}
className="flex-1 min-w-0 overflow-hidden"
>
<p className="text-sm font-medium truncate">{session.title}</p>
<p className="text-xs text-neutral-400 truncate">
{formatTime(session.updatedAt ?? session.createdAt)}
</p>
</motion.div>
)}
</AnimatePresence>
{!collapsed && hoveredSession === session.id && (
<button
onClick={(e) => {
e.stopPropagation();
onDeleteSession(session.id);
}}
className="absolute right-2 top-1/2 -translate-y-1/2 p-1.5 rounded-lg text-neutral-400 hover:text-red-500 hover:bg-red-50 transition-all"
aria-label="Delete session"
>
<Trash2 className="h-3.5 w-3.5" />
</button>
)}
</div>
);
})}
</div>
)}
</div>
{/* Footer */}
<div className="border-t border-neutral-100 p-3">
<div className="flex items-center gap-3 rounded-xl px-2 py-2">
<div className="h-7 w-7 rounded-full bg-gradient-to-br from-brand-green-light to-brand-green flex items-center justify-center flex-shrink-0">
<span className="text-xs font-bold text-white">
{user?.name?.[0]?.toUpperCase() ?? "U"}
</span>
</div>
<AnimatePresence>
{!collapsed && (
<motion.div
initial={{ opacity: 0, width: 0 }}
animate={{ opacity: 1, width: "auto" }}
exit={{ opacity: 0, width: 0 }}
transition={{ duration: 0.2 }}
className="flex-1 min-w-0 overflow-hidden"
>
<p className="text-sm font-semibold text-neutral-900 truncate whitespace-nowrap">
{user?.name ?? "User"}
</p>
<p className="text-xs text-neutral-400 truncate whitespace-nowrap">
{user?.email ?? ""}
</p>
</motion.div>
)}
</AnimatePresence>
{!collapsed && (
<div className="flex items-center gap-0.5">
<button
className="p-1.5 rounded-lg text-neutral-400 hover:text-brand-green hover:bg-brand-green-50 transition-all"
aria-label="Settings"
>
<Settings className="h-4 w-4" />
</button>
<button
onClick={onLogout}
className="p-1.5 rounded-lg text-neutral-400 hover:text-red-500 hover:bg-red-50 transition-all"
aria-label="Logout"
>
<LogOut className="h-4 w-4" />
</button>
</div>
)}
{collapsed && (
<button
onClick={onLogout}
className="p-1.5 rounded-lg text-neutral-400 hover:text-red-500 hover:bg-red-50 transition-all"
aria-label="Logout"
>
<LogOut className="h-4 w-4" />
</button>
)}
</div>
</div>
</motion.div>
</>
);
}
|