Skip to content

Commit

Permalink
refactor(form-builder): simplify cross dataset reference config (#3670)
Browse files Browse the repository at this point in the history
* refactor(form-builder): remove token and project id support in cross dataset references

* refactor(base): remove token header from preview requests

* refactor(schema): remove project and token validation from cross dataset reference schema

* refactor(schema): derive search fields from preview definition for cross dataset references

* refactor(desk-tool): remove cross dataset reference tokens from confirm delete dialog requests

* chore(test-studio): update cross dataset reference schema in test studio

* test(form-builder): remove project and experimental search from cross dataset reference test

* refactor(types): remove token and project id support in cross dataset references

* fix(desk-tool): replace projects terminology in confirm delete dialog

[sc-25932]

* fix(form-builder): remove extra whitespace on reference inputs when empty

[sc-25936]

* fix(form-builder): fix inconsistent padding on CDR preview

[sc-25927]

* fix(desk-tool): handle unavailable dataset names in delete dialog

[sc-25932]

* fix(form-builder): update inconsistent copy for missing document

[sc-25929]

* fix(form-builder): handle invalid image sources in preview

* fix(form-builder): limit CDR warning tooltip width

* fix(form-builder): fix focus handling when editing array reference

* feat(form-builder): show warning if cdr feature is disabled

* fix(form-builder): removed cached inital state

* refactor(form-builder): remove unnecessary conditional

Co-authored-by: Espen Hovlandsdal <espen@hovlandsdal.com>
  • Loading branch information
sjelfull and rexxars committed Oct 20, 2022
1 parent 5d1e161 commit 6b0afbd
Show file tree
Hide file tree
Showing 29 changed files with 585 additions and 646 deletions.
1 change: 0 additions & 1 deletion dev/test-studio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
"react": "17.0.1",
"react-dom": "17.0.1",
"react-icons": "^3.11.0",
"sanity-plugin-cross-project-tokens": "^1.0.0",
"sanity-plugin-dashboard-widget-cats": "^0.0.2",
"sanity-plugin-dashboard-widget-document-list": "^0.0.13",
"sanity-plugin-markdown": "^2.0.4",
Expand Down
3 changes: 1 addition & 2 deletions dev/test-studio/sanity.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
{
"name": "playground",
"title": "Private",
"title": "Private (playground)",
"api": {
"dataset": "playground"
}
Expand All @@ -38,7 +38,6 @@
"@sanity/studio-hints",
"dashboard-widget-cats",
"dashboard-widget-document-list",
"cross-project-tokens",
"markdown",
"mux-input"
],
Expand Down
6 changes: 0 additions & 6 deletions dev/test-studio/schema/debug/circularCrossDatasetReference.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ export const circularCrossDatasetReferenceTest = {
{
type: 'circularCrossDatasetReferenceTest',
icon: CircleIcon,
// eslint-disable-next-line camelcase
__experimental_search: [{path: 'title', weight: 10}],
preview: {
select: {
title: 'title',
Expand All @@ -48,8 +46,6 @@ export const circularCrossDatasetReferenceTest = {
{
type: 'circularCrossDatasetReferenceTest',
icon: CircleIcon,
// eslint-disable-next-line camelcase
__experimental_search: [{path: 'title', weight: 10}],
preview: {
select: {
title: 'title',
Expand All @@ -71,8 +67,6 @@ export const circularCrossDatasetReferenceTest = {
{
type: 'article',
icon: BookIcon,
// eslint-disable-next-line camelcase
__experimental_search: [{path: 'title', weight: 10}],
preview: {
select: {
title: 'title',
Expand Down
92 changes: 7 additions & 85 deletions dev/test-studio/schema/standard/crossDatasetReference.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@ export const crossDatasetSubtype = {
name: 'crossDatasetSubtype',
title: 'Subtype of cross dataset references',
dataset: 'playground',
projectId: 'ppsg7ml5',
to: [
{
type: 'book',
icon: BookIcon,
// eslint-disable-next-line camelcase
__experimental_search: [{path: ['title'], weight: 10}],
preview: {
select: {
title: 'title',
Expand All @@ -38,13 +35,9 @@ export default {
{name: 'title', type: 'string'},
{
name: 'bookInPlayground',
title: 'Reference to book or author in the "playground" dataset in project "ppsg7ml5"',
description:
"Note: The current token for the dataset does not have read access to documents in the 'restricted.**' path",
title: 'Reference to book in the "playground" dataset in project "ppsg7ml5"',
type: 'crossDatasetReference',
dataset: 'playground',
projectId: 'ppsg7ml5',
tokenId: 'restricted',
studioUrl: ({id, type}) => {
return type
? `${document.location.protocol}//${document.location.host}/playground/desk/${type};${id}`
Expand All @@ -54,16 +47,16 @@ export default {
{
type: 'book',
icon: BookIcon,
// eslint-disable-next-line camelcase
__experimental_search: [{path: 'title', weight: 10}],
preview: {
select: {
title: 'title',
subtitle: 'descriptionMd',
media: 'coverImage',
},
prepare(val) {
return {
title: val.title,
subtitle: val.subtitle,
media: val.coverImage,
}
},
Expand All @@ -76,8 +69,6 @@ export default {
name: 'bookOrAuthorInPlayground',
type: 'crossDatasetReference',
dataset: 'playground',
projectId: 'ppsg7ml5',
tokenId: 'readToken',
studioUrl: ({id, type}) => {
return type
? `${document.location.protocol}//${document.location.host}/playground/desk/${type};${id}`
Expand All @@ -87,16 +78,16 @@ export default {
{
type: 'book',
icon: BookIcon,
// eslint-disable-next-line camelcase
__experimental_search: [{path: 'title', weight: 10}],
preview: {
select: {
title: 'title',
subtitle: 'descriptionMd',
coverImage: 'coverImage',
},
prepare(val) {
return {
title: val.title,
subtitle: val.subtitle,
media: val.coverImage,
}
},
Expand All @@ -105,45 +96,20 @@ export default {
{
type: 'author',
icon: UserIcon,
// eslint-disable-next-line camelcase
__experimental_search: [{path: 'name', weight: 10}],
preview: {
select: {
name: 'name',
title: 'name',
},
prepare(val) {
return {
title: val.name,
title: val.title,
media: val.media,
}
},
},
},
],
},
{
title: 'Article in docs dataset',
name: 'docsArticle',
type: 'crossDatasetReference',
dataset: 'next',
projectId: '3do82whm',
studioUrl: ({id, type}) => {
return type ? `https://admin.sanity.io/desk/docs;${type};${id}` : null
},
to: [
{
type: 'article',
icon: BookIcon,
// eslint-disable-next-line camelcase
__experimental_search: [{path: 'title', weight: 10}],
preview: {
select: {
title: 'title',
},
},
},
],
},
{
title: 'Cross Dataset reference in PTE',
name: 'portableText',
Expand All @@ -155,28 +121,6 @@ export default {
name: 'crossDatasetSubtype',
type: 'crossDatasetSubtype',
},
{
type: 'crossDatasetReference',
dataset: 'next',
projectId: '3do82whm',
studioUrl: ({id, type}) => {
return type ? `https://admin.sanity.io/desk/docs;${type};${id}` : null
},
title: 'Cross dataset reference to docs article in admin.sanity.io',
to: [
{
type: 'article',
icon: BookIcon,
// eslint-disable-next-line camelcase
__experimental_search: [{path: 'title', weight: 10}],
preview: {
select: {
title: 'title',
},
},
},
],
},
],
},
{
Expand All @@ -189,28 +133,6 @@ export default {
name: 'crossDatasetSubtype',
type: 'crossDatasetSubtype',
},
{
type: 'crossDatasetReference',
dataset: 'next',
projectId: '3do82whm',
studioUrl: ({id, type}) => {
return type ? `https://admin.sanity.io/desk/docs;${type};${id}` : null
},
title: 'Cross dataset reference to docs article in admin.sanity.io',
to: [
{
type: 'article',
icon: BookIcon,
// eslint-disable-next-line camelcase
__experimental_search: [{path: 'title', weight: 10}],
preview: {
select: {
title: 'title',
},
},
},
],
},
],
},
{
Expand Down
6 changes: 0 additions & 6 deletions packages/@sanity/base/src/_exports/_internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ export {
export {observePathsDocumentPair as unstable_observePathsDocumentPair} from '../preview/documentPair'
// eslint-disable-next-line camelcase
export {observeDocumentPairAvailability as unstable_observeDocumentPairAvailability} from '../preview/availability'
export {
getProjectIdFromTokenDocumentId,
getTokenDocumentId,
fetchCrossProjectToken,
fetchAllCrossProjectTokens,
} from '../datastores/crossProjectToken'

export type {DocumentAvailability} from '../preview/types'
export {AvailabilityReason} from '../preview/types'
Expand Down
60 changes: 0 additions & 60 deletions packages/@sanity/base/src/datastores/crossProjectToken.ts

This file was deleted.

33 changes: 7 additions & 26 deletions packages/@sanity/base/src/preview/observeFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
import {difference, flatten, memoize} from 'lodash'
import {SanityClient} from '@sanity/client'
import {versionedClient} from '../client/versionedClient'
import {getTokenDocumentId} from '../datastores/crossProjectToken'
import {debounceCollect} from './utils/debounceCollect'
import {combineSelections, reassemble, toQuery} from './utils/optimizeQuery'
import {ApiConfig, FieldName, Id, Path, Selection} from './types'
Expand Down Expand Up @@ -84,18 +83,11 @@ function listen(id: Id) {
)
}

interface CrossProjectToken {
projectId: string
value: string
}

function fetchAllDocumentPathsWith(client: SanityClient, token?: CrossProjectToken) {
const headers = token ? {'sanity-project-tokens': `${token.projectId}=${token.value}`} : {}

function fetchAllDocumentPathsWith(client: SanityClient) {
return function fetchAllDocumentPath(selections: Selection[]) {
const combinedSelections = combineSelections(selections)
return client.observable
.fetch(toQuery(combinedSelections), {}, {tag: 'preview.document-paths', headers})
.fetch(toQuery(combinedSelections), {}, {tag: 'preview.document-paths'})
.pipe(map((result: any) => reassemble(result, combinedSelections)))
}
}
Expand Down Expand Up @@ -143,9 +135,9 @@ type Cache = {
const CACHE: Cache = {} // todo: use a LRU cache instead (e.g. hashlru or quick-lru)

const getBatchFetcherForDataset = memoize(
function getBatchFetcherForDataset(apiConfig: ApiConfig, token?: CrossProjectToken | undefined) {
function getBatchFetcherForDataset(apiConfig: ApiConfig) {
const client = versionedClient.withConfig(apiConfig)
const fetchAll = fetchAllDocumentPathsWith(client, token)
const fetchAll = fetchAllDocumentPathsWith(client)
return debounceCollect(fetchAll, 10)
},
(apiConfig) => apiConfig.dataset + apiConfig.projectId
Expand All @@ -163,20 +155,9 @@ const visiblePoll$ = fromEvent(document, 'visibilitychange').pipe(
)

function crossDatasetListenFields(id: Id, fields: Path[], apiConfig: ApiConfig) {
const token$ = observePaths(getTokenDocumentId({projectId: apiConfig.projectId}), ['token']).pipe(
map((document) => document?.token as string | undefined)
)
return combineLatest([token$, visiblePoll$.pipe(startWith(0))]).pipe(
switchMap(([token]) => {
const batchFetcher = getBatchFetcherForDataset(
apiConfig,
token
? {
projectId: apiConfig.projectId,
value: token,
}
: undefined
)
return visiblePoll$.pipe(startWith(0)).pipe(
switchMap(() => {
const batchFetcher = getBatchFetcherForDataset(apiConfig)
return batchFetcher(id, fields)
})
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ export function ConfirmDeleteDialog({
isLoading,
totalCount,
projectIds,
datasetNames,
hasUnknownDatasetNames,
} = useReferringDocuments(id)
const capitalizedAction = `${action.substring(0, 1).toUpperCase()}${action.substring(1)}`

Expand Down Expand Up @@ -99,6 +101,8 @@ export function ConfirmDeleteDialog({
totalCount={totalCount}
action={action}
projectIds={projectIds}
datasetNames={datasetNames}
hasUnknownDatasetNames={hasUnknownDatasetNames}
onReferenceLinkClick={onCancel}
/>
) : (
Expand Down

2 comments on commit 6b0afbd

@vercel
Copy link

@vercel vercel bot commented on 6b0afbd Oct 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

studio-workshop – ./

studio-workshop-git-next.sanity.build
studio-workshop.sanity.build

@vercel
Copy link

@vercel vercel bot commented on 6b0afbd Oct 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

test-studio – ./

test-studio-git-next.sanity.build
test-studio.sanity.build

Please sign in to comment.