Merge pull request #2 from lavaFrai/feat_player-and-voiceover-saving

Saving the user's preferred voiceover and video source
This commit is contained in:
Kentai Radiquum 2024-12-05 17:52:55 +05:00 committed by GitHub
commit 2fb6fe47e0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 66 additions and 4 deletions

View file

@ -2,6 +2,7 @@
import { Spinner } from "#/components/Spinner/Spinner"; import { Spinner } from "#/components/Spinner/Spinner";
import { useUserStore } from "#/store/auth"; import { useUserStore } from "#/store/auth";
import { useUserPlayerPreferencesStore } from "#/store/player";
import { Card, Dropdown, Button } from "flowbite-react"; import { Card, Dropdown, Button } from "flowbite-react";
import { ENDPOINTS } from "#/api/config"; import { ENDPOINTS } from "#/api/config";
import { useState, useEffect } from "react"; import { useState, useEffect } from "react";
@ -139,20 +140,35 @@ const saveAnonEpisodeWatched = (
export const ReleasePlayer = (props: { id: number }) => { export const ReleasePlayer = (props: { id: number }) => {
const userStore = useUserStore(); const userStore = useUserStore();
const preferredVoiceoverStore = useUserPlayerPreferencesStore();
const storedPreferredVoiceover = preferredVoiceoverStore.getPreferredVoiceover(props.id);
const storedPreferredPlayer = preferredVoiceoverStore.getPreferredPlayer(props.id);
const [voiceoverInfo, setVoiceoverInfo] = useState(null); const [voiceoverInfo, setVoiceoverInfo] = useState(null);
const [selectedVoiceover, setSelectedVoiceover] = useState(null); const [selectedVoiceover, setSelectedVoiceover] = useState(null);
const [sourcesInfo, setSourcesInfo] = useState(null); const [sourcesInfo, setSourcesInfo] = useState(null);
const [selectedSource, setSelectedSource] = useState(null); const [selectedSource, setSelectedSource] = useState(null);
const [episodeInfo, setEpisodeInfo] = useState(null); const [episodeInfo, setEpisodeInfo] = useState(null);
const [selectedEpisode, setSelectedEpisode] = useState(null); const [selectedEpisode, setSelectedEpisode] = useState(null);
const setSelectedVoiceoverAndSaveAsPreferred = (voiceover: any) => {
setSelectedVoiceover(voiceover);
preferredVoiceoverStore.setPreferredVoiceover(props.id, voiceover.name);
}
const setSelectedPlayerAndSaveAsPreferred = (player: any) => {
setSelectedSource(player);
preferredVoiceoverStore.setPreferredPlayer(props.id, player.name);
}
useEffect(() => { useEffect(() => {
async function _fetchInfo() { async function _fetchInfo() {
const voiceover = await _fetch( const voiceover = await _fetch(
`${ENDPOINTS.release.episode}/${props.id}` `${ENDPOINTS.release.episode}/${props.id}`
); );
const preferredVoiceover = voiceover.types.find(
(voiceover: any) => voiceover.name === storedPreferredVoiceover
) || voiceover.types[0];
setVoiceoverInfo(voiceover.types); setVoiceoverInfo(voiceover.types);
setSelectedVoiceover(voiceover.types[0]); setSelectedVoiceover(preferredVoiceover);
} }
_fetchInfo(); _fetchInfo();
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
@ -163,8 +179,12 @@ export const ReleasePlayer = (props: { id: number }) => {
const sources = await _fetch( const sources = await _fetch(
`${ENDPOINTS.release.episode}/${props.id}/${selectedVoiceover.id}` `${ENDPOINTS.release.episode}/${props.id}/${selectedVoiceover.id}`
); );
const preferredSource = sources.sources.find(
(source: any) => source.name === storedPreferredPlayer
) || sources.sources[0];
setSourcesInfo(sources.sources); setSourcesInfo(sources.sources);
setSelectedSource(sources.sources[0]); setSelectedSource(preferredSource);
} }
if (selectedVoiceover) { if (selectedVoiceover) {
_fetchInfo(); _fetchInfo();
@ -227,7 +247,7 @@ export const ReleasePlayer = (props: { id: number }) => {
{voiceoverInfo.map((voiceover: any) => ( {voiceoverInfo.map((voiceover: any) => (
<Dropdown.Item <Dropdown.Item
key={`voiceover_${voiceover.id}`} key={`voiceover_${voiceover.id}`}
onClick={() => setSelectedVoiceover(voiceover)} onClick={() => setSelectedVoiceoverAndSaveAsPreferred(voiceover)}
> >
{voiceover.name} {voiceover.name}
</Dropdown.Item> </Dropdown.Item>
@ -241,7 +261,7 @@ export const ReleasePlayer = (props: { id: number }) => {
{sourcesInfo.map((source: any) => ( {sourcesInfo.map((source: any) => (
<Dropdown.Item <Dropdown.Item
key={`source_${source.id}`} key={`source_${source.id}`}
onClick={() => setSelectedSource(source)} onClick={() => setSelectedPlayerAndSaveAsPreferred(source)}
> >
{source.name} {source.name}
</Dropdown.Item> </Dropdown.Item>

42
app/store/player.ts Normal file
View file

@ -0,0 +1,42 @@
"use client";
import { create } from "zustand";
import { persist } from "zustand/middleware";
interface userPlayerPreferencesState {
voiceover: Map<number, string>;
player: Map<number, string>;
getPreferredVoiceover: (id: number) => (string | undefined);
setPreferredVoiceover: (id: number, voiceover: string) => void;
getPreferredPlayer: (id: number) => (string | undefined);
setPreferredPlayer: (id: number, player: string) => void;
}
export const useUserPlayerPreferencesStore = create<userPlayerPreferencesState>()(
persist(
(set, get) => ({
voiceover: new Map<number, string>(),
player: new Map<number, string>(),
getPreferredVoiceover: (id: number) => get().voiceover[id],
setPreferredVoiceover: (id: number, voiceover: string) => {
let current = get().voiceover
current[id] = voiceover
set({
voiceover: current,
player: get().player,
});
},
getPreferredPlayer: (id: number) => get().player[id],
setPreferredPlayer: (id: number, player: string) => {
let current = get().player
current[id] = player
set({
voiceover: get().voiceover,
player: current,
})
}
}),
{
name: "player-preferences",
}
)
);