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): update getPublishDateFromRelease, add ScheduledRelease type #7754

Open
wants to merge 3 commits into
base: corel
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
1 change: 1 addition & 0 deletions packages/sanity/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export {
isDraftPerspective,
isPublishedPerspective,
isReleaseDocument,
isScheduledRelease,
LATEST,
ReleaseAvatar,
ReleaseBadge,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export const PublishedRelease = defineEvent({
/** When a release is successfully scheduled
* @internal
*/
export const ScheduledRelease = defineEvent({
export const ReleaseScheduledEvent = defineEvent({
name: 'Schedule release',
version: 1,
description: 'User scheduled a release',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -170,7 +170,7 @@ export const GlobalPerspectiveMenuItem = forwardRef<
</Text>
{!isPublishedPerspective(release) &&
release.metadata.releaseType !== 'undecided' &&
(release.publishAt || release.metadata.intendedPublishAt) && (
Copy link
Member

Choose a reason for hiding this comment

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

These aren't a like for like swap I don't think, since isScheduledRelease only looks at intendedPublishAt. I can't recall exactly but it might be worth verifying that when a scheduled release is actually scheduled, and it's possible to reconfirm the scheduled datetime, whether we also update the intendedPublishAt to reflex this too.
Screenshot 2024-11-05 at 13 40 15

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are we removing the intendedPublishAt when scheduling it?
It's confusing how close this names are.

Maybe we need to find a better name for the releaseType ="scheduled"

Copy link
Member

Choose a reason for hiding this comment

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

I think we leave intendedPublishAt as whatever it was, and only persist this new date into publishAt via the schedule action. We should agree either to clear it or always keep it aligned. But of course the schedule action can also be triggered outwith studio, so guaranteeing they match isn't possible

isScheduledRelease(release) && (
<Text muted size={1}>
{formatRelative(getPublishDateFromRelease(release), new Date())}
</Text>
Expand Down
17 changes: 17 additions & 0 deletions packages/sanity/src/core/releases/store/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,23 @@ export interface ReleaseSystemDocument {
state: ReleaseState
publishAt?: string
}

/**
* @internal
*/
export interface ScheduledRelease extends ReleaseDocument {
publishAt?: string
metadata: ReleaseDocument['metadata'] & {
intendedPublishAt: string
releaseType: 'scheduled'
}
}
/**
* @internal
*/
export const isScheduledRelease = (release: ReleaseDocument): release is ScheduledRelease =>
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, the naming here is vague to me, because a scheduled release to me has state of scheduled or scheduling. This is like a prescheduled release, no? I wonder if we should introduce a name like intendedScheduledRelease?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I think you are right, we need to find a better name for the intended schedule releases

release.metadata.releaseType === 'scheduled' && 'intendedPublishAt' in release.metadata

/**
* @internal
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {DateTimeInput} from '../../../../../ui-components/inputs/DateInputs/Date
import {ToneIcon} from '../../../../../ui-components/toneIcon/ToneIcon'
import {getCalendarLabels} from '../../../../form/inputs/DateInputs/utils'
import {Translate, useTranslation} from '../../../../i18n'
import {ScheduledRelease} from '../../../__telemetry__/releases.telemetry'
import {ReleaseScheduledEvent} from '../../../__telemetry__/releases.telemetry'
import {releasesLocaleNamespace} from '../../../i18n'
import {type ReleaseDocument} from '../../../index'
import {useReleaseOperations} from '../../../store/useReleaseOperations'
Expand Down Expand Up @@ -45,7 +45,7 @@ export const ReleaseScheduleButton = ({
try {
setStatus('scheduling')
await schedule(release._id, publishAt)
telemetry.log(ScheduledRelease)
telemetry.log(ReleaseScheduledEvent)
toast.push({
closable: true,
status: 'success',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
}
Expand Down Expand Up @@ -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,
Expand Down
20 changes: 2 additions & 18 deletions packages/sanity/src/core/releases/util/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

/**
Expand Down Expand Up @@ -64,28 +64,12 @@ export function getCreateVersionOrigin(documentId: string): VersionOriginTypes {
}

/** @internal */
export function getPublishDateFromRelease(release: ReleaseDocument): Date {
export function getPublishDateFromRelease(release: ScheduledRelease): Date {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated the type to ScheduledRelease to assert that a date will always be returned.

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<ReleaseDocument, 'publishAt'> & {
metadata: Pick<ReleaseDocument['metadata'], 'intendedPublishAt'>
},
): 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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
getBundleIdFromReleaseDocumentId,
getPublishDateFromRelease,
getReleaseTone,
isScheduledRelease,
LATEST,
type ReleaseDocument,
Translate,
Expand All @@ -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)
Expand All @@ -46,7 +44,7 @@ export function AddToReleaseBanner({
content={
<Flex align="center" justify="space-between" gap={1} flex={1}>
<Text size={1}>
{isScheduled ? (
{isScheduledRelease(currentRelease) ? (
<Flex align="center" justify="center" gap={2}>
<LockIcon />{' '}
<Translate
Expand Down Expand Up @@ -89,7 +87,7 @@ export function AddToReleaseBanner({
)}
</Text>

{!isScheduled && (
{!isScheduledRelease(currentRelease) && (
<Button
text={t('banners.release.action.add-to-release')}
tone={tone}
Expand Down
Loading