From f41a90a7b86e198c01bc49cf420f18003a040483 Mon Sep 17 00:00:00 2001 From: Nya Candy Date: Tue, 20 Jun 2023 01:14:32 +0800 Subject: [PATCH] fix: init attributes when undefined (#657) --- src/queries/site.ts | 124 +++++++++++++------------------------------- 1 file changed, 35 insertions(+), 89 deletions(-) diff --git a/src/queries/site.ts b/src/queries/site.ts index f454d9ad..886f9c24 100644 --- a/src/queries/site.ts +++ b/src/queries/site.ts @@ -184,97 +184,43 @@ export function useUpdateSite() { } // attributes - if (input.navigation !== undefined) { - const navigation = metadataDraft.attributes?.find( - (attr) => attr.trait_type === "xlog_navigation", - ) - if (navigation) { - navigation.value = JSON.stringify(input.navigation) - } else { - metadataDraft.attributes?.push({ - trait_type: "xlog_navigation", - value: JSON.stringify(input.navigation), - }) - } - } - if (input.css !== undefined) { - const css = metadataDraft.attributes?.find( - (attr) => attr.trait_type === "xlog_css", - ) - if (css) { - css.value = input.css - } else { - metadataDraft.attributes?.push({ - trait_type: "xlog_css", - value: input.css, - }) - } - } - if (input.ga !== undefined) { - const ga = metadataDraft.attributes?.find( - (attr) => attr.trait_type === "xlog_ga", - ) - if (ga) { - ga.value = input.ga - } else { - metadataDraft.attributes?.push({ - trait_type: "xlog_ga", - value: input.ga, - }) - } - } - if (input.ua !== undefined) { - const ua = metadataDraft.attributes?.find( - (attr) => attr.trait_type === "xlog_ua", - ) - if (ua) { - ua.value = input.ua - } else { - metadataDraft.attributes?.push({ - trait_type: "xlog_ua", - value: input.ua, - }) - } - } - if (input.uh !== undefined) { - const uh = metadataDraft.attributes?.find( - (attr) => attr.trait_type === "xlog_uh", - ) - if (uh) { - uh.value = input.uh - } else { - metadataDraft.attributes?.push({ - trait_type: "xlog_uh", - value: input.uh, - }) - } - } - if (input.custom_domain !== undefined) { - const custom_domain = metadataDraft.attributes?.find( - (attr) => attr.trait_type === "xlog_custom_domain", - ) - if (custom_domain) { - custom_domain.value = input.custom_domain - } else { - metadataDraft.attributes?.push({ - trait_type: "xlog_custom_domain", - value: input.custom_domain, - }) - } - } - if (input.site_name !== undefined) { - const site_name = metadataDraft.attributes?.find( - (attr) => attr.trait_type === "xlog_site_name", - ) - if (site_name) { - site_name.value = input.site_name - } else { - metadataDraft.attributes?.push({ - trait_type: "xlog_site_name", - value: input.site_name, - }) + const setAttribute = ( + inputKey: keyof typeof input, + typeKey: string, + needStringify: boolean = false, + ) => { + if (input[inputKey] !== undefined) { + // Initialize attributes + const newAttribute: { + trait_type: string + value: any + } = { + trait_type: `xlog_${typeKey}`, + value: needStringify + ? JSON.stringify(input[inputKey]) + : input[inputKey], + } + if (!metadataDraft.attributes) { + metadataDraft.attributes = [newAttribute] + } else { + const oldAttribute = metadataDraft.attributes.find( + (attr) => attr.trait_type === newAttribute.trait_type, + ) + if (oldAttribute) { + oldAttribute.value = newAttribute.value + } else { + metadataDraft.attributes.push(newAttribute) + } + } } } + setAttribute("navigation", "navigation", true) + setAttribute("css", "css") + setAttribute("ga", "ga") + setAttribute("ua", "ua") + setAttribute("uh", "uh") + setAttribute("custom_domain", "custom_domain") + setAttribute("site_name", "site_name") }, }, {