Skip to content

Commit

Permalink
feat(corel): releases DocumentRow updates
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrobonamin committed Jul 8, 2024
1 parent aa946a7 commit 47e6dda
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 78 deletions.
48 changes: 31 additions & 17 deletions packages/sanity/src/core/releases/tool/detail/ReleaseOverview.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import {DocumentsIcon} from '@sanity/icons'
import {type SanityDocument} from '@sanity/types'
import {AvatarStack, Card, Flex, Heading, Stack, Text, useToast} from '@sanity/ui'
import {useCallback, useState} from 'react'
import {type SanityDocument, useAddonDataset, UserAvatar, useRelativeTime} from 'sanity'

import {BundleIconEditorPicker} from '../../../bundles/components/dialog/BundleIconEditorPicker'
import {RelativeTime} from '../../../components/RelativeTime'
import {UserAvatar} from '../../../components/userAvatar/UserAvatar'
import {type BundleDocument} from '../../../store/bundles/types'
import {useAddonDataset} from '../../../studio/addonDataset/useAddonDataset'
import {Chip} from '../../components/Chip'
import {DocumentTable} from './documentTable'

Expand All @@ -25,30 +28,29 @@ export function ReleaseOverview(props: {documents: SanityDocument[]; release: Bu
const toast = useToast()
const handleIconValueChange = useCallback(
async (value: {hue: BundleDocument['hue']; icon: BundleDocument['icon']}) => {
setIconValue(value)
if (client) {
try {
await client?.patch(release._id).set(value).commit()
} catch (e) {
toast.push({
closable: true,
status: 'error',
title: 'Failed to save changes',
})
}
} else {
if (!client) {
toast.push({
closable: true,
status: 'error',
title: 'Failed to save changes',
description: 'AddonDataset client not found',
})
return
}

setIconValue(value)
try {
await client?.patch(release._id).set(value).commit()
} catch (e) {
toast.push({
closable: true,
status: 'error',
title: 'Failed to save changes',
})
}
},
[client, release._id, toast],
)
const relativeCreatedAt = useRelativeTime(release._createdAt)
const relativePublishedAt = useRelativeTime(release.publishedAt || '')

return (
<Stack paddingX={4} space={5}>
Expand Down Expand Up @@ -81,7 +83,11 @@ export function ReleaseOverview(props: {documents: SanityDocument[]; release: Bu
{/* Created */}
<Chip
avatar={<UserAvatar size={0} user={release.authorId} />}
text={`Created ${relativeCreatedAt}`}
text={
<span>
Created <RelativeTime time={release._createdAt} useTemporalPhrase />
</span>
}
/>

{/* Published */}
Expand All @@ -90,7 +96,15 @@ export function ReleaseOverview(props: {documents: SanityDocument[]; release: Bu
avatar={
release.publishedBy ? <UserAvatar size={0} user={release.publishedBy} /> : null
}
text={release.publishedAt ? `Published ${relativePublishedAt}` : 'Not published'}
text={
release.publishedAt ? (
<span>
Published <RelativeTime time={release.publishedAt} useTemporalPhrase />
</span>
) : (
'Not published'
)
}
/>
)}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function DocumentHeader(props: {
</Stack>

{/* Created */}
<Flex paddingY={3} sizing="border" style={{width: 100}}>
<Flex paddingY={3} sizing="border" style={{width: 130}}>
<Button
iconRight={sort.property === '_createdAt' ? sortIcon : undefined}
mode="bleed"
Expand All @@ -48,7 +48,7 @@ export function DocumentHeader(props: {
</Flex>

{/* Updated */}
<Flex paddingY={3} sizing="border" style={{width: 100}}>
<Flex paddingY={3} sizing="border" style={{width: 130}}>
<Button
iconRight={sort.property === '_updatedAt' ? sortIcon : undefined}
mode="bleed"
Expand All @@ -69,7 +69,7 @@ export function DocumentHeader(props: {
</Flex>

{/* Published */}
<Flex paddingY={3} sizing="border" style={{width: 100}}>
<Flex paddingY={3} sizing="border" style={{width: 130}}>
<Button
iconRight={sort.property === '_publishedAt' ? sortIcon : undefined}
mode="bleed"
Expand Down Expand Up @@ -107,14 +107,14 @@ export function DocumentHeader(props: {
</Box>
</Flex>

{/* Actions */}
<Flex paddingY={3} sizing="border" style={{width: 49}}>
{/* Actions is empty - don't render yet */}
{/* <Flex paddingY={3} sizing="border" style={{width: 49}}>
<Box padding={2}>
<Text muted size={1} weight="medium">
&nbsp;
</Text>
</Box>
</Flex>
</Flex> */}
</Flex>
</Card>
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {CheckmarkCircleIcon, EllipsisHorizontalIcon, EmptyIcon, Progress50Icon} from '@sanity/icons'
import {AvatarStack, Box, Button, Card, Flex, Text} from '@sanity/ui'
import {CheckmarkCircleIcon, EmptyIcon, Progress50Icon} from '@sanity/icons'
import {type SanityDocument} from '@sanity/types'
import {AvatarStack, Box, Card, Flex, Text} from '@sanity/ui'
import {
type Dispatch,
type ForwardedRef,
Expand Down Expand Up @@ -56,15 +57,15 @@ const DocumentStatus = ({status}: {status: keyof typeof DOCUMENT_STATUS}) => {
</Box>
)
}

export function DocumentRow(props: {
documentId: string
documentTypeName: string
// releaseName: string
document: SanityDocument
release: BundleDocument
setCollaborators: Dispatch<SetStateAction<string[]>>
}) {
const {documentId, documentTypeName, release, setCollaborators} = props

const {document, release, setCollaborators} = props
const documentId = document._id
const documentTypeName = document._type
const schema = useSchema()
const schemaType = schema.get(documentTypeName) as SchemaType | undefined
if (!schemaType) {
Expand All @@ -81,20 +82,13 @@ export function DocumentRow(props: {
[documentId, documentPreviewStore, perspective, schemaType],
)

const {
draft,
published,
version = {},
isLoading,
} = useObservable(previewStateObservable, {
const {draft, published, version, isLoading} = useObservable(previewStateObservable, {
draft: null,
isLoading: true,
published: null,
})

const value = version ?? draft ?? published

const history = useVersionHistory(documentId, value?._rev)
const history = useVersionHistory(documentId, document?._rev)

useEffect(() => {
setCollaborators((pre) => Array.from(new Set([...pre, ...history.editors])))
Expand All @@ -120,20 +114,12 @@ export function DocumentRow(props: {
}),
[documentId, documentTypeName, release.name],
)
if (!value) {
return (
<Card border radius={3} style={{height: '57px'}} paddingX={3}>
<Flex align="center" style={{height: '100%'}}>
<Text muted>Loading...</Text>
</Flex>
</Card>
)
}

const status =
// eslint-disable-next-line no-nested-ternary
value._state === 'ready'
document._state === 'ready'
? 'ready'
: value._updatedAt === value._createdAt
: document._updatedAt === document._createdAt
? 'noChanges'
: 'edited'

Expand All @@ -142,57 +128,56 @@ export function DocumentRow(props: {
<Flex style={{margin: -1}}>
<Box flex={1} padding={1}>
<Card as={LinkComponent} radius={2} data-as="a">
{schemaType && (
// eslint-disable-next-line react/jsx-no-undef
<SanityDefaultPreview
{...getPreviewValueWithFallback({
value,
draft,
published,
version,
perspective,
})}
/>
)}
<SanityDefaultPreview
{...getPreviewValueWithFallback({
value: document,
draft,
published,
version,
perspective,
})}
isPlaceholder={isLoading}
/>
</Card>
</Box>

{/* Created */}
<Flex align="center" paddingX={2} paddingY={3} sizing="border" style={{width: 100}}>
{value._createdAt && (
<Flex align="center" paddingX={2} paddingY={3} sizing="border" style={{width: 130}}>
{document._createdAt && (
<Flex align="center" gap={2}>
{history.createdBy && <UserAvatar size={0} user={history.createdBy} />}
<Text muted size={1}>
<RelativeTime time={value._createdAt} />
<RelativeTime time={document._createdAt} useTemporalPhrase minimal />
</Text>
</Flex>
)}
</Flex>

{/* Updated */}
<Flex align="center" paddingX={2} paddingY={3} sizing="border" style={{width: 100}}>
{value._updatedAt && (
<Flex align="center" paddingX={2} paddingY={3} sizing="border" style={{width: 130}}>
{document._updatedAt && (
<Flex align="center" gap={2}>
{history.lastEditedBy && <UserAvatar size={0} user={history.lastEditedBy} />}
<Text muted size={1}>
<RelativeTime time={value._updatedAt} />
<RelativeTime time={document._updatedAt} useTemporalPhrase minimal />
</Text>
</Flex>
)}
</Flex>

{/* Published */}
<Flex align="center" paddingX={2} paddingY={3} sizing="border" style={{width: 100}}>
{value._publishedAt && (
<Flex align="center" paddingX={2} paddingY={3} sizing="border" style={{width: 130}}>
{/* TODO: How to get the publishedAt date from the document, consider history API */}
{/* {document._publishedAt && (
<Flex align="center" gap={2}>
<UserAvatar size={0} user={value._publishedBy} />
<UserAvatar size={0} user={document._publishedBy} />
<Text muted size={1}>
<RelativeTime time={value._publishedAt} />
<RelativeTime time={document._publishedAt} />
</Text>
</Flex>
)}
)} */}

{!value._publishedAt && (
{!document._publishedAt && (
<Text muted size={1}>
&nbsp;
</Text>
Expand All @@ -211,14 +196,15 @@ export function DocumentRow(props: {
<DocumentStatus status={status} />
</Flex>

<Flex align="center" flex="none" padding={3}>
{/* Actions is empty - don't render yet */}
{/* <Flex align="center" flex="none" padding={3}>
<Button
disabled={Boolean(release?.archived || release?.publishedAt)}
icon={EllipsisHorizontalIcon}
mode="bleed"
padding={2}
/>
</Flex>
</Flex> */}
</Flex>
</Card>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ export function DocumentTable(props: {
<RowStack>
{sortedDocuments.map((d) => (
<DocumentRow
documentId={d._id}
documentTypeName={d._type}
document={d}
key={d._id}
release={release}
setCollaborators={setCollaborators}
Expand Down

0 comments on commit 47e6dda

Please sign in to comment.