Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): disable scheduledPublishing and tasks if /features returns error #7517

Open
wants to merge 2 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion packages/sanity/src/core/scheduledPublishing/tool/Tool.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Box, Flex, Text, useTheme} from '@sanity/ui'
import {Box, Container, Flex, Text, useTheme} from '@sanity/ui'
import {parse} from 'date-fns'
import {useEffect, useMemo, useRef} from 'react'
import {type RouterContextValue, useRouter} from 'sanity/router'
Expand All @@ -11,6 +11,7 @@ import {SCHEDULE_FILTERS, TOOL_HEADER_HEIGHT} from '../constants'
import usePollSchedules from '../hooks/usePollSchedules'
import useTimeZone from '../hooks/useTimeZone'
import {type Schedule, type ScheduleState} from '../types'
import {useScheduledPublishingEnabled} from './contexts/ScheduledPublishingEnabledProvider'
import {SchedulesProvider} from './contexts/schedules'
import {ScheduleFilters} from './scheduleFilters'
import {Schedules} from './schedules'
Expand All @@ -32,6 +33,7 @@ export default function Tool() {

const {sanity: theme} = useTheme()
const {error, isInitialLoading, schedules = NO_SCHEDULE} = usePollSchedules()
const {enabled} = useScheduledPublishingEnabled()

const lastScheduleState = useRef<ScheduleState | undefined>()

Expand Down Expand Up @@ -74,6 +76,19 @@ export default function Tool() {
}
}

if (!enabled) {
return (
<Container width={1} paddingTop={4}>
<Box paddingTop={4} paddingX={4}>
<ErrorCallout
description="You do not have permission to edit schedules."
title="Insufficient permissions"
Copy link
Contributor Author

@pedrobonamin pedrobonamin Sep 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ We don't have translations in scheduledPublishing
Instead of showing the same error that we show when the request fails we add an specific not enabled error.

Current error

Screenshot 2024-09-17 at 17 23 06

Updated error
Screenshot 2024-09-17 at 17 24 34

/>
</Box>
</Container>
)
}

return (
<SchedulesProvider value={schedulesContext}>
<Flex direction="column" height="fill" flex={1} overflow="hidden">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,21 @@ describe('ScheduledPublishingEnabledProvider', () => {
expect(value.result.current).toEqual({enabled: false, mode: null})
})

it('should not show the plugin if useFeatureEnabled has an error', () => {
require('../../../hooks').useFeatureEnabled.mockReturnValue({
enabled: false,
isLoading: true,
error: new Error('Something went wrong'),
})
require('../../../studio').useWorkspace.mockReturnValue({scheduledPublishing: {enabled: true}})

const value = renderHook(useScheduledPublishingEnabled, {
wrapper: ScheduledPublishingEnabledProvider,
})

expect(value.result.current).toEqual({enabled: false, mode: null})
})

it('should call "useFeatureEnabled" with "scheduledPublishing"', () => {
require('../../../studio').useWorkspace.mockReturnValue({scheduledPublishing: {enabled: false}})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ interface TaksEnabledProviderProps {
*/

export function ScheduledPublishingEnabledProvider({children}: TaksEnabledProviderProps) {
const {enabled, isLoading} = useFeatureEnabled('scheduledPublishing')
const {enabled, isLoading, error} = useFeatureEnabled('scheduledPublishing')
const {scheduledPublishing} = useWorkspace()

const isWorkspaceEnabled = scheduledPublishing.enabled

const value: ScheduledPublishingEnabledContextValue = useMemo(() => {
if (!isWorkspaceEnabled || isLoading) {
if (!isWorkspaceEnabled || isLoading || error) {
return {
enabled: false,
mode: null,
Expand All @@ -42,7 +42,7 @@ export function ScheduledPublishingEnabledProvider({children}: TaksEnabledProvid
enabled: true,
mode: enabled ? 'default' : 'upsell',
}
}, [enabled, isLoading, isWorkspaceEnabled])
}, [enabled, isLoading, isWorkspaceEnabled, error])

return (
<ScheduledPublishingEnabledContext.Provider value={value}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@ describe('TasksEnabledProvider', () => {
expect(value.result.current).toEqual({enabled: false, mode: null})
})

it('should not show the plugin if useFeatureEnabled has an error', () => {
require('../../../hooks').useFeatureEnabled.mockReturnValue({
enabled: false,
isLoading: true,
error: new Error('Something went wrong'),
})
require('../../../studio').useWorkspace.mockReturnValue({tasks: {enabled: true}})

const value = renderHook(useTasksEnabled, {wrapper: TasksEnabledProvider})

expect(value.result.current).toEqual({enabled: false, mode: null})
})

it('should call "useFeatureEnabled" with "sanityTasks"', () => {
require('../../../studio').useWorkspace.mockReturnValue({tasks: {enabled: false}})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ interface TaksEnabledProviderProps {
* @internal
*/
export function TasksEnabledProvider({children}: TaksEnabledProviderProps) {
const {enabled, isLoading} = useFeatureEnabled('sanityTasks')
const {enabled, isLoading, error} = useFeatureEnabled('sanityTasks')

const isWorkspaceEnabled = useWorkspace().tasks?.enabled

const value: TasksEnabledContextValue = useMemo(() => {
if (!isWorkspaceEnabled || isLoading) {
if (!isWorkspaceEnabled || isLoading || error) {
return {
enabled: false,
mode: null,
Expand All @@ -28,7 +28,7 @@ export function TasksEnabledProvider({children}: TaksEnabledProviderProps) {
enabled: true,
mode: enabled ? 'default' : 'upsell',
}
}, [enabled, isLoading, isWorkspaceEnabled])
}, [enabled, isLoading, isWorkspaceEnabled, error])

return <TasksEnabledContext.Provider value={value}>{children}</TasksEnabledContext.Provider>
}
Loading