[feature-107] lot of changes but mainly, maps now use a cache and load almost instantly after first load

This commit is contained in:
MathieuG-P
2024-02-18 22:24:57 +01:00
parent 9a3ebf2971
commit b42231b134
43 changed files with 1256 additions and 305 deletions
@@ -3,13 +3,11 @@ import { BSVersion } from "shared/bs-version.interface";
import { forwardRef, useCallback, useEffect, useImperativeHandle, useRef, useState } from "react";
import { BsmLocalMap } from "shared/models/maps/bsm-local-map.interface";
import { Subscription, BehaviorSubject } from "rxjs";
import { MapFilter } from "shared/models/maps/beat-saver.model";
import { MapFilter, MapTag } from "shared/models/maps/beat-saver.model";
import { MapsDownloaderService } from "renderer/services/maps-downloader.service";
import { VariableSizeList } from "react-window";
import { MapsRow } from "./maps-row.component";
import { debounceTime, last, mergeMap, tap } from "rxjs/operators";
import { BeatSaverService } from "renderer/services/thrird-partys/beat-saver.service";
import { OsDiagnosticService } from "renderer/services/os-diagnostic.service";
import { debounceTime, last, tap } from "rxjs/operators";
import { useTranslation } from "renderer/hooks/use-translation.hook";
import BeatWaitingImg from "../../../../../assets/images/apngs/beat-waiting.png";
import BeatConflict from "../../../../../assets/images/apngs/beat-conflict.png";
@@ -33,8 +31,6 @@ type Props = {
export const LocalMapsListPanel = forwardRef(({ version, className, filter, search, linkedState, isActive }: Props, forwardRef) => {
const mapsManager = useService(MapsManagerService);
const mapsDownloader = useService(MapsDownloaderService);
const bsaver = useService(BeatSaverService);
const os = useService(OsDiagnosticService);
const t = useTranslation();
const ref = useRef(null);
@@ -43,7 +39,7 @@ export const LocalMapsListPanel = forwardRef(({ version, className, filter, sear
const [selectedMaps$] = useState(new BehaviorSubject<BsmLocalMap[]>([]));
const [itemPerRow, setItemPerRow] = useState(2);
const [listHeight, setListHeight] = useState(0);
const isActiveOnce = useChangeUntilEqual(isActive, { untilEqual: true, emitOnEqual: true });
const isActiveOnce = useChangeUntilEqual(isActive, { untilEqual: true });
const [linked, setLinked] = useState(false);
const [loadPercent$] = useState(new BehaviorSubject(0));
@@ -135,21 +131,10 @@ export const LocalMapsListPanel = forwardRef(({ version, className, filter, sear
loadMapsObs$
.pipe(
tap(progress => loadPercent$.next(Math.floor((progress.loaded / progress.total) * 100))),
last(),
mergeMap(async progress => {
if (os.isOffline) {
return progress.maps;
}
const { maps } = progress;
const details = await bsaver.getMapDetailsFromHashs(maps.map(map => map.hash));
return maps.map(map => {
map.bsaverInfo = details.find(d => d.versions.at(0).hash === map.hash);
return map;
});
})
last()
)
.subscribe({
next: maps => setMaps(() => maps),
next: progress => setMaps(() => progress.maps),
complete: () => loadPercent$.next(0),
})
);
@@ -194,13 +179,14 @@ export const LocalMapsListPanel = forwardRef(({ version, className, filter, sear
// Can be more clean and optimized i think
const fitEnabledTags = (() => {
if (!filter?.enabledTags || filter.enabledTags.size === 0) {
return true;
}
if (!map?.bsaverInfo?.tags) {
if (!map?.songDetails?.tags) {
return false;
}
return Array.from(filter.enabledTags.values()).every(tag => map.bsaverInfo.tags.some(mapTag => mapTag === tag));
return Array.from(filter.enabledTags.values()).every(tag => map.songDetails.tags.some(mapTag => mapTag === tag));
})();
if (!fitEnabledTags) {
@@ -211,10 +197,10 @@ export const LocalMapsListPanel = forwardRef(({ version, className, filter, sear
if (!filter?.excludedTags || filter.excludedTags.size === 0) {
return true;
}
if (!map?.bsaverInfo?.tags) {
if (!map?.songDetails?.tags) {
return true;
}
return !map.bsaverInfo.tags.some(tag => filter.excludedTags.has(tag));
return !map.songDetails?.tags.some(tag => filter.excludedTags.has(tag as MapTag));
})();
if (!fitExcluedTags) {
@@ -225,12 +211,8 @@ export const LocalMapsListPanel = forwardRef(({ version, className, filter, sear
if (!filter?.minNps) {
return true;
}
if (!map?.bsaverInfo?.versions?.at(0)) {
return false;
}
return !map.bsaverInfo.versions.some(version => {
return version.diffs.some(diff => diff.nps < filter.minNps);
});
return map.songDetails?.difficulties.some(diff => diff.nps > filter.minNps);
})();
if (!fitMinNps) {
@@ -241,12 +223,8 @@ export const LocalMapsListPanel = forwardRef(({ version, className, filter, sear
if (!filter?.maxNps) {
return true;
}
if (!map?.bsaverInfo?.versions?.at(0)) {
return false;
}
return !map.bsaverInfo.versions.some(version => {
return version.diffs.some(diff => diff.nps > filter.maxNps);
});
return map.songDetails?.difficulties.some(diff => diff.nps < filter.maxNps);
})();
if (!fitMaxNps) {
@@ -258,10 +236,10 @@ export const LocalMapsListPanel = forwardRef(({ version, className, filter, sear
return true;
}
if (!map?.bsaverInfo?.metadata?.duration) {
if (!map?.songDetails?.metadata?.duration) {
return false;
}
return map.bsaverInfo.metadata.duration >= filter.minDuration;
return map.songDetails?.metadata.duration >= filter.minDuration;
})();
if (!fitMinDuration) {
@@ -272,10 +250,10 @@ export const LocalMapsListPanel = forwardRef(({ version, className, filter, sear
if (!filter?.maxDuration) {
return true;
}
if (!map?.bsaverInfo?.metadata?.duration) {
if (!map?.songDetails?.metadata?.duration) {
return false;
}
return map.bsaverInfo.metadata.duration <= filter.maxDuration;
return map.songDetails?.metadata.duration <= filter.maxDuration;
})();
if (!fitMaxDuration) {
@@ -286,10 +264,7 @@ export const LocalMapsListPanel = forwardRef(({ version, className, filter, sear
if (!filter?.noodle) {
return true;
}
if (!map?.bsaverInfo?.versions?.at(0)) {
return false;
}
return map.bsaverInfo.versions.some(version => version.diffs.some(diff => !!diff.ne));
return map.songDetails?.difficulties.some(diff => !!diff.ne);
})();
if (!fitNoodle) {
@@ -300,10 +275,8 @@ export const LocalMapsListPanel = forwardRef(({ version, className, filter, sear
if (!filter?.me) {
return true;
}
if (!map?.bsaverInfo?.versions?.at(0)) {
return false;
}
return map.bsaverInfo.versions.some(version => version.diffs.some(diff => !!diff.me));
return map.songDetails?.difficulties.some(diff => !!diff.me);
})();
if (!fitMe) {
@@ -314,10 +287,8 @@ export const LocalMapsListPanel = forwardRef(({ version, className, filter, sear
if (!filter?.cinema) {
return true;
}
if (!map?.bsaverInfo?.versions?.at(0)) {
return false;
}
return map.bsaverInfo.versions.some(version => version.diffs.some(diff => !!diff.cinema));
return map.songDetails?.difficulties.some(diff => !!diff.cinema);
})();
if (!fitCinema) {
@@ -328,10 +299,8 @@ export const LocalMapsListPanel = forwardRef(({ version, className, filter, sear
if (!filter?.chroma) {
return true;
}
if (!map?.bsaverInfo?.versions?.at(0)) {
return false;
}
return map.bsaverInfo.versions.some(version => version.diffs.some(diff => !!diff.chroma));
return map.songDetails?.difficulties.some(diff => !!diff.chroma);
})();
if (!fitChroma) {
@@ -342,31 +311,29 @@ export const LocalMapsListPanel = forwardRef(({ version, className, filter, sear
if (!filter?.fullSpread) {
return true;
}
if (!map?.bsaverInfo?.versions?.at(0)) {
return false;
}
return map.bsaverInfo.versions.some(version => version?.diffs?.length >= 5);
return map.songDetails?.difficulties.length >= 5;
})();
if (!fitFullSpread) {
return false;
}
if (!(filter?.automapper ? map.bsaverInfo?.automapper === filter.automapper : true)) {
if (filter?.automapper && (map.songDetails && !map.songDetails?.automapper)) {
return false;
}
if (!(filter?.ranked ? map.bsaverInfo?.ranked === filter.ranked : true)) {
if (!(filter?.ranked ? map.songDetails?.ranked === filter.ranked : true)) {
return false;
}
if (!(filter?.curated ? !!map.bsaverInfo?.curatedAt : true)) {
if (!(filter?.curated ? !!map.songDetails?.curated === filter.curated : true)) {
return false;
}
if (!(filter?.verified ? !!map.bsaverInfo?.uploader?.verifiedMapper : true)) {
if (!(filter?.verified ? !!map.songDetails?.uploader?.verified : true)) {
return false;
}
const searchCheck = (() => {
return ((map.rawInfo?._songName ?? map.bsaverInfo?.name) || "")?.toLowerCase().includes(search.toLowerCase()) || ((map.rawInfo?._songAuthorName ?? map.bsaverInfo?.metadata?.songAuthorName) || "")?.toLowerCase().includes(search.toLowerCase()) || ((map.rawInfo?._levelAuthorName ?? map.bsaverInfo?.metadata?.levelAuthorName) || "")?.toLowerCase().includes(search.toLowerCase());
return ((map.rawInfo?._songName ?? map.songDetails?.name) || "")?.toLowerCase().includes(search.toLowerCase()) || ((map.rawInfo?._songAuthorName ?? map.songDetails?.metadata?.songAuthorName) || "")?.toLowerCase().includes(search.toLowerCase()) || ((map.rawInfo?._levelAuthorName ?? map.songDetails?.metadata?.levelAuthorName) || "")?.toLowerCase().includes(search.toLowerCase());
})();
if (!searchCheck) {
@@ -1,5 +1,5 @@
import { BsmImage } from "../../shared/bsm-image.component";
import { BsvMapCharacteristic, BsvMapDifficultyType } from "shared/models/maps/beat-saver.model";
import { BsvMapDifficultyType } from "shared/models/maps/beat-saver.model";
import { useThemeColor } from "renderer/hooks/use-theme-color.hook";
import { BsmLink } from "../../shared/bsm-link.component";
import { BsmIcon } from "../../svgs/bsm-icon.component";
@@ -22,8 +22,9 @@ import { GlowEffect } from "../../shared/glow-effect.component";
import { useDelayedState } from "renderer/hooks/use-delayed-state.hook";
import { useService } from "renderer/hooks/use-service.hook";
import Tippy from "@tippyjs/react";
import { SongDetailDiffCharactertistic, SongDiffName } from "shared/models/maps";
export type ParsedMapDiff = { type: BsvMapDifficultyType; name: string; stars: number };
export type ParsedMapDiff = { type: SongDiffName; name: string; stars: number };
export type MapItemProps<T = unknown> = {
hash: string;
@@ -34,12 +35,12 @@ export type MapItemProps<T = unknown> = {
songUrl: string;
autorId: number;
mapId: string;
diffs: Map<BsvMapCharacteristic, ParsedMapDiff[]>;
diffs: Map<SongDetailDiffCharactertistic, ParsedMapDiff[]>;
ranked: boolean;
bpm: number;
duration: number;
likes: number;
createdAt: string;
createdAt: number;
selected?: boolean;
downloading?: boolean;
showOwned?: boolean;
@@ -165,7 +166,7 @@ export const MapItem = memo(({ hash, title, autor, songAutor, coverUrl, songUrl,
<li key={`${type}${name}${stars}`} className="w-full h-4 flex items-center gap-1">
<BsmIcon className="h-full w-fit p-px" icon={charac} />
<span className="h-full px-2 flex items-center text-xs font-bold bg-current rounded-full" style={{ color: MAP_DIFFICULTIES_COLORS[type] }}>
{stars ? <span className="h-full block brightness-[.25]">★ {stars}</span> : <span className="h-full brightness-[.25] leading-4 pb-[2px] capitalize">{parseDiffLabel(type)}</span>}
{stars ? <span className="h-full block brightness-[.25]">★ {stars.toFixed(2)}</span> : <span className="h-full brightness-[.25] leading-4 pb-[2px] capitalize">{parseDiffLabel(type)}</span>}
</span>
<span className="text-sm leading-4 pb-[2px] capitalize">{parseDiffLabel(name)}</span>
</li>
@@ -2,10 +2,10 @@ import { CSSProperties, memo } from "react";
import { useObservable } from "renderer/hooks/use-observable.hook";
import { distinctUntilChanged, map } from "rxjs/operators";
import { BehaviorSubject } from "rxjs";
import { BsvMapCharacteristic } from "shared/models/maps/beat-saver.model";
import { BsmLocalMap } from "shared/models/maps/bsm-local-map.interface";
import { ParsedMapDiff, MapItem } from "./map-item.component";
import equal from "fast-deep-equal/es6";
import { SongDetailDiffCharactertistic } from "shared/models/maps/song-details-cache.model";
type Props = {
maps: BsmLocalMap[];
@@ -21,10 +21,10 @@ export const MapsRow = memo(({ maps, style, selectedMaps$, onMapSelect, onMapDel
distinctUntilChanged(equal),
), []);
const extractMapDiffs = (map: BsmLocalMap): Map<BsvMapCharacteristic, ParsedMapDiff[]> => {
const res = new Map<BsvMapCharacteristic, ParsedMapDiff[]>();
if (map.bsaverInfo?.versions[0]?.diffs) {
map.bsaverInfo.versions[0].diffs.forEach(diff => {
const extractMapDiffs = (map: BsmLocalMap): Map<SongDetailDiffCharactertistic, ParsedMapDiff[]> => {
const res = new Map<SongDetailDiffCharactertistic, ParsedMapDiff[]>();
if (map.songDetails?.difficulties) {
map.songDetails?.difficulties.forEach(diff => {
const arr = res.get(diff.characteristic) || [];
const diffName = map.rawInfo._difficultyBeatmapSets.find(set => set._beatmapCharacteristicName === diff.characteristic)._difficultyBeatmaps.find(rawDiff => rawDiff._difficulty === diff.difficulty)?._customData?._difficultyLabel || diff.difficulty;
arr.push({ name: diffName, type: diff.difficulty, stars: diff.stars });
@@ -45,24 +45,26 @@ export const MapsRow = memo(({ maps, style, selectedMaps$, onMapSelect, onMapDel
};
const renderMapItem = (map: BsmLocalMap) => {
return <MapItem
key={map.hash}
hash={map.hash}
title={map.rawInfo._songName}
coverUrl={map.coverUrl}
songUrl={map.songUrl}
autor={map.rawInfo._levelAuthorName}
songAutor={map.rawInfo._songAuthorName}
bpm={map.rawInfo._beatsPerMinute}
duration={map.bsaverInfo?.metadata?.duration}
selected={selectedMaps.some(selected => selected.hash === map.hash)}
diffs={extractMapDiffs(map)} mapId={map.bsaverInfo?.id}
ranked={map.bsaverInfo?.ranked}
autorId={map.bsaverInfo?.uploader?.id}
likes={map.bsaverInfo?.stats?.upvotes}
createdAt={map.bsaverInfo?.createdAt}
onDelete={onMapDelete}
onSelected={onMapSelect}
return <MapItem
key={map.hash}
hash={map.hash}
title={map.rawInfo._songName}
coverUrl={map.coverUrl}
songUrl={map.songUrl}
autor={map.rawInfo._levelAuthorName}
songAutor={map.rawInfo._songAuthorName}
bpm={map.rawInfo._beatsPerMinute}
duration={map.songDetails?.metadata.duration}
selected={selectedMaps.some(selected => selected.hash === map.hash)}
diffs={extractMapDiffs(map)}
mapId={map.songDetails?.id}
ranked={map.songDetails?.ranked}
autorId={map.songDetails?.uploader.id}
likes={map.songDetails?.upVotes}
createdAt={map.songDetails?.uploadedAt}
onDelete={onMapDelete}
onSelected={onMapSelect}
callBackParam={map}
/>;
};
@@ -1,5 +1,8 @@
import { useChangeUntilEqual } from "renderer/hooks/use-change-until-equal.hook";
import { useOnUpdate } from "renderer/hooks/use-on-update.hook";
import { FolderLinkState } from "renderer/services/version-folder-linker.service";
import { BSVersion } from "shared/bs-version.interface";
import { noop } from "shared/helpers/function.helpers";
type Props = {
version: BSVersion;
@@ -9,6 +12,17 @@ type Props = {
};
export function LocalPlaylistsListPanel({ version, className, isActive, linkedState }: Props) {
const isActiveOnce = useChangeUntilEqual(isActive, { untilEqual: true });
useOnUpdate(() => {
if(!isActiveOnce){ return noop(); }
// load playlists
}, [isActiveOnce]);
return (
<div>local-playlists-list-panel.component</div>
)