From 9eef570740e8d48649f6f5027b31390bf8e61527 Mon Sep 17 00:00:00 2001 From: Kendall Garner <17521368+kgarner7@users.noreply.github.com> Date: Sat, 3 Jun 2023 00:39:50 -0700 Subject: [PATCH] support .txt --- src/renderer/api/controller.ts | 4 ++-- src/renderer/api/jellyfin/jellyfin-controller.ts | 10 +++++++--- src/renderer/api/jellyfin/jellyfin-types.ts | 2 +- src/renderer/api/types.ts | 2 ++ src/renderer/features/lyrics/lyrics.tsx | 15 ++++++++------- 5 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/renderer/api/controller.ts b/src/renderer/api/controller.ts index 5271f144..280ea7be 100644 --- a/src/renderer/api/controller.ts +++ b/src/renderer/api/controller.ts @@ -47,7 +47,7 @@ import type { SearchArgs, SearchResponse, LyricsArgs, - SynchronizedLyricsArray, + LyricsResponse, } from '/@/renderer/api/types'; import { ServerType } from '/@/renderer/types'; import { DeletePlaylistResponse, RandomSongListArgs } from './types'; @@ -78,7 +78,7 @@ export type ControllerEndpoint = Partial<{ getFolderList: () => void; getFolderSongs: () => void; getGenreList: (args: GenreListArgs) => Promise; - getLyrics: (args: LyricsArgs) => Promise; + getLyrics: (args: LyricsArgs) => Promise; getMusicFolderList: (args: MusicFolderListArgs) => Promise; getPlaylistDetail: (args: PlaylistDetailArgs) => Promise; getPlaylistList: (args: PlaylistListArgs) => Promise; diff --git a/src/renderer/api/jellyfin/jellyfin-controller.ts b/src/renderer/api/jellyfin/jellyfin-controller.ts index 81f1b144..01d85477 100644 --- a/src/renderer/api/jellyfin/jellyfin-controller.ts +++ b/src/renderer/api/jellyfin/jellyfin-controller.ts @@ -45,7 +45,7 @@ import { RandomSongListResponse, RandomSongListArgs, LyricsArgs, - SynchronizedLyricsArray, + LyricsResponse, } from '/@/renderer/api/types'; import { jfApiClient } from '/@/renderer/api/jellyfin/jellyfin-api'; import { jfNormalize } from './jellyfin-normalize'; @@ -849,7 +849,7 @@ const getRandomSongList = async (args: RandomSongListArgs): Promise => { +const getLyrics = async (args: LyricsArgs): Promise => { const { query, apiClientProps } = args; if (!apiClientProps.server?.userId) { @@ -867,7 +867,11 @@ const getLyrics = async (args: LyricsArgs): Promise => throw new Error('Failed to get lyrics'); } - return res.body.Lyrics.map((lyric) => [lyric.Start / 1e4, lyric.Text]); + if (res.body.Lyrics.length > 0 && res.body.Lyrics[0].Start === undefined) { + return res.body.Lyrics[0].Text; + } + + return res.body.Lyrics.map((lyric) => [lyric.Start! / 1e4, lyric.Text]); }; export const jfController = { diff --git a/src/renderer/api/jellyfin/jellyfin-types.ts b/src/renderer/api/jellyfin/jellyfin-types.ts index 374e3978..1173bfe6 100644 --- a/src/renderer/api/jellyfin/jellyfin-types.ts +++ b/src/renderer/api/jellyfin/jellyfin-types.ts @@ -632,7 +632,7 @@ const searchParameters = paginationParameters.merge(baseParameters); const search = z.any(); const lyricText = z.object({ - Start: z.number(), + Start: z.number().optional(), Text: z.string(), }); diff --git a/src/renderer/api/types.ts b/src/renderer/api/types.ts index 5c206aae..8f610b09 100644 --- a/src/renderer/api/types.ts +++ b/src/renderer/api/types.ts @@ -1027,6 +1027,8 @@ export type LyricsArgs = { export type SynchronizedLyricsArray = Array<[number, string]>; +export type LyricsResponse = SynchronizedLyricsArray | string; + export const instanceOfCancellationError = (error: any) => { return 'revert' in error; }; diff --git a/src/renderer/features/lyrics/lyrics.tsx b/src/renderer/features/lyrics/lyrics.tsx index f41a022c..a9698635 100644 --- a/src/renderer/features/lyrics/lyrics.tsx +++ b/src/renderer/features/lyrics/lyrics.tsx @@ -9,7 +9,7 @@ import { LyricLine } from '/@/renderer/features/lyrics/lyric-line'; import { Center, Group } from '@mantine/core'; import { RiInformationFill } from 'react-icons/ri'; import { TextTitle } from '/@/renderer/components'; -import { SynchronizedLyricsArray } from '/@/renderer/api/types'; +import { LyricsResponse, SynchronizedLyricsArray } from '/@/renderer/api/types'; import { useSongLyrics } from '/@/renderer/features/lyrics/queries/lyric-query'; const lyrics = isElectron() ? window.electron.lyrics : null; @@ -25,7 +25,7 @@ export const Lyrics = () => { const [override, setOverride] = useState(null); const [source, setSource] = useState(null); - const [songLyrics, setSongLyrics] = useState(null); + const [songLyrics, setSongLyrics] = useState(null); const remoteLyrics = useSongLyrics({ query: { songId: currentSong?.id ?? '' }, @@ -48,7 +48,7 @@ export const Lyrics = () => { }, []); useEffect(() => { - if (currentSong && !currentSong.lyrics && !remoteLyrics.isLoading && !remoteLyrics.isSuccess) { + if (currentSong && !currentSong.lyrics && !remoteLyrics.isLoading && !remoteLyrics.data) { lyrics?.fetchLyrics(currentSong); } @@ -56,7 +56,7 @@ export const Lyrics = () => { setOverride(null); setSource(null); - }, [currentSong, remoteLyrics.isLoading, remoteLyrics.isSuccess]); + }, [currentSong, remoteLyrics.isLoading, remoteLyrics.data]); useEffect(() => { let lyrics: string | null = null; @@ -67,9 +67,10 @@ export const Lyrics = () => { setSource(currentServer?.name ?? 'music server'); } else if (override) { lyrics = override; - } else if (remoteLyrics.isSuccess) { + } else if (remoteLyrics.data) { + console.log(remoteLyrics.data); setSource(currentServer?.name ?? 'music server'); - setSongLyrics(remoteLyrics.data!); + setSongLyrics(remoteLyrics.data); return; } @@ -96,7 +97,7 @@ export const Lyrics = () => { } else { setSongLyrics(null); } - }, [currentServer?.name, currentSong, override, remoteLyrics.data, remoteLyrics.isSuccess]); + }, [currentServer?.name, currentSong, override, remoteLyrics.data]); return (