diff --git a/src/renderer/components/nav-bar/nav-bar.component.tsx b/src/renderer/components/nav-bar/nav-bar.component.tsx index 79275e2d..42ee5658 100644 --- a/src/renderer/components/nav-bar/nav-bar.component.tsx +++ b/src/renderer/components/nav-bar/nav-bar.component.tsx @@ -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); } diff --git a/src/renderer/services/bs-version-manager.service.ts b/src/renderer/services/bs-version-manager.service.ts index 9fca08db..d1f66d24 100644 --- a/src/renderer/services/bs-version-manager.service.ts +++ b/src/renderer/services/bs-version-manager.service.ts @@ -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([ diff --git a/src/shared/helpers/array.helpers.ts b/src/shared/helpers/array.helpers.ts index 3a3b6a97..4c52e2b7 100644 --- a/src/shared/helpers/array.helpers.ts +++ b/src/shared/helpers/array.helpers.ts @@ -10,3 +10,11 @@ export function swapElements(from: number, to: number, arr: T[]): T [arr[from], arr[to]] = [arr[to], arr[from]]; return arr; } + +export function popElement(func: (element: T) => boolean, arr: T[]): T { + const index = arr.findIndex(func); + if (index === -1) { + return null; + } + return arr.splice(index, 1)[0]; +}