Skip to content

Commit

Permalink
add TagJsonWithCount type
Browse files Browse the repository at this point in the history
  • Loading branch information
notbakaneko committed Dec 13, 2024
1 parent 93dc799 commit 0a3f673
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
14 changes: 8 additions & 6 deletions resources/js/beatmapsets-show/controller.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

import BeatmapJson from 'interfaces/beatmap-json';
import { BeatmapsetJsonForShow } from 'interfaces/beatmapset-extended-json';
import { TagJsonWithCount } from 'interfaces/tag-json';
import UserJson from 'interfaces/user-json';
import { keyBy } from 'lodash';
import { action, computed, makeObservable, observable, runInAction } from 'mobx';
Expand Down Expand Up @@ -73,15 +73,17 @@ export default class Controller {

@computed
get tags() {
const summedTags: Partial<Record<number, BeatmapJson['tags']>> = {};
const summedTags: Partial<Record<number, TagJsonWithCount>> = {};
for (const beatmap of this.beatmapset.beatmaps) {
if (beatmap.tags == null) continue;

for (const tag of beatmap.tags) {
if (summedTags[tag.id] != null) {
summedTags[tag.id].count += tag.count;
} else {
summedTags[tag.id] = tag;
const summedTag = summedTags[tag.id];
if (summedTag != null) {
summedTag.count += tag.count;
}

summedTags[tag.id] = summedTag;
}
}

Expand Down
4 changes: 2 additions & 2 deletions resources/js/interfaces/beatmap-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import BeatmapOwnerJson from './beatmap-owner-json';
import BeatmapsetJson from './beatmapset-json';
import Ruleset from './ruleset';
import TagJson from './tag-json';
import { TagJsonWithCount } from './tag-json';
import UserJson from './user-json';

interface BeatmapFailTimesArray {
Expand All @@ -18,7 +18,7 @@ interface BeatmapJsonAvailableIncludes {
failtimes: BeatmapFailTimesArray;
max_combo: number;
owners: BeatmapOwnerJson[];
tags: (TagJson & Required<Pick<TagJson, 'count'>>)[];
tags: TagJsonWithCount[];
user: UserJson;
}

Expand Down
2 changes: 2 additions & 0 deletions resources/js/interfaces/tag-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ export default interface TagJson {
id: number;
name: string;
}

export type TagJsonWithCount = TagJson & Required<Pick<TagJson, 'count'>>;

0 comments on commit 0a3f673

Please sign in to comment.