Skip to content

Commit

Permalink
fix(core): remove useBundleDeletedToast, move to usePerspective hook
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrobonamin committed Jan 15, 2025
1 parent 66626eb commit 1006800
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 49 deletions.
4 changes: 4 additions & 0 deletions packages/sanity/src/core/i18n/bundles/studio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,10 @@ export const studioLocaleStrings = defineLocalesResources('studio', {
'release.navbar.tooltip': 'Releases',
/** The placeholder text when the release doesn't have a title */
'release.placeholder-untitled-release': 'Untitled release',
/**The toast title that will be shown when the user has a release perspective which is now archived */
'release.toast.archived-release.title': "The '{{title}}' release was archived",
/**The toast title that will be shown when the user has a release perspective which is now deleted */
'release.toast.deleted-release.title': "The '{{title}}' release was deleted",
/** Label for when a version of a document has already been added to the release */
'release.tooltip.already-added': 'A version of this document has already been added',
/** Label for when a release is scheduled / scheduling and a user can't add a document version to it */
Expand Down
35 changes: 34 additions & 1 deletion packages/sanity/src/core/releases/hooks/usePerspective.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import {type ReleaseId} from '@sanity/client'
import {Text, useToast} from '@sanity/ui'
import {useCallback, useEffect, useMemo} from 'react'
import {useRouter} from 'sanity/router'

import {useTranslation} from '../../i18n/hooks/useTranslation'
import {studioLocaleNamespace} from '../../i18n/localeNamespaces'
import {Translate} from '../../i18n/Translate'
import {type ReleaseDocument} from '../store/types'
import {useReleases} from '../store/useReleases'
import {LATEST} from '../util/const'
Expand Down Expand Up @@ -46,6 +50,9 @@ const EMPTY_ARRAY: string[] = []
*/
export function usePerspective(): PerspectiveValue {
const router = useRouter()
const toast = useToast()
const {t} = useTranslation(studioLocaleNamespace)

const {data: releases, archivedReleases, loading: releasesLoading} = useReleases()
const selectedPerspectiveName = router.stickyParams.perspective as
| 'published'
Expand Down Expand Up @@ -90,8 +97,34 @@ export function usePerspective(): PerspectiveValue {
)
if (!isCurrentPerspectiveValid) {
setPerspective(LATEST)
const archived = archivedReleases.find(
(r) => getReleaseIdFromReleaseDocumentId(r._id) === selectedPerspectiveName,
)

toast.push({
id: `bundle-deleted-toast-${selectedPerspectiveName}`,
status: 'warning',
title: (
<Text muted size={1}>
<Translate
t={t}
i18nKey={archived ? 'toast.archived-release.title' : 'toast.deleted-release.title'}
values={{title: archived?.metadata?.title || selectedPerspectiveName}}
/>
</Text>
),
duration: 10000,
})
}
}, [archivedReleases, selectedPerspectiveName, releases, releasesLoading, setPerspective])
}, [
archivedReleases,
selectedPerspectiveName,
releases,
releasesLoading,
setPerspective,
toast,
t,
])

const selectedPerspective: SelectedPerspective = useMemo(() => {
if (!selectedPerspectiveName) return 'drafts'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import {DocumentStatusBar} from '../statusBar'
import {useDocumentPane} from '../useDocumentPane'
import {usePreviewUrl} from '../usePreviewUrl'
import {DocumentLayoutError} from './DocumentLayoutError'
import {useBundleDeletedToast} from './useBundleDeletedToast'

const EMPTY_ARRAY: [] = []

Expand Down Expand Up @@ -113,7 +112,6 @@ export function DocumentLayout() {
focusPath,
value,
})
useBundleDeletedToast()

const [inspectorMenuItems, setInspectorMenuItems] = useState<DocumentInspectorMenuItem[]>([])
const [rootFieldActionNodes, setRootFieldActionNodes] = useState<DocumentFieldActionNode[]>([])
Expand Down

This file was deleted.

0 comments on commit 1006800

Please sign in to comment.