[feature-75] rework fontend functions to import maps

This commit is contained in:
MathieuG-P
2024-10-16 20:35:34 +02:00
parent 4e6948693d
commit 9b8d6ddda0
3 changed files with 76 additions and 108 deletions
@@ -14,7 +14,7 @@ import { BsmButton } from "../shared/bsm-button.component";
import { MapIcon } from "../svgs/icons/map-icon.component";
import { PlaylistIcon } from "../svgs/icons/playlist-icon.component";
import { useObservable } from "renderer/hooks/use-observable.hook";
import { BehaviorSubject, of } from "rxjs";
import { BehaviorSubject, lastValueFrom, of } from "rxjs";
import { LocalPlaylistsListPanel, LocalPlaylistsListRef } from "./playlists/local-playlists-list-panel.component";
import { PlaylistsManagerService } from "renderer/services/playlists-manager.service";
import { BsmLocalMap } from "shared/models/maps/bsm-local-map.interface";
@@ -25,6 +25,7 @@ import { LocalPlaylistFilter, LocalPlaylistFilterPanel } from "./playlists/local
import { noop } from "shared/helpers/function.helpers";
import { Dropzone } from "../shared/dropzone.component";
import { NotificationService } from "renderer/services/notification.service";
import { logRenderError } from "renderer";
type Props = {
readonly version?: BSVersion;
@@ -122,7 +123,8 @@ export function MapsPlaylistsPanel({ version, isActive }: Props) {
return;
}
await mapsManager.importMaps(paths, version);
const import$ = mapsManager.importMaps(paths, version);
return lastValueFrom(import$).catch(logRenderError);
}
const dropDownItems = ((): DropDownItem[] => {
@@ -5,7 +5,7 @@ import { BsmLocalMap } from "shared/models/maps/bsm-local-map.interface";
import { Subscription, BehaviorSubject, lastValueFrom } from "rxjs";
import { MapFilter } from "shared/models/maps/beat-saver.model";
import { MapsDownloaderService } from "renderer/services/maps-downloader.service";
import { last, tap } from "rxjs/operators";
import { bufferTime, last, tap } from "rxjs/operators";
import { useTranslation } from "renderer/hooks/use-translation.hook";
import BeatConflict from "../../../../../assets/images/apngs/beat-conflict.png";
import { BsmImage } from "../../shared/bsm-image.component";
@@ -104,46 +104,28 @@ export const LocalMapsListPanel = forwardRef<LocalMapsListPanelRef, Props>(({ ve
useEffect(() => {
let sub: Subscription;
const subs: Subscription[] = [];
if(isActiveOnce){
sub = mapsDownloader.lastDownloadedMap$.subscribe({ next: ({map, version: targetVersion}) => {
subs.push(mapsDownloader.lastDownloadedMap$.subscribe({ next: ({map, version: targetVersion}) => {
if (!equal(targetVersion, version)) {
return;
}
setMaps((maps$.value ? [map, ...maps$.value] : [map]));
}});
mapsManager.addImportListener(importListener);
}}));
subs.push(mapsManager.$onMapImported(version).pipe(bufferTime(500)).subscribe({ next: newMaps => {
const maps = (maps$.value ?? []).filter(map => !newMaps.some(newMap => newMap.path === map.path));
setMaps([...newMaps, ...maps]);
}}));
}
return () => {
sub?.unsubscribe();
mapsManager.removeImportListener(importListener);
subs.forEach(s => s.unsubscribe());
}
}, [isActiveOnce, version])
const importListener = (importMaps: BsmLocalMap[], targetVersion?: BSVersion) => {
if (!equal(targetVersion, version)) {
return;
}
if (!maps$.value) {
setMaps(importMaps);
return;
}
const mapsCopy = [ ...maps$.value ];
for (const importMap of importMaps) {
const index = mapsCopy.findIndex(map => map.hash === importMap.hash);
if (index > -1) {
mapsCopy.splice(index, 1);
}
}
setMaps([...importMaps, ...mapsCopy]);
};
const loadMaps = () => {
setMaps(null);
loadPercent$.next(0);