Skip to content

Commit

Permalink
fix(stega): append perspective to edit links (#979)
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan authored Jan 22, 2025
1 parent ea1d41e commit 65ce82d
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 17 deletions.
8 changes: 7 additions & 1 deletion src/csm/createEditUrl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {getPublishedId, isDraftId} from './draftUtils'
import {getPublishedId, getVersionFromId, isDraftId, isPublishedId, isVersionId} from './draftUtils'
import {jsonPathToStudioPath} from './jsonPath'
import * as studioPath from './studioPath'
import type {CreateEditUrlOptions, EditIntentUrl, StudioBaseUrl} from './types'
Expand Down Expand Up @@ -56,6 +56,12 @@ export function createEditUrl(options: CreateEditUrlOptions): `${StudioBaseUrl}$
if (dataset) {
searchParams.set('dataset', dataset)
}
if (isPublishedId(_id)) {
searchParams.set('perspective', 'published')
} else if (isVersionId(_id)) {
const versionId = getVersionFromId(_id)!
searchParams.set('perspective', versionId)
}
if (isDraftId(_id)) {
searchParams.set('isDraft', '')
}
Expand Down
21 changes: 21 additions & 0 deletions test/csm/createEditUrl.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {getPublishedId, getVersionId} from '@sanity/client/csm'
import {expect, test} from 'vitest'

import {createEditUrl} from '../../src/csm/createEditUrl'
Expand Down Expand Up @@ -36,6 +37,26 @@ const cases = [
expected:
'/staging/intent/edit/mode=presentation;id=homepage;type=page;path=foo[_key=="section-1"][0];tool=content?baseUrl=/&id=homepage&type=page&path=foo[_key=="section-1"][0]&workspace=staging&tool=content&projectId=a1b2c3d4&dataset=production&isDraft=',
},
{
context: {baseUrl: '/', workspace, tool, id: getPublishedId(id), type, projectId, dataset},
path: parseJsonPath("$['foo'][?(@._key=='section-1')][0]"),
expected:
'/staging/intent/edit/mode=presentation;id=homepage;type=page;path=foo[_key=="section-1"][0];tool=content?baseUrl=/&id=homepage&type=page&path=foo[_key=="section-1"][0]&workspace=staging&tool=content&projectId=a1b2c3d4&dataset=production&perspective=published',
},
{
context: {
baseUrl: '/',
workspace,
tool,
id: getVersionId(id, 'rABC123'),
type,
projectId,
dataset,
},
path: parseJsonPath("$['foo'][?(@._key=='section-1')][0]"),
expected:
'/staging/intent/edit/mode=presentation;id=homepage;type=page;path=foo[_key=="section-1"][0];tool=content?baseUrl=/&id=homepage&type=page&path=foo[_key=="section-1"][0]&workspace=staging&tool=content&projectId=a1b2c3d4&dataset=production&perspective=rABC123',
},
]

test.each(cases)('$expected', ({context, path, expected}) => {
Expand Down
54 changes: 45 additions & 9 deletions test/csm/resolveEditUrl.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {type ContentSourceMap, resolveEditUrl} from '@sanity/client/csm'
import {type ContentSourceMap, getDraftId, getVersionId, resolveEditUrl} from '@sanity/client/csm'
import {expect, test} from 'vitest'

const mock = {
Expand Down Expand Up @@ -233,23 +233,23 @@ const mock = {
resultSourceMap: {
documents: [
{
_id: 'drafts.462efcc6-3c8b-47c6-8474-5544e1a4acde',
_id: '462efcc6-3c8b-47c6-8474-5544e1a4acde',
_type: 'product',
},
{
_id: 'drafts.807cc05c-8c4c-443a-a9c1-198fd3fd7b16',
_id: getDraftId('807cc05c-8c4c-443a-a9c1-198fd3fd7b16'),
_type: 'product',
},
{
_id: 'drafts.a643da0c-2cc6-439e-92b7-9f31c822ee05',
_id: getVersionId('a643da0c-2cc6-439e-92b7-9f31c822ee05', 'rABC123'),
_type: 'product',
},
{
_id: 'drafts.c9de5527-ebd9-4f90-8c30-a26e3439ca2d',
_id: 'c9de5527-ebd9-4f90-8c30-a26e3439ca2d',
_type: 'product',
},
{
_id: 'drafts.e1bf9f1f-efdb-4105-8c26-6b64f897e9c1',
_id: 'e1bf9f1f-efdb-4105-8c26-6b64f897e9c1',
_type: 'product',
},
{
Expand Down Expand Up @@ -501,19 +501,55 @@ const cases = [
path: 'products[0].title',
studioUrl: 'https://test.sanity.studio',
expected:
'https://test.sanity.studio/intent/edit/mode=presentation;id=462efcc6-3c8b-47c6-8474-5544e1a4acde;type=product;path=title?baseUrl=https://test.sanity.studio&id=462efcc6-3c8b-47c6-8474-5544e1a4acde&type=product&path=title&isDraft=',
'https://test.sanity.studio/intent/edit/mode=presentation;id=462efcc6-3c8b-47c6-8474-5544e1a4acde;type=product;path=title?baseUrl=https://test.sanity.studio&id=462efcc6-3c8b-47c6-8474-5544e1a4acde&type=product&path=title&perspective=published',
},
{
path: 'products[0].media.alt',
studioUrl: '/',
expected:
'/intent/edit/mode=presentation;id=462efcc6-3c8b-47c6-8474-5544e1a4acde;type=product;path=media[_key=="cee5fbb69da2"].alt?baseUrl=/&id=462efcc6-3c8b-47c6-8474-5544e1a4acde&type=product&path=media[_key=="cee5fbb69da2"].alt&isDraft=',
'/intent/edit/mode=presentation;id=462efcc6-3c8b-47c6-8474-5544e1a4acde;type=product;path=media[_key=="cee5fbb69da2"].alt?baseUrl=/&id=462efcc6-3c8b-47c6-8474-5544e1a4acde&type=product&path=media[_key=="cee5fbb69da2"].alt&perspective=published',
},
{
path: 'products[0].description[0].children[0].text',
studioUrl: '/',
expected:
'/intent/edit/mode=presentation;id=462efcc6-3c8b-47c6-8474-5544e1a4acde;type=product;path=description[0].children[0].text?baseUrl=/&id=462efcc6-3c8b-47c6-8474-5544e1a4acde&type=product&path=description[0].children[0].text&isDraft=',
'/intent/edit/mode=presentation;id=462efcc6-3c8b-47c6-8474-5544e1a4acde;type=product;path=description[0].children[0].text?baseUrl=/&id=462efcc6-3c8b-47c6-8474-5544e1a4acde&type=product&path=description[0].children[0].text&perspective=published',
},
{
path: 'products[1].title',
studioUrl: 'https://test.sanity.studio',
expected:
'https://test.sanity.studio/intent/edit/mode=presentation;id=807cc05c-8c4c-443a-a9c1-198fd3fd7b16;type=product;path=title?baseUrl=https://test.sanity.studio&id=807cc05c-8c4c-443a-a9c1-198fd3fd7b16&type=product&path=title&isDraft=',
},
{
path: 'products[1].media.alt',
studioUrl: '/',
expected:
'/intent/edit/mode=presentation;id=807cc05c-8c4c-443a-a9c1-198fd3fd7b16;type=product;path=media[_key=="55659c72ec46"].alt?baseUrl=/&id=807cc05c-8c4c-443a-a9c1-198fd3fd7b16&type=product&path=media[_key=="55659c72ec46"].alt&isDraft=',
},
{
path: 'products[1].description[0].children[0].text',
studioUrl: '/',
expected:
'/intent/edit/mode=presentation;id=807cc05c-8c4c-443a-a9c1-198fd3fd7b16;type=product;path=description[0].children[0].text?baseUrl=/&id=807cc05c-8c4c-443a-a9c1-198fd3fd7b16&type=product&path=description[0].children[0].text&isDraft=',
},
{
path: 'products[2].title',
studioUrl: 'https://test.sanity.studio',
expected:
'https://test.sanity.studio/intent/edit/mode=presentation;id=a643da0c-2cc6-439e-92b7-9f31c822ee05;type=product;path=title?baseUrl=https://test.sanity.studio&id=a643da0c-2cc6-439e-92b7-9f31c822ee05&type=product&path=title&perspective=rABC123',
},
{
path: 'products[2].media.alt',
studioUrl: '/',
expected:
'/intent/edit/mode=presentation;id=a643da0c-2cc6-439e-92b7-9f31c822ee05;type=product;path=media[_key=="f304342d5bb0"].alt?baseUrl=/&id=a643da0c-2cc6-439e-92b7-9f31c822ee05&type=product&path=media[_key=="f304342d5bb0"].alt&perspective=rABC123',
},
{
path: 'products[2].description[0].children[0].text',
studioUrl: '/',
expected:
'/intent/edit/mode=presentation;id=a643da0c-2cc6-439e-92b7-9f31c822ee05;type=product;path=description[0].children[0].text?baseUrl=/&id=a643da0c-2cc6-439e-92b7-9f31c822ee05&type=product&path=description[0].children[0].text&perspective=rABC123',
},
]

Expand Down
14 changes: 7 additions & 7 deletions test/stega/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,18 @@ describe('@sanity/client/stega', async () => {
expect(vercelStegaSplit(res[0].title).cleaned).toBe(result[0].title)
expect(vercelStegaDecode(res[0].title)).toMatchInlineSnapshot(`
{
"href": "/studio/intent/edit/mode=presentation;id=njgNkngskjg;type=beer;path=title?baseUrl=%2Fstudio&id=njgNkngskjg&type=beer&path=title",
"href": "/studio/intent/edit/mode=presentation;id=njgNkngskjg;type=beer;path=title?baseUrl=%2Fstudio&id=njgNkngskjg&type=beer&path=title&perspective=published",
"origin": "sanity.io",
}
`)
expect(vercelStegaDecodeAll(JSON.stringify(res))).toMatchInlineSnapshot(`
[
{
"href": "/studio/intent/edit/mode=presentation;id=njgNkngskjg;type=beer;path=title?baseUrl=%2Fstudio&id=njgNkngskjg&type=beer&path=title",
"href": "/studio/intent/edit/mode=presentation;id=njgNkngskjg;type=beer;path=title?baseUrl=%2Fstudio&id=njgNkngskjg&type=beer&path=title&perspective=published",
"origin": "sanity.io",
},
{
"href": "/studio/intent/edit/mode=presentation;id=njgNkngskjg;type=beer;path=country?baseUrl=%2Fstudio&id=njgNkngskjg&type=beer&path=country",
"href": "/studio/intent/edit/mode=presentation;id=njgNkngskjg;type=beer;path=country?baseUrl=%2Fstudio&id=njgNkngskjg&type=beer&path=country&perspective=published",
"origin": "sanity.io",
},
]
Expand Down Expand Up @@ -192,11 +192,11 @@ describe('@sanity/client/stega', async () => {
expect(vercelStegaDecodeAll(JSON.stringify(res))).toMatchInlineSnapshot(`
[
{
"href": "/studio/intent/edit/mode=presentation;id=njgNkngskjg;type=beer;path=title?baseUrl=%2Fstudio&id=njgNkngskjg&type=beer&path=title",
"href": "/studio/intent/edit/mode=presentation;id=njgNkngskjg;type=beer;path=title?baseUrl=%2Fstudio&id=njgNkngskjg&type=beer&path=title&perspective=published",
"origin": "sanity.io",
},
{
"href": "/studio/intent/edit/mode=presentation;id=njgNkngskjg;type=beer;path=country?baseUrl=%2Fstudio&id=njgNkngskjg&type=beer&path=country",
"href": "/studio/intent/edit/mode=presentation;id=njgNkngskjg;type=beer;path=country?baseUrl=%2Fstudio&id=njgNkngskjg&type=beer&path=country&perspective=published",
"origin": "sanity.io",
},
]
Expand Down Expand Up @@ -229,11 +229,11 @@ describe('@sanity/client/stega', async () => {
expect(vercelStegaDecodeAll(JSON.stringify(res))).toMatchInlineSnapshot(`
[
{
"href": "/admin/intent/edit/mode=presentation;id=njgNkngskjg;type=beer;path=title?baseUrl=%2Fadmin&id=njgNkngskjg&type=beer&path=title",
"href": "/admin/intent/edit/mode=presentation;id=njgNkngskjg;type=beer;path=title?baseUrl=%2Fadmin&id=njgNkngskjg&type=beer&path=title&perspective=published",
"origin": "sanity.io",
},
{
"href": "/admin/intent/edit/mode=presentation;id=njgNkngskjg;type=beer;path=country?baseUrl=%2Fadmin&id=njgNkngskjg&type=beer&path=country",
"href": "/admin/intent/edit/mode=presentation;id=njgNkngskjg;type=beer;path=country?baseUrl=%2Fadmin&id=njgNkngskjg&type=beer&path=country&perspective=published",
"origin": "sanity.io",
},
]
Expand Down

0 comments on commit 65ce82d

Please sign in to comment.