-
Notifications
You must be signed in to change notification settings - Fork 444
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core): create usePerspectiveStack from usePerspective
- Loading branch information
Showing
13 changed files
with
74 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
packages/sanity/src/core/releases/hooks/__tests__/__mocks__/usePerspectiveStack.mock.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
packages/sanity/src/core/releases/hooks/usePerspectiveStack.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters