diff --git a/packages/next/src/views/Version/SelectComparison/index.tsx b/packages/next/src/views/Version/SelectComparison/index.tsx index 82f8ef2958d..37c50742e19 100644 --- a/packages/next/src/views/Version/SelectComparison/index.tsx +++ b/packages/next/src/views/Version/SelectComparison/index.tsx @@ -2,7 +2,14 @@ import type { PaginatedDocs, Where } from 'payload' -import { fieldBaseClass, Pill, ReactSelect, useConfig, useTranslation } from '@payloadcms/ui' +import { + fieldBaseClass, + Pill, + ReactSelect, + useConfig, + useDocumentInfo, + useTranslation, +} from '@payloadcms/ui' import { formatDate } from '@payloadcms/ui/shared' import { stringify } from 'qs-esm' import React, { useCallback, useEffect, useState } from 'react' @@ -37,6 +44,8 @@ export const SelectComparison: React.FC = (props) => { }, } = useConfig() + const { hasPublishedDoc } = useDocumentInfo() + const [options, setOptions] = useState< { label: React.ReactNode | string @@ -109,7 +118,10 @@ export const SelectComparison: React.FC = (props) => { }, published: { currentLabel: t('version:currentPublishedVersion'), - latestVersion: latestPublishedVersion, + // The latest published version does not necessarily equal the current published version, + // because the latest published version might have been unpublished in the meantime. + // Hence, we should only use the latest published version if there is a published document. + latestVersion: hasPublishedDoc ? latestPublishedVersion : undefined, pillStyle: 'success', previousLabel: t('version:previouslyPublished'), }, diff --git a/packages/next/src/views/Versions/index.tsx b/packages/next/src/views/Versions/index.tsx index 54e05e00884..744ddfd08bf 100644 --- a/packages/next/src/views/Versions/index.tsx +++ b/packages/next/src/views/Versions/index.tsx @@ -85,13 +85,34 @@ export async function VersionsView(props: DocumentViewServerProps) { payload, status: 'draft', }) - latestPublishedVersion = await getLatestVersion({ - slug: collectionSlug, - type: 'collection', - parentID: id, - payload, - status: 'published', + const publishedDoc = await payload.count({ + collection: collectionSlug, + depth: 0, + overrideAccess: true, + req, + where: { + id: { + equals: id, + }, + _status: { + equals: 'published', + }, + }, }) + + // If we pass a latestPublishedVersion to buildVersionColumns, + // this will be used to display it as the "current published version". + // However, the latest published version might have been unpublished in the meantime. + // Hence, we should only pass the latest published version if there is a published document. + latestPublishedVersion = + publishedDoc.totalDocs > 0 && + (await getLatestVersion({ + slug: collectionSlug, + type: 'collection', + parentID: id, + payload, + status: 'published', + })) } } catch (err) { logError({ err, payload }) diff --git a/test/versions/e2e.spec.ts b/test/versions/e2e.spec.ts index ce20a3d124b..07c55f3ebc9 100644 --- a/test/versions/e2e.spec.ts +++ b/test/versions/e2e.spec.ts @@ -201,6 +201,46 @@ describe('Versions', () => { await expect(page.locator('#field-title')).toHaveValue('v1') }) + test('should show currently published version status in versions view', async () => { + const publishedDoc = await payload.create({ + collection: draftCollectionSlug, + data: { + _status: 'published', + title: 'title', + description: 'description', + }, + overrideAccess: true, + }) + + await page.goto(`${url.edit(publishedDoc.id)}/versions`) + await expect(page.locator('main.versions')).toContainText('Current Published Version') + }) + + test('should show unpublished version status in versions view', async () => { + const publishedDoc = await payload.create({ + collection: draftCollectionSlug, + data: { + _status: 'published', + title: 'title', + description: 'description', + }, + overrideAccess: true, + }) + + // Unpublish the document + await payload.update({ + collection: draftCollectionSlug, + id: publishedDoc.id, + data: { + _status: 'draft', + }, + draft: false, + }) + + await page.goto(`${url.edit(publishedDoc.id)}/versions`) + await expect(page.locator('main.versions')).toContainText('Previously Published') + }) + test('should show global versions view level action in globals versions view', async () => { const global = new AdminUrlUtil(serverURL, draftGlobalSlug) await page.goto(`${global.global(draftGlobalSlug)}/versions`)