From 1839f3b037ef02f0880d2d01e12fdaa12e3a185c Mon Sep 17 00:00:00 2001 From: Marcus Forsberg Date: Mon, 6 May 2024 08:14:10 +0200 Subject: [PATCH] fix: set default type to "internal" if it's somehow missing --- src/components/LinkTypeInput.tsx | 8 ++++++++ src/helpers/typeGuards.ts | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/components/LinkTypeInput.tsx b/src/components/LinkTypeInput.tsx index 816c704..6e7ffb7 100644 --- a/src/components/LinkTypeInput.tsx +++ b/src/components/LinkTypeInput.tsx @@ -5,6 +5,7 @@ import {set, type StringInputProps} from 'sanity' import styled from 'styled-components' import {CustomLinkType, LinkFieldPluginOptions, LinkType} from '../types' +import {useEffect} from 'react' const defaultLinkTypes: LinkType[] = [ {title: 'Internal', value: 'internal', icon: LinkIcon}, @@ -52,6 +53,13 @@ export function LinkTypeInput({ const selectedType = linkTypes.find((type) => type.value === value) || linkTypes[0] + useEffect(() => { + // Make sure there's always a default value + if (!value) { + onChange(set('internal')) + } + }, [onChange, value]) + return ( link.type === export const isPhoneLink = (link: LinkValue): link is PhoneLink => link.type === 'phone' export const isCustomLink = (link: LinkValue): link is CustomLink => - !['internal', 'external', 'email', 'phone'].includes(link.type) + !!link.type && !['internal', 'external', 'email', 'phone'].includes(link.type)