Skip to content

Commit

Permalink
fix: supports autoconfirming start in create dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
snorrees committed Oct 19, 2024
1 parent 8662291 commit 9bfdd9d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
17 changes: 17 additions & 0 deletions packages/sanity/src/core/create/createStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {supportsLocalStorage} from '../util/supportsLocalStorage'

const AUTO_CONFIRM_KEY = 'sanityStudio:start-in-create:auto-confirm'

export function isStartInCreateAutoConfirmed(): boolean {
if (!supportsLocalStorage) {
return false
}
return localStorage.getItem(AUTO_CONFIRM_KEY) === 'true'
}

export function setStartInCreateAutoConfirm(enabled: boolean): void {
if (!supportsLocalStorage) {
return
}
localStorage.setItem(AUTO_CONFIRM_KEY, `${enabled}`)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
type DocumentActionProps,
} from '../../config'
import {useTranslation} from '../../i18n'
import {isStartInCreateAutoConfirmed, setStartInCreateAutoConfirm} from '../createStorage'
import {createLocaleNamespace} from '../i18n'
import {type AppIdCache} from '../studio-app/appIdCache'
import {useStudioAppIdStore} from '../studio-app/useStudioAppIdStore'
Expand Down Expand Up @@ -40,9 +41,14 @@ export function StartInCreateAction(

const [isDialogOpen, setDialogOpen] = useState(false)
const [isLinking, setLinking] = useState(false)
const [autoConfirm, setAutoConfirm] = useState(() => isStartInCreateAutoConfirmed())

const closeDialog = useCallback(() => setDialogOpen(false), [])
const linkingStarted = useCallback(() => setLinking(true), [])
const linkingStarted = useCallback((dontShowAgain: boolean) => {
setStartInCreateAutoConfirm(dontShowAgain)
setAutoConfirm(dontShowAgain)
setLinking(true)
}, [])

const createLinkId = (draft?._id ?? published?._id ?? liveEdit) ? id : `drafts.${id}`

Expand Down Expand Up @@ -70,6 +76,7 @@ export function StartInCreateAction(
createLinkId={createLinkId}
appId={appId}
type={type}
autoConfirm={autoConfirm}
/>
),
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {LaunchIcon} from '@sanity/icons'
import {Box, Checkbox, Flex, Stack, Text, useToast} from '@sanity/ui'
import {useCallback, useId} from 'react'
import {useCallback, useEffect, useId, useState} from 'react'

import {Button} from '../../../ui-components'
import {set, toMutationPatches} from '../../form'
Expand All @@ -20,13 +20,16 @@ export interface StartInCreateDialogProps {
createLinkId: string
appId: string
type: string
onLinkingStarted: () => void
onLinkingStarted: (autoConfirm: boolean) => void
autoConfirm: boolean
}

export function StartInCreateDialog(props: StartInCreateDialogProps) {
const {publicId, createLinkId, appId, type, onLinkingStarted} = props
const {publicId, createLinkId, appId, type, onLinkingStarted, autoConfirm} = props
const {t} = useTranslation(createLocaleNamespace)
const checkboxId = useId()
const [dontShowAgain, setDontShowAgain] = useState(false)
const toggleDontShowAgain = useCallback(() => setDontShowAgain((current) => !current), [])

const {patch} = useDocumentOperation(publicId, type)

Expand All @@ -51,7 +54,7 @@ export function StartInCreateDialog(props: StartInCreateDialogProps) {
return
}
window?.open(createUrl, CREATE_LINK_TARGET)?.focus()
onLinkingStarted()
onLinkingStarted(autoConfirm || dontShowAgain)

//@todo delete me
setTimeout(() => {
Expand All @@ -69,8 +72,14 @@ export function StartInCreateDialog(props: StartInCreateDialogProps) {
),
]),
)
}, 2000)
}, [patch, createUrl, onLinkingStarted, pushToast, t])
}, 10000)
}, [patch, createUrl, onLinkingStarted, pushToast, t, dontShowAgain, autoConfirm])

useEffect(() => {
if (autoConfirm && createUrl) {
startLinking()
}
}, [autoConfirm, startLinking, createUrl])

return (
<Stack space={4}>
Expand All @@ -84,7 +93,7 @@ export function StartInCreateDialog(props: StartInCreateDialogProps) {
<Text>{t('start-in-create-dialog.details')}</Text>
</Box>
<Flex gap={2} align="center">
<Checkbox id={checkboxId} />
<Checkbox id={checkboxId} checked={dontShowAgain} onChange={toggleDontShowAgain} />
<Text as="label" htmlFor={checkboxId}>
{t('start-in-create-dialog.dont-remind-me-checkbox')}
</Text>
Expand Down

0 comments on commit 9bfdd9d

Please sign in to comment.