-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[front] feat: add a tab for "good short videos"
Merge pull request #1825 from tournesol-app/good-short-videos-tab
- Loading branch information
Showing
7 changed files
with
73 additions
and
3 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
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,15 @@ | ||
import { useSelector } from 'react-redux'; | ||
import { selectSettings } from 'src/features/settings/userSettingsSlice'; | ||
import { PollUserSettingsKeys } from 'src/utils/types'; | ||
import { recommendationsLanguagesFromNavigator } from 'src/utils/recommendationsLanguages'; | ||
|
||
export const usePreferredLanguages = ({ pollName }: { pollName: string }) => { | ||
const userSettings = useSelector(selectSettings).settings; | ||
let preferredLanguages = | ||
userSettings?.[pollName as PollUserSettingsKeys] | ||
?.recommendations__default_languages; | ||
|
||
preferredLanguages ??= recommendationsLanguagesFromNavigator().split(','); | ||
|
||
return preferredLanguages; | ||
}; |
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,38 @@ | ||
import { PollsService } from 'src/services/openapi'; | ||
import { Recommendation } from 'src/services/openapi'; | ||
|
||
const shuffleRecommendations = (array: Recommendation[]) => { | ||
// https://stackoverflow.com/a/12646864/188760 | ||
for (let i = array.length - 1; i > 0; i--) { | ||
const j = Math.floor(Math.random() * (i + 1)); | ||
[array[i], array[j]] = [array[j], array[i]]; | ||
} | ||
}; | ||
|
||
export const getGoodShortVideos = async ({ | ||
preferredLanguages, | ||
}: { | ||
preferredLanguages: string[]; | ||
}): Promise<Recommendation[]> => { | ||
const metadata: Record<string, string | string[]> = {}; | ||
|
||
const minutesMax = 5; | ||
const top = 100; | ||
|
||
metadata['duration:lte:int'] = (60 * minutesMax).toString(); | ||
metadata['language'] = preferredLanguages; | ||
|
||
const videos = await PollsService.pollsRecommendationsList({ | ||
name: 'videos', | ||
metadata: metadata, | ||
limit: top, | ||
excludeComparedEntities: true, | ||
}).then((data) => data.results ?? []); | ||
|
||
const returnCount = 20; | ||
|
||
shuffleRecommendations(videos); | ||
videos.splice(returnCount); | ||
|
||
return videos; | ||
}; |