Skip to content

Commit

Permalink
refactor(core): update should update useEffect
Browse files Browse the repository at this point in the history
  • Loading branch information
RitaDias committed Feb 19, 2025
1 parent 86a2586 commit 2a9b4c0
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 11 deletions.
10 changes: 9 additions & 1 deletion packages/sanity/src/core/perspective/navbar/ReleasesList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,17 @@ export function ReleasesList({
const {t} = useTranslation()

useEffect(() => {
let shouldUpdate = true

checkWithPermissionGuard(createRelease, createReleaseMetadata(DEFAULT_RELEASE)).then(
setHasCreatePermission,
(hasPermission) => {
if (shouldUpdate) setHasCreatePermission(hasPermission)
},
)

return () => {
shouldUpdate = false
}
}, [checkWithPermissionGuard, createRelease, createReleaseMetadata])

const handleCreateBundleClick = useCallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,15 @@ export const VersionContextMenu = memo(function VersionContextMenu(props: {
const hasDiscardPermission = !isPermissionsLoading && permissions?.granted

useEffect(() => {
checkWithPermissionGuard(createRelease, DEFAULT_RELEASE).then(setHasCreatePermission)
let shouldUpdate = true

checkWithPermissionGuard(createRelease, DEFAULT_RELEASE).then((hasPermission) => {
if (shouldUpdate) setHasCreatePermission(hasPermission)
})

return () => {
shouldUpdate = false
}
}, [checkWithPermissionGuard, createRelease])

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,15 @@ export const ReleasePublishAllButton = ({
disabled || isValidatingDocuments || hasDocumentValidationErrors || !publishPermission

useEffect(() => {
checkWithPermissionGuard(publishRelease, release._id).then((hasPermission) =>
setPublishPermission(hasPermission),
)
let shouldUpdate = true

checkWithPermissionGuard(publishRelease, release._id).then((hasPermission) => {
if (shouldUpdate) setPublishPermission(hasPermission)
})

return () => {
shouldUpdate = false
}
}, [checkWithPermissionGuard, publishRelease, release._id, release.metadata.intendedPublishAt])

const handleConfirmPublishAll = useCallback(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,15 @@ export const ReleaseScheduleButton = ({
disabled || isValidatingDocuments || !schedulePermission || hasDocumentValidationErrors

useEffect(() => {
checkWithPermissionGuard(schedule, release._id, new Date()).then((hasPermission) =>
setSchedulePermission(hasPermission),
)
let shouldUpdate = true

checkWithPermissionGuard(schedule, release._id, new Date()).then((hasPermission) => {
if (shouldUpdate) setSchedulePermission(hasPermission)
})

return () => {
shouldUpdate = false
}
}, [checkWithPermissionGuard, release._id, release.metadata.intendedPublishAt, schedule])

const isScheduledDateInPast = useCallback(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,24 @@ export function ReleaseDashboardDetails({release}: {release: ReleaseDocument}) {
const [shouldDisplayPermissionWarning, setShouldDisplayPermissionWarning] = useState(false)
const shouldDisplayWarnings = isActive && shouldDisplayPermissionWarning
useEffect(() => {
let shouldUpdate = true

// only run if the release is active
if (isActive) {
checkWithPermissionGuard(publishRelease, release._id).then((hasPermission) => {
setShouldDisplayPermissionWarning(!hasPermission)
if (shouldUpdate) setShouldDisplayPermissionWarning(!hasPermission)
})

// if it's a release that can be scheduled, check if it can be scheduled
if (release.metadata.intendedPublishAt && isAtTimeRelease) {
checkWithPermissionGuard(schedule, release._id, new Date()).then((hasPermission) => {
setShouldDisplayPermissionWarning(!hasPermission)
if (shouldUpdate) setShouldDisplayPermissionWarning(!hasPermission)
})
}
}
return () => {
shouldUpdate = false
}
}, [
checkWithPermissionGuard,
isActive,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,14 @@ export function ReleasesOverview() {
}, [hasReleases, releasesMetadata, releases])

useEffect(() => {
checkWithPermissionGuard(createRelease, DEFAULT_RELEASE).then(setHasCreatePermission)
let shouldUpdate = true
checkWithPermissionGuard(createRelease, DEFAULT_RELEASE).then((hasPermissions) => {
if (shouldUpdate) setHasCreatePermission(hasPermissions)
})

return () => {
shouldUpdate = false
}
}, [checkWithPermissionGuard, createRelease])

// switch to open mode if on archived mode and there are no archived releases
Expand Down

0 comments on commit 2a9b4c0

Please sign in to comment.