@nocode-factory/sanity-kit 2.5.0 → 2.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +43 -0
- package/bin/ncf-kit.mjs +165 -4
- package/dist/index.d.ts +1 -1
- package/dist/index.js +105 -14
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/hooks/useOpenOnline.ts +90 -0
- package/src/lib/studioLinks.ts +52 -8
- package/src/tools/navbar/PreviewLauncher.tsx +7 -7
- package/src/tools/pages/PathCell.tsx +24 -0
- package/src/tools/pages/board/PagePanel.tsx +13 -2
- package/src/version.ts +1 -1
package/package.json
CHANGED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import {useCallback} from 'react'
|
|
2
|
+
import {useKitConfig} from '../context/KitContext'
|
|
3
|
+
import {useKitClient} from './useKitClient'
|
|
4
|
+
import {explainGrantError, requestPreviewGrant, type PreviewGrant} from '../lib/service'
|
|
5
|
+
import {isPublicEnvironment, onlineUrl, type PreviewPerspective} from '../lib/studioLinks'
|
|
6
|
+
import type {NcfEnvironment} from '../config/types'
|
|
7
|
+
|
|
8
|
+
/** Libellé des laissez-passer délivrés pour soi, distinct des liens partagés. */
|
|
9
|
+
const SELF_LABEL = 'Ouverture depuis le Studio'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Laissez-passer déjà obtenus, par projet. Un clic ne doit pas en fabriquer un
|
|
13
|
+
* nouveau à chaque fois : chacun est daté, compté, et visible dans le suivi des
|
|
14
|
+
* partages. On réutilise donc le même tant qu'il reste au moins une heure.
|
|
15
|
+
*/
|
|
16
|
+
const cache = new Map<string, PreviewGrant>()
|
|
17
|
+
const STILL_VALID_MS = 60 * 60 * 1000
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Ouvre le vrai site, en ligne, dans un nouvel onglet.
|
|
21
|
+
*
|
|
22
|
+
* Sur une préversion gardée par `ncfPreviewGuard`, un lien nu tombe sur
|
|
23
|
+
* « Cette version du site est privée » — même pour l'éditeur connecté au
|
|
24
|
+
* Studio. On demande donc au service un laissez-passer pour soi (le même que
|
|
25
|
+
* « Partager la préversion », délivré seulement à qui a accès au projet Sanity)
|
|
26
|
+
* et on le pose sur l'URL.
|
|
27
|
+
*
|
|
28
|
+
* L'onglet est ouvert **avant** l'appel réseau : un navigateur bloque une
|
|
29
|
+
* fenêtre ouverte après un `await`, faute de geste de l'utilisateur.
|
|
30
|
+
*/
|
|
31
|
+
export function useOpenOnline() {
|
|
32
|
+
const config = useKitConfig()
|
|
33
|
+
const client = useKitClient()
|
|
34
|
+
|
|
35
|
+
return useCallback(
|
|
36
|
+
async (
|
|
37
|
+
environment: NcfEnvironment | null,
|
|
38
|
+
options: {path?: string | null; perspective?: PreviewPerspective} = {},
|
|
39
|
+
) => {
|
|
40
|
+
const perspective = options.perspective ?? 'published'
|
|
41
|
+
const plain = onlineUrl(environment, perspective, {path: options.path, previewUrl: config.previewUrl})
|
|
42
|
+
if (!environment || !plain) return
|
|
43
|
+
|
|
44
|
+
// Production, ou service coupé : rien à prouver, l'URL simple suffit.
|
|
45
|
+
if (isPublicEnvironment(environment, config.previewUrl) || !config.serviceEndpoint) {
|
|
46
|
+
window.open(plain, '_blank', 'noopener')
|
|
47
|
+
return
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const tab = window.open('about:blank', '_blank')
|
|
51
|
+
if (!tab) return
|
|
52
|
+
tab.opener = null
|
|
53
|
+
tab.document.title = 'Ouverture de la préversion…'
|
|
54
|
+
tab.document.body.innerHTML =
|
|
55
|
+
'<p style="font:15px/1.6 system-ui,sans-serif;color:#57534e;padding:2rem">Ouverture de la préversion…</p>'
|
|
56
|
+
|
|
57
|
+
try {
|
|
58
|
+
const {projectId, dataset, token} = client.config()
|
|
59
|
+
if (!projectId || !dataset) throw new Error('unknown_project')
|
|
60
|
+
const key = `${config.serviceEndpoint}|${projectId}|${dataset}`
|
|
61
|
+
let grant = cache.get(key)
|
|
62
|
+
if (!grant || new Date(grant.expiresAt).getTime() - Date.now() < STILL_VALID_MS) {
|
|
63
|
+
grant = await requestPreviewGrant(config.serviceEndpoint, {
|
|
64
|
+
projectId,
|
|
65
|
+
dataset,
|
|
66
|
+
sanityToken: token ?? '',
|
|
67
|
+
label: SELF_LABEL,
|
|
68
|
+
days: 1,
|
|
69
|
+
})
|
|
70
|
+
cache.set(key, grant)
|
|
71
|
+
}
|
|
72
|
+
const signed = onlineUrl(environment, perspective, {
|
|
73
|
+
path: options.path,
|
|
74
|
+
grant: grant.token,
|
|
75
|
+
previewUrl: config.previewUrl,
|
|
76
|
+
})
|
|
77
|
+
tab.location.href = signed ?? plain
|
|
78
|
+
} catch (cause) {
|
|
79
|
+
// On le dit dans l'onglet ouvert, plutôt que de laisser une page blanche
|
|
80
|
+
// ou d'envoyer sur la page de refus sans explication.
|
|
81
|
+
const message = explainGrantError(cause)
|
|
82
|
+
tab.document.title = 'Préversion indisponible'
|
|
83
|
+
tab.document.body.innerHTML = `<main style="font:15px/1.6 system-ui,sans-serif;color:#1c1917;padding:2rem;max-width:32rem"><h1 style="font-size:18px">Impossible d'ouvrir la préversion</h1><p style="color:#57534e"></p></main>`
|
|
84
|
+
const paragraph = tab.document.querySelector('p')
|
|
85
|
+
if (paragraph) paragraph.textContent = message
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
[client, config.previewUrl, config.serviceEndpoint],
|
|
89
|
+
)
|
|
90
|
+
}
|
package/src/lib/studioLinks.ts
CHANGED
|
@@ -30,24 +30,68 @@ export function urlOn(environment: NcfEnvironment | null, path: string | null):
|
|
|
30
30
|
return `${trimEnd(environment.url)}${path.startsWith('/') ? path : `/${path}`}`
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
+
/** Paramètre d'URL porteur du laissez-passer de préversion (lu par `ncfPreviewGuard`). */
|
|
34
|
+
export const GRANT_PARAM = 'ncf_preview'
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* L'environnement est-il le site public ?
|
|
38
|
+
*
|
|
39
|
+
* La production n'a ni garde ni brouillons : on y va par l'URL simple. Tout le
|
|
40
|
+
* reste est une préversion, gardée — y compris une adresse collée à la volée.
|
|
41
|
+
*/
|
|
42
|
+
export function isPublicEnvironment(
|
|
43
|
+
environment: NcfEnvironment | null,
|
|
44
|
+
previewUrl: string | null | undefined,
|
|
45
|
+
): boolean {
|
|
46
|
+
if (!environment) return false
|
|
47
|
+
if (environment.id === 'production') return true
|
|
48
|
+
return Boolean(previewUrl) && trimEnd(environment.url) === trimEnd(previewUrl as string)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface OnlineUrlOptions {
|
|
52
|
+
/** Chemin de la page (défaut : l'accueil). */
|
|
53
|
+
path?: string | null
|
|
54
|
+
/**
|
|
55
|
+
* Laissez-passer de préversion. Sans lui, une préversion gardée répond
|
|
56
|
+
* « Cette version du site est privée » : c'est lui qui ouvre la porte.
|
|
57
|
+
*/
|
|
58
|
+
grant?: string | null
|
|
59
|
+
/** URL du site public, pour reconnaître la production. */
|
|
60
|
+
previewUrl?: string | null
|
|
61
|
+
}
|
|
62
|
+
|
|
33
63
|
/**
|
|
34
64
|
* URL du vrai site, en ligne, pour un environnement et un état de contenu.
|
|
35
65
|
*
|
|
36
|
-
*
|
|
37
|
-
* de
|
|
38
|
-
*
|
|
39
|
-
*
|
|
66
|
+
* - Production : l'URL simple. Elle ne montre jamais de brouillons, et la
|
|
67
|
+
* route de bascule y refuserait.
|
|
68
|
+
* - Préversion, brouillons : la route de bascule pose le cookie de mode
|
|
69
|
+
* brouillon, puis renvoie vers la page **avec le laissez-passer** — c'est la
|
|
70
|
+
* page d'arrivée que la garde contrôle, pas la route, toujours ouverte.
|
|
71
|
+
* - Préversion, en ligne : la page avec le laissez-passer.
|
|
40
72
|
*/
|
|
41
73
|
export function onlineUrl(
|
|
42
74
|
environment: NcfEnvironment | null,
|
|
43
75
|
perspective: PreviewPerspective,
|
|
76
|
+
options: OnlineUrlOptions = {},
|
|
44
77
|
): string | null {
|
|
45
78
|
if (!environment?.url) return null
|
|
46
|
-
const base = environment.url
|
|
47
|
-
|
|
48
|
-
|
|
79
|
+
const base = trimEnd(environment.url)
|
|
80
|
+
const path = options.path ? (options.path.startsWith('/') ? options.path : `/${options.path}`) : '/'
|
|
81
|
+
const isPublic =
|
|
82
|
+
environment.id === 'production' || isPublicEnvironment(environment, options.previewUrl)
|
|
83
|
+
|
|
84
|
+
if (isPublic) return path === '/' ? base : `${base}${path}`
|
|
85
|
+
|
|
86
|
+
const landing = options.grant
|
|
87
|
+
? `${path}${path.includes('?') ? '&' : '?'}${GRANT_PARAM}=${encodeURIComponent(options.grant)}`
|
|
88
|
+
: path
|
|
89
|
+
|
|
90
|
+
if (perspective === 'drafts') {
|
|
91
|
+
const redirect = options.grant ? encodeURIComponent(landing) : landing
|
|
92
|
+
return `${base}/api/draft-mode/toggle?enable=true&redirect=${redirect}`
|
|
49
93
|
}
|
|
50
|
-
return base
|
|
94
|
+
return landing === '/' ? base : `${base}${landing}`
|
|
51
95
|
}
|
|
52
96
|
|
|
53
97
|
export interface PresentationOptions {
|
|
@@ -3,7 +3,8 @@ import {useRouter} from 'sanity/router'
|
|
|
3
3
|
import type {ToolMenuProps} from 'sanity'
|
|
4
4
|
import {InlinePopover} from '../../ui/Popover'
|
|
5
5
|
import {KitProvider, useKitConfig} from '../../context/KitContext'
|
|
6
|
-
import {presentationPath,
|
|
6
|
+
import {presentationPath, PERSPECTIVES, type PreviewPerspective} from '../../lib/studioLinks'
|
|
7
|
+
import {useOpenOnline} from '../../hooks/useOpenOnline'
|
|
7
8
|
import type {ConfigIssue, NcfEnvironment, ResolvedKitConfig} from '../../config/types'
|
|
8
9
|
import {EyeOpenIcon} from '@sanity/icons/EyeOpen'
|
|
9
10
|
import {LaunchIcon} from '@sanity/icons/Launch'
|
|
@@ -24,6 +25,7 @@ import {NcfKitUpdate} from './KitUpdate'
|
|
|
24
25
|
export function NcfPreviewLauncher(props: ToolMenuProps) {
|
|
25
26
|
const config = useKitConfig()
|
|
26
27
|
const router = useRouter()
|
|
28
|
+
const open = useOpenOnline()
|
|
27
29
|
const environments = config.environments
|
|
28
30
|
|
|
29
31
|
if (environments.length === 0) {
|
|
@@ -42,13 +44,11 @@ export function NcfPreviewLauncher(props: ToolMenuProps) {
|
|
|
42
44
|
if (path) router.navigateUrl({path})
|
|
43
45
|
}
|
|
44
46
|
|
|
45
|
-
// Ouvre le vrai site EN LIGNE, dans un nouvel onglet.
|
|
46
|
-
//
|
|
47
|
-
// l'
|
|
48
|
-
// production, qui refuse les brouillons — retombe sur l'URL simple.
|
|
47
|
+
// Ouvre le vrai site EN LIGNE, dans un nouvel onglet. Sur une préversion,
|
|
48
|
+
// avec un laissez-passer : sans lui, la garde du site refuse l'accès, même à
|
|
49
|
+
// l'éditeur connecté. « Brouillons » passe par la route de bascule du site.
|
|
49
50
|
const openOnline = (environment: NcfEnvironment, perspective: PreviewPerspective) => {
|
|
50
|
-
|
|
51
|
-
if (target) window.open(target, '_blank', 'noopener')
|
|
51
|
+
void open(environment, {perspective})
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
// En colonne dans le volet latéral, en ligne dans la barre : le menu des
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import {Flex, Text} from '@sanity/ui'
|
|
2
2
|
import {MONO} from '../../ui/tokens'
|
|
3
|
+
import {usePreviewTarget} from '../../context/PreviewTargetContext'
|
|
4
|
+
import {useOpenOnline} from '../../hooks/useOpenOnline'
|
|
5
|
+
import {isPublicEnvironment} from '../../lib/studioLinks'
|
|
6
|
+
import {useKitConfig} from '../../context/KitContext'
|
|
3
7
|
|
|
4
8
|
/**
|
|
5
9
|
* Chemin d'une page.
|
|
@@ -17,6 +21,12 @@ export function PathCell(props: {
|
|
|
17
21
|
url: string | null
|
|
18
22
|
}) {
|
|
19
23
|
const {path, locale, showLocale, url} = props
|
|
24
|
+
const config = useKitConfig()
|
|
25
|
+
const {environment, perspective} = usePreviewTarget()
|
|
26
|
+
const open = useOpenOnline()
|
|
27
|
+
// Une préversion est gardée : le lien nu y serait refusé. On l'ouvre avec un
|
|
28
|
+
// laissez-passer ; le lien reste un vrai lien (copiable, survolable).
|
|
29
|
+
const guarded = Boolean(environment) && !isPublicEnvironment(environment, config.previewUrl)
|
|
20
30
|
|
|
21
31
|
if (!path) {
|
|
22
32
|
return (
|
|
@@ -47,6 +57,11 @@ export function PathCell(props: {
|
|
|
47
57
|
target="_blank"
|
|
48
58
|
rel="noreferrer"
|
|
49
59
|
title={`Ouvrir ${url}`}
|
|
60
|
+
onClick={(event) => {
|
|
61
|
+
if (!guarded) return
|
|
62
|
+
event.preventDefault()
|
|
63
|
+
void open(environment, {path: pathnameOf(url), perspective})
|
|
64
|
+
}}
|
|
50
65
|
style={{color: 'inherit', textDecoration: 'none', minWidth: 0, display: 'block'}}
|
|
51
66
|
onMouseEnter={(event) => {
|
|
52
67
|
event.currentTarget.style.textDecoration = 'underline'
|
|
@@ -69,3 +84,12 @@ export function PathCell(props: {
|
|
|
69
84
|
</Flex>
|
|
70
85
|
)
|
|
71
86
|
}
|
|
87
|
+
|
|
88
|
+
/** Chemin d'une URL absolue, ou la valeur telle quelle. */
|
|
89
|
+
function pathnameOf(url: string): string {
|
|
90
|
+
try {
|
|
91
|
+
return new URL(url).pathname
|
|
92
|
+
} catch {
|
|
93
|
+
return url
|
|
94
|
+
}
|
|
95
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {useState} from 'react'
|
|
1
|
+
import {useState, type MouseEvent} from 'react'
|
|
2
2
|
import {Box, Button, Card, Flex, Select, Stack, Text} from '@sanity/ui'
|
|
3
3
|
import {IntentLink} from 'sanity/router'
|
|
4
4
|
import {CloseIcon} from '@sanity/icons/Close'
|
|
@@ -7,6 +7,9 @@ import {MONO} from '../../../ui/tokens'
|
|
|
7
7
|
import {StatusBadge} from '../../../ui/StatusBadge'
|
|
8
8
|
import {FieldValue} from '../FieldValue'
|
|
9
9
|
import {usePreviewTarget} from '../../../context/PreviewTargetContext'
|
|
10
|
+
import {useOpenOnline} from '../../../hooks/useOpenOnline'
|
|
11
|
+
import {isPublicEnvironment} from '../../../lib/studioLinks'
|
|
12
|
+
import {useKitConfig} from '../../../context/KitContext'
|
|
10
13
|
import type {NcfBoardTag, NcfInternalField} from '../../../config/types'
|
|
11
14
|
import type {PageRow} from '../../../lib/pages'
|
|
12
15
|
|
|
@@ -58,7 +61,9 @@ export function PagePanel(props: PagePanelProps) {
|
|
|
58
61
|
onClose,
|
|
59
62
|
} = props
|
|
60
63
|
const [failed, setFailed] = useState(false)
|
|
61
|
-
const {siteUrlFor, environments, environment} = usePreviewTarget()
|
|
64
|
+
const {siteUrlFor, environments, environment, perspective} = usePreviewTarget()
|
|
65
|
+
const openOnline = useOpenOnline()
|
|
66
|
+
const config = useKitConfig()
|
|
62
67
|
const siteUrl = siteUrlFor(row.url)
|
|
63
68
|
|
|
64
69
|
return (
|
|
@@ -211,6 +216,12 @@ export function PagePanel(props: PagePanelProps) {
|
|
|
211
216
|
href={siteUrl}
|
|
212
217
|
target="_blank"
|
|
213
218
|
rel="noreferrer"
|
|
219
|
+
// Préversion gardée : ouverte avec un laissez-passer, sinon refusée.
|
|
220
|
+
onClick={(event: MouseEvent) => {
|
|
221
|
+
if (!environment || isPublicEnvironment(environment, config.previewUrl)) return
|
|
222
|
+
event.preventDefault()
|
|
223
|
+
void openOnline(environment, {path: new URL(siteUrl).pathname, perspective})
|
|
224
|
+
}}
|
|
214
225
|
mode="ghost"
|
|
215
226
|
fontSize={1}
|
|
216
227
|
padding={2}
|
package/src/version.ts
CHANGED