From 8b7028a6e5ea8bfd1e0e796341139ec718e387a7 Mon Sep 17 00:00:00 2001 From: Julian Mills Date: Tue, 8 Oct 2024 12:46:31 +0200 Subject: [PATCH 1/4] refactor: improve surrealist mini component --- src/components/SurrealistMini.astro | 40 ++++++++++++++++++----------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/src/components/SurrealistMini.astro b/src/components/SurrealistMini.astro index 0b586162d..8f4cf1eb4 100644 --- a/src/components/SurrealistMini.astro +++ b/src/components/SurrealistMini.astro @@ -1,30 +1,40 @@ --- type Props = { - dataset?: 'surreal-deal'; + url?: string; + dataset?: 'surreal-deal-store'; setup?: string; query?: string; variables?: string | Record; - theme?: 'automatic' | 'light' | 'dark'; - url?: string; + theme?: 'auto' | 'light' | 'dark'; height?: string; + compact?: boolean; + transparent?: boolean; }; -const { url: _url, height, ...props } = Astro.props; +const { url, height, ...props } = Astro.props; +const endpoint = new URL(url ?? "https://surrealist.app/mini"); +const config = new URLSearchParams(endpoint.search); + +const values = { + compact: false, + theme: 'auto', + ...props, +} + +for (const [k, v] of Object.entries(values)) { + if (v === false) { + config.delete(k); + } else { + config.set(k, typeof v === 'string' ? v : JSON.stringify(v)); + } +} + +endpoint.search = config.toString(); -props.variables = - typeof props.variables === 'string' - ? props.variables - : JSON.stringify(props.variables); -const stringified = Object.fromEntries( - Object.entries(props).map(([k, v]) => [k, (v ?? '').toString()]) -); -const search = new URLSearchParams(stringified).toString(); -// biome-ignore lint/style/useTemplate: We join here because otherwise it becomes a string literal nesting mess -const url = (_url ?? `https://surrealist.app/mini?${search}`) + '&compact'; ---