diff --git a/docs/components/topicComponent.md b/docs/components/topicComponent.md new file mode 100644 index 000000000..a23908b9a --- /dev/null +++ b/docs/components/topicComponent.md @@ -0,0 +1,382 @@ +# Topic Component - Video Playback Issues + +## Issue: YouTube Video Overlay Not Loading on Subsequent Visits + +### Problem Description +When users visit a topic task with an embedded YouTube video for the first time, the video loads and plays correctly with the Plyr overlay controls. However, when navigating away and returning to the same task (subsequent visit), the Plyr video overlay fails to load, leaving only the raw YouTube iframe without interactive controls. + +### Root Cause Analysis + +**Date Identified:** January 16, 2026 +**Component:** `projects/v3/src/app/components/topic/topic.component.ts` +**Affected Files:** +- `topic.component.ts` +- `topic.component.html` + +#### Technical Root Cause + +The issue was caused by Angular's change detection not triggering DOM re-rendering when the same HTML content is set, combined with persistent state flags preventing re-initialization: + +1. **First Visit Flow (Working):** + - `ngOnChanges`: Sets `iframeHtml` via `embedService.embed()` with unique iframe ID + - Template renders: `
` + - `ngAfterViewChecked`: Queries `.video-embed`, finds it + - Sets `plyrNeedsInit = false` and `plyrInitialized = true` + - Calls `_initVideoPlayer()` successfully + - Plyr wraps the element and adds controls ✅ + +2. **Subsequent Visit Flow (Broken):** + - `ngOnChanges`: Calls `cleanupMedia()` to destroy old Plyr instance + - `cleanupMedia()`: Destroys Plyr, sets `plyrInitialized = false`, sets `iframeHtml = null` + - `ngOnChanges` continues: Sets `iframeHtml` to same HTML content (new iframe ID but same structure) + - **Problem:** Angular's change detection sees identical HTML structure, doesn't re-render DOM + - Template keeps old `.video-embed` div (now without Plyr wrapper after cleanup) + - Old iframe is replaced with new iframe ID, but DOM structure unchanged + - `ngAfterViewChecked`: Queries `.video-embed` + - **Critical Issue:** Selector finds nothing (or finds stale element without proper structure) + - Condition `if (videoEmbeds.length > 0)` fails + - `_initVideoPlayer()` never called + - Video remains without Plyr controls ❌ + +#### DOM Comparison + +**First Visit HTML (Working):** +```html +