diff --git a/src/frontend/packages/lib_video/src/components/common/VideoWidgetProvider/LocalizedTimedTextTrackUpload/LanguageSelect/index.spec.tsx b/src/frontend/packages/lib_video/src/components/common/VideoWidgetProvider/LocalizedTimedTextTrackUpload/LanguageSelect/index.spec.tsx
index 88c9eab100..662e0b87eb 100644
--- a/src/frontend/packages/lib_video/src/components/common/VideoWidgetProvider/LocalizedTimedTextTrackUpload/LanguageSelect/index.spec.tsx
+++ b/src/frontend/packages/lib_video/src/components/common/VideoWidgetProvider/LocalizedTimedTextTrackUpload/LanguageSelect/index.spec.tsx
@@ -53,7 +53,7 @@ describe('', () => {
expect(within(button).getByText('French')).toBeInTheDocument();
});
- it('renders the component with instructor local language unavailable', async () => {
+ it('propagates correctly undefined when the local language is unavailable', async () => {
render(
', () => {
);
await waitFor(() =>
- expect(onChangeMock).toHaveBeenLastCalledWith({
- label: 'English',
- value: 'en',
- }),
+ expect(onChangeMock).toHaveBeenLastCalledWith(undefined),
);
- const button = await screen.findByRole('combobox', {
- name: 'Choose the language',
- });
- expect(within(button).getByText('English')).toBeInTheDocument();
+ expect(
+ await screen.findByRole('combobox', {
+ name: 'Choose the language',
+ }),
+ ).toBeInTheDocument();
});
it('renders the component with some languages already having some subtitles uploaded', async () => {
@@ -120,41 +118,6 @@ describe('', () => {
).not.toBeInTheDocument();
});
- it('renders the component with no languages', async () => {
- render(
- ,
- { intlOptions: { locale: 'fr-FR' } },
- );
-
- await waitFor(() =>
- expect(onChangeMock).toHaveBeenLastCalledWith({
- label: 'No language availables',
- value: 'error',
- }),
- );
-
- expect(
- screen.queryByRole('button', {
- name: 'Select the language for which you want to upload a timed text file; Selected: fr',
- }),
- ).not.toBeInTheDocument();
- expect(
- screen.queryByRole('textbox', {
- name: 'Select the language for which you want to upload a timed text file, fr',
- }),
- ).not.toBeInTheDocument();
-
- const button = await screen.findByRole('combobox', {
- name: 'Choose the language',
- });
- expect(
- within(button).getByText('No language availables'),
- ).toBeInTheDocument();
- });
-
it('changes the selected language', async () => {
render(
void;
+ onChange: (selection?: LanguageChoice) => void;
timedTextModeWidget: timedTextMode;
choices?: LanguageChoice[];
}
@@ -40,17 +39,14 @@ export const LanguageSelect = ({
}: LanguageSelectProps) => {
const intl = useIntl();
- const errorLanguageChoice = useMemo(
- () => ({
- label: intl.formatMessage(messages.noLanguageAvailableLabel),
- value: 'error',
+ const timedTextTrackFn = useCallback(
+ (state: TimedTextTrackState) => ({
+ timedTextTracks: state.getTimedTextTracks(),
}),
- [intl],
+ [],
);
- const { timedTextTracks } = useTimedTextTrack((state) => ({
- timedTextTracks: state.getTimedTextTracks(),
- }));
+ const { timedTextTracks } = useTimedTextTrack(timedTextTrackFn);
const availableSelectableLanguages = useMemo(() => {
const filteredTimedTextTracks = timedTextTracks.filter(
@@ -61,59 +57,42 @@ export const LanguageSelect = ({
);
return (
- choices &&
choices
- .filter((lang) => !excludedLanguages.includes(lang.value))
- .sort((a, b) => a.label.localeCompare(b.label))
+ ?.filter((lang) => !excludedLanguages.includes(lang.value))
+ .sort((a, b) => a.label.localeCompare(b.label)) || []
);
}, [choices, timedTextTracks, timedTextModeWidget]);
- const userLocalAvailableLanguage = useMemo(() => {
- const userLocalLanguage = intl.locale;
-
- return availableSelectableLanguages
- ? availableSelectableLanguages.find((availableLanguage) =>
- userLocalLanguage.startsWith(availableLanguage.value),
- )
- : errorLanguageChoice;
- }, [availableSelectableLanguages, intl, errorLanguageChoice]);
-
- const [selectedLanguage, setSelectedLanguage] = useState<{
- label: string;
- value: string;
- }>(
- userLocalAvailableLanguage ??
- (availableSelectableLanguages
- ? availableSelectableLanguages[0]
- : errorLanguageChoice),
+ const [selectedLanguage, setSelectedLanguage] = useState>(
+ availableSelectableLanguages?.[0]?.value,
);
useEffect(() => {
- onChange(selectedLanguage);
- }, [onChange, selectedLanguage]);
-
- if (
- availableSelectableLanguages &&
- availableSelectableLanguages.length > 0 &&
- !availableSelectableLanguages.includes(selectedLanguage)
- ) {
- setSelectedLanguage(
- userLocalAvailableLanguage ?? availableSelectableLanguages[0],
+ const userLocalAvailableLanguage = availableSelectableLanguages?.find(
+ (availableLanguage) => intl.locale.startsWith(availableLanguage.value),
);
- }
+
+ setSelectedLanguage(userLocalAvailableLanguage?.value);
+ onChange(userLocalAvailableLanguage);
+ }, [intl.locale, availableSelectableLanguages, onChange]);
return (