Skip to content

Commit

Permalink
fix(structure): set right document if it makes sense (where there is …
Browse files Browse the repository at this point in the history
…a orphan version but no draft)

fix(structre): make sure that you get the first version of the document in case if there are no drafts or published when viewing a different release

fix(structure): fix issue when reading versins when they don't exist / drafts and published exist

fix(structure): fix issue where you couldn't remove the pinned version
  • Loading branch information
RitaDias committed Feb 25, 2025
1 parent 63e89b9 commit 235c143
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
useCopyPaste,
useDocumentOperation,
useDocumentValuePermissions,
useDocumentVersions,
useEditState,
useFormState,
useInitialValue,
Expand Down Expand Up @@ -126,6 +127,7 @@ export const DocumentPaneProvider = memo((props: DocumentPaneProviderProps) => {
const params = useUnique(paneRouter.params) || EMPTY_PARAMS

const perspective = usePerspective()
const {data: documentVersions} = useDocumentVersions({documentId})

const {isReleaseLocked, selectedReleaseId, selectedPerspectiveName} = useMemo(() => {
// TODO: COREL - Remove this after updating sanity-assist to use <PerspectiveProvider>
Expand Down Expand Up @@ -168,10 +170,22 @@ export const DocumentPaneProvider = memo((props: DocumentPaneProviderProps) => {

const initialValue = useUnique(initialValueRaw)
const isInitialValueLoading = initialValue.loading
const onlyHasVersions =
documentVersions && documentVersions.length === 1 && isVersionId(documentVersions[0])

const {patch} = useDocumentOperation(documentId, documentType, selectedReleaseId)
const schemaType = schema.get(documentType) as ObjectSchemaType | undefined
const editState = useEditState(documentId, documentType, 'default', selectedReleaseId)
const editState = useEditState(
documentId,
documentType,
'default',
// check if the selected version is the only version, if it isn't and it doesn't exist in hte release
// then it needs to use the doucmentverisons
!documentVersions || !onlyHasVersions
? selectedReleaseId
: getVersionFromId(documentVersions[0]),
)

const {validation: validationRaw} = useValidationStatus(
documentId,
documentType,
Expand All @@ -190,6 +204,10 @@ export const DocumentPaneProvider = memo((props: DocumentPaneProviderProps) => {
(liveEdit ? initialValue.value : {_id: documentId, _type: documentType})
)
}
// if no version is selected, but there is only version, it should default to the version version it finds
if (!selectedPerspectiveName && onlyHasVersions) {
return editState.version || editState.draft || editState.published || initialValue.value
}
return editState.draft || editState.published || initialValue.value
}, [
documentId,
Expand All @@ -199,6 +217,7 @@ export const DocumentPaneProvider = memo((props: DocumentPaneProviderProps) => {
editState.version,
initialValue.value,
liveEdit,
onlyHasVersions,
selectedPerspectiveName,
selectedReleaseId,
])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,19 +150,14 @@ export const DocumentPanel = function DocumentPanel(props: DocumentPanelProps) {
if (params?.historyVersion) {
return <ArchivedReleaseDocumentBanner />
}
const isCreatingDocument = displayed && !displayed._createdAt
const isScheduledRelease =
isReleaseDocument(selectedPerspective) && isReleaseScheduledOrScheduling(selectedPerspective)

if (isScheduledRelease) {
return <ScheduledReleaseBanner currentRelease={selectedPerspective as ReleaseDocument} />
}
if (
displayed?._id &&
getVersionFromId(displayed._id) !== selectedReleaseId &&
ready &&
!isCreatingDocument
) {

if (displayed?._id && getVersionFromId(displayed._id) !== selectedReleaseId && ready) {
return (
<AddToReleaseBanner
documentId={value._id}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ export const DocumentPerspectiveList = memo(function DocumentPerspectiveList() {
const {editState, displayed, documentType, documentId} = useDocumentPane()
const {data: documentVersions} = useDocumentVersions({documentId})
const isCreatingDocument = displayed && !displayed._createdAt
const onlyHasVersions =
(!editState?.published && !editState?.draft && documentVersions?.length > 0) ||
isCreatingDocument

const filteredReleases: FilterReleases = useMemo(() => {
if (!documentVersions) return {notCurrentReleases: [], currentReleases: [], inCreation: null}
Expand Down Expand Up @@ -231,7 +234,7 @@ export const DocumentPerspectiveList = memo(function DocumentPerspectiveList() {

const isDraftDisabled: boolean = useMemo(() => {
// Draft is disabled when the document has no published or draft but has versions
if (!editState?.published && !editState?.draft && documentVersions?.length) {
if (onlyHasVersions) {
return true
}

Expand All @@ -244,14 +247,7 @@ export const DocumentPerspectiveList = memo(function DocumentPerspectiveList() {
if (isCreatingDocument && selectedReleaseId) return true
if (isLiveEdit) return true
return false
}, [
documentVersions?.length,
editState?.draft,
editState?.published,
isCreatingDocument,
isLiveEdit,
selectedReleaseId,
])
}, [editState?.draft, isCreatingDocument, isLiveEdit, onlyHasVersions, selectedReleaseId])

return (
<>
Expand Down

0 comments on commit 235c143

Please sign in to comment.