From 9bfdd9d5fa777c027514fab0017516fb1b04007c Mon Sep 17 00:00:00 2001 From: Snorre Eskeland Brekke Date: Sun, 20 Oct 2024 00:44:25 +0200 Subject: [PATCH] fix: supports autoconfirming start in create dialog --- .../sanity/src/core/create/createStorage.ts | 17 ++++++++++++++ .../start-in-create/StartInCreateAction.tsx | 9 +++++++- .../start-in-create/StartInCreateDialog.tsx | 23 +++++++++++++------ 3 files changed, 41 insertions(+), 8 deletions(-) create mode 100644 packages/sanity/src/core/create/createStorage.ts diff --git a/packages/sanity/src/core/create/createStorage.ts b/packages/sanity/src/core/create/createStorage.ts new file mode 100644 index 000000000000..8becae43810e --- /dev/null +++ b/packages/sanity/src/core/create/createStorage.ts @@ -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}`) +} diff --git a/packages/sanity/src/core/create/start-in-create/StartInCreateAction.tsx b/packages/sanity/src/core/create/start-in-create/StartInCreateAction.tsx index a44bff7a4f7e..bada2bf003c2 100644 --- a/packages/sanity/src/core/create/start-in-create/StartInCreateAction.tsx +++ b/packages/sanity/src/core/create/start-in-create/StartInCreateAction.tsx @@ -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' @@ -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}` @@ -70,6 +76,7 @@ export function StartInCreateAction( createLinkId={createLinkId} appId={appId} type={type} + autoConfirm={autoConfirm} /> ), }, diff --git a/packages/sanity/src/core/create/start-in-create/StartInCreateDialog.tsx b/packages/sanity/src/core/create/start-in-create/StartInCreateDialog.tsx index a7993f6e66a7..e7a382c26de9 100644 --- a/packages/sanity/src/core/create/start-in-create/StartInCreateDialog.tsx +++ b/packages/sanity/src/core/create/start-in-create/StartInCreateDialog.tsx @@ -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' @@ -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) @@ -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(() => { @@ -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 ( @@ -84,7 +93,7 @@ export function StartInCreateDialog(props: StartInCreateDialogProps) { {t('start-in-create-dialog.details')} - + {t('start-in-create-dialog.dont-remind-me-checkbox')}