From 6e7407f4e2a958bfca784fb7f930ea7a10d43736 Mon Sep 17 00:00:00 2001 From: pedrobonamin Date: Tue, 5 Nov 2024 11:52:52 +0100 Subject: [PATCH 1/3] fix(core): update getPublishDateFromRelease, add ScheduledRelease type --- packages/sanity/src/core/index.ts | 1 + .../navbar/GlobalPerspectiveMenuItem.tsx | 4 ++-- .../sanity/src/core/releases/store/types.ts | 11 ++++++++++ .../overview/ReleasesOverviewColumnDefs.tsx | 16 +++++++++------ .../sanity/src/core/releases/util/util.ts | 20 ++----------------- .../banners/AddToReleaseBanner.tsx | 8 +++----- 6 files changed, 29 insertions(+), 31 deletions(-) diff --git a/packages/sanity/src/core/index.ts b/packages/sanity/src/core/index.ts index 74803b6ada0..7e24853d8d0 100644 --- a/packages/sanity/src/core/index.ts +++ b/packages/sanity/src/core/index.ts @@ -37,6 +37,7 @@ export { isDraftPerspective, isPublishedPerspective, isReleaseDocument, + isScheduledRelease, LATEST, ReleaseAvatar, ReleaseBadge, diff --git a/packages/sanity/src/core/releases/navbar/GlobalPerspectiveMenuItem.tsx b/packages/sanity/src/core/releases/navbar/GlobalPerspectiveMenuItem.tsx index 32e17b18183..7dce8865f28 100644 --- a/packages/sanity/src/core/releases/navbar/GlobalPerspectiveMenuItem.tsx +++ b/packages/sanity/src/core/releases/navbar/GlobalPerspectiveMenuItem.tsx @@ -9,7 +9,7 @@ import {Tooltip} from '../../../ui-components/tooltip' import {useTranslation} from '../../i18n/hooks/useTranslation' import {ReleaseAvatar} from '../components/ReleaseAvatar' import {usePerspective} from '../hooks/usePerspective' -import {type ReleaseDocument} from '../store/types' +import {isScheduledRelease, type ReleaseDocument} from '../store/types' import {getBundleIdFromReleaseDocumentId} from '../util/getBundleIdFromReleaseDocumentId' import {getReleaseTone} from '../util/getReleaseTone' import {getPublishDateFromRelease, isPublishedPerspective} from '../util/util' @@ -170,7 +170,7 @@ export const GlobalPerspectiveMenuItem = forwardRef< {!isPublishedPerspective(release) && release.metadata.releaseType !== 'undecided' && - (release.publishAt || release.metadata.intendedPublishAt) && ( + isScheduledRelease(release) && ( {formatRelative(getPublishDateFromRelease(release), new Date())} diff --git a/packages/sanity/src/core/releases/store/types.ts b/packages/sanity/src/core/releases/store/types.ts index 0043e9a6126..13656519190 100644 --- a/packages/sanity/src/core/releases/store/types.ts +++ b/packages/sanity/src/core/releases/store/types.ts @@ -35,6 +35,17 @@ export interface ReleaseSystemDocument { /** * @internal */ + +export interface ScheduledRelease extends ReleaseDocument { + publishAt?: string + metadata: ReleaseDocument['metadata'] & { + intendedPublishAt: string + releaseType: 'scheduled' + } +} +export const isScheduledRelease = (release: ReleaseDocument): release is ScheduledRelease => + release.metadata.releaseType === 'scheduled' && 'intendedPublishAt' in release.metadata + export interface ReleaseDocument { /** * typically diff --git a/packages/sanity/src/core/releases/tool/overview/ReleasesOverviewColumnDefs.tsx b/packages/sanity/src/core/releases/tool/overview/ReleasesOverviewColumnDefs.tsx index 9916243629c..556917b43c2 100644 --- a/packages/sanity/src/core/releases/tool/overview/ReleasesOverviewColumnDefs.tsx +++ b/packages/sanity/src/core/releases/tool/overview/ReleasesOverviewColumnDefs.tsx @@ -11,9 +11,10 @@ import {Translate, useTranslation} from '../../../i18n' import {ReleaseAvatar} from '../../components/ReleaseAvatar' import {usePerspective} from '../../hooks/usePerspective' import {releasesLocaleNamespace} from '../../i18n' +import {isScheduledRelease} from '../../store/types' import {getBundleIdFromReleaseDocumentId} from '../../util/getBundleIdFromReleaseDocumentId' import {getReleaseTone} from '../../util/getReleaseTone' -import {getReleasePublishDate} from '../../util/util' +import {getPublishDateFromRelease} from '../../util/util' import {type TableRowProps} from '../components/Table/Table' import {Headers} from '../components/Table/TableHeader' import {type Column} from '../components/Table/types' @@ -32,7 +33,7 @@ const ReleaseTime = ({release}: {release: TableRelease}) => { return t('release.type.undecided') } - const publishDate = getReleasePublishDate(release) + const publishDate = isScheduledRelease(release) ? getPublishDateFromRelease(release) : undefined return publishDate ? format(new Date(publishDate), 'PPpp') : null } @@ -137,11 +138,14 @@ export const releasesOverviewColumnDefs: ( { id: 'publishAt', sorting: true, - sortTransform: ({metadata, publishAt}) => { - if (metadata.releaseType === 'undecided') return Infinity + sortTransform: (release) => { + if (release.metadata.releaseType === 'undecided') return Infinity - const publishDate = getReleasePublishDate({metadata, publishAt}) - if (metadata.releaseType === 'asap' || !publishDate) return 0 + const publishDate = isScheduledRelease(release) + ? getPublishDateFromRelease(release) + : undefined + + if (release.metadata.releaseType === 'asap' || !publishDate) return 0 return new Date(publishDate).getTime() }, width: 250, diff --git a/packages/sanity/src/core/releases/util/util.ts b/packages/sanity/src/core/releases/util/util.ts index 40949cedfbf..9dfc2680af4 100644 --- a/packages/sanity/src/core/releases/util/util.ts +++ b/packages/sanity/src/core/releases/util/util.ts @@ -7,7 +7,7 @@ import { } from '../../util' import {type CurrentPerspective} from '../hooks/usePerspective' import {type VersionOriginTypes} from '../index' -import {type ReleaseDocument} from '../store/types' +import {type ReleaseDocument, type ScheduledRelease} from '../store/types' import {LATEST} from './const' /** @@ -64,28 +64,12 @@ export function getCreateVersionOrigin(documentId: string): VersionOriginTypes { } /** @internal */ -export function getPublishDateFromRelease(release: ReleaseDocument): Date { +export function getPublishDateFromRelease(release: ScheduledRelease): Date { const dateString = release.publishAt || release.metadata.intendedPublishAt - if (!dateString) { - console.error('No publish date found on release', release) - return new Date() - } return new Date(dateString) } -/** @internal */ -export function getReleasePublishDate( - release: Pick & { - metadata: Pick - }, -): Date | null { - const publishDate = release.metadata.intendedPublishAt || release.publishAt - - if (!publishDate) return null - return new Date(publishDate) -} - /** @internal */ export function isPublishedPerspective(bundle: CurrentPerspective | string): bundle is 'published' { return bundle === 'published' diff --git a/packages/sanity/src/structure/panes/document/documentPanel/banners/AddToReleaseBanner.tsx b/packages/sanity/src/structure/panes/document/documentPanel/banners/AddToReleaseBanner.tsx index 3a2453919ce..162ae837faa 100644 --- a/packages/sanity/src/structure/panes/document/documentPanel/banners/AddToReleaseBanner.tsx +++ b/packages/sanity/src/structure/panes/document/documentPanel/banners/AddToReleaseBanner.tsx @@ -6,6 +6,7 @@ import { getBundleIdFromReleaseDocumentId, getPublishDateFromRelease, getReleaseTone, + isScheduledRelease, LATEST, type ReleaseDocument, Translate, @@ -30,9 +31,6 @@ export function AddToReleaseBanner({ const {createVersion} = useVersionOperations() - const isScheduled = - currentRelease?.state === 'scheduled' || currentRelease?.state === 'scheduling' - const handleAddToRelease = useCallback(async () => { if (currentRelease._id) { await createVersion(getBundleIdFromReleaseDocumentId(currentRelease._id), documentId) @@ -46,7 +44,7 @@ export function AddToReleaseBanner({ content={ - {isScheduled ? ( + {isScheduledRelease(currentRelease) ? ( {' '} - {!isScheduled && ( + {!isScheduledRelease(currentRelease) && (