Skip to content

Commit

Permalink
refactor(core): create usePerspectiveStack from usePerspective
Browse files Browse the repository at this point in the history
  • Loading branch information
RitaDias committed Jan 20, 2025
1 parent 846ac0d commit 11fb258
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {

import {type FIXME} from '../../../FIXME'
import {useSchema} from '../../../hooks'
import {usePerspective} from '../../../releases/hooks/usePerspective'
import {usePerspectiveStack} from '../../../releases/hooks/usePerspectiveStack'
import {useActiveReleases} from '../../../releases/store/useActiveReleases'
import {useReleasesIds} from '../../../releases/store/useReleasesIds'
import {useDocumentPreviewStore} from '../../../store'
Expand Down Expand Up @@ -40,8 +40,8 @@ interface Options {
export function useReferenceInput(options: Options) {
const {path, schemaType, version} = options
const schema = useSchema()
const perspective = usePerspective()
const {data} = useActiveReleases()
const {perspectiveStack} = usePerspectiveStack()
const {releasesIds} = useReleasesIds(data)
const documentPreviewStore = useDocumentPreviewStore()
const {EditReferenceLinkComponent, onEditReference, activePath, initialValueTemplateItems} =
Expand Down Expand Up @@ -131,10 +131,10 @@ export function useReferenceInput(options: Options) {
{version},
{
bundleIds: releasesIds,
bundleStack: perspective.perspectiveStack,
bundleStack: perspectiveStack,
},
),
[documentPreviewStore, schemaType, version, releasesIds, perspective.perspectiveStack],
[documentPreviewStore, schemaType, version, releasesIds, perspectiveStack],
)

return {
Expand Down
1 change: 1 addition & 0 deletions packages/sanity/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export {
useDocumentVersions,
useIsReleaseActive,
usePerspective,
usePerspectiveStack,
useReleasesIds,
useSelectedPerspectiveProps,
useVersionOperations,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {type PerspectiveValue, usePerspective} from '../../usePerspective'
export const usePerspectiveMockReturn: Mocked<PerspectiveValue> = {
setPerspective: vi.fn(),
selectedPerspective: 'drafts',
perspectiveStack: [],
}

export const mockUsePerspective = usePerspective as Mock<typeof usePerspective>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {type Mock, type Mocked} from 'vitest'

import {type PerspectiveStackValue, usePerspectiveStack} from '../../usePerspectiveStack'

export const usePerspectiveMockReturn: Mocked<PerspectiveStackValue> = {
perspectiveStack: [],
}

export const mockUsePerspective = usePerspectiveStack as Mock<typeof usePerspectiveStack>
1 change: 1 addition & 0 deletions packages/sanity/src/core/releases/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './useDocumentVersions'
export * from './useIsReleaseActive'
export * from './usePerspective'
export * from './usePerspectiveStack'
export * from './useSelectedPerspectiveProps'
export * from './useVersionOperations'
18 changes: 1 addition & 17 deletions packages/sanity/src/core/releases/hooks/usePerspective.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {LATEST} from '../util/const'
import {getReleaseIdFromReleaseDocumentId} from '../util/getReleaseIdFromReleaseDocumentId'
import {isPublishedPerspective} from '../util/util'
import {useSelectedPerspectiveProps} from './useSelectedPerspectiveProps'
import {getReleasesPerspectiveStack} from './utils'

/**
* @internal
Expand All @@ -27,10 +26,6 @@ export interface PerspectiveValue {
selectedPerspective: SelectedPerspective
/* Change the perspective in the studio based on the perspective name */
setPerspective: (perspectiveId: 'published' | 'drafts' | ReleaseId | undefined) => void
/**
* The stacked array of releases ids ordered chronologically to represent the state of documents at the given point in time.
*/
perspectiveStack: string[]
}

const EMPTY_ARRAY: string[] = []
Expand Down Expand Up @@ -126,23 +121,12 @@ export function usePerspective(): PerspectiveValue {
return selectedRelease || 'drafts'
}, [selectedPerspectiveName, releases])

const perspectiveStack = useMemo(
() =>
getReleasesPerspectiveStack({
releases,
selectedPerspectiveName,
excludedPerspectives,
}),
[releases, selectedPerspectiveName, excludedPerspectives],
)

return useMemo(
() => ({
selectedPerspective,
perspectiveStack,

setPerspective,
}),
[selectedPerspective, perspectiveStack, setPerspective],
[selectedPerspective, setPerspective],
)
}
44 changes: 44 additions & 0 deletions packages/sanity/src/core/releases/hooks/usePerspectiveStack.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {useMemo} from 'react'
import {useRouter} from 'sanity/router'

import {useActiveReleases} from '../store/useActiveReleases'
import {useSelectedPerspectiveProps} from './useSelectedPerspectiveProps'
import {getReleasesPerspectiveStack} from './utils'

export interface PerspectiveStackValue {
/**
* The stacked array of releases ids ordered chronologically to represent the state of documents at the given point in time.
*/
perspectiveStack: string[]
}

const EMPTY_ARRAY: string[] = []

/**
* Perspective stack hook
* @internal
*/
export function usePerspectiveStack(): PerspectiveStackValue {
const {
stickyParams: {excludedPerspectives: routerExcludedPerspectives},
} = useRouter()
const {data: releases} = useActiveReleases()
const {selectedPerspectiveName} = useSelectedPerspectiveProps()

const excludedPerspectives = useMemo(
() => routerExcludedPerspectives?.split(',') || EMPTY_ARRAY,
[routerExcludedPerspectives],
)

const perspectiveStack = useMemo(
() =>
getReleasesPerspectiveStack({
releases,
selectedPerspectiveName,
excludedPerspectives,
}),
[releases, selectedPerspectiveName, excludedPerspectives],
)
return useMemo(() => ({perspectiveStack}), [perspectiveStack])
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
getPreviewValueWithFallback,
SanityDefaultPreview,
} from '../../../../../../../preview'
import {usePerspective} from '../../../../../../../releases/hooks/usePerspective'
import {usePerspectiveStack} from '../../../../../../../releases/hooks/usePerspectiveStack'
import {useActiveReleases} from '../../../../../../../releases/store/useActiveReleases'
import {useReleasesIds} from '../../../../../../../releases/store/useReleasesIds'
import {isPerspectiveRaw} from '../../../../../../../search/common/isPerspectiveRaw'
Expand Down Expand Up @@ -57,7 +57,7 @@ export function SearchResultItemPreview({
const documentPreviewStore = useDocumentPreviewStore()
const {data, loading} = useActiveReleases()
const {releasesIds} = useReleasesIds(data)
const {perspectiveStack} = usePerspective()
const {perspectiveStack} = usePerspectiveStack()
const {state} = useSearchState()
const isRaw = isPerspectiveRaw(state.perspective)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {SearchContext} from 'sanity/_singletons'

import {type CommandListHandle} from '../../../../../../components'
import {useSchema} from '../../../../../../hooks'
import {usePerspective} from '../../../../../../releases/hooks/usePerspective'
import {usePerspectiveStack} from '../../../../../../releases/hooks/usePerspectiveStack'
import {useActiveReleases} from '../../../../../../releases/store/useActiveReleases'
import {isPerspectiveRaw, type SearchTerms} from '../../../../../../search'
import {useCurrentUser} from '../../../../../../store'
Expand Down Expand Up @@ -48,7 +48,7 @@ export function SearchProvider({
}: SearchProviderProps) {
const [onClose, setOnClose] = useState<(() => void) | null>(null)
const [searchCommandList, setSearchCommandList] = useState<CommandListHandle | null>(null)
const {perspectiveStack} = usePerspective()
const {perspectiveStack} = usePerspectiveStack()
const {data: releases} = useActiveReleases()
const schema = useSchema()
const currentUser = useCurrentUser()
Expand Down
4 changes: 2 additions & 2 deletions packages/sanity/src/presentation/PresentationTool.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
type SanityDocument,
type Tool,
useDataset,
usePerspective,
usePerspectiveStack,
useProjectId,
useSelectedPerspectiveProps,
useUnique,
Expand Down Expand Up @@ -107,7 +107,7 @@ export default function PresentationTool(props: {
state: PresentationStateParams
}
const routerSearchParams = useUnique(Object.fromEntries(routerState._searchParams || []))
const {perspectiveStack} = usePerspective()
const {perspectiveStack} = usePerspectiveStack()
const {selectedPerspectiveName = 'previewDrafts'} = useSelectedPerspectiveProps()
const perspective = (
selectedPerspectiveName.startsWith('r') ? perspectiveStack : selectedPerspectiveName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
isRecord,
SanityDefaultPreview,
useActiveReleases,
usePerspective,
usePerspectiveStack,
useReleasesIds,
useSelectedPerspectiveProps,
} from 'sanity'
Expand Down Expand Up @@ -50,7 +50,7 @@ export function PaneItemPreview(props: PaneItemPreviewProps) {
const {data, loading} = useActiveReleases()
const {releasesIds} = useReleasesIds(data)
const {selectedPerspectiveName} = useSelectedPerspectiveProps()
const {perspectiveStack} = usePerspective()
const {perspectiveStack} = usePerspectiveStack()
const previewStateObservable = useMemo(
() =>
getPreviewStateObservable(props.documentPreviewStore, schemaType, value._id, title, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
useActiveReleases,
useDocumentPreviewStore,
usePerspective,
usePerspectiveStack,
useSchema,
useSelectedPerspectiveProps,
useSyncState,
Expand All @@ -29,7 +30,9 @@ export function DocumentStatusLine() {
const schema = useSchema()
const schemaType = schema.get(documentType)
const releases = useActiveReleases()
const {selectedPerspective, perspectiveStack} = usePerspective()
const {selectedPerspective} = usePerspective()
const {perspectiveStack} = usePerspectiveStack()

const {selectedReleaseId} = useSelectedPerspectiveProps()

const previewStateObservable = useMemo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
type GeneralPreviewLayoutKey,
useActiveReleases,
useI18nText,
usePerspective,
usePerspectiveStack,
useSchema,
useTranslation,
useUnique,
Expand Down Expand Up @@ -76,7 +76,7 @@ export const DocumentListPane = memo(function DocumentListPane(props: DocumentLi
const {childItemId, isActive, pane, paneKey, sortOrder: sortOrderRaw, layout} = props
const schema = useSchema()
const releases = useActiveReleases()
const {perspectiveStack} = usePerspective()
const {perspectiveStack} = usePerspectiveStack()
const {displayOptions, options} = pane
const {apiVersion, filter} = options
const params = useShallowUnique(options.params || EMPTY_RECORD)
Expand Down

0 comments on commit 11fb258

Please sign in to comment.