feat(ai-writing): 课题研究笔记本内嵌打开 + 新建/所有空间入口(0616 #5)
- 左栏点笔记本/所有空间 → 右侧内嵌打开(不跳路由),写作布局保持挂载(display:none,不丢 setup 线程),顶部「返回写作」 - ResearchSidebar 加「新建空间」(弹窗 createSearchSpace) 与「所有空间」入口 - StudioNotebookDetailPage 加可选 notebookId prop;StudioNotebooksPage 加可选 onOpenNotebook(内嵌时卡片走回调而非 Link),列表内点空间内嵌打开详情 - 内嵌笔记本详情用 ChatRuntime 包裹(修复 useArtifacts must be used within an ArtifactsProvider) Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
f39cffcb9a
commit
0e4caa5a66
4 files changed
+243
-47
No files matched your search
@@ -1,8 +1,18 @@
|
||||
"use client";
|
||||
|
||||
import { BookOpen, FileText } from "lucide-react";
|
||||
import { useEffect } from "react";
|
||||
import { BookOpen, FileText, LayoutGrid, Loader2, Plus } from "lucide-react";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useQueryClient } from "@tanstack/react-query";
|
||||
|
||||
import { Button } from "@/components/ui/button";
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from "@/components/ui/dialog";
|
||||
import { getStoredAuth } from "@/core/auth";
|
||||
import { createSearchSpace } from "@/core/studio/api";
|
||||
import { useStudioNotebooks } from "@/core/studio/hooks";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
@@ -16,38 +26,64 @@ import { useWritingForm } from "../../contexts/WritingFormContext";
|
||||
* - 下半「笔记本」= 我的空间(``useStudioNotebooks``);点击设为当前课题研究的素材来源
|
||||
* (materialSource='notebook' + 选中该本)。
|
||||
*/
|
||||
export function ResearchSidebar() {
|
||||
export function ResearchSidebar({
|
||||
viewingNotebookId,
|
||||
onOpenNotebook,
|
||||
onOpenAllSpaces,
|
||||
}: {
|
||||
/** 当前在右侧查看的笔记本 id(高亮用)。 */
|
||||
viewingNotebookId?: number | null;
|
||||
/** 点击笔记本 → 在右侧内嵌打开该笔记本页(替换右侧内容)。 */
|
||||
onOpenNotebook: (notebookId: number) => void;
|
||||
/** 点击「所有空间」→ 在右侧内嵌打开空间列表页。 */
|
||||
onOpenAllSpaces: () => void;
|
||||
}) {
|
||||
const { articleTypes, refresh } = useArticleTypes();
|
||||
const { notebooks } = useStudioNotebooks();
|
||||
const {
|
||||
articleType,
|
||||
setArticleType,
|
||||
setWordCountInput,
|
||||
setFormExpanded,
|
||||
materialSource,
|
||||
setMaterialSource,
|
||||
selectedNotebookId,
|
||||
setSelectedNotebookId,
|
||||
setSelectedDocumentIds,
|
||||
} = useWritingForm();
|
||||
notebooks,
|
||||
isLoading: notebooksLoading,
|
||||
isError: notebooksError,
|
||||
} = useStudioNotebooks();
|
||||
const { articleType, setArticleType, setWordCountInput, setFormExpanded } =
|
||||
useWritingForm();
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
// 新建空间弹窗(与 workspace-nav-studio 同款)。
|
||||
const [createOpen, setCreateOpen] = useState(false);
|
||||
const [name, setName] = useState("");
|
||||
const [creating, setCreating] = useState(false);
|
||||
const [createError, setCreateError] = useState("");
|
||||
|
||||
useEffect(() => {
|
||||
void refresh();
|
||||
}, [refresh]);
|
||||
|
||||
const handleCreate = async () => {
|
||||
const trimmed = name.trim();
|
||||
if (!trimmed) return;
|
||||
setCreating(true);
|
||||
setCreateError("");
|
||||
try {
|
||||
const email = getStoredAuth()?.email ?? "";
|
||||
const token = queryClient.getQueryData<string>(["studio", "token", email]);
|
||||
if (!token) throw new Error("未登录或 token 已过期,请刷新页面");
|
||||
await createSearchSpace(trimmed, token);
|
||||
setCreateOpen(false);
|
||||
setName("");
|
||||
await queryClient.invalidateQueries({ queryKey: ["studio", "notebooks"] });
|
||||
} catch (e) {
|
||||
setCreateError(e instanceof Error ? e.message : "创建失败");
|
||||
} finally {
|
||||
setCreating(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleTemplateClick = (key: string, defaultWordCount: number) => {
|
||||
setArticleType(key);
|
||||
if (defaultWordCount > 0) setWordCountInput(String(defaultWordCount));
|
||||
setFormExpanded(true);
|
||||
};
|
||||
|
||||
const handleNotebookClick = (notebookId: number) => {
|
||||
setMaterialSource("notebook");
|
||||
setSelectedNotebookId(notebookId);
|
||||
setSelectedDocumentIds([]);
|
||||
setFormExpanded(true);
|
||||
};
|
||||
|
||||
return (
|
||||
<aside className="flex h-full w-64 shrink-0 flex-col overflow-hidden border-r border-border/70 bg-card/60 backdrop-blur-sm">
|
||||
{/* 上半:研究模板(文章类型) */}
|
||||
@@ -95,18 +131,37 @@ export function ResearchSidebar() {
|
||||
<h2 className="text-muted-foreground/70 px-2 mb-1.5 text-[10px] font-semibold tracking-[0.12em] uppercase select-none">
|
||||
笔记本
|
||||
</h2>
|
||||
{notebooks.length === 0 ? (
|
||||
<p className="px-2 py-1.5 text-xs text-muted-foreground/70">暂无笔记本</p>
|
||||
) : (
|
||||
<div className="flex flex-col gap-0.5">
|
||||
{notebooks.map((nb) => {
|
||||
const active =
|
||||
materialSource === "notebook" && selectedNotebookId === nb.id;
|
||||
<div className="flex flex-col gap-0.5">
|
||||
{/* 新建空间 */}
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => {
|
||||
setName("");
|
||||
setCreateError("");
|
||||
setCreateOpen(true);
|
||||
}}
|
||||
className="flex items-center gap-2 rounded-lg px-2.5 py-1.5 text-left text-[13px] text-muted-foreground transition-colors hover:bg-sidebar-accent/60 hover:text-foreground"
|
||||
>
|
||||
<Plus className="size-3.5 shrink-0 text-muted-foreground/70" />
|
||||
<span>新建空间</span>
|
||||
</button>
|
||||
|
||||
{notebooksLoading ? (
|
||||
<p className="px-2 py-1.5 text-xs text-muted-foreground/70">加载中…</p>
|
||||
) : notebooksError ? (
|
||||
<p className="px-2 py-1.5 text-xs text-amber-600">
|
||||
笔记本服务暂不可用(检查空间服务地址 / 账号)
|
||||
</p>
|
||||
) : notebooks.length === 0 ? (
|
||||
<p className="px-2 py-1.5 text-xs text-muted-foreground/70">暂无笔记本</p>
|
||||
) : (
|
||||
notebooks.map((nb) => {
|
||||
const active = viewingNotebookId === nb.id;
|
||||
return (
|
||||
<button
|
||||
key={nb.id}
|
||||
type="button"
|
||||
onClick={() => handleNotebookClick(nb.id)}
|
||||
onClick={() => onOpenNotebook(nb.id)}
|
||||
title={nb.description || nb.name}
|
||||
className={cn(
|
||||
"flex items-center gap-2 rounded-lg px-2.5 py-1.5 text-left text-[13px] transition-colors",
|
||||
@@ -119,10 +174,60 @@ export function ResearchSidebar() {
|
||||
<span className="truncate">{nb.name}</span>
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
)}
|
||||
})
|
||||
)}
|
||||
|
||||
{/* 所有空间 */}
|
||||
<button
|
||||
type="button"
|
||||
onClick={onOpenAllSpaces}
|
||||
className="flex items-center gap-2 rounded-lg px-2.5 py-1.5 text-left text-[13px] text-muted-foreground transition-colors hover:bg-sidebar-accent/60 hover:text-foreground"
|
||||
>
|
||||
<LayoutGrid className="size-3.5 shrink-0 text-muted-foreground/70" />
|
||||
<span>所有空间</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 新建空间弹窗 */}
|
||||
<Dialog open={createOpen} onOpenChange={(v) => !creating && setCreateOpen(v)}>
|
||||
<DialogContent className="sm:max-w-md">
|
||||
<DialogHeader>
|
||||
<DialogTitle>新建空间</DialogTitle>
|
||||
</DialogHeader>
|
||||
<div className="flex flex-col gap-4 pt-2">
|
||||
<input
|
||||
autoFocus
|
||||
value={name}
|
||||
onChange={(e) => setName(e.target.value)}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === "Enter") void handleCreate();
|
||||
}}
|
||||
placeholder="空间名称"
|
||||
disabled={creating}
|
||||
className="w-full rounded-lg border border-border bg-background px-3 py-2 text-sm outline-none focus:ring-2 focus:ring-primary disabled:opacity-50"
|
||||
/>
|
||||
{createError && <p className="text-xs text-destructive">{createError}</p>}
|
||||
<div className="flex justify-end gap-2">
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
disabled={creating}
|
||||
onClick={() => setCreateOpen(false)}
|
||||
>
|
||||
取消
|
||||
</Button>
|
||||
<Button
|
||||
size="sm"
|
||||
disabled={!name.trim() || creating}
|
||||
onClick={() => void handleCreate()}
|
||||
>
|
||||
{creating ? <Loader2 className="size-4 animate-spin" /> : "创建"}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
</aside>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user