Update SpotifyLyrics!! (#252)

* 1

* Update SpotifyLyrics!!

* Update pnpm-lock.yaml

---------

Co-authored-by: Crxa <c@crxaw.tech>
Co-authored-by: thororen <78185467+thororen1234@users.noreply.github.com>
This commit is contained in:
Crxaw 2025-05-04 02:00:29 +01:00 committed by GitHub
parent c9d8f3cc72
commit fa7f7f2d81
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 19 additions and 44 deletions

View file

@ -49,7 +49,7 @@ export async function getLyricsLrclib(track: Track): Promise<LyricsData | null>
if (!data.syncedLyrics) return null;
const lyrics = data.syncedLyrics;
const lines = lyrics.split("\n");
const lines = lyrics.split("\n").filter(line => line.trim() !== "");
return {
useLyric: Provider.Lrclib,

View file

@ -102,38 +102,20 @@ export const SpotifyLrcStore = proxyLazyWebpack(() => {
store.emitChange();
return;
}
// stops spamming noftications for translation when there is no lyrics x1
if (provider === Provider.Translated || provider === Provider.Romanized) {
if (!currentInfo?.useLyric || !currentInfo.lyricsVersions[currentInfo.useLyric]) {
console.log("Failed to Translate");
const now = Date.now();
if (!window.__lastTranslateFailure) {
window.__lastTranslateFailure = now;
} else if (now - window.__lastTranslateFailure < 120000) { // 2 minutes
window.__lastTranslateFailure = null;
return null;
} else {
window.__lastTranslateFailure = now;
}
return null;
if (!currentInfo?.lyricsVersions[Provider.Spotify] && !currentInfo?.lyricsVersions[Provider.Lrclib]) {
showNotif("No lyrics", `No lyrics to ${provider === Provider.Translated ? "translate" : "romanize"}`);
return;
}
const fetcher = provider === Provider.Translated ? translateLyrics : romanizeLyrics;
const fetchResult = await fetcher(currentInfo.lyricsVersions[currentInfo.useLyric]);
// stops spamming noftications for when there is no lyrics / cannot be translated x2
if (!fetchResult) {
console.log("Lyrics fetch failed", `Failed to fetch ${provider === Provider.Translated ? "translation" : "romanization"}`);
const now = Date.now();
if (!window.__lastTranslateFailure) {
window.__lastTranslateFailure = now;
} else if (now - window.__lastTranslateFailure < 120000) { // 2 minutes
window.__lastTranslateFailure = null;
return null;
} else {
window.__lastTranslateFailure = now;
}
return null;
showNotif("Lyrics fetch failed", `Failed to fetch ${provider === Provider.Translated ? "translation" : "romanization"}`);
return;
}
store.lyricsInfo = {
@ -141,7 +123,7 @@ export const SpotifyLrcStore = proxyLazyWebpack(() => {
useLyric: provider,
lyricsVersions: {
...currentInfo.lyricsVersions,
[Provider.Translated]: fetchResult
[provider]: fetchResult
}
};
@ -152,19 +134,9 @@ export const SpotifyLrcStore = proxyLazyWebpack(() => {
}
const newLyricsInfo = await lyricFetchers[e.provider](store.track!);
// stops spamming noftications for when there is no lyrics / cannot be translated x3
if (!newLyricsInfo) {
console.log("Lyrics fetch failed", `Failed to fetch ${e.provider} lyrics`);
const now = Date.now();
if (!window.__lastLyricsFetchFailure) {
window.__lastLyricsFetchFailure = now;
} else if (now - window.__lastLyricsFetchFailure < 120000) { // 2 minutes
window.__lastLyricsFetchFailure = null;
return null;
} else {
window.__lastLyricsFetchFailure = now;
}
return null;
showNotif("Lyrics fetch failed", `Failed to fetch ${e.provider} lyrics`);
return;
}
store.lyricsInfo = newLyricsInfo;

View file

@ -8,7 +8,7 @@ import { definePluginSettings } from "@api/Settings";
import { makeRange, SettingSliderComponent } from "@components/PluginSettings/components";
import { useAwaiter } from "@utils/react";
import { OptionType } from "@utils/types";
import { Button, showToast, Text, Toasts } from "@webpack/common";
import { Button, showToast, Text, Toasts, useMemo } from "@webpack/common";
import { clearLyricsCache, getLyricsCount, removeTranslations } from "./api";
import { Lyrics } from "./components/lyrics";
@ -24,10 +24,13 @@ const sliderOptions = {
function Details() {
const { lyricsInfo } = useLyrics();
const [count, error, loading] = useAwaiter(getLyricsCount, {
onError: () => console.error("Failed to get lyrics count"),
fallbackValue: null,
});
const [count, error, loading] = useAwaiter(
useMemo(() => getLyricsCount, []),
{
onError: () => console.error("Failed to get lyrics count"),
fallbackValue: null,
}
);
return (
<>