Skip to content

Commit

Permalink
feat: deeplinking to published or archived versions
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanl17 committed Dec 11, 2024
1 parent e5e17ab commit 577843f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {IntentLink} from 'sanity/router'
import {DocumentPreviewPresence} from '../../../presence'
import {SanityDefaultPreview} from '../../../preview/components/SanityDefaultPreview'
import {getPublishedId} from '../../../util/draftUtils'
import {useDocumentPresence} from '../../index'
import {type ReleaseState, useDocumentPresence} from '../../index'
import {getReleaseIdFromReleaseDocumentId} from '../../util/getReleaseIdFromReleaseDocumentId'

interface ReleaseDocumentPreviewProps {
Expand All @@ -15,8 +15,8 @@ interface ReleaseDocumentPreviewProps {
releaseId: string
previewValues: PreviewValue
isLoading: boolean
revision?: string
hasValidationError?: boolean
releaseState?: ReleaseState
documentRevision?: string
}

export function ReleaseDocumentPreview({
Expand All @@ -25,11 +25,24 @@ export function ReleaseDocumentPreview({
releaseId,
previewValues,
isLoading,
revision,
hasValidationError,
releaseState,
documentRevision,
}: ReleaseDocumentPreviewProps) {
const documentPresence = useDocumentPresence(documentId)

const intentParams = useMemo(() => {
if (releaseState !== 'published' && releaseState !== 'archived') return {}

const rev = releaseState === 'archived' ? '@lastEdited' : '@lastPublished'

return {
rev,
inspect: 'sanity/structure/history',
historyEvent: documentRevision,
historyVersion: getReleaseIdFromReleaseDocumentId(releaseId),
}
}, [documentRevision, releaseId, releaseState])

const LinkComponent = useMemo(
() =>
// eslint-disable-next-line @typescript-eslint/no-shadow
Expand All @@ -41,30 +54,31 @@ export function ReleaseDocumentPreview({
params={{
id: getPublishedId(documentId),
type: documentTypeName,
...intentParams,
}}
searchParams={[['perspective', getReleaseIdFromReleaseDocumentId(releaseId)]]}
searchParams={[
[
'perspective',
releaseState === 'published'
? 'published'
: getReleaseIdFromReleaseDocumentId(releaseId),
],
]}
ref={ref}
/>
)
}),
[documentId, documentTypeName, releaseId],
[documentId, documentTypeName, intentParams, releaseId, releaseState],
)

const previewPresence = useMemo(
() => documentPresence?.length > 0 && <DocumentPreviewPresence presence={documentPresence} />,
[documentPresence],
)

const preview = (
<SanityDefaultPreview {...previewValues} status={previewPresence} isPlaceholder={isLoading} />
)

/** @todo revision deeplink support for archived and published version docs */
if (revision) return preview

return (
<Card tone="inherit" as={LinkComponent} radius={2} data-as="a">
{preview}
<SanityDefaultPreview {...previewValues} status={previewPresence} isPlaceholder={isLoading} />
</Card>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,23 @@ const MemoReleaseDocumentPreview = memo(
function MemoReleaseDocumentPreview({
item,
releaseId,
revision,
releaseState,
documentRevision,
}: {
item: DocumentInRelease
releaseId: string
revision?: string
releaseState?: ReleaseState
documentRevision?: string
}) {
return (
<ReleaseDocumentPreview
documentId={item.document._id}
documentTypeName={item.document._type}
releaseId={releaseId}
revision={revision}
releaseState={releaseState}
documentRevision={documentRevision}
previewValues={item.previewValues.values}
isLoading={item.previewValues.isLoading}
hasValidationError={item.validation?.hasError}
/>
)
},
Expand Down Expand Up @@ -135,11 +137,8 @@ export const getDocumentTableColumnDefs: (
<MemoReleaseDocumentPreview
item={datum}
releaseId={releaseId}
revision={
releaseState === 'archived' || releaseState === 'published'
? datum.document._rev
: undefined
}
releaseState={releaseState}
documentRevision={datum.document._rev}
/>
</Box>
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ export function DocumentReviewHeader({
releaseId={releaseId}
previewValues={previewValues}
isLoading={isLoading}
hasValidationError={validation?.hasError}
/>
</Box>
<Flex gap={2} padding={3} style={{flexShrink: 0}}>
Expand Down

0 comments on commit 577843f

Please sign in to comment.