From 33b63f7fbc95c6f1055e2cfc68cd51f7bbffa88c Mon Sep 17 00:00:00 2001 From: Deeksha Deepak Date: Mon, 10 Aug 2026 16:50:26 +0530 Subject: [PATCH 1/3] feat: add TranscriptsDrawer component for managing video transcripts - Implemented TranscriptsDrawer to display and manage transcripts for video content. - Added functionality to parse and display transcript segments, including editing capabilities. - Integrated approval and rejection workflows for transcripts with backend service calls. - Enhanced user experience with loading states, error handling, and status updates. test: add unit tests for useEditor transcript generation logic - Created tests to verify transcript creation on video upload based on user input. - Ensured that transcript creation is skipped for non-video files and unchecked options. - Validated that upload success is not blocked by failed transcript creation. - Confirmed that transcript creation logic is correctly decoupled from other submission actions. --- package.json | 2 +- src/ContentEditor.tsx | 2 + src/components/EditorPreview.test.tsx | 27 +- src/components/EditorPreview.tsx | 57 +- src/components/Header.test.tsx | 26 + src/components/Header.tsx | 17 +- src/components/TranscriptsDrawer.test.tsx | 642 ++++++++++++++++++++++ src/components/TranscriptsDrawer.tsx | 568 +++++++++++++++++++ src/components/UploadCanvas.test.tsx | 46 ++ src/components/UploadCanvas.tsx | 58 +- src/constants.ts | 10 + src/editor.scss | 295 ++++++++++ src/i18n/translations.en.ts | 41 ++ src/icons.tsx | 9 + src/services/ContentEditorService.test.ts | 208 +++++++ src/services/ContentEditorService.ts | 84 ++- src/test/mockEd.ts | 5 + src/types.ts | 29 +- src/useEditor.test.ts | 130 +++++ src/useEditor.ts | 41 +- 20 files changed, 2270 insertions(+), 27 deletions(-) create mode 100644 src/components/TranscriptsDrawer.test.tsx create mode 100644 src/components/TranscriptsDrawer.tsx create mode 100644 src/useEditor.test.ts diff --git a/package.json b/package.json index 1de2393..d6e72f8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@project-sunbird/generic-editor-v2", - "version": "2.0.4", + "version": "2.0.5", "description": "Sunbird Generic Editor — React content upload/generic editor (theme-reactive, multilingual). Fork of @sunbird/content-editor that previews via the legacy ekstep content renderer.", "main": "dist/sunbird-generic-editor.umd.js", "module": "dist/sunbird-generic-editor.es.js", diff --git a/src/ContentEditor.tsx b/src/ContentEditor.tsx index 7646d8f..94e06c5 100644 --- a/src/ContentEditor.tsx +++ b/src/ContentEditor.tsx @@ -23,6 +23,7 @@ import MetadataDrawer from './components/MetadataDrawer'; import CollaboratorDrawer from './components/CollaboratorDrawer'; import ReviewDrawer from './components/ReviewDrawer'; import ReviewCommentsDrawer from './components/ReviewCommentsDrawer'; +import TranscriptsDrawer from './components/TranscriptsDrawer'; import AssetPickerModal from './components/AssetPickerModal'; import Toast from './components/Toast'; import { t } from './i18n/i18n'; @@ -56,6 +57,7 @@ const ContentEditor: React.FC = (props) => { + diff --git a/src/components/EditorPreview.test.tsx b/src/components/EditorPreview.test.tsx index 78c93ea..4feeb0f 100644 --- a/src/components/EditorPreview.test.tsx +++ b/src/components/EditorPreview.test.tsx @@ -1,7 +1,7 @@ -import { render, screen } from '@testing-library/react'; -import { describe, it, expect } from 'vitest'; +import { render, screen, waitFor } from '@testing-library/react'; +import { describe, it, expect, vi } from 'vitest'; import EditorPreview from './EditorPreview'; -import { makeEd, mockContext, mockContent } from '../test/mockEd'; +import { makeEd, mockContext, mockContent, mockService } from '../test/mockEd'; describe('', () => { it('renders nothing when there is no content (negative)', () => { @@ -31,4 +31,25 @@ describe('', () => { const { container } = render(); expect(container.querySelector('iframe')).toHaveAttribute('src', '/preview.html?foo=1&webview=true'); }); + + it('does not fetch transcripts for non-video content (negative)', () => { + const service = mockService(); + const ed = makeEd({ content: mockContent, service }); // mockContent is application/pdf + render(); + expect(service.readTranscripts).not.toHaveBeenCalled(); + }); + + it('fetches and maps transcripts for video content, remounting the iframe once they arrive', async () => { + const service = mockService({ + readTranscripts: vi.fn().mockResolvedValue([ + { code: 'c_en', language: 'English', languageCode: 'en', captionsUrl: 'https://x/en.vtt', status: 'Live', sourceLanguage: true }, + { code: 'c_fr', language: 'French', languageCode: 'fr', status: 'Draft' }, // filtered: not Live + ]), + }); + const videoContent = { ...mockContent, mimeType: 'video/mp4' }; + const ed = makeEd({ content: videoContent, service }); + const { container } = render(); + await waitFor(() => expect(service.readTranscripts).toHaveBeenCalledWith('do_1')); + await waitFor(() => expect(container.querySelector('iframe')).toBeInTheDocument()); + }); }); diff --git a/src/components/EditorPreview.tsx b/src/components/EditorPreview.tsx index a1d6073..8b8203f 100644 --- a/src/components/EditorPreview.tsx +++ b/src/components/EditorPreview.tsx @@ -1,8 +1,38 @@ -import React, { useRef } from 'react'; +import React, { useRef, useEffect, useState } from 'react'; import type { EditorController } from '../useEditor'; -import type { ContentData, EditorContext } from '../types'; +import type { ContentData, EditorContext, RawTranscript } from '../types'; import { getMimeTypeLabel, t } from '../i18n/i18n'; import { FileIcon } from '../icons'; +import { isVideoMimeType } from '../constants'; + +interface PlayerTranscript { + language: string; + identifier: string; + languageCode: string; + artifactUrl: string; + wordByWordUrl?: string; + sourceLanguage?: boolean; +} + +/** + * Maps enrichment.transcripts (raw) into the shape sunbird-video-player expects - + * mirrors the portal's ContentService.contentRead mapping exactly, since the legacy + * preview iframe's video sub-player is the same component. artifactUrl here must be + * the VTT (captionsUrl), not the raw transcript.json. + */ +function mapRawTranscripts(raw: RawTranscript[]): PlayerTranscript[] { + return raw + .filter((e): e is RawTranscript & { captionsUrl: string } => + !!e.captionsUrl && (e.status ?? 'Live').toLowerCase() === 'live') + .map((e) => ({ + language: e.language || (e.languageCode || 'Unknown').toUpperCase(), + identifier: e.code ?? e.identifier ?? '', + languageCode: e.languageCode || '', + artifactUrl: e.captionsUrl, + wordByWordUrl: e.captionsUrl, + sourceLanguage: !!e.sourceLanguage, + })); +} /** * Preview via the legacy ekstep content renderer — the same mechanism the old @@ -55,9 +85,26 @@ const RendererPreview: React.FC<{ }; const EditorPreview: React.FC<{ ed: EditorController; context: EditorContext }> = ({ ed, context }) => { - const { content, lang, previewUrl, previewConfig } = ed; + const { content, lang, previewUrl, previewConfig, service } = ed; + const [transcripts, setTranscripts] = useState([]); + + useEffect(() => { + setTranscripts([]); + if (!content?.identifier || !isVideoMimeType(content.mimeType)) return; + service + .readTranscripts(content.identifier) + .then((raw) => setTranscripts(mapRawTranscripts(raw))) + .catch(() => setTranscripts([])); + }, [content?.identifier, content?.mimeType, service]); + if (!content) return null; + // Merging transcripts changes content's identity without changing identifier/artifactUrl, + // so the iframe key below also folds in transcripts.length - forcing exactly one remount + // (+ one re-init) once they arrive, instead of silently never surfacing them. Acceptable + // here (unlike a live learner player) since this is the creator's own edit-time preview. + const previewMetadata = transcripts.length ? { ...content, transcripts } : content; + return (
@@ -68,8 +115,8 @@ const EditorPreview: React.FC<{ ed: EditorController; context: EditorContext }>
', () => { fireEvent.click(btn); expect(publish).toHaveBeenCalled(); }); + + it('shows "View transcript" for video content once transcripts exist, and opens the drawer', () => { + const setDrawer = vi.fn(); + const ed = makeEd({ + view: 'player', mode: 'edit', setDrawer, hasTranscripts: true, + content: { ...mockContent, mimeType: 'video/mp4' }, + }); + render(
); + fireEvent.click(screen.getByRole('button', { name: 'View transcript' })); + expect(setDrawer).toHaveBeenCalledWith('transcripts'); + }); + + it('hides "View transcript" for video content with no transcripts yet (negative)', () => { + const ed = makeEd({ + view: 'player', mode: 'edit', hasTranscripts: false, + content: { ...mockContent, mimeType: 'video/mp4' }, + }); + render(
); + expect(screen.queryByRole('button', { name: 'View transcript' })).not.toBeInTheDocument(); + }); + + it('hides "View transcript" for non-video content (negative)', () => { + const ed = makeEd({ view: 'player', mode: 'edit', content: { ...mockContent, mimeType: 'application/pdf' } }); + render(
); + expect(screen.queryByRole('button', { name: 'View transcript' })).not.toBeInTheDocument(); + }); }); diff --git a/src/components/Header.tsx b/src/components/Header.tsx index 1f77671..85a1f40 100644 --- a/src/components/Header.tsx +++ b/src/components/Header.tsx @@ -2,12 +2,12 @@ import React from 'react'; import type { EditorController } from '../useEditor'; import { t } from '../i18n/i18n'; import { - ImageIcon, PencilIcon, SaveIcon, CloseIcon, SendIcon, UserPlusIcon, CheckIcon, CommentIcon, + ImageIcon, PencilIcon, SaveIcon, CloseIcon, SendIcon, UserPlusIcon, CheckIcon, CommentIcon, CaptionsIcon, } from '../icons'; import { STATUS } from '../constants'; const Header: React.FC<{ ed: EditorController }> = ({ ed }) => { - const { content, lang, mode, setDrawer, saveDraft, close, busy, busyAction, setReviewErrors, setReviewSubmitMode, headerLogo, hasReviewComments } = ed; + const { content, lang, mode, setDrawer, saveDraft, close, busy, busyAction, setReviewErrors, setReviewSubmitMode, headerLogo, hasReviewComments, hasTranscripts } = ed; const savingDraft = busyAction === 'save-draft'; const hasContent = !!content?.identifier && ed.view === 'player'; const isDraft = (content?.status ?? STATUS.DRAFT) === STATUS.DRAFT; @@ -61,6 +61,19 @@ const Header: React.FC<{ ed: EditorController }> = ({ ed }) => { )} + {hasContent && hasTranscripts && ( + + )} + {canEdit && !isReviewer && hasReviewComments && ( +
+
{activeLang!.language}
+ {pendingExpectedStatus ? ( + // Once the user has acted, don't keep showing "Needs review" (as if nothing + // happened) while we wait for the server to catch up - show a neutral pending state instead. + + {t(lang, 'STATUS_UPDATING')} + + ) : ( + + {statusLabel(lang, activeLang!.status)} + + )} +
+ {tone === 'review' && !isEditingSegments && !pendingExpectedStatus && ( +
+ + +
+ )} + {actionError && ( +
+

{actionError}

+ {pendingExpectedStatus && ( + + )} +
+ )} + {canShowEditControls && ( +
+ {isEditingSegments ? ( + <> + + + + ) : ( + + )} +
+ )} + {saveError &&

{saveError}

} + {segmentsLoading ? ( +
+
+
+ ) : segmentsError || !displaySegments ? ( +

{t(lang, 'SEGMENTS_LOAD_ERROR')}

+ ) : ( +
+ {displaySegments.map((seg, i) => ( +
+ {(seg.start != null || seg.end != null) && ( + + {formatTimestamp(seg.start)}{seg.end != null ? `–${formatTimestamp(seg.end)}` : ''} + + )} + {isEditingSegments ? ( +