Files
deerflow-code/frontend-web/src/pages/PublicSharePage.tsx
AdministratorandClaude Opus 4.8 da677f87d5 feat: 公开页背景色支持 iframe 外部配置 + CM助手旁排行榜入口
- 新增 usePublicBackground:公开页(排行榜/分享/会商分享/计划任务)支持
  嵌套方通过 ?bg=/?text= 查询参数或 postMessage 覆盖背景/文字色
- Header 左上角 CM助手 旁新增「排行榜」按钮,新标签打开公开排行榜页

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-12 12:21:25 +08:00

158 lines
5.1 KiB
TypeScript

"use client";
import type { BaseStream } from "@langchain/langgraph-sdk/react";
import { useQuery } from "@tanstack/react-query";
import { Loader2 } from "lucide-react";
import { useEffect, useMemo } from "react";
import { useParams } from "react-router-dom";
import {
ArtifactFileDetail,
ArtifactsProvider,
useArtifacts,
} from "@/components/workspace/artifacts";
import { MessageList } from "@/components/workspace/messages";
import { ThreadContext } from "@/components/workspace/messages/context";
import { SubtasksProvider } from "@/core/tasks/context";
import { usePublicBackground } from "@/core/embed/background";
import { getPublicShareSnapshot } from "@/core/thread-shares/api";
import type { AgentThreadState } from "@/core/threads";
/**
* Conversation + artifact split shown inside the share's providers.
*
* The public page has no workspace layout, so it renders its own artifact
* panel: clicking a file in the conversation selects it via `ArtifactsProvider`
* and the detail view appears on the right.
*/
function PublicShareBody({
thread,
shareCode,
artifacts,
}: {
thread: BaseStream<AgentThreadState>;
shareCode?: string;
artifacts: string[];
}) {
const { selectedArtifact, open, setArtifacts } = useArtifacts();
const threadId = shareCode ?? "public-share";
// Feed the snapshot's artifact list to the provider so the detail view's
// file switcher works.
useEffect(() => {
setArtifacts(artifacts);
}, [artifacts, setArtifacts]);
const showArtifact = open && !!selectedArtifact;
return (
<div className="flex size-full min-h-0">
<div className="min-h-0 min-w-0 flex-1">
<MessageList threadId={threadId} thread={thread} paddingBottom={48} />
</div>
{showArtifact && (
<div className="flex min-h-0 w-1/2 min-w-0 shrink-0 flex-col border-l bg-[#fffdfa] p-4">
<ArtifactFileDetail
className="size-full"
filepath={selectedArtifact}
threadId={threadId}
/>
</div>
)}
</div>
);
}
/**
* Public, read-only conversation viewer.
*
* Reachable at `#/share/<code>` with NO authentication — anyone with the link
* can open it. It renders a frozen snapshot of a `view`-mode shared
* conversation; there is no input box, so viewers cannot send new messages.
*/
export default function PublicSharePage() {
const { shareCode } = useParams<{ shareCode: string }>();
// 嵌套方可用 ?bg=/?text= 或 postMessage 覆盖背景/文字色。
const pageBg = usePublicBackground();
const { data, isLoading, isError } = useQuery({
queryKey: ["public-share", shareCode],
queryFn: () => getPublicShareSnapshot(shareCode!),
enabled: !!shareCode,
retry: false,
refetchOnWindowFocus: false,
});
// A minimal static stand-in for the live LangGraph stream. MessageList only
// reads `messages`, `values.artifacts`, `isLoading` and `isThreadLoading`.
const thread = useMemo(() => {
const messages = data?.messages ?? [];
return {
messages,
values: {
title: data?.title ?? "",
messages,
artifacts: data?.artifacts ?? [],
todos: [],
},
isLoading: false,
isThreadLoading: false,
error: undefined,
interrupt: undefined,
} as unknown as BaseStream<AgentThreadState>;
}, [data]);
if (isLoading) {
return (
<div className="bg-background text-muted-foreground flex h-screen items-center justify-center gap-2 text-sm" style={pageBg}>
<Loader2 className="size-4 animate-spin" />
正在加载分享内容…
</div>
);
}
if (isError || !data) {
return (
<div className="bg-background flex h-screen flex-col items-center justify-center gap-2 px-6 text-center" style={pageBg}>
<p className="text-lg font-medium">分享不可用</p>
<p className="text-muted-foreground text-sm">
该分享链接不存在、已被撤销,或不是只读分享。
</p>
</div>
);
}
return (
<div className="bg-background flex h-screen flex-col" style={pageBg}>
<header className="flex shrink-0 flex-col gap-1 border-b px-6 py-3">
<div className="flex items-center gap-2">
<h1 className="truncate text-base font-semibold">
{data.title || "未命名对话"}
</h1>
<span className="bg-muted text-muted-foreground shrink-0 rounded-full px-2 py-0.5 text-xs">
只读分享
</span>
</div>
<p className="text-muted-foreground text-xs">
这是一个只读对话快照,你可以查看全部内容,但无法继续提问。
</p>
</header>
<main className="min-h-0 flex-1">
<SubtasksProvider>
<ArtifactsProvider>
<ThreadContext.Provider
value={{ thread, isMock: true, publicShareCode: shareCode }}
>
<PublicShareBody
thread={thread}
shareCode={shareCode}
artifacts={data.artifacts ?? []}
/>
</ThreadContext.Provider>
</ArtifactsProvider>
</SubtasksProvider>
</main>
</div>
);
}