-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ensure transcript button appears in video player (#1135)
* fix: ensure transcript button appears in video player * chore: unmute video by default * chore: fix * chore: lint * chore: lint * fix: only include vjstranscribe plugin if showTranscripts is true * chore: tests * fix: added test coverage and some nits --------- Co-authored-by: Maham Akif <[email protected]>
- Loading branch information
1 parent
5efbadd
commit 7981e83
Showing
9 changed files
with
289 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { logError } from '@edx/frontend-platform/logging'; | ||
|
||
import { fetchAndAddTranscripts } from './service'; | ||
|
||
export function useTranscripts({ player, customOptions }) { | ||
const shouldUseTranscripts = !!(customOptions?.showTranscripts && customOptions?.transcriptUrls); | ||
const [isLoading, setIsLoading] = useState(shouldUseTranscripts); | ||
const [textTracks, setTextTracks] = useState([]); | ||
const [transcriptUrl, setTranscriptUrl] = useState(null); | ||
|
||
useEffect(() => { | ||
const fetchFn = async () => { | ||
setIsLoading(true); | ||
if (shouldUseTranscripts) { | ||
try { | ||
const result = await fetchAndAddTranscripts(customOptions.transcriptUrls, player); | ||
setTextTracks(result); | ||
// We are only catering to English transcripts for now as we don't have the option to change | ||
// the transcript language yet. | ||
if (result.en) { | ||
setTranscriptUrl(result.en); | ||
} | ||
} catch (error) { | ||
logError(`Error fetching transcripts for player: ${error}`); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
} | ||
}; | ||
fetchFn(); | ||
}, [customOptions?.transcriptUrls, player, shouldUseTranscripts]); | ||
|
||
return { | ||
textTracks, | ||
transcriptUrl, | ||
isLoading, | ||
}; | ||
} | ||
|
||
export function usePlayerOptions({ | ||
transcripts, | ||
options, | ||
customOptions, | ||
}) { | ||
const plugins = { ...options.plugins }; | ||
if (customOptions?.showTranscripts && transcripts.transcriptUrl) { | ||
const existingTranscribeUrls = plugins.vjstranscribe?.urls || []; | ||
plugins.vjstranscribe = { | ||
...plugins.vjstranscribe, | ||
urls: [transcripts.transcriptUrl, ...existingTranscribeUrls], | ||
}; | ||
} | ||
return { | ||
...options, | ||
plugins, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export * from './constants'; | ||
export * from './hooks'; | ||
export * from './service'; | ||
export * from './utils'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { logError } from '@edx/frontend-platform/logging'; | ||
import { useTranscripts } from '../hooks'; | ||
import { fetchAndAddTranscripts } from '../service'; | ||
|
||
// Mocking dependencies | ||
jest.mock('../service', () => ({ | ||
fetchAndAddTranscripts: jest.fn(), | ||
})); | ||
|
||
jest.mock('@edx/frontend-platform/logging', () => ({ | ||
logError: jest.fn(), | ||
})); | ||
|
||
describe('useTranscripts', () => { | ||
const customOptions = { | ||
showTranscripts: true, | ||
transcriptUrls: { | ||
en: 'https://example.com/transcript-en.txt', | ||
}, | ||
}; | ||
const mockPlayer = {}; | ||
|
||
it('should set isLoading to true initially if showTranscripts is true', () => { | ||
const { result } = renderHook(() => useTranscripts({ player: mockPlayer, customOptions })); | ||
expect(result.current.isLoading).toBe(true); | ||
}); | ||
|
||
it('should fetch and set textTracks and transcriptUrl correctly', async () => { | ||
const textTracks = { en: 'https://example.com/transcript-en.txt' }; | ||
fetchAndAddTranscripts.mockResolvedValueOnce(textTracks); | ||
|
||
const { result, waitForNextUpdate } = renderHook(() => useTranscripts({ player: mockPlayer, customOptions })); | ||
|
||
await waitForNextUpdate(); | ||
|
||
expect(result.current.isLoading).toBe(false); | ||
expect(result.current.textTracks).toEqual(textTracks); | ||
expect(result.current.transcriptUrl).toBe(textTracks.en); | ||
}); | ||
|
||
it('should log error and set isLoading to false if fetching transcripts fails', async () => { | ||
const errorMessage = 'Error fetching transcripts'; | ||
fetchAndAddTranscripts.mockRejectedValueOnce(new Error(errorMessage)); | ||
|
||
const { result, waitForNextUpdate } = renderHook(() => useTranscripts({ player: mockPlayer, customOptions })); | ||
|
||
await waitForNextUpdate(); | ||
|
||
expect(logError).toHaveBeenCalledWith(`Error fetching transcripts for player: Error: ${errorMessage}`); | ||
expect(result.current.isLoading).toBe(false); | ||
expect(result.current.textTracks).toEqual([]); | ||
expect(result.current.transcriptUrl).toBeNull(); | ||
}); | ||
|
||
it('should not fetch transcripts if showTranscripts is false', async () => { | ||
const customOptionsWithoutTranscripts = { | ||
showTranscripts: false, | ||
transcriptUrls: undefined, | ||
}; | ||
|
||
const { result } = renderHook(() => useTranscripts({ | ||
player: mockPlayer, | ||
customOptions: customOptionsWithoutTranscripts, | ||
})); | ||
|
||
expect(result.current.textTracks).toEqual([]); | ||
expect(result.current.transcriptUrl).toBeNull(); | ||
}); | ||
}); |
Oops, something went wrong.