Skip to content

Commit

Permalink
fix(core): support serverDocumentActions flag in plugins (#8233)
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrobonamin authored Jan 13, 2025
1 parent ed37a55 commit 4d4aba8
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 35 deletions.
25 changes: 2 additions & 23 deletions dev/test-studio/sanity.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ const sharedSettings = definePlugin({
],
})

const defaultWorkspace = {
const defaultWorkspace = defineConfig({
name: 'default',
title: 'Test Studio',
projectId: 'ppsg7ml5',
Expand All @@ -168,20 +168,14 @@ const defaultWorkspace = {
},
basePath: '/test',
icon: SanityMonogram,
// eslint-disable-next-line camelcase
__internal_serverDocumentActions: {
// TODO: Switched off because Actions API doesn't support versions (yet).
enabled: false,
},
scheduledPublishing: {
enabled: true,
inputDateTimeFormat: 'MM/dd/yy h:mm a',
},
tasks: {
enabled: true,
},
beta: {eventsAPI: {enabled: true}},
}
})

export default defineConfig([
defaultWorkspace,
Expand Down Expand Up @@ -227,11 +221,6 @@ export default defineConfig([
dataset: 'playground',
plugins: [sharedSettings()],
basePath: '/playground',
// eslint-disable-next-line camelcase
__internal_serverDocumentActions: {
// TODO: Switched off because Actions API doesn't support versions (yet).
enabled: false,
},
beta: {eventsAPI: {enabled: true}},
},
{
Expand Down Expand Up @@ -261,11 +250,6 @@ export default defineConfig([
plugins: [sharedSettings()],
basePath: '/staging',
apiHost: 'https://api.sanity.work',
// eslint-disable-next-line camelcase
__internal_serverDocumentActions: {
// TODO: Switched off because Actions API doesn't support versions (yet).
enabled: false,
},
beta: {eventsAPI: {enabled: true}},
auth: {
loginMethod: 'token',
Expand Down Expand Up @@ -364,11 +348,6 @@ export default defineConfig([
title: 'Presentation Studio',
projectId: 'ppsg7ml5',
dataset: 'playground',
// eslint-disable-next-line camelcase
__internal_serverDocumentActions: {
// TODO: Switched off because Actions API doesn't support versions (yet).
enabled: false,
},
plugins: [
debugSecrets(),
presentationTool({
Expand Down
25 changes: 24 additions & 1 deletion packages/sanity/src/core/config/configPropertyReducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,30 @@ export const eventsAPIReducer = (opts: {config: PluginOptions; initialValue: boo
if (typeof enabled === 'boolean') return enabled

throw new Error(
`Expected \`beta.eventsAPI.enabled\` to be an object with footerAction, but received ${getPrintableType(
`Expected \`beta.eventsAPI.enabled\` to be a boolean, but received ${getPrintableType(
enabled,
)}`,
)
}, initialValue)

return result
}

export const serverDocumentActionsReducer = (opts: {
config: PluginOptions
initialValue: boolean | undefined
}): boolean | undefined => {
const {config, initialValue} = opts
const flattenedConfig = flattenConfig(config, [])

const result = flattenedConfig.reduce((acc: boolean | undefined, {config: innerConfig}) => {
const enabled = innerConfig.__internal_serverDocumentActions?.enabled

if (typeof enabled === 'undefined') return acc
if (typeof enabled === 'boolean') return enabled

throw new Error(
`Expected \`__internal_serverDocumentActions\` to be a boolean, but received ${getPrintableType(
enabled,
)}`,
)
Expand Down
7 changes: 5 additions & 2 deletions packages/sanity/src/core/config/prepareConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
resolveProductionUrlReducer,
schemaTemplatesReducer,
searchStrategyReducer,
serverDocumentActionsReducer,
startInCreateEnabledReducer,
toolsReducer,
} from './configPropertyReducers'
Expand Down Expand Up @@ -210,8 +211,6 @@ export function prepareConfig(
__internal: {
sources: resolvedSources,
},
// eslint-disable-next-line camelcase
__internal_serverDocumentActions: rawWorkspace.__internal_serverDocumentActions,
...defaultPluginsOptions,
}
preparedWorkspaces.set(rawWorkspace, workspaceSummary)
Expand Down Expand Up @@ -659,6 +658,10 @@ function resolveSource({
fallbackStudioOrigin: createFallbackOriginReducer(config),
},
},
// eslint-disable-next-line camelcase
__internal_serverDocumentActions: {
enabled: serverDocumentActionsReducer({config, initialValue: undefined}),
},

announcements: {
enabled: announcementsEnabledReducer({config, initialValue: true}),
Expand Down
4 changes: 3 additions & 1 deletion packages/sanity/src/core/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,9 @@ export interface PluginOptions {
enableLegacySearch?: boolean
}

/** @internal */
__internal_serverDocumentActions?: WorkspaceOptions['__internal_serverDocumentActions']

/** Configuration for studio beta features.
* @internal
*/
Expand Down Expand Up @@ -878,7 +881,6 @@ export interface WorkspaceSummary extends DefaultPluginsWorkspaceOptions {
source: Observable<Source>
}>
}
__internal_serverDocumentActions: WorkspaceOptions['__internal_serverDocumentActions']
}

/**
Expand Down
4 changes: 4 additions & 0 deletions packages/sanity/src/core/releases/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,8 @@ export const releases = definePlugin({
document: {
actions: (actions, context) => resolveDocumentActions(actions, context),
},
// eslint-disable-next-line camelcase
__internal_serverDocumentActions: {
enabled: false,
},
})
4 changes: 3 additions & 1 deletion test/e2e/tests/document-actions/fetchFeatureToggle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import {test} from '@sanity/test'

import {mockActionsFeatureToggle} from '../../helpers/mockActionsFeatureToggle'

test('Actions API should be used if the feature toggle is enabled and the Studio version satisfies the `compatibleStudioVersions` constraint', async ({
// This test is skipped because the feature toggle is disable by the use of `releases`, see https://github.com/sanity-io/sanity/blob/corel/packages/sanity/src/core/releases/plugin/index.ts#L61-L62
// Re enable once the feature toggle is removed and we support serverDocumentActions with releases.
test.skip('Actions API should be used if the feature toggle is enabled and the Studio version satisfies the `compatibleStudioVersions` constraint', async ({
page,
createDraftDocument,
}) => {
Expand Down
11 changes: 4 additions & 7 deletions test/e2e/tests/inputs/text.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,18 @@ test.describe('inputs: text', () => {
await expect(page.getByTestId('document-panel-scroller')).toBeAttached()

await titleInput.fill('Title A')

// generally waiting for timeouts is not a good idea but for this specific instance
// since we are using `.fill` and `.click` they can cause the draft creation and publish to happen at the same exact time.
// We are waiting for 1s to make sure the draft actually gets created and click action is not too eager
await page.waitForTimeout(1000)
await expect(paneFooter).toHaveText(/draft Edited just now/i)

// Wait for the document to be published.
publishButton.click()
expect(await paneFooter.textContent()).toMatch(/published/i)
await expect(paneFooter).toHaveText(/published/i)

// Change the title.
await titleInput.fill('Title B')
await expect(paneFooter).toHaveText(/draft Edited just now/i)

// Wait for the document to be published.
publishButton.click()
expect(await paneFooter.textContent()).toMatch(/published/i)
await expect(paneFooter).toHaveText(/published/i)
})
})

0 comments on commit 4d4aba8

Please sign in to comment.