Skip to content

Commit

Permalink
fix(structure): update documentPerspectiveList tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrobonamin committed Jan 19, 2025
1 parent 11bd15a commit 7e99e03
Show file tree
Hide file tree
Showing 3 changed files with 433 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
type ReleaseDocument,
Translate,
useDateTimeFormat,
type UseDateTimeFormatOptions,
useDocumentVersions,
usePerspective,
useReleases,
Expand Down Expand Up @@ -70,15 +71,16 @@ const TooltipContent = ({release}: {release: ReleaseDocument}) => {
return null
}

const DATE_TIME_FORMAT: UseDateTimeFormatOptions = {
dateStyle: 'medium',
timeStyle: 'short',
}

export const DocumentPerspectiveList = memo(function DocumentPerspectiveList() {
const {selectedPerspectiveName} = usePerspective()
const {setPerspective, selectedReleaseId, selectedPerspectiveName} = usePerspective()
const {t} = useTranslation()
const {setPerspective, selectedReleaseId} = usePerspective()
const {params} = usePaneRouter()
const dateTimeFormat = useDateTimeFormat({
dateStyle: 'medium',
timeStyle: 'short',
})
const dateTimeFormat = useDateTimeFormat(DATE_TIME_FORMAT)
const schema = useSchema()
const {data: releases, loading, archivedReleases} = useReleases()
const {editState, displayed, documentType, documentId} = useDocumentPane()
Expand Down Expand Up @@ -146,11 +148,11 @@ export const DocumentPerspectiveList = memo(function DocumentPerspectiveList() {
const schemaType = schema.get(documentType)
// If it's a live edit document the only option to edit it is through
// the published perspective, users should be able to select it.
if (schemaType?.liveEdit) return false
if (schemaType?.liveEdit && !selectedReleaseId) return false

// If it's not live edit, we want to check for the existence of the published doc.
return !editState?.published
}, [schema, documentType, editState?.published])
}, [schema, documentType, editState?.published, selectedReleaseId])

const getReleaseChipState = useCallback(
(release: ReleaseDocument): {selected: boolean; disabled?: boolean} => {
Expand All @@ -177,13 +179,12 @@ export const DocumentPerspectiveList = memo(function DocumentPerspectiveList() {
*/
if (
isPublishedId(displayed?._id || '') &&
editState?.published &&
isPublishedPerspective(selectedPerspectiveName || '')
) {
return true
}
return false
}, [displayed, editState, selectedPerspectiveName])
}, [displayed, selectedPerspectiveName])

const isDraftSelected: boolean = useMemo(() => {
const displayedId = displayed?._id || ''
Expand All @@ -207,6 +208,16 @@ export const DocumentPerspectiveList = memo(function DocumentPerspectiveList() {
return true
}, [displayed?._id, editState, params?.historyVersion, selectedPerspectiveName])

const isDraftDisabled: boolean = useMemo(() => {
// Draft is disabled when we are creating a new document inside a release
// or when the document is live edit and there is no draft
if (editState?.draft) return false

if (isCreatingDocument && selectedReleaseId) return true
const isLiveEdit = schema.get(documentType)?.liveEdit
if (isLiveEdit) return true
return false
}, [documentType, editState?.draft, isCreatingDocument, schema, selectedReleaseId])
return (
<>
<VersionChip
Expand Down Expand Up @@ -264,7 +275,7 @@ export const DocumentPerspectiveList = memo(function DocumentPerspectiveList() {
</Text>
}
selected={isDraftSelected}
disabled={!isDraftSelected && !editState?.draft}
disabled={isDraftDisabled}
text={t('release.chip.draft')}
tone="caution"
onClick={handlePerspectiveChange('drafts')}
Expand Down
Loading

0 comments on commit 7e99e03

Please sign in to comment.