fix: auto-recover corrupted chapter data on startup
This commit is contained in:
1 parent
2f511500bf
commit
647034f3c0
3 files changed
+16
-4
No files matched your search
@@ -96,7 +96,7 @@ export default function Sidebar() {
|
||||
}
|
||||
}, [activeChapterId, chapters, showToast, t]);
|
||||
|
||||
const totalWords = chapters.reduce((sum, ch) => sum + (ch.wordCount || 0), 0);
|
||||
const totalWords = Array.isArray(chapters) ? chapters.reduce((sum, ch) => sum + (ch.wordCount || 0), 0) : 0;
|
||||
|
||||
return (
|
||||
<>
|
||||
|
||||
+14
-2
@@ -68,7 +68,19 @@ export default function Home() {
|
||||
// 初始化数据
|
||||
useEffect(() => {
|
||||
const initData = async () => {
|
||||
const saved = await getChapters();
|
||||
let saved = await getChapters();
|
||||
// 自动修复:过滤掉损坏的章节数据(如缺少 id 或非对象的条目)
|
||||
if (Array.isArray(saved)) {
|
||||
const cleaned = saved.filter(ch => ch && typeof ch === 'object' && ch.id);
|
||||
if (cleaned.length !== saved.length) {
|
||||
console.warn(`[数据修复] 发现 ${saved.length - cleaned.length} 条损坏的章节数据,已自动清理`);
|
||||
saved = cleaned;
|
||||
const { saveChapters } = await import('./lib/storage');
|
||||
await saveChapters(saved);
|
||||
}
|
||||
} else {
|
||||
saved = [];
|
||||
}
|
||||
if (saved.length === 0) {
|
||||
const first = await createChapter(t('page.firstChapterTitle'));
|
||||
setChapters([first]);
|
||||
@@ -147,7 +159,7 @@ export default function Home() {
|
||||
}, []);
|
||||
|
||||
// 当前活跃章节
|
||||
const activeChapter = chapters.find(ch => ch.id === activeChapterId);
|
||||
const activeChapter = Array.isArray(chapters) ? chapters.find(ch => ch.id === activeChapterId) : null;
|
||||
|
||||
const handleEditorUpdate = useCallback(async ({ html, wordCount }) => {
|
||||
if (!activeChapterId) return;
|
||||
|
||||
@@ -4,7 +4,7 @@ export const useAppStore = create((set, get) => ({
|
||||
// --- Chapter State ---
|
||||
chapters: [],
|
||||
activeChapterId: null,
|
||||
setChapters: (chapters) => set({ chapters }),
|
||||
setChapters: (chapters) => set({ chapters: Array.isArray(chapters) ? chapters.filter(ch => ch && typeof ch === 'object' && ch.id) : [] }),
|
||||
setActiveChapterId: (id) => set({ activeChapterId: id }),
|
||||
addChapter: (chapter) => set((state) => ({ chapters: [...state.chapters, chapter] })),
|
||||
deleteChapter: (id) => set((state) => ({ chapters: state.chapters.filter((ch) => ch.id !== id) })),
|
||||
|
||||
Reference in new issue
Block a user