From 431152a69d9d565d883175529631c43461998a05 Mon Sep 17 00:00:00 2001 From: Mathias Oterhals Myklebust Date: Thu, 19 Sep 2024 08:49:35 +0200 Subject: [PATCH] fix(footerSection): check SoMe links existence before using structure tool crashed if the SoMe links document was not available, since this is set as initialValue for footerSectionID.soMeLinks --- studio/schemas/objects/footerSection.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/studio/schemas/objects/footerSection.ts b/studio/schemas/objects/footerSection.ts index 4304ee1a0..d5dbface0 100644 --- a/studio/schemas/objects/footerSection.ts +++ b/studio/schemas/objects/footerSection.ts @@ -1,6 +1,8 @@ +import { groq } from "next-sanity"; import { StringInputProps, defineType } from "sanity"; import { StringInputWithCharacterCount } from "studio/components/stringInputWithCharacterCount/StringInputWithCharacterCount"; +import { client } from "studio/lib/client"; import { soMeLinksID } from "studio/schemas/documents/siteSettings/socialMediaProfiles"; import { richText } from "studio/schemas/fields/text"; @@ -86,9 +88,17 @@ export const footerSection = defineType({ description: "This section automatically uses your social media links. Any updates to your social media links will appear here.", hidden: ({ parent }) => parent?.sectionType !== SectionType.SocialMedia, - initialValue: { - _type: "reference", - _ref: soMeLinksID, + initialValue: async () => { + // use Social Media Profiles singleton document if it exists + const links = await client.fetch( + groq`*[_type=="${soMeLinksID}" && _id=="${soMeLinksID}"][0]`, + ); + return links !== null + ? { + _type: "reference", + _ref: soMeLinksID, + } + : undefined; }, }, ],