mirror of
https://github.com/jeffvli/feishin.git
synced 2026-06-18 01:14:22 +02:00
fetch local lyrics first if preferred (#2100)
This commit is contained in:
@@ -109,12 +109,8 @@ export function computeSelectedFromResult(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const hasLocalLocal =
|
|
||||||
(Array.isArray(local) && local.length > 0) ||
|
|
||||||
(local != null && !Array.isArray(local) && 'lyrics' in local && Boolean(local.lyrics));
|
|
||||||
|
|
||||||
// If setting is set to prefer local lyrics, return the local lyrics if available
|
// If setting is set to prefer local lyrics, return the local lyrics if available
|
||||||
if (preferLocalLyrics && hasLocalLocal) {
|
if (preferLocalLyrics && hasLocalLyrics(local)) {
|
||||||
if (Array.isArray(local) && local.length > 0) {
|
if (Array.isArray(local) && local.length > 0) {
|
||||||
const item = local[Math.min(selectedStructuredIndex, local.length - 1)];
|
const item = local[Math.min(selectedStructuredIndex, local.length - 1)];
|
||||||
return { selected: item, selectedSynced: item.synced };
|
return { selected: item, selectedSynced: item.synced };
|
||||||
@@ -236,6 +232,13 @@ export function getDisplayOffset(
|
|||||||
return storedOffsetMs;
|
return storedOffsetMs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function hasLocalLyrics(local: FullLyricsMetadata | null | StructuredLyric[]): boolean {
|
||||||
|
return (
|
||||||
|
(Array.isArray(local) && local.length > 0) ||
|
||||||
|
(local != null && !Array.isArray(local) && 'lyrics' in local && Boolean(local.lyrics))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const emptyResult = (): LyricsQueryResult => ({
|
const emptyResult = (): LyricsQueryResult => ({
|
||||||
local: null,
|
local: null,
|
||||||
overrideData: null,
|
overrideData: null,
|
||||||
@@ -277,16 +280,11 @@ export const lyricsQueries = {
|
|||||||
const selectedOffsetMs = prev?.selectedOffsetMs ?? 0;
|
const selectedOffsetMs = prev?.selectedOffsetMs ?? 0;
|
||||||
const preferLocalLyrics = useSettingsStore.getState().lyrics.preferLocalLyrics;
|
const preferLocalLyrics = useSettingsStore.getState().lyrics.preferLocalLyrics;
|
||||||
|
|
||||||
// Fetch local lyrics
|
|
||||||
const localPromise = fetchLocalLyrics({ serverId: args.serverId, signal, song });
|
|
||||||
|
|
||||||
// Fetch remote auto lyrics
|
|
||||||
const remoteAutoPromise =
|
const remoteAutoPromise =
|
||||||
suppressRemoteAuto || !useSettingsStore.getState().lyrics.fetch
|
suppressRemoteAuto || !useSettingsStore.getState().lyrics.fetch
|
||||||
? null
|
? null
|
||||||
: fetchRemoteLyricsAuto(song);
|
: fetchRemoteLyricsAuto(song);
|
||||||
|
|
||||||
// Fetch override data
|
|
||||||
const overrideDataPromise = overrideSelection
|
const overrideDataPromise = overrideSelection
|
||||||
? fetchRemoteLyricsById({
|
? fetchRemoteLyricsById({
|
||||||
remoteSongId: overrideSelection.id,
|
remoteSongId: overrideSelection.id,
|
||||||
@@ -295,11 +293,40 @@ export const lyricsQueries = {
|
|||||||
})
|
})
|
||||||
: null;
|
: null;
|
||||||
|
|
||||||
const [local, remoteAuto, overrideData] = await Promise.all([
|
const localPromise = fetchLocalLyrics({ serverId: args.serverId, signal, song });
|
||||||
localPromise,
|
|
||||||
remoteAutoPromise,
|
let local: FullLyricsMetadata | null | StructuredLyric[];
|
||||||
overrideDataPromise,
|
let remoteAuto: FullLyricsMetadata | null;
|
||||||
]);
|
let overrideData: LyricsResponse | null;
|
||||||
|
|
||||||
|
if (preferLocalLyrics) {
|
||||||
|
local = await localPromise;
|
||||||
|
|
||||||
|
if (hasLocalLyrics(local)) {
|
||||||
|
overrideData = overrideDataPromise ? await overrideDataPromise : null;
|
||||||
|
remoteAuto = null;
|
||||||
|
|
||||||
|
if (remoteAutoPromise) {
|
||||||
|
void remoteAutoPromise.then((fetchedRemoteAuto) => {
|
||||||
|
if (signal.aborted || !fetchedRemoteAuto) return;
|
||||||
|
queryClient.setQueryData<LyricsQueryResult>(lyricsKey, (prev) =>
|
||||||
|
prev ? { ...prev, remoteAuto: fetchedRemoteAuto } : prev,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
[remoteAuto, overrideData] = await Promise.all([
|
||||||
|
remoteAutoPromise,
|
||||||
|
overrideDataPromise,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
[local, remoteAuto, overrideData] = await Promise.all([
|
||||||
|
localPromise,
|
||||||
|
remoteAutoPromise,
|
||||||
|
overrideDataPromise,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
const partial: Pick<
|
const partial: Pick<
|
||||||
LyricsQueryResult,
|
LyricsQueryResult,
|
||||||
@@ -320,13 +347,12 @@ export const lyricsQueries = {
|
|||||||
preferLocalLyrics,
|
preferLocalLyrics,
|
||||||
selectedStructuredIndex,
|
selectedStructuredIndex,
|
||||||
);
|
);
|
||||||
const displayOffset = getDisplayOffset(
|
const resultSelectedOffsetMs = getDisplayOffset(
|
||||||
selected,
|
selected,
|
||||||
selectedOffsetMs,
|
selectedOffsetMs,
|
||||||
selectedStructuredIndex,
|
selectedStructuredIndex,
|
||||||
local,
|
local,
|
||||||
);
|
);
|
||||||
const resultSelectedOffsetMs = displayOffset;
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...emptyResult(),
|
...emptyResult(),
|
||||||
|
|||||||
Reference in New Issue
Block a user