123 lines
4.6 KiB
TypeScript
123 lines
4.6 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useState } from "react";
|
|
import { usePathname, useRouter } from "next/navigation";
|
|
import { ChevronDown } from "lucide-react";
|
|
import { cn } from "@/lib/utils";
|
|
import {
|
|
SidebarGroup,
|
|
SidebarGroupContent,
|
|
SidebarGroupLabel,
|
|
SidebarMenu,
|
|
SidebarMenuItem,
|
|
SidebarMenuButton,
|
|
} from "@/components/ui/sidebar";
|
|
import {
|
|
formatRelative,
|
|
STATUS_LABELS,
|
|
} from "@/open-canvas/components/ai-writing/AIWritingHistoryDropdown";
|
|
import { useAIWritingSessions } from "@/open-canvas/hooks/useAIWritingSessions";
|
|
import type { AIWritingSessionSummary } from "@/open-canvas/api/ai-writing-sessions";
|
|
|
|
const AI_WRITING_PATH = "/page/workspace/ai-writing";
|
|
|
|
function openSession(
|
|
session: AIWritingSessionSummary,
|
|
isOnPage: boolean,
|
|
router: ReturnType<typeof useRouter>,
|
|
) {
|
|
if (isOnPage) {
|
|
// 已在 AI 写作页面,直接通过自定义事件通知面板
|
|
window.dispatchEvent(
|
|
new CustomEvent("ai-writing:open-session", { detail: session }),
|
|
);
|
|
} else {
|
|
// 不在该页面,把 session 存入 sessionStorage 后跳转
|
|
sessionStorage.setItem("ai-writing:pending-session", JSON.stringify(session));
|
|
router.push(AI_WRITING_PATH);
|
|
}
|
|
}
|
|
|
|
export function RecentWritingList() {
|
|
const router = useRouter();
|
|
const pathname = usePathname();
|
|
const { sessions, loading, refresh } = useAIWritingSessions();
|
|
const [collapsed, setCollapsed] = useState(
|
|
() => localStorage.getItem("sidebar:recent-writing-collapsed") === "true",
|
|
);
|
|
|
|
const toggleCollapsed = () => {
|
|
const next = !collapsed;
|
|
setCollapsed(next);
|
|
localStorage.setItem("sidebar:recent-writing-collapsed", String(next));
|
|
};
|
|
|
|
useEffect(() => {
|
|
void refresh();
|
|
}, [refresh]);
|
|
|
|
if (!loading && sessions.length === 0) return null;
|
|
|
|
const isOnPage = pathname === AI_WRITING_PATH;
|
|
|
|
return (
|
|
<SidebarGroup className="px-2 pt-3 mt-0 border-t border-sidebar-border/60">
|
|
<SidebarGroupLabel
|
|
className="text-muted-foreground/70 px-3 mb-1 text-[10px] font-semibold tracking-[0.12em] uppercase cursor-pointer select-none flex items-center justify-between hover:text-muted-foreground transition-colors"
|
|
onClick={toggleCollapsed}
|
|
>
|
|
<span>最近写作</span>
|
|
<ChevronDown
|
|
className={`w-3 h-3 shrink-0 transition-transform duration-200 ${collapsed ? "-rotate-90" : ""}`}
|
|
/>
|
|
</SidebarGroupLabel>
|
|
<SidebarGroupContent
|
|
className={`group-data-[collapsible=icon]:pointer-events-none group-data-[collapsible=icon]:-mt-8 group-data-[collapsible=icon]:opacity-0 overflow-hidden transition-all duration-200 ${
|
|
collapsed ? "max-h-0 opacity-0 pointer-events-none" : "max-h-[600px] opacity-100"
|
|
}`}
|
|
>
|
|
<SidebarMenu>
|
|
<div className="flex w-full flex-col gap-0.5">
|
|
{loading && sessions.length === 0 ? (
|
|
<p className="px-3 py-1 text-[12px] text-muted-foreground">加载中…</p>
|
|
) : (
|
|
sessions.slice(0, 10).map((session) => {
|
|
const { label, color } = STATUS_LABELS[session.status] ?? {
|
|
label: session.status,
|
|
color: "text-muted-foreground",
|
|
};
|
|
return (
|
|
<SidebarMenuItem key={session.id} className="group/side-menu-item">
|
|
<SidebarMenuButton
|
|
className={
|
|
"rounded-lg h-auto py-1.5 px-3 text-muted-foreground " +
|
|
"hover:text-foreground hover:bg-sidebar-accent/60"
|
|
}
|
|
onClick={() => openSession(session, isOnPage, router)}
|
|
>
|
|
<div className="flex flex-col items-start min-w-0 w-full">
|
|
<span className="text-[13px] truncate w-full leading-snug">
|
|
{session.title}
|
|
</span>
|
|
<div className="flex items-center gap-1 mt-0.5">
|
|
<span className={cn("text-[10px] font-medium", color)}>
|
|
{label}
|
|
</span>
|
|
<span className="text-[10px] text-muted-foreground">·</span>
|
|
<span className="text-[10px] text-muted-foreground">
|
|
{formatRelative(session.updated_at)}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
</SidebarMenuButton>
|
|
</SidebarMenuItem>
|
|
);
|
|
})
|
|
)}
|
|
</div>
|
|
</SidebarMenu>
|
|
</SidebarGroupContent>
|
|
</SidebarGroup>
|
|
);
|
|
}
|