Skip to content

Commit 79cc330

Browse files
authored
fix(account): fix not all results shown if result has a funbox unknown to stored filter (@fehmer) (monkeytypegame#6608)
Merge with default filters to ensure all languages and funboxes are present in the result filter.
1 parent 33a6bc0 commit 79cc330

File tree

4 files changed

+38
-31
lines changed

4 files changed

+38
-31
lines changed

frontend/src/ts/constants/default-result-filters.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { ResultFilters } from "@monkeytype/contracts/schemas/users";
22
import { deepClone } from "../utils/misc";
3+
import { LanguageList } from "./languages";
4+
import { getFunboxNames } from "@monkeytype/funbox";
35

46
const object: ResultFilters = {
57
_id: "default",
@@ -58,9 +60,10 @@ const object: ResultFilters = {
5860
tags: {
5961
none: true,
6062
},
61-
language: {},
63+
language: Object.fromEntries(LanguageList.map((lang) => [lang, true])),
6264
funbox: {
6365
none: true,
66+
...Object.fromEntries(getFunboxNames().map((funbox) => [funbox, true])),
6467
},
6568
};
6669

frontend/src/ts/controllers/account-controller.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import * as URLHandler from "../utils/url-handler";
1919
import * as Account from "../pages/account";
2020
import * as Alerts from "../elements/alerts";
2121
import * as AccountSettings from "../pages/account-settings";
22-
import { getAllFunboxes } from "@monkeytype/funbox";
2322
import {
2423
GoogleAuthProvider,
2524
GithubAuthProvider,
@@ -46,10 +45,8 @@ import * as ConnectionState from "../states/connection";
4645
import { navigate } from "./route-controller";
4746
import { FirebaseError } from "firebase/app";
4847
import * as PSA from "../elements/psa";
49-
import defaultResultFilters from "../constants/default-result-filters";
5048
import { getActiveFunboxesWithFunction } from "../test/funbox/list";
5149
import { Snapshot } from "../constants/default-snapshot";
52-
import { LanguageList } from "../constants/languages";
5350
import * as Sentry from "../sentry";
5451

5552
export const gmailProvider = new GoogleAuthProvider();
@@ -136,13 +133,6 @@ async function getDataAndInit(): Promise<boolean> {
136133

137134
ResultFilters.loadTags(snapshot.tags);
138135

139-
for (const language of LanguageList) {
140-
defaultResultFilters.language[language] = true;
141-
}
142-
143-
for (const funbox of getAllFunboxes()) {
144-
defaultResultFilters.funbox[funbox.name] = true;
145-
}
146136
// filters = defaultResultFilters;
147137
void ResultFilters.load();
148138

frontend/src/ts/elements/account/result-filters.ts

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ function save(): void {
9090

9191
export async function load(): Promise<void> {
9292
try {
93-
filters = resultFiltersLS.get();
93+
filters = mergeWithDefaultFilters(resultFiltersLS.get());
9494

9595
const newTags: Record<string, boolean> = { none: false };
9696
Object.keys(defaultResultFilters.tags).forEach((tag) => {
@@ -743,18 +743,37 @@ let selectChangeCallbackFn: () => void = () => {
743743
};
744744

745745
export function updateTagsDropdownOptions(): void {
746-
const el = document.querySelector<HTMLElement>(
747-
".pageAccount .content .filterButtons .buttonsAndTitle.tags .select select"
748-
);
749-
750-
if (!(el instanceof HTMLElement)) return;
751-
752746
const snapshot = DB.getSnapshot();
753747

754748
if (snapshot === undefined) {
755749
return;
756750
}
757751

752+
const newTags = snapshot.tags.filter(
753+
(it) => defaultResultFilters.tags[it._id] === undefined
754+
);
755+
if (newTags.length > 0) {
756+
const everythingSelected = Object.values(filters.tags).every((v) => v);
757+
758+
defaultResultFilters.tags = {
759+
...defaultResultFilters.tags,
760+
...Object.fromEntries(newTags.map((tag) => [tag._id, true])),
761+
};
762+
763+
filters.tags = {
764+
...filters.tags,
765+
...Object.fromEntries(
766+
newTags.map((tag) => [tag._id, everythingSelected])
767+
),
768+
};
769+
}
770+
771+
const el = document.querySelector<HTMLElement>(
772+
".pageAccount .content .filterButtons .buttonsAndTitle.tags .select select"
773+
);
774+
775+
if (!(el instanceof HTMLElement)) return;
776+
758777
let html = "";
759778

760779
html += "<option value='all'>all</option>";
@@ -907,15 +926,9 @@ $(".group.presetFilterButtons .filterBtns").on(
907926
);
908927

909928
function verifyResultFiltersStructure(filterIn: ResultFilters): ResultFilters {
910-
const filter = Misc.sanitize(ResultFiltersSchema, Misc.deepClone(filterIn));
911-
912-
Object.entries(defaultResultFilters).forEach((entry) => {
913-
const key = entry[0] as ResultFiltersGroup;
914-
const value = entry[1];
915-
if (filter[key] === undefined) {
916-
// @ts-expect-error key and value is based on default filter so this is safe to ignore
917-
filter[key] = value;
918-
}
919-
});
929+
const filter = mergeWithDefaultFilters(
930+
Misc.sanitize(ResultFiltersSchema, Misc.deepClone(filterIn))
931+
);
932+
920933
return filter;
921934
}

packages/contracts/src/schemas/users.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ import {
1010
QuoteLengthSchema,
1111
DifficultySchema,
1212
} from "./shared";
13-
import { CustomThemeColorsSchema } from "./configs";
13+
import { CustomThemeColorsSchema, FunboxNameSchema } from "./configs";
1414
import { doesNotContainProfanity } from "../validation/validation";
1515

16+
const NoneFilterSchema = z.literal("none");
1617
export const ResultFiltersSchema = z.object({
1718
_id: IdSchema,
1819
name: z
@@ -51,9 +52,9 @@ export const ResultFiltersSchema = z.object({
5152
all: z.boolean(),
5253
})
5354
.strict(),
54-
tags: z.record(z.string(), z.boolean()),
55+
tags: z.record(IdSchema.or(NoneFilterSchema), z.boolean()),
5556
language: z.record(LanguageSchema, z.boolean()),
56-
funbox: z.record(z.string(), z.boolean()),
57+
funbox: z.record(FunboxNameSchema.or(NoneFilterSchema), z.boolean()),
5758
});
5859
export type ResultFilters = z.infer<typeof ResultFiltersSchema>;
5960

0 commit comments

Comments
 (0)