diff --git a/frontend/src/stores/roms.ts b/frontend/src/stores/roms.ts index fc8436b56..c5312b437 100644 --- a/frontend/src/stores/roms.ts +++ b/frontend/src/stores/roms.ts @@ -41,25 +41,16 @@ export default defineStore("roms", { }, actions: { - _reorder() { - // Sort roms by comparator string - this.allRoms = uniqBy(this.allRoms, "id").sort((a, b) => { - return a.sort_comparator.localeCompare(b.sort_comparator); - }); - - // Check if roms should be grouped - const groupRoms = isNull(localStorage.getItem("settings.groupRoms")) + _shouldGroupRoms(): boolean { + return isNull(localStorage.getItem("settings.groupRoms")) ? true : localStorage.getItem("settings.groupRoms") === "true"; - if (!groupRoms) { - this._grouped = this.allRoms; - return; - } - - // Group roms by external id - this._grouped = Object.values( + }, + _getGroupedRoms(roms: SimpleRom[]): SimpleRom[] { + // Group roms by external id. + return Object.values( groupBy( - this.allRoms, + roms, (game) => // If external id is null, generate a random id so that the roms are not grouped game.igdb_id || game.moby_id || nanoid(), @@ -70,7 +61,22 @@ export default defineStore("roms", { return ( games.find((game) => game.rom_user?.is_main_sibling) || games[0] ); - }) + }); + }, + _reorder() { + // Sort roms by comparator string + this.allRoms = uniqBy(this.allRoms, "id").sort((a, b) => { + return a.sort_comparator.localeCompare(b.sort_comparator); + }); + + // Check if roms should be grouped + if (!this._shouldGroupRoms()) { + this._grouped = this.allRoms; + return; + } + + // Group roms by external id + this._grouped = this._getGroupedRoms(this.allRoms) .sort((a, b) => { return a.sort_comparator.localeCompare(b.sort_comparator); }); @@ -82,7 +88,15 @@ export default defineStore("roms", { this.currentRom = rom; }, setRecentRoms(roms: SimpleRom[]) { - this.recentRoms = roms; + if (this._shouldGroupRoms()) { + // Group by external ID to only display a single entry per sibling, + // and sorted on rom ID in descending order. + this.recentRoms = this._getGroupedRoms(roms).sort( + (a, b) => b.id - a.id + ); + } else { + this.recentRoms = roms; + } }, setContinuePlayedRoms(roms: SimpleRom[]) { this.continuePlayingRoms = roms;