Skip to content

Commit

Permalink
refactor: memoizing result from usePerspective
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanl17 committed Oct 28, 2024
1 parent 1c6133e commit 367e123
Showing 1 changed file with 35 additions and 20 deletions.
55 changes: 35 additions & 20 deletions packages/sanity/src/core/releases/hooks/usePerspective.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,23 @@ export function usePerspective(): PerspectiveValue {
: LATEST

// TODO: Improve naming; this may not be global.
const currentGlobalBundle =
perspective === 'published'
? {
_id: 'published',
metadata: {
title: 'Published',
},
}
: selectedBundle || LATEST

const setPerspectiveFromRelease = (releaseId: string) =>
setPerspective(getBundleIdFromReleaseId(releaseId))
const currentGlobalBundle = useMemo(
() =>
perspective === 'published'
? {
_id: 'published',
metadata: {
title: 'Published',
},
}
: selectedBundle || LATEST,
[perspective, selectedBundle],
)

const setPerspectiveFromRelease = useCallback(
(releaseId: string) => setPerspective(getBundleIdFromReleaseId(releaseId)),
[setPerspective],
)

const bundlesPerspective = useMemo(
() =>
Expand Down Expand Up @@ -123,12 +128,22 @@ export function usePerspective(): PerspectiveValue {
[excludedPerspectives],
)

return {
setPerspective,
setPerspectiveFromRelease,
toggleExcludedPerspective,
currentGlobalBundle: currentGlobalBundle,
bundlesPerspective,
isPerspectiveExcluded,
}
return useMemo(
() => ({
setPerspective,
setPerspectiveFromRelease,
toggleExcludedPerspective,
currentGlobalBundle,
bundlesPerspective,
isPerspectiveExcluded,
}),
[
bundlesPerspective,
currentGlobalBundle,
isPerspectiveExcluded,
setPerspective,
setPerspectiveFromRelease,
toggleExcludedPerspective,
],
)
}

0 comments on commit 367e123

Please sign in to comment.