From 082d732b3fe6ee42c0383200899cf2a6712662f9 Mon Sep 17 00:00:00 2001 From: Kalle <38327916+Sendouc@users.noreply.github.com> Date: Sat, 19 Aug 2023 13:57:35 +0300 Subject: [PATCH] Fix joining q directly on Safari 15 crashing --- app/features/sendouq/q-schemas.server.ts | 3 ++- app/utils/zod.ts | 23 +++++++++++++---------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/app/features/sendouq/q-schemas.server.ts b/app/features/sendouq/q-schemas.server.ts index 21141b78b..dc48a4731 100644 --- a/app/features/sendouq/q-schemas.server.ts +++ b/app/features/sendouq/q-schemas.server.ts @@ -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"), diff --git a/app/utils/zod.ts b/app/utils/zod.ts index e5b2108f6..43a07f329 100644 --- a/app/utils/zod.ts +++ b/app/utils/zod.ts @@ -188,14 +188,17 @@ export function checkboxValueToDbBoolean(value: unknown) { } export const _action = (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; +}