- 后端新增 PUT /api/ai-writing/sessions/{id}/draft(属主校验,draft_title 可选)
- 前端历史模式放开只读,工具栏新增「保存」按钮持久化修改
- 实时写作完成态保持原样(仅导出,不落库)
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
96 lines
3.2 KiB
TypeScript
96 lines
3.2 KiB
TypeScript
import { createContext, useCallback, useContext, useMemo, useState, type ReactNode } from 'react';
|
|
|
|
import { getAIWritingSession, updateAIWritingDraft } from '../api/ai-writing-sessions';
|
|
import type { AIWritingSessionSummary } from '../api/ai-writing-sessions';
|
|
import { useCanvasGraphContext } from './CanvasGraphContext';
|
|
import { useCanvasUIContext } from './CanvasUIContext';
|
|
|
|
type AIWritingHistoryContextValue = {
|
|
historySession: AIWritingSessionSummary | null;
|
|
isHistoryMode: boolean;
|
|
openHistory: (session: AIWritingSessionSummary) => Promise<void>;
|
|
closeHistory: () => void;
|
|
/** 历史回看时把用户修改后的正文保存回该会话(持久化到后端)。 */
|
|
saveHistoryDraft: (markdown: string, title?: string) => Promise<void>;
|
|
};
|
|
|
|
const AIWritingHistoryContext = createContext<AIWritingHistoryContextValue | null>(null);
|
|
|
|
export function AIWritingHistoryProvider({ children }: { children: ReactNode }) {
|
|
const { setArtifact } = useCanvasGraphContext();
|
|
const { setIsDraftEditable } = useCanvasUIContext();
|
|
const [historySession, setHistorySession] = useState<AIWritingSessionSummary | null>(null);
|
|
|
|
const openHistory = useCallback(async (session: AIWritingSessionSummary) => {
|
|
let full = session;
|
|
if (!session.draft_markdown || !session.transcript) {
|
|
try {
|
|
full = await getAIWritingSession(session.id);
|
|
} catch {
|
|
// 列表项已够用,拉详情失败则继续用摘要
|
|
}
|
|
}
|
|
setHistorySession(full);
|
|
// 完成态布局(Tiptap + 左侧目录),由 DraftPanel 根据 isHistoryMode 只读展示
|
|
setIsDraftEditable(true);
|
|
if (full.draft_markdown) {
|
|
setArtifact({
|
|
currentIndex: 0,
|
|
contents: [{
|
|
index: 0,
|
|
type: 'text',
|
|
title: full.draft_title ?? full.title,
|
|
fullMarkdown: full.draft_markdown,
|
|
}],
|
|
});
|
|
} else {
|
|
setArtifact(null);
|
|
}
|
|
}, [setArtifact, setIsDraftEditable]);
|
|
|
|
const closeHistory = useCallback(() => {
|
|
setHistorySession(null);
|
|
setArtifact(null);
|
|
setIsDraftEditable(true);
|
|
}, [setArtifact, setIsDraftEditable]);
|
|
|
|
const saveHistoryDraft = useCallback(
|
|
async (markdown: string, title?: string) => {
|
|
if (!historySession) return;
|
|
const updated = await updateAIWritingDraft(historySession.id, markdown, title);
|
|
// 回写本地会话,保证后续重开/重命名读到最新成稿
|
|
setHistorySession((prev) => (prev && prev.id === updated.id ? updated : prev));
|
|
},
|
|
[historySession],
|
|
);
|
|
|
|
const value = useMemo(
|
|
() => ({
|
|
historySession,
|
|
isHistoryMode: historySession !== null,
|
|
openHistory,
|
|
closeHistory,
|
|
saveHistoryDraft,
|
|
}),
|
|
[historySession, openHistory, closeHistory, saveHistoryDraft],
|
|
);
|
|
|
|
return (
|
|
<AIWritingHistoryContext.Provider value={value}>
|
|
{children}
|
|
</AIWritingHistoryContext.Provider>
|
|
);
|
|
}
|
|
|
|
export function useAIWritingHistory() {
|
|
const ctx = useContext(AIWritingHistoryContext);
|
|
if (!ctx) {
|
|
throw new Error('useAIWritingHistory must be used within AIWritingHistoryProvider');
|
|
}
|
|
return ctx;
|
|
}
|
|
|
|
export function useAIWritingHistoryOptional() {
|
|
return useContext(AIWritingHistoryContext);
|
|
}
|