-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
161 additions
and
136 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
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
83 changes: 83 additions & 0 deletions
83
src/pages/Video/components/tools/hooks/useTranslationTools.tsx
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,83 @@ | ||
import { useParams } from 'react-router-dom'; | ||
import { FEATURE_FLAG_AI_TRANSLATION } from 'src/constants'; | ||
import { useFeatureFlag } from 'src/hooks/useFeatureFlag'; | ||
import { | ||
GetVideosByVidTranslationApiResponse, | ||
useGetVideosByVidQuery, | ||
useGetVideosByVidTranslationQuery, | ||
} from 'src/features/api'; | ||
import { useEffect, useState } from 'react'; | ||
import { usePreferredLanguage } from '../usePreferredLanguage'; | ||
import { useToolsContext } from '../context/ToolsContext'; | ||
|
||
export const useTranslationTools = () => { | ||
const { videoId } = useParams(); | ||
const [data, setData] = useState<{ | ||
hasQuickTranslate?: boolean; | ||
preferredLanguage?: string; | ||
translation?: GetVideosByVidTranslationApiResponse; | ||
canQuickTranslate?: boolean; | ||
}>({ | ||
hasQuickTranslate: false, | ||
canQuickTranslate: true, | ||
}); | ||
|
||
const { hasFeatureFlag } = useFeatureFlag(); | ||
const hasAIFeatureFlag = hasFeatureFlag(FEATURE_FLAG_AI_TRANSLATION); | ||
const preferredLanguage = usePreferredLanguage(); | ||
const { language } = useToolsContext(); | ||
|
||
const { | ||
data: video, | ||
isLoading: isLoadingVideo, | ||
isError: isErrorVideo, | ||
} = useGetVideosByVidQuery( | ||
{ | ||
vid: videoId || '', | ||
}, | ||
{ | ||
skip: !videoId, | ||
} | ||
); | ||
|
||
const { | ||
data: translation, | ||
isLoading: isLoadingTranslation, | ||
isError: isErrorTranslation, | ||
} = useGetVideosByVidTranslationQuery( | ||
{ | ||
vid: videoId || '', | ||
...(language && { lang: language }), | ||
}, | ||
{ | ||
skip: !hasAIFeatureFlag || !videoId || !language, | ||
} | ||
); | ||
|
||
useEffect(() => { | ||
if (video) { | ||
const hasQuickTranslate = | ||
!!preferredLanguage && // Exists a preferred language | ||
video.language.localeCompare(preferredLanguage) !== 0; // Video language is different from preferred language | ||
|
||
const isPrefTranslated = !!( | ||
preferredLanguage && | ||
translation?.language.localeCompare(preferredLanguage) === 0 | ||
); | ||
|
||
setData({ | ||
hasQuickTranslate, | ||
preferredLanguage, | ||
canQuickTranslate: hasQuickTranslate && !isPrefTranslated, | ||
translation, | ||
}); | ||
} | ||
}, [video, translation, preferredLanguage]); | ||
|
||
return { | ||
isError: !hasAIFeatureFlag || isErrorVideo || isErrorTranslation, | ||
isProcessing: | ||
isLoadingVideo || isLoadingTranslation || translation?.processing === 1, | ||
data, | ||
}; | ||
}; |
Oops, something went wrong.