Skip to content

Commit

Permalink
fix: set default type to "internal" if it's somehow missing
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusforsberg committed May 6, 2024
1 parent 35ff362 commit 1839f3b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/components/LinkTypeInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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 (
<MenuButton
button={
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/typeGuards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ export const isEmailLink = (link: LinkValue): link is EmailLink => 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)

0 comments on commit 1839f3b

Please sign in to comment.