feat: AI writing history, Word export, roundtable dispatch chain
This commit is contained in:
1 parent
c0e3c5d692
commit
10334ddfe1
21 files changed
+1888
-143
No files matched your search
@@ -0,0 +1,82 @@
|
||||
import { createContext, useCallback, useContext, useMemo, useState, type ReactNode } from 'react';
|
||||
|
||||
import { getAIWritingSession } 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;
|
||||
};
|
||||
|
||||
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 value = useMemo(
|
||||
() => ({
|
||||
historySession,
|
||||
isHistoryMode: historySession !== null,
|
||||
openHistory,
|
||||
closeHistory,
|
||||
}),
|
||||
[historySession, openHistory, closeHistory],
|
||||
);
|
||||
|
||||
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);
|
||||
}
|
||||
Reference in new issue
Block a user