From 31308ef1269c973fa6a7614438a81a52c24c659c Mon Sep 17 00:00:00 2001 From: whidix Date: Sat, 14 Mar 2026 17:13:13 +0100 Subject: [PATCH] refactor: enhance affiliation handling and improve type definitions in GuessHistoryTable component --- src/lib/components/GuessHistoryTable.svelte | 116 +++++++++++--------- 1 file changed, 62 insertions(+), 54 deletions(-) diff --git a/src/lib/components/GuessHistoryTable.svelte b/src/lib/components/GuessHistoryTable.svelte index 7f10399..1f3e4a3 100644 --- a/src/lib/components/GuessHistoryTable.svelte +++ b/src/lib/components/GuessHistoryTable.svelte @@ -4,7 +4,63 @@ export let selectedCharacters: CharacterWithRelations[]; export let dailyCharacter: CharacterWithRelations; - export let columnVisibility: any; + export let columnVisibility: { + status?: boolean; + gender?: boolean; + affiliations?: boolean; + devilFruitType?: boolean; + haki?: boolean; + bounty?: boolean; + height?: boolean; + origin?: boolean; + arc?: boolean; + }; + + function normalizeAffiliations(value: unknown): string[] { + if (Array.isArray(value)) { + return value + .map((entry) => (typeof entry === 'string' ? entry.trim() : '')) + .filter((entry) => entry.length > 0); + } + + if (typeof value === 'string') { + const trimmed = value.trim(); + if (trimmed.length === 0) { + return []; + } + + if (trimmed.startsWith('[')) { + try { + const parsed = JSON.parse(trimmed); + if (Array.isArray(parsed)) { + return parsed + .map((entry) => (typeof entry === 'string' ? entry.trim() : '')) + .filter((entry) => entry.length > 0); + } + } catch { + return []; + } + } + + return trimmed + .split(',') + .map((entry) => entry.trim()) + .filter((entry) => entry.length > 0); + } + + return []; + } + + function firstAffiliation(value: unknown): string | null { + const affiliations = normalizeAffiliations(value); + return affiliations.length > 0 ? affiliations[0] : null; + } + + function hasMatchingPrimaryAffiliation(characterAffiliations: unknown, dailyAffiliations: unknown): boolean { + const characterPrimary = firstAffiliation(characterAffiliations); + const dailyPrimary = firstAffiliation(dailyAffiliations); + return characterPrimary === dailyPrimary; + }
{#if columnVisibility.affiliations !== false}
- {#if character.affiliations} - {@const parsedAffiliations = - typeof character.affiliations === 'string' - ? character.affiliations.includes('[') - ? JSON.parse(character.affiliations) - : character.affiliations.split(',').map((a: string) => a.trim()) - : character.affiliations} - {#if Array.isArray(parsedAffiliations) && parsedAffiliations.length > 0} + {#if firstAffiliation(character.affiliations)}

- {parsedAffiliations[0]} + {firstAffiliation(character.affiliations)}

- {:else} -

- {parsedAffiliations} -

- {/if} {:else}

-