SessionSidebar.tsx 4.52 KB
"use client";

import { useState } from "react";
import { Loader2, MessageSquare, SquarePen, Trash2 } from "lucide-react";

import { cn } from "@/lib/utils";
import type { SessionItem } from "@/lib/auth";

interface SessionSidebarProps {
  sessions: SessionItem[];
  currentId: string | null;
  loading?: boolean;
  creating?: boolean;
  onSelect: (id: string) => void;
  onCreate: () => void;
  onDelete: (session: SessionItem) => void;
}

/** 相对时间展示,避免侧栏出现冗长的绝对时间戳。 */
function relativeTime(iso: string | null): string {
  if (!iso) return "";
  const then = new Date(iso).getTime();
  if (Number.isNaN(then)) return "";

  const diff = Date.now() - then;
  const minute = 60_000;
  const hour = 60 * minute;
  const day = 24 * hour;

  if (diff < minute) return "刚刚";
  if (diff < hour) return `${Math.floor(diff / minute)} 分钟前`;
  if (diff < day) return `${Math.floor(diff / hour)} 小时前`;
  if (diff < 2 * day) return "昨天";
  if (diff < 7 * day) return `${Math.floor(diff / day)} 天前`;
  return new Date(iso).toLocaleDateString("zh-CN", { month: "numeric", day: "numeric" });
}

export default function SessionSidebar({
  sessions,
  currentId,
  loading = false,
  creating = false,
  onSelect,
  onCreate,
  onDelete,
}: SessionSidebarProps) {
  const [hoveredId, setHoveredId] = useState<string | null>(null);

  return (
    <aside className="w-[220px] shrink-0 bg-slate-50/80 border-r border-slate-200 flex flex-col">
      <div className="p-3 border-b border-slate-200">
        <button
          onClick={onCreate}
          disabled={creating}
          className="w-full flex items-center justify-center gap-1.5 bg-blue-600 hover:bg-blue-700 disabled:bg-blue-400 disabled:cursor-wait text-white text-xs font-semibold py-2 rounded-lg transition"
        >
          {creating ? (
            <>
              <Loader2 className="w-3.5 h-3.5 animate-spin" />
              新建中…
            </>
          ) : (
            <>
              <SquarePen className="w-3.5 h-3.5" />
              新建会话
            </>
          )}
        </button>
      </div>

      <div className="flex-1 overflow-y-auto scrollbar-thin p-2 space-y-0.5">
        {loading && (
          <div className="text-[11px] text-slate-400 text-center py-4">加载中…</div>
        )}

        {!loading && sessions.length === 0 && (
          <div className="text-center py-8 px-3">
            <MessageSquare className="w-8 h-8 mx-auto text-slate-300 mb-2" />
            <p className="text-[11px] text-slate-400 leading-relaxed">
              还没有会话
              <br />
              点上方按钮开始提问
            </p>
          </div>
        )}

        {sessions.map((session) => {
          const active = session.id === currentId;
          return (
            <div
              key={session.id}
              onMouseEnter={() => setHoveredId(session.id)}
              onMouseLeave={() => setHoveredId(null)}
              onClick={() => onSelect(session.id)}
              className={cn(
                "group relative px-2.5 py-2 rounded-lg cursor-pointer transition",
                active ? "bg-blue-50 text-blue-700" : "hover:bg-slate-200/60 text-slate-700"
              )}
            >
              <div className="flex items-center gap-1.5 pr-5">
                <MessageSquare
                  className={cn("w-3.5 h-3.5 shrink-0", active ? "text-blue-600" : "text-slate-400")}
                />
                <span className="text-xs font-medium truncate">{session.title}</span>
              </div>
              <div className="flex items-center gap-2 mt-0.5 pl-5">
                <span className="text-[10px] text-slate-400">{relativeTime(session.updatedAt)}</span>
                {session.totalTurns > 0 && (
                  <span className="text-[10px] text-slate-400">{session.totalTurns} 轮</span>
                )}
              </div>

              {/* 删除图标平时隐藏,hover 才浮出,避免误触 */}
              {hoveredId === session.id && (
                <button
                  onClick={(e) => {
                    e.stopPropagation();
                    onDelete(session);
                  }}
                  title="删除会话"
                  className="absolute right-1.5 top-1/2 -translate-y-1/2 p-1 rounded-md text-slate-400 hover:text-red-600 hover:bg-white transition"
                >
                  <Trash2 className="w-3.5 h-3.5" />
                </button>
              )}
            </div>
          );
        })}
      </div>
    </aside>
  );
}