[feat-75] support multiple drag and drop zip files for maps

fixed issue in map downloads where download listener in MapsDownloaderService was not being removed
This commit is contained in:
silentrald
2024-09-10 00:03:02 +08:00
parent 0f3de55e9e
commit 7c88ea46e0
14 changed files with 144 additions and 31 deletions
@@ -104,8 +104,16 @@ export function MapsPlaylistsPanel({ version, isActive }: Props) {
return playlistsManager.unlinkVersion(version);
}
const handleFileDrop = async (file: File) => {
if (file.type !== "application/zip") {
const handleFileDrop = async (files: FileList) => {
const paths: string[] = [];
for (let i = 0; i < files.length; ++i) {
const file = files[i];
if (file.type === "application/zip") {
paths.push(file.path);
}
}
if (paths.length === 0) {
notifications.notifyError({
title: "notifications.maps.import-map.titles.error",
desc: "notifications.maps.import-map.msgs.zip-accept-only"
@@ -113,15 +121,23 @@ export function MapsPlaylistsPanel({ version, isActive }: Props) {
return;
}
const localMap = await mapsManager.importMap(file.path, version);
if (localMap) {
const importCount = await mapsManager.importMaps(paths, version);
if (importCount === 0) {
return;
}
if (importCount < paths.length) {
notifications.notifySuccess({
title: "notifications.maps.import-map.titles.success",
desc: t("notifications.maps.import-map.msgs.success", {
mapName: localMap.rawInfo._songName
})
desc: "notifications.maps.import-map.msgs.some-success",
});
return;
}
notifications.notifySuccess({
title: "notifications.maps.import-map.titles.success",
desc: "notifications.maps.import-map.msgs.success",
});
}
const dropDownItems = ((): DropDownItem[] => {
@@ -201,7 +217,7 @@ export function MapsPlaylistsPanel({ version, isActive }: Props) {
<Dropzone
className="w-full h-full shrink-0"
onDrop={event => {
handleFileDrop(event.dataTransfer.files[0]);
handleFileDrop(event.dataTransfer.files);
}}
overlay={
<div className="text-3xl pointer-events-none">
@@ -113,14 +113,32 @@ export const LocalMapsListPanel = forwardRef<LocalMapsListPanelRef, Props>(({ ve
}
setMaps((maps$.value ? [map, ...maps$.value] : [map]));
}});
mapsManager.addImportListener(importListener);
}
return () => {
sub?.unsubscribe();
mapsManager.removeImportListener(importListener);
}
}, [isActiveOnce, version])
const importListener = (importMap: BsmLocalMap, targetVersion?: BSVersion) => {
if (!equal(targetVersion, version)) {
return;
}
const mapsCopy = maps ? [ ...maps ] : [];
// importMap can collide with existing map
const index = maps.findIndex(map => map.songDetails.name === importMap.songDetails.name);
if (index > -1) {
mapsCopy.splice(index, 1);
}
setMaps([importMap, ...mapsCopy]);
};
const loadMaps = () => {
setMaps(null);
loadPercent$.next(0);