Skip to content

Commit

Permalink
feat(sanity): add document to a release from the release summary
Browse files Browse the repository at this point in the history
  • Loading branch information
RitaDias committed Oct 28, 2024
1 parent b30828d commit 32a0bb5
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 6 deletions.
2 changes: 2 additions & 0 deletions packages/sanity/src/core/releases/i18n/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* @internal
*/
const releasesLocaleStrings = {
/** Action text for adding a document to release */
'action.add-document': 'Add document',
/** Action text for archiving a release */
'action.archive': 'Archive',
/** Action text for showing the archived releases */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,6 @@ const TableInner = <TableData, AdditionalRowTableData>({
<div
style={
{
'height': `${rowVirtualizer.getTotalSize()}px`,
// The padding accounts for the height of the <TableHeader> and extra space for padding at the bottom
'paddingBottom': '110px',
'width': '100%',
'position': 'relative',
'--maxInlineSize': rem(maxInlineSize),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import {LayerProvider, PortalProvider, useToast} from '@sanity/ui'
import {useCallback} from 'react'
import {getBundleIdFromReleaseId, useReleaseOperations} from 'sanity'

import {type SanityDocumentLike} from '../../../../../../@sanity/types/src/documents/types'
import {SearchPopover} from '../../../studio/components/navbar/search/components/SearchPopover'
import {SearchProvider} from '../../../studio/components/navbar/search/contexts/search/SearchProvider'

export function AddDocumentSearch({
open,
onClose,
releaseId,
}: {
open: boolean
onClose: () => void
releaseId: string
}): JSX.Element {
const {createVersion} = useReleaseOperations()
const toast = useToast()

const addDocument = useCallback(
(item: Pick<SanityDocumentLike, '_id' | '_type'>) => {
createVersion(item._id, getBundleIdFromReleaseId(releaseId)).then((msg) => {
toast.push({
closable: true,
status: 'success',
title: 'Document added to release',
})
})
},
[createVersion, releaseId, toast],
)

const handleClose = useCallback(() => {
onClose()
}, [onClose])

return (
<LayerProvider zOffset={1}>
<SearchProvider>
<PortalProvider>
<SearchPopover
onClose={handleClose}
onItemSelect={addDocument}
open={open}
disableIntentLink
/>
</PortalProvider>
</SearchProvider>
</LayerProvider>
)
}
29 changes: 27 additions & 2 deletions packages/sanity/src/core/releases/tool/detail/ReleaseSummary.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import {Card} from '@sanity/ui'
import {type RefObject, useCallback, useMemo} from 'react'
import {AddIcon} from '@sanity/icons'
import {Card, Container} from '@sanity/ui'
import {type RefObject, useCallback, useMemo, useState} from 'react'

import {Button} from '../../../../ui-components'
import {useTranslation} from '../../../i18n'
import {type ReleaseDocument} from '../../../store/release/types'
import {releasesLocaleNamespace} from '../../i18n'
import {Table, type TableProps} from '../components/Table/Table'
import {AddDocumentSearch} from './AddDocumentSearch'
import {DocumentActions} from './documentTable/DocumentActions'
import {getDocumentTableColumnDefs} from './documentTable/DocumentTableColumnDefs'
import {type DocumentHistory} from './documentTable/useReleaseHistory'
Expand All @@ -29,6 +32,7 @@ const getRowProps: TableProps<DocumentWithHistory, undefined>['rowProps'] = (dat

export function ReleaseSummary(props: ReleaseSummaryProps) {
const {documents, documentsHistory, release, scrollContainerRef} = props
const [openAddDocumentDialog, setAddDocumentDialog] = useState(false)

const {t} = useTranslation(releasesLocaleNamespace)

Expand Down Expand Up @@ -66,6 +70,10 @@ export function ReleaseSummary(props: ReleaseSummaryProps) {
[],
)

const closeAddDialog = useCallback(() => {
setAddDocumentDialog(false)
}, [])

return (
<Card borderTop ref={scrollContainerRef}>
<Table<DocumentWithHistory>
Expand All @@ -79,6 +87,23 @@ export function ReleaseSummary(props: ReleaseSummaryProps) {
rowProps={getRowProps}
scrollContainerRef={scrollContainerRef}
/>
<Container width={3}>
<Card padding={3}>
<Button
icon={AddIcon}
mode="bleed"
onClick={() => setAddDocumentDialog(true)}
padding={2}
text={t('action.add-document')}
/>
</Card>
</Container>

<AddDocumentSearch
open={openAddDocumentDialog}
onClose={closeAddDialog}
releaseId={release._id}
/>
</Card>
)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import {type Action, type SanityClient, type SanityDocument} from '@sanity/client'
import {type User} from '@sanity/types'
import {omit} from 'lodash'
import {type EditableReleaseDocument, getBundleIdFromReleaseId, getPublishedId} from 'sanity'
import {
type EditableReleaseDocument,
getBundleIdFromReleaseId,
getPublishedId,
getVersionId,
} from 'sanity'

import {RELEASE_METADATA_TMP_DOC_PATH, RELEASE_METADATA_TMP_DOC_TYPE} from './constants'

Expand All @@ -16,6 +21,7 @@ export interface ReleaseOperationsStore {
unschedule: (releaseId: string) => Promise<void>
updateRelease: (release: EditableReleaseDocument) => Promise<void>
createRelease: (release: EditableReleaseDocument) => Promise<void>
createVersion: (releaseId: string, documentId: string) => Promise<void>
}

export function createReleaseOperationsStore(options: {
Expand Down Expand Up @@ -122,12 +128,20 @@ export function createReleaseOperationsStore(options: {
]).then(() => {})
}

const handleCreateVersion = async (documentId: string, releaseId: string) => {
// fetch original document
const doc = await client.fetch(`*[_id == $documentId][0]`, {documentId})

return client.create({...doc, _id: getVersionId(documentId, releaseId)}).then(() => {})
}

return {
schedule: handleScheduleRelease,
unschedule: handleUnscheduleRelease,
createRelease: handleCreateRelease,
updateRelease: handleUpdateRelease,
publishRelease: handlePublishRelease,
createVersion: handleCreateVersion,
}
}

Expand Down

0 comments on commit 32a0bb5

Please sign in to comment.