Fix chapters persisting when switching between lessons in editor

The ChaptersEditor component had its own internal state (chaptersList) that was
only initialized once from the chapters prop. When switching between lessons,
the prop would change but the internal state wouldn't update, causing the
previous lesson's chapters to persist.

Added a useEffect to sync the internal state whenever the chapters prop changes.
Now when you switch from lesson 3 (with chapters) to lesson 2 (no chapters),
the editor properly resets to show a single empty chapter.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
dwindown
2026-01-04 20:29:43 +07:00
parent 269e384665
commit a801e2d344

View File

@@ -1,4 +1,4 @@
import { useState } from 'react';
import { useState, useEffect } from 'react';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
@@ -21,6 +21,15 @@ export function ChaptersEditor({ chapters, onChange, className = '' }: ChaptersE
chapters.length > 0 ? chapters : [{ time: 0, title: '' }]
);
// Sync internal state when prop changes (e.g., when switching between lessons)
useEffect(() => {
if (chapters.length > 0) {
setChaptersList(chapters);
} else {
setChaptersList([{ time: 0, title: '' }]);
}
}, [chapters]);
const updateTime = (index: number, value: string) => {
const newChapters = [...chaptersList];
const parts = value.split(':').map(Number);