From a801e2d344deb20ee13355433f69a14e99d86d4b Mon Sep 17 00:00:00 2001 From: dwindown Date: Sun, 4 Jan 2026 20:29:43 +0700 Subject: [PATCH] Fix chapters persisting when switching between lessons in editor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/components/admin/ChaptersEditor.tsx | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/components/admin/ChaptersEditor.tsx b/src/components/admin/ChaptersEditor.tsx index 8d9eb69..27c4ed6 100644 --- a/src/components/admin/ChaptersEditor.tsx +++ b/src/components/admin/ChaptersEditor.tsx @@ -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);