[bugfix] Steam and Oculus are not at the top of the versions list

This commit is contained in:
MathieuG-P
2023-09-20 17:32:45 +02:00
parent d8a4889298
commit 34936f6666
3 changed files with 24 additions and 14 deletions
@@ -30,7 +30,7 @@ export function NavBar() {
if (downloadingVersion){ versions.push(downloadingVersion); }
const sorted = versions.sort((a, b) => +b.ReleaseDate - +a.ReleaseDate);
const sorted = BSVersionManagerService.sortVersions(versions);
return BSVersionManagerService.removeDuplicateVersions(sorted);
}
@@ -5,7 +5,7 @@ import { ModalExitCode, ModalService } from "./modale.service";
import { NotificationService } from "./notification.service";
import { ProgressBarService } from "./progress-bar.service";
import { EditVersionModal } from "renderer/components/modal/modal-types/edit-version-modal.component";
import { swapElements } from "shared/helpers/array.helpers";
import { popElement } from "shared/helpers/array.helpers";
export class BSVersionManagerService {
private static instance: BSVersionManagerService;
@@ -34,18 +34,7 @@ export class BSVersionManagerService {
}
public setInstalledVersions(versions: BSVersion[]) {
const sorted: BSVersion[] = versions.sort((a, b) => +b.ReleaseDate - +a.ReleaseDate);
const steamIndex = sorted.findIndex(v => v.steam);
const oculusIndex = sorted.findIndex(v => v.oculus);
if (steamIndex > 0) {
swapElements(steamIndex, 0, sorted);
}
if (oculusIndex > 0) {
swapElements(oculusIndex, steamIndex >= 0 ? 1 : 0, sorted);
}
const sorted = BSVersionManagerService.sortVersions(versions);
this.installedVersions$.next(BSVersionManagerService.removeDuplicateVersions(sorted));
}
@@ -123,6 +112,19 @@ export class BSVersionManagerService {
return this.ipcService.sendV2("get-version-full-path", { args: version });
}
public static sortVersions(versions: BSVersion[]): BSVersion[] {
const steamVersion = popElement(v => v.steam, versions);
const oculusVersion = popElement(v => v.oculus, versions);
const compare = (a: BSVersion, b: BSVersion) => ( +b.ReleaseDate - +a.ReleaseDate )
const sorted: BSVersion[] = versions.sort(compare);
sorted.unshift(...[steamVersion, oculusVersion].filter(Boolean).sort(compare));
return sorted;
}
public static removeDuplicateVersions(versions: BSVersion[]): BSVersion[] {
return Array.from(new Map(versions.map(version => ([
JSON.stringify([
+8
View File
@@ -10,3 +10,11 @@ export function swapElements<T = unknown>(from: number, to: number, arr: T[]): T
[arr[from], arr[to]] = [arr[to], arr[from]];
return arr;
}
export function popElement<T = unknown>(func: (element: T) => boolean, arr: T[]): T {
const index = arr.findIndex(func);
if (index === -1) {
return null;
}
return arr.splice(index, 1)[0];
}