Fix joining q directly on Safari 15 crashing

This commit is contained in:
Kalle 2023-08-19 13:57:35 +03:00
parent c2b72246d0
commit 082d732b3f
2 changed files with 15 additions and 11 deletions

View File

@ -7,6 +7,7 @@ import {
import {
_action,
checkboxValueToBoolean,
deduplicate,
id,
safeJSONParse,
weaponSplId,
@ -18,7 +19,7 @@ export const frontPageSchema = z.union([
_action: _action("JOIN_QUEUE"),
mapListPreference: z.enum(MAP_LIST_PREFERENCE_OPTIONS),
mapPool: z.string(),
direct: z.literal("true").nullish(),
direct: z.preprocess(deduplicate, z.literal("true").nullish()),
}),
z.object({
_action: _action("JOIN_TEAM"),

View File

@ -188,14 +188,17 @@ export function checkboxValueToDbBoolean(value: unknown) {
}
export const _action = <T extends z.Primitive>(value: T) =>
z.preprocess((valueToParse) => {
if (typeof valueToParse === "string") return valueToParse;
// Fix bug at least in Safari 15 where SubmitButton value might get sent twice
if (Array.isArray(valueToParse)) {
const [one, two, ...rest] = valueToParse;
if (rest.length > 0) return valueToParse;
if (one !== two) return valueToParse;
z.preprocess(deduplicate, z.literal(value));
return one;
}
}, z.literal(value));
// Fix bug at least in Safari 15 where SubmitButton value might get sent twice
export function deduplicate(value: unknown) {
if (Array.isArray(value)) {
const [one, two, ...rest] = value;
if (rest.length > 0) return value;
if (one !== two) return value;
return one;
}
return value;
}