mirror of
https://github.com/Zagrios/bs-manager.git
synced 2026-07-03 14:08:25 +02:00
[bugfix-342] Fix unable to download playlist with OneClick from certain sites
This commit is contained in:
@@ -1,85 +1,69 @@
|
||||
import { motion } from "framer-motion";
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import { useEffect, useRef } from "react";
|
||||
import { BsmProgressBar } from "renderer/components/progress-bar/bsm-progress-bar.component";
|
||||
import { BsmImage } from "renderer/components/shared/bsm-image.component";
|
||||
import TitleBar from "renderer/components/title-bar/title-bar.component";
|
||||
import { useObservable } from "renderer/hooks/use-observable.hook";
|
||||
import { useTranslation } from "renderer/hooks/use-translation.hook";
|
||||
import { IpcService } from "renderer/services/ipc.service";
|
||||
import { NotificationService } from "renderer/services/notification.service";
|
||||
import { PlaylistDownloaderService } from "renderer/services/playlist-downloader.service";
|
||||
import { BeatSaverService } from "renderer/services/thrird-partys/beat-saver.service";
|
||||
import { map, filter } from "rxjs/operators";
|
||||
import { BsvPlaylist } from "shared/models/maps/beat-saver.model";
|
||||
import { map, filter, take, lastValueFrom } from "rxjs";
|
||||
import defaultImage from "../../../../assets/images/default-version-img.jpg";
|
||||
import { useService } from "renderer/hooks/use-service.hook";
|
||||
import { lastValueFrom } from "rxjs";
|
||||
import { useConstant } from "renderer/hooks/use-constant.hook";
|
||||
import { BPList } from "shared/models/playlists/playlist.interface";
|
||||
import { useOnUpdate } from "renderer/hooks/use-on-update.hook";
|
||||
|
||||
export default function OneClickDownloadPlaylist() {
|
||||
|
||||
|
||||
const ipc = useService(IpcService);
|
||||
const bSaver = useService(BeatSaverService);
|
||||
const playlistDownloader = useService(PlaylistDownloaderService);
|
||||
const mapsContainer = useRef<HTMLDivElement>(null);
|
||||
const notification = useService(NotificationService);
|
||||
|
||||
const [playlist, setPlaylist] = useState<BsvPlaylist>(null);
|
||||
const downloadedMap = useObservable(
|
||||
playlistDownloader.progress$.pipe(
|
||||
filter(download => !!download),
|
||||
map(download => [...(download.downloadedMaps ?? []), download?.current])
|
||||
),
|
||||
[]
|
||||
);
|
||||
const t = useTranslation();
|
||||
|
||||
const cover = playlist ? playlist.playlistImage : null;
|
||||
const title = playlist ? playlist.name : null;
|
||||
const playlistDownloader = useService(PlaylistDownloaderService);
|
||||
const notification = useService(NotificationService);
|
||||
|
||||
const mapsContainer = useRef<HTMLDivElement>(null);
|
||||
const playlistUrl = useConstant(() => new URLSearchParams(window.location.search).get("playlistUrl"));
|
||||
const download$ = useConstant(() => playlistDownloader.oneClickInstallPlaylist(playlistUrl));
|
||||
const playlistInfos = useObservable<BPList>(download$.pipe(filter(progress => !!progress.data?.playlistInfos), map(progress => progress.data.playlistInfos), take(1)));
|
||||
const downloadedMaps = useObservable(download$.pipe(filter(progress => !!progress.data?.downloadedMaps), map(progress => progress.data.downloadedMaps)));
|
||||
|
||||
useEffect(() => {
|
||||
|
||||
const promise = (async () => {
|
||||
const infos = await lastValueFrom(ipc.sendV2<{ bpListUrl: string; id: string }>("one-click-playlist-info"));
|
||||
|
||||
bSaver.getPlaylistDetailsById(infos.id).then(details => setPlaylist(details));
|
||||
|
||||
return lastValueFrom(playlistDownloader.oneClickInstallPlaylist(infos.bpListUrl));
|
||||
})();
|
||||
|
||||
promise.catch(() => {
|
||||
notification.notifySystem({ title: t("notifications.types.error"), body: t("notifications.playlists.one-click-install.error") });
|
||||
});
|
||||
|
||||
promise.then(() => {
|
||||
lastValueFrom(download$).then(() => {
|
||||
notification.notifySystem({ title: "OneClick", body: t("notifications.playlists.one-click-install.success") });
|
||||
});
|
||||
|
||||
promise.finally(() => {
|
||||
}).catch(() => {
|
||||
notification.notifySystem({ title: t("notifications.types.error"), body: t("notifications.playlists.one-click-install.error") });
|
||||
}).finally(() => {
|
||||
window.electron.window.close();
|
||||
});
|
||||
|
||||
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
setTimeout(
|
||||
() =>
|
||||
mapsContainer.current.scrollTo({
|
||||
left: mapsContainer.current.scrollWidth,
|
||||
behavior: "smooth",
|
||||
}),
|
||||
500
|
||||
);
|
||||
}, [downloadedMap]);
|
||||
useOnUpdate(() => {
|
||||
setTimeout(() => {
|
||||
mapsContainer.current.scrollTo({ left: mapsContainer.current.scrollWidth, behavior: "smooth",});
|
||||
}, 500);
|
||||
}, [downloadedMaps]);
|
||||
|
||||
const playlistImage = () => {
|
||||
if(!playlistInfos?.image) return defaultImage;
|
||||
if (playlistInfos?.image.startsWith("data:image")) {
|
||||
return playlistInfos?.image;
|
||||
}
|
||||
return `data:image/png;base64,${playlistInfos?.image}`;
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="relative w-screen h-screen overflow-hidden">
|
||||
{playlist && <BsmImage className="absolute top-0 left-0 w-full h-full" image={playlist.playlistImage} />}
|
||||
{playlistInfos && <BsmImage className="absolute top-0 left-0 w-full h-full" placeholder={defaultImage} image={playlistImage()} errorImage={defaultImage} />}
|
||||
<div className="w-full h-full backdrop-brightness-50 backdrop-blur-md flex flex-col justify-start items-center">
|
||||
<TitleBar template="oneclick-download-playlist.html" />
|
||||
<BsmImage className="mt-2 aspect-square w-1/2 object-cover rounded-md shadow-black shadow-lg" placeholder={defaultImage} image={cover} errorImage={defaultImage} />
|
||||
<h1 className="mt-4 overflow-hidden font-bold italic text-xl text-gray-200 tracking-wide w-full text-center whitespace-nowrap text-ellipsis px-2">{title}</h1>
|
||||
<BsmImage className="mt-2 aspect-square w-1/2 object-cover rounded-md shadow-black shadow-lg" placeholder={defaultImage} image={playlistImage()} errorImage={defaultImage} />
|
||||
<h1 className="mt-4 overflow-hidden font-bold italic text-xl text-gray-200 tracking-wide w-full text-center whitespace-nowrap text-ellipsis px-2">{playlistInfos?.playlistTitle}</h1>
|
||||
<div className="w-full py-3 flex items-center justify-center max-w-full overflow-x-scroll overflow-y-hidden scrollbar scrollbar-thin scrollbar-track-transparent scrollbar-thumb-neutral-900" ref={mapsContainer}>
|
||||
<div className="flex justify-start items-start gap-2.5">{downloadedMap?.map(map => map?.versions?.at(0)?.coverURL && <motion.img layout="position" key={map.id} className="block aspect-square w-14 object-cover rounded-md shadow-black shadow-md" src={map?.versions?.at(0)?.coverURL} initial={{ scale: 0 }} animate={{ scale: 1 }} whileHover={{ rotate: 5 }} />)}</div>
|
||||
<div className="flex justify-start items-start gap-2.5">{downloadedMaps?.map(map => map?.coverUrl &&
|
||||
<motion.img layout="position" key={map.hash} className="block aspect-square w-14 object-cover rounded-md shadow-black shadow-md" src={map?.coverUrl} initial={{ scale: 0 }} animate={{ scale: 1 }} whileHover={{ rotate: 5 }} />
|
||||
)}</div>
|
||||
</div>
|
||||
</div>
|
||||
<BsmProgressBar />
|
||||
|
||||
Reference in New Issue
Block a user