-
Notifications
You must be signed in to change notification settings - Fork 435
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add absolute tooltip dates to document statusbar, don't render …
…statusbar until ready, minor cleanup
- Loading branch information
Showing
10 changed files
with
139 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import {PreviewValue, SanityDocument} from '@sanity/types' | ||
import {useIntlDateTimeFormat} from '../i18n' | ||
import {useRelativeTime} from './useRelativeTime' | ||
|
||
interface DocumentStatusTimeAgoOptions { | ||
draft?: PreviewValue | Partial<SanityDocument> | null | ||
hidePublishedDate?: boolean | ||
published?: PreviewValue | Partial<SanityDocument> | null | ||
absoluteDate?: boolean | ||
} | ||
|
||
/** | ||
* React hook which returns a human readable string of the provided document's status. | ||
* | ||
* @internal | ||
* @hidden | ||
*/ | ||
export function useDocumentStatus({ | ||
absoluteDate, | ||
draft, | ||
hidePublishedDate, | ||
published, | ||
}: DocumentStatusTimeAgoOptions): string | undefined { | ||
const draftUpdatedAt = draft && '_updatedAt' in draft ? draft._updatedAt : '' | ||
const publishedUpdatedAt = published && '_updatedAt' in published ? published._updatedAt : '' | ||
|
||
const intlDateFormat = useIntlDateTimeFormat({ | ||
dateStyle: 'medium', | ||
timeStyle: 'short', | ||
}) | ||
|
||
const draftDateAbsolute = draftUpdatedAt && intlDateFormat.format(new Date(draftUpdatedAt)) | ||
const publishedDateAbsolute = | ||
publishedUpdatedAt && intlDateFormat.format(new Date(publishedUpdatedAt)) | ||
|
||
const draftUpdatedTimeAgo = useRelativeTime(draftUpdatedAt || '', { | ||
minimal: true, | ||
useTemporalPhrase: true, | ||
}) | ||
const publishedUpdatedTimeAgo = useRelativeTime(publishedUpdatedAt || '', { | ||
minimal: true, | ||
useTemporalPhrase: true, | ||
}) | ||
|
||
const publishedDate = absoluteDate ? publishedDateAbsolute : publishedUpdatedTimeAgo | ||
const updatedDate = absoluteDate ? draftDateAbsolute : draftUpdatedTimeAgo | ||
|
||
const documentStatus = [ | ||
// Published status | ||
publishedUpdatedTimeAgo | ||
? `Published${hidePublishedDate ? '' : ` ${publishedDate}`}` | ||
: `Not published`, | ||
// Last updated (draft) status | ||
...(draftUpdatedTimeAgo ? [`(Updated ${updatedDate})`] : []), | ||
] | ||
|
||
return documentStatus.join(' ') | ||
} |
43 changes: 0 additions & 43 deletions
43
packages/sanity/src/core/hooks/useDocumentStatusTimeAgo.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
packages/sanity/src/desk/panes/document/statusBar/sparkline/DocumentStatusLine.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import {Box, Flex, Text} from '@sanity/ui' | ||
import React from 'react' | ||
import {Tooltip} from '../../../../../ui' | ||
import {DocumentStatus} from '../../../../../ui/documentStatus' | ||
import {EditStateFor, useDocumentStatus} from 'sanity' | ||
|
||
interface DocumentStatusLineProps { | ||
collapsed?: boolean | ||
editState: EditStateFor | null | ||
} | ||
|
||
export function DocumentStatusLine({collapsed, editState}: DocumentStatusLineProps) { | ||
const statusTimeAgo = useDocumentStatus({ | ||
draft: editState?.draft, | ||
hidePublishedDate: collapsed, | ||
published: editState?.published, | ||
}) | ||
|
||
const statusAbsolute = useDocumentStatus({ | ||
absoluteDate: true, | ||
draft: editState?.draft, | ||
published: editState?.published, | ||
}) | ||
|
||
if (!editState) { | ||
return null | ||
} | ||
|
||
return ( | ||
<Tooltip content={statusAbsolute}> | ||
<Flex align="center" gap={3} paddingLeft={1} paddingY={2}> | ||
<DocumentStatus | ||
draft={editState?.draft} | ||
published={editState?.published} | ||
showPublishedIcon | ||
/> | ||
<Box flex={1}> | ||
<Text muted textOverflow="ellipsis" size={1} weight="medium"> | ||
{statusTimeAgo} | ||
</Text> | ||
</Box> | ||
</Flex> | ||
</Tooltip> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters