backend / frontend /src /components /chat /ChatSidebar.jsx
muhammadhasnain100's picture
add code
49f22ac
Raw History Blame Contribute Delete
7.84 kB
import ComsatsLogo from '../ComsatsLogo'
function PlusIcon({ className = '' }) {
return (
<svg className={className} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" aria-hidden>
<path strokeLinecap="round" strokeLinejoin="round" d="M12 4v16m8-8H4" />
</svg>
)
}
function ChevronLeftIcon({ className = '' }) {
return (
<svg className={className} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" aria-hidden>
<path strokeLinecap="round" strokeLinejoin="round" d="M15 19l-7-7 7-7" />
</svg>
)
}
function UserCircleIcon({ className = '' }) {
return (
<svg className={className} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.75" aria-hidden>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M15.75 6a3.75 3.75 0 11-7.5 0 3.75 3.75 0 017.5 0zM4.501 20.118a7.5 7.5 0 0114.998 0A17.933 17.933 0 0112 21.75c-2.676 0-5.216-.584-7.499-1.632z"
/>
</svg>
)
}
function LogoutIcon({ className = '' }) {
return (
<svg className={className} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.75" aria-hidden>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M15.75 9V5.25A2.25 2.25 0 0013.5 3h-6a2.25 2.25 0 00-2.25 2.25v13.5A2.25 2.25 0 007.5 21h6a2.25 2.25 0 002.25-2.25V15m3 0l3-3m0 0l-3-3m3 3H9"
/>
</svg>
)
}
export default function ChatSidebar({
user,
chats,
selectedId,
onSelectChat,
onNewChat,
onDeleteChat,
onEditProfile,
onLogout,
onCloseSidebar,
loadingList,
hasMoreChats,
loadingMoreChats,
onLoadMoreChats,
chatListTotal,
}) {
return (
<aside className="relative flex h-full min-h-0 w-full min-w-0 flex-col bg-chat-sidebar">
<div className="shrink-0 border-b border-slate-200/60 bg-gradient-to-br from-white via-chat-sidebar to-comsats-purple/[0.04] px-3 py-3.5 md:px-3.5">
<div className="flex items-start gap-2">
<div className="flex min-w-0 flex-1 items-center gap-3">
<ComsatsLogo className="h-10 w-10 shrink-0 shadow-sm ring-2 ring-white md:h-11 md:w-11" />
<div className="min-w-0 flex-1">
<p className="truncate text-[14px] font-bold tracking-tight text-comsats-purple">
CUI Connect
</p>
<p className="truncate text-[11px] leading-tight text-slate-500">{user?.email}</p>
</div>
</div>
<button
type="button"
onClick={onCloseSidebar}
aria-label="Close sidebar"
title="Close sidebar"
className="flex h-9 w-9 shrink-0 items-center justify-center rounded-xl border border-slate-200/80 bg-white/90 text-slate-600 shadow-sm transition-colors hover:border-comsats-purple/25 hover:bg-white hover:text-comsats-purple"
>
<ChevronLeftIcon className="h-[18px] w-[18px]" />
</button>
</div>
</div>
<div className="shrink-0 px-2.5 pb-2 pt-3 md:px-3">
<button
type="button"
onClick={onNewChat}
className="flex w-full items-center justify-center gap-2 rounded-xl bg-gradient-to-r from-comsats-purple to-[#3d2469] px-3 py-2.5 text-[13px] font-semibold text-white shadow-md shadow-comsats-purple/25 transition-all hover:brightness-110 hover:shadow-lg active:scale-[0.99]"
>
<PlusIcon className="h-4 w-4 opacity-95" />
New chat
</button>
</div>
<div className="min-h-0 flex-1 overflow-y-auto overflow-x-hidden px-2 pb-2 md:px-2.5">
<p className="flex items-center justify-between px-1.5 pb-2 pt-1 text-[11px] font-semibold uppercase tracking-[0.08em] text-slate-400">
<span>Your chats</span>
{chatListTotal > 0 ? (
<span className="font-medium normal-case tracking-normal text-slate-400 tabular-nums">
{chats.length}/{chatListTotal}
</span>
) : null}
</p>
{loadingList ? (
<p className="px-2 py-3 text-[13px] text-slate-500">Loading…</p>
) : chats.length === 0 ? (
<div className="mx-2 rounded-xl border border-dashed border-slate-200/90 bg-white/50 px-3 py-6 text-center">
<p className="text-[13px] leading-relaxed text-slate-500">No chats yet. Start one above.</p>
</div>
) : (
<ul className="flex flex-col gap-1">
{chats.map((c) => {
const active = selectedId === c.chat_id
return (
<li key={c.chat_id}>
<div
className={`group flex items-stretch overflow-hidden rounded-xl border transition-all ${
active
? 'border-comsats-purple/25 bg-white shadow-[0_4px_14px_-6px_rgba(75,46,131,0.35)] ring-1 ring-comsats-purple/15'
: 'border-transparent hover:border-slate-200/80 hover:bg-white/70'
}`}
>
<button
type="button"
onClick={() => onSelectChat(c.chat_id)}
className={`min-w-0 flex-1 border-l-[3px] py-2.5 pl-3 pr-2 text-left ${
active ? 'border-l-comsats-purple' : 'border-l-transparent'
}`}
>
<span
className={`line-clamp-2 text-[13px] leading-snug ${
active ? 'font-semibold text-slate-900' : 'font-medium text-slate-700'
}`}
>
{c.preview}
</span>
<span className="mt-1 block text-[11px] text-slate-400">
{c.message_count ? `${c.message_count} messages` : 'Empty'}
</span>
</button>
<button
type="button"
title="Delete chat"
onClick={(e) => {
e.stopPropagation()
onDeleteChat(c.chat_id)
}}
className="shrink-0 px-2.5 text-slate-400 opacity-0 transition-opacity hover:text-red-600 group-hover:opacity-100"
>
×
</button>
</div>
</li>
)
})}
</ul>
)}
{hasMoreChats ? (
<div className="px-1 pt-3">
<button
type="button"
disabled={loadingMoreChats || loadingList}
onClick={onLoadMoreChats}
className="w-full rounded-xl py-2.5 text-[12px] font-semibold text-comsats-purple hover:bg-white/70 disabled:opacity-50"
>
{loadingMoreChats ? 'Loading…' : 'Load more chats'}
</button>
</div>
) : null}
</div>
<div className="shrink-0 border-t border-slate-200/60 bg-white/40 p-2 md:p-2.5">
<button
type="button"
onClick={onEditProfile}
className="flex w-full items-center gap-2.5 rounded-xl px-3 py-2.5 text-left text-[13px] font-medium text-slate-700 transition-colors hover:bg-white"
>
<UserCircleIcon className="h-5 w-5 shrink-0 text-slate-500" />
Edit profile
</button>
<button
type="button"
onClick={onLogout}
className="flex w-full items-center gap-2.5 rounded-xl px-3 py-2.5 text-left text-[13px] font-medium text-red-600 transition-colors hover:bg-red-50"
>
<LogoutIcon className="h-5 w-5 shrink-0 opacity-90" />
Log out
</button>
</div>
</aside>
)
}