-
Notifications
You must be signed in to change notification settings - Fork 427
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
feat(core): add versions to references and status icons #7690
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
102 changes: 66 additions & 36 deletions
102
packages/sanity/src/core/components/documentStatusIndicator/DocumentStatusIndicator.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 |
---|---|---|
@@ -1,62 +1,92 @@ | ||
import {DotIcon} from '@sanity/icons' | ||
import {type PreviewValue, type SanityDocument} from '@sanity/types' | ||
import {Text} from '@sanity/ui' | ||
import {useMemo} from 'react' | ||
import {Flex, Text} from '@sanity/ui' | ||
import {type ComponentType, useMemo} from 'react' | ||
import {styled} from 'styled-components' | ||
|
||
import {type VersionsRecord} from '../../preview/utils/getPreviewStateObservable' | ||
import {useReleases} from '../../store/release/useReleases' | ||
|
||
interface DocumentStatusProps { | ||
draft?: PreviewValue | Partial<SanityDocument> | null | ||
published?: PreviewValue | Partial<SanityDocument> | null | ||
version?: PreviewValue | Partial<SanityDocument> | null | ||
// eslint-disable-next-line | ||
versions?: VersionsRecord | ||
versions: VersionsRecord | undefined | ||
} | ||
|
||
const Root = styled(Text)` | ||
&[data-status='edited'] { | ||
--card-icon-color: var(--card-badge-caution-dot-color); | ||
} | ||
&[data-status='unpublished'] { | ||
&[data-status='not-published'] { | ||
--card-icon-color: var(--card-badge-default-dot-color); | ||
opacity: 0.5 !important; | ||
} | ||
&[data-status='draft'] { | ||
--card-icon-color: var(--card-badge-caution-dot-color); | ||
} | ||
&[data-status='asap'] { | ||
--card-icon-color: var(--card-badge-critical-dot-color); | ||
} | ||
&[data-status='undecided'] { | ||
--card-icon-color: var(--card-badge-explore-dot-color); | ||
} | ||
&[data-status='scheduled'] { | ||
--card-icon-color: var(--card-badge-primary-dot-color); | ||
} | ||
` | ||
|
||
type Status = 'not-published' | 'draft' | 'asap' | 'scheduled' | 'undecided' | ||
|
||
/** | ||
* Renders a dot indicating the current document status. | ||
* | ||
* - Yellow (caution) for published documents with edits | ||
* - Gray (default) for unpublished documents (with or without edits) | ||
* | ||
* No dot will be displayed for published documents without edits or for version documents. | ||
* | ||
* @internal | ||
*/ | ||
export function DocumentStatusIndicator({draft, published, version}: DocumentStatusProps) { | ||
const $draft = Boolean(draft) | ||
const $published = Boolean(published) | ||
const $version = Boolean(version) | ||
|
||
const status = useMemo(() => { | ||
if ($version) return undefined | ||
if ($draft && !$published) return 'unpublished' | ||
return 'edited' | ||
}, [$draft, $published, $version]) | ||
|
||
// Return null if the document is: | ||
// - Published without edits | ||
// - Neither published or without edits (this shouldn't be possible) | ||
// - A version | ||
if ((!$draft && !$published) || (!$draft && $published) || $version) { | ||
return null | ||
} | ||
export function DocumentStatusIndicator({draft, published, versions}: DocumentStatusProps) { | ||
const {data: releases} = useReleases() | ||
const versionsList = useMemo( | ||
() => | ||
versions | ||
? Object.keys(versions).map((versionName) => { | ||
const release = releases?.find((r) => r.name === versionName) | ||
return release?.metadata.releaseType | ||
}) | ||
: [], | ||
[releases, versions], | ||
) | ||
|
||
const indicators: { | ||
status: Status | ||
show: boolean | ||
}[] = [ | ||
{ | ||
status: draft && !published ? 'not-published' : 'draft', | ||
show: Boolean(draft), | ||
}, | ||
{ | ||
status: 'asap', | ||
show: versionsList.includes('asap'), | ||
}, | ||
{ | ||
status: 'scheduled', | ||
show: versionsList.includes('scheduled'), | ||
}, | ||
{ | ||
status: 'undecided', | ||
show: versionsList.includes('undecided'), | ||
}, | ||
] | ||
|
||
// TODO: Remove debug `status[0]` output. | ||
return ( | ||
<Root data-status={status} size={1}> | ||
<DotIcon /> | ||
</Root> | ||
<Flex> | ||
{indicators | ||
.filter(({show}) => show) | ||
.map(({status}) => ( | ||
<Dot key={status} status={status} /> | ||
))} | ||
</Flex> | ||
) | ||
} | ||
|
||
const Dot: ComponentType<{status: Status}> = ({status}) => ( | ||
<Root data-status={status} size={1}> | ||
<DotIcon /> | ||
</Root> | ||
) |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The simplification of this makes me so happy 🥹