@nocode-factory/sanity-kit 2.3.0 → 2.5.0
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/dist/index.d.ts +50 -2
- package/dist/index.js +272 -61
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/hooks/useKitUpdate.ts +56 -0
- package/src/index.ts +4 -0
- package/src/lib/studioLinks.ts +20 -0
- package/src/lib/version.ts +31 -0
- package/src/tools/navbar/KitUpdate.tsx +106 -0
- package/src/tools/navbar/PreviewLauncher.tsx +67 -28
- package/src/version.ts +1 -1
package/package.json
CHANGED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import {useEffect, useState} from 'react'
|
|
2
|
+
import {KIT_VERSION} from '../version'
|
|
3
|
+
import {compareVersions, isPrerelease} from '../lib/version'
|
|
4
|
+
|
|
5
|
+
const REGISTRY = 'https://registry.npmjs.org/@nocode-factory/sanity-kit/latest'
|
|
6
|
+
const PACKAGE = '@nocode-factory/sanity-kit'
|
|
7
|
+
|
|
8
|
+
export interface KitUpdate {
|
|
9
|
+
current: string
|
|
10
|
+
latest: string | null
|
|
11
|
+
/** Vrai seulement quand une version stable dépasse celle qui s'exécute. */
|
|
12
|
+
available: boolean
|
|
13
|
+
/** Commande à lancer, prête à copier. */
|
|
14
|
+
command: string
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Compare la version exécutée à la dernière publiée.
|
|
19
|
+
*
|
|
20
|
+
* Un Studio tourne dans un navigateur : il ne peut ni installer un paquet ni
|
|
21
|
+
* se redéployer. Il peut en revanche dire qu'il est en retard, et donner la
|
|
22
|
+
* commande — c'est tout ce qu'un bouton peut honnêtement offrir ici.
|
|
23
|
+
*
|
|
24
|
+
* Le registre npm autorise les requêtes navigateur, et le paquet est public :
|
|
25
|
+
* aucune authentification n'est nécessaire.
|
|
26
|
+
*
|
|
27
|
+
* Échoue en silence : ne pas savoir si une mise à jour existe ne doit gêner
|
|
28
|
+
* personne.
|
|
29
|
+
*/
|
|
30
|
+
export function useKitUpdate(): KitUpdate {
|
|
31
|
+
const [latest, setLatest] = useState<string | null>(null)
|
|
32
|
+
|
|
33
|
+
useEffect(() => {
|
|
34
|
+
const controller = new AbortController()
|
|
35
|
+
fetch(REGISTRY, {signal: controller.signal})
|
|
36
|
+
.then((response) => (response.ok ? response.json() : null))
|
|
37
|
+
.then((body: {version?: string} | null) => {
|
|
38
|
+
if (!controller.signal.aborted && body?.version) setLatest(body.version)
|
|
39
|
+
})
|
|
40
|
+
.catch(() => {
|
|
41
|
+
// Registre injoignable, navigation hors ligne : sans conséquence.
|
|
42
|
+
})
|
|
43
|
+
return () => controller.abort()
|
|
44
|
+
}, [])
|
|
45
|
+
|
|
46
|
+
const available = Boolean(
|
|
47
|
+
latest && !isPrerelease(latest) && compareVersions(latest, KIT_VERSION) > 0,
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
current: KIT_VERSION,
|
|
52
|
+
latest,
|
|
53
|
+
available,
|
|
54
|
+
command: `npm install ${PACKAGE}@${latest ?? 'latest'}`,
|
|
55
|
+
}
|
|
56
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -184,6 +184,10 @@ export type {PreviewPerspective, PresentationOptions} from './lib/studioLinks'
|
|
|
184
184
|
export {usePreviewTarget} from './context/PreviewTargetContext'
|
|
185
185
|
export {NcfPreviewHeader, ncfPreviewComponents} from './tools/presentation/PreviewHeader'
|
|
186
186
|
export {NcfPreviewLauncher, createPreviewLauncher} from './tools/navbar/PreviewLauncher'
|
|
187
|
+
export {NcfKitUpdate} from './tools/navbar/KitUpdate'
|
|
188
|
+
export {useKitUpdate} from './hooks/useKitUpdate'
|
|
189
|
+
export type {KitUpdate} from './hooks/useKitUpdate'
|
|
190
|
+
export {compareVersions, isPrerelease} from './lib/version'
|
|
187
191
|
export {SharePreview} from './tools/pages/SharePreview'
|
|
188
192
|
export {requestPreviewGrant, explainGrantError} from './lib/service'
|
|
189
193
|
export type {PreviewGrant} from './lib/service'
|
package/src/lib/studioLinks.ts
CHANGED
|
@@ -30,6 +30,26 @@ export function urlOn(environment: NcfEnvironment | null, path: string | null):
|
|
|
30
30
|
return `${trimEnd(environment.url)}${path.startsWith('/') ? path : `/${path}`}`
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
+
/**
|
|
34
|
+
* URL du vrai site, en ligne, pour un environnement et un état de contenu.
|
|
35
|
+
*
|
|
36
|
+
* Les brouillons passent par la route de bascule du site, qui pose le cookie
|
|
37
|
+
* de mode brouillon puis renvoie à l'accueil. La production ne montre jamais
|
|
38
|
+
* de brouillons : on y retombe sur l'URL simple, et la route refuserait de
|
|
39
|
+
* toute façon.
|
|
40
|
+
*/
|
|
41
|
+
export function onlineUrl(
|
|
42
|
+
environment: NcfEnvironment | null,
|
|
43
|
+
perspective: PreviewPerspective,
|
|
44
|
+
): string | null {
|
|
45
|
+
if (!environment?.url) return null
|
|
46
|
+
const base = environment.url.replace(/\/+$/, '')
|
|
47
|
+
if (perspective === 'drafts' && environment.id !== 'production') {
|
|
48
|
+
return `${base}/api/draft-mode/toggle?enable=true&redirect=/`
|
|
49
|
+
}
|
|
50
|
+
return base
|
|
51
|
+
}
|
|
52
|
+
|
|
33
53
|
export interface PresentationOptions {
|
|
34
54
|
/** Front à charger dans l'aperçu. */
|
|
35
55
|
environment?: NcfEnvironment | null
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Comparaison de deux versions sémantiques.
|
|
3
|
+
*
|
|
4
|
+
* Suffisant pour ce qu'on en fait — savoir si une version publiée dépasse
|
|
5
|
+
* celle qu'on exécute. Les mentions de pré-publication (`2.4.0-rc.1`) sont
|
|
6
|
+
* ignorées : on ne propose jamais d'y passer.
|
|
7
|
+
*
|
|
8
|
+
* Rend 1 si `a` est postérieure, -1 si antérieure, 0 si égales.
|
|
9
|
+
*/
|
|
10
|
+
export function compareVersions(a: string, b: string): number {
|
|
11
|
+
const parse = (v: string) =>
|
|
12
|
+
v
|
|
13
|
+
.trim()
|
|
14
|
+
.replace(/^v/, '')
|
|
15
|
+
.split('-')[0]!
|
|
16
|
+
.split('.')
|
|
17
|
+
.map((part) => Number.parseInt(part, 10) || 0)
|
|
18
|
+
|
|
19
|
+
const left = parse(a)
|
|
20
|
+
const right = parse(b)
|
|
21
|
+
|
|
22
|
+
for (let i = 0; i < 3; i += 1) {
|
|
23
|
+
const l = left[i] ?? 0
|
|
24
|
+
const r = right[i] ?? 0
|
|
25
|
+
if (l !== r) return l > r ? 1 : -1
|
|
26
|
+
}
|
|
27
|
+
return 0
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Une version de pré-publication ne se propose pas. */
|
|
31
|
+
export const isPrerelease = (version: string): boolean => version.includes('-')
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import {useState} from 'react'
|
|
2
|
+
import {Box, Button, Card, Flex, Stack, Text} from '@sanity/ui'
|
|
3
|
+
import {ArrowUpIcon} from '@sanity/icons/ArrowUp'
|
|
4
|
+
import {CheckmarkIcon} from '@sanity/icons/Checkmark'
|
|
5
|
+
import {CopyIcon} from '@sanity/icons/Copy'
|
|
6
|
+
import {InlinePopover} from '../../ui/Popover'
|
|
7
|
+
import {useKitUpdate} from '../../hooks/useKitUpdate'
|
|
8
|
+
import {BORDER, MONO} from '../../ui/tokens'
|
|
9
|
+
|
|
10
|
+
const CHANGELOG = 'https://github.com/nocode-factory/ncf-sanity-kit/blob/main/docs/CHANGELOG.md'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Signale qu'une version plus récente du kit existe.
|
|
14
|
+
*
|
|
15
|
+
* **Il ne met pas à jour.** Un Studio tourne dans un navigateur : il ne peut
|
|
16
|
+
* ni installer un paquet sur le poste d'un développeur, ni se redéployer. Ce
|
|
17
|
+
* bouton dit ce qui manque et donne la commande — prétendre faire plus serait
|
|
18
|
+
* mentir sur ce qui se passe.
|
|
19
|
+
*
|
|
20
|
+
* Invisible tant que le kit est à jour : un bouton permanent qui ne dit rien
|
|
21
|
+
* finit par ne plus être lu.
|
|
22
|
+
*/
|
|
23
|
+
export function NcfKitUpdate(props: {inline?: boolean}) {
|
|
24
|
+
const update = useKitUpdate()
|
|
25
|
+
const [copied, setCopied] = useState(false)
|
|
26
|
+
|
|
27
|
+
if (!update.available) return null
|
|
28
|
+
|
|
29
|
+
const copy = async () => {
|
|
30
|
+
try {
|
|
31
|
+
await navigator.clipboard.writeText(update.command)
|
|
32
|
+
setCopied(true)
|
|
33
|
+
} catch {
|
|
34
|
+
// Presse-papiers refusé : la commande reste sélectionnable à la main.
|
|
35
|
+
setCopied(false)
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return (
|
|
40
|
+
<InlinePopover
|
|
41
|
+
width={318}
|
|
42
|
+
trigger={({toggle}) => (
|
|
43
|
+
<Button
|
|
44
|
+
mode="bleed"
|
|
45
|
+
tone="primary"
|
|
46
|
+
fontSize={1}
|
|
47
|
+
padding={props.inline ? 2 : 3}
|
|
48
|
+
icon={ArrowUpIcon}
|
|
49
|
+
text={`Kit ${update.latest}`}
|
|
50
|
+
title={`Une version plus récente du kit est disponible (vous exécutez la ${update.current})`}
|
|
51
|
+
justify={props.inline ? undefined : 'flex-start'}
|
|
52
|
+
onClick={toggle}
|
|
53
|
+
/>
|
|
54
|
+
)}
|
|
55
|
+
>
|
|
56
|
+
{() => (
|
|
57
|
+
<Stack gap={3} padding={2}>
|
|
58
|
+
<Stack gap={2}>
|
|
59
|
+
<Text size={1} weight="semibold">
|
|
60
|
+
Kit {update.latest} disponible
|
|
61
|
+
</Text>
|
|
62
|
+
<Text size={0} muted>
|
|
63
|
+
Vous exécutez la {update.current}.
|
|
64
|
+
</Text>
|
|
65
|
+
</Stack>
|
|
66
|
+
|
|
67
|
+
<Card padding={2} radius={2} style={{border: `1px solid ${BORDER}`}}>
|
|
68
|
+
<Text size={0} style={{...MONO, wordBreak: 'break-all'}}>
|
|
69
|
+
{update.command}
|
|
70
|
+
</Text>
|
|
71
|
+
</Card>
|
|
72
|
+
|
|
73
|
+
<Flex gap={2} wrap="wrap">
|
|
74
|
+
<Button
|
|
75
|
+
mode="default"
|
|
76
|
+
tone="primary"
|
|
77
|
+
fontSize={1}
|
|
78
|
+
padding={2}
|
|
79
|
+
icon={copied ? CheckmarkIcon : CopyIcon}
|
|
80
|
+
text={copied ? 'Copié' : 'Copier la commande'}
|
|
81
|
+
onClick={copy}
|
|
82
|
+
/>
|
|
83
|
+
<Button
|
|
84
|
+
as="a"
|
|
85
|
+
href={CHANGELOG}
|
|
86
|
+
target="_blank"
|
|
87
|
+
rel="noreferrer"
|
|
88
|
+
mode="bleed"
|
|
89
|
+
fontSize={1}
|
|
90
|
+
padding={2}
|
|
91
|
+
text="Nouveautés"
|
|
92
|
+
/>
|
|
93
|
+
</Flex>
|
|
94
|
+
|
|
95
|
+
<Box style={{borderTop: `1px solid ${BORDER}`, paddingTop: 10}}>
|
|
96
|
+
<Text size={0} muted>
|
|
97
|
+
À lancer dans <code>studio/</code> puis <code>web/</code>, suivi d'un{' '}
|
|
98
|
+
<code>npx sanity deploy</code>. Le Studio ne peut pas le faire lui-même : il
|
|
99
|
+
s'exécute dans votre navigateur, pas sur la machine qui le construit.
|
|
100
|
+
</Text>
|
|
101
|
+
</Box>
|
|
102
|
+
</Stack>
|
|
103
|
+
)}
|
|
104
|
+
</InlinePopover>
|
|
105
|
+
)
|
|
106
|
+
}
|
|
@@ -3,11 +3,12 @@ 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, PERSPECTIVES, type PreviewPerspective} from '../../lib/studioLinks'
|
|
6
|
+
import {presentationPath, onlineUrl, PERSPECTIVES, type PreviewPerspective} from '../../lib/studioLinks'
|
|
7
7
|
import type {ConfigIssue, NcfEnvironment, ResolvedKitConfig} from '../../config/types'
|
|
8
8
|
import {EyeOpenIcon} from '@sanity/icons/EyeOpen'
|
|
9
9
|
import {LaunchIcon} from '@sanity/icons/Launch'
|
|
10
10
|
import {BORDER} from '../../ui/tokens'
|
|
11
|
+
import {NcfKitUpdate} from './KitUpdate'
|
|
11
12
|
|
|
12
13
|
/**
|
|
13
14
|
* Lanceur d'aperçu, posé dans la barre du haut à droite des outils.
|
|
@@ -25,13 +26,31 @@ export function NcfPreviewLauncher(props: ToolMenuProps) {
|
|
|
25
26
|
const router = useRouter()
|
|
26
27
|
const environments = config.environments
|
|
27
28
|
|
|
28
|
-
if (environments.length === 0)
|
|
29
|
+
if (environments.length === 0) {
|
|
30
|
+
return (
|
|
31
|
+
<Flex align="center" gap={2}>
|
|
32
|
+
{props.renderDefault(props)}
|
|
33
|
+
<NcfKitUpdate inline={props.context === 'topbar'} />
|
|
34
|
+
</Flex>
|
|
35
|
+
)
|
|
36
|
+
}
|
|
29
37
|
|
|
30
|
-
|
|
38
|
+
// Ouvre l'aperçu DANS le Studio : la page apparaît dans Presentation, sans
|
|
39
|
+
// quitter le Studio.
|
|
40
|
+
const openInStudio = (environment: NcfEnvironment, perspective: PreviewPerspective) => {
|
|
31
41
|
const path = presentationPath(environment.url, {environment, perspective})
|
|
32
42
|
if (path) router.navigateUrl({path})
|
|
33
43
|
}
|
|
34
44
|
|
|
45
|
+
// Ouvre le vrai site EN LIGNE, dans un nouvel onglet. La perspective est
|
|
46
|
+
// respectée en passant par la route de bascule du site : « brouillons »
|
|
47
|
+
// l'active, « en ligne » la coupe. Un site sans cette route — ou la
|
|
48
|
+
// production, qui refuse les brouillons — retombe sur l'URL simple.
|
|
49
|
+
const openOnline = (environment: NcfEnvironment, perspective: PreviewPerspective) => {
|
|
50
|
+
const target = onlineUrl(environment, perspective)
|
|
51
|
+
if (target) window.open(target, '_blank', 'noopener')
|
|
52
|
+
}
|
|
53
|
+
|
|
35
54
|
// En colonne dans le volet latéral, en ligne dans la barre : le menu des
|
|
36
55
|
// outils change de forme selon la place, le lanceur doit suivre.
|
|
37
56
|
const inline = props.context === 'topbar'
|
|
@@ -44,6 +63,8 @@ export function NcfPreviewLauncher(props: ToolMenuProps) {
|
|
|
44
63
|
>
|
|
45
64
|
{props.renderDefault(props)}
|
|
46
65
|
|
|
66
|
+
<NcfKitUpdate inline={inline} />
|
|
67
|
+
|
|
47
68
|
<InlinePopover
|
|
48
69
|
width={248}
|
|
49
70
|
trigger={({toggle}) => (
|
|
@@ -61,6 +82,11 @@ export function NcfPreviewLauncher(props: ToolMenuProps) {
|
|
|
61
82
|
>
|
|
62
83
|
{({close}) => (
|
|
63
84
|
<Stack gap={1}>
|
|
85
|
+
<Box paddingX={2} paddingTop={1} paddingBottom={2}>
|
|
86
|
+
<Text size={0} muted>
|
|
87
|
+
Cliquer : ouvre dans le Studio. L'icône ↗ : ouvre le site en ligne.
|
|
88
|
+
</Text>
|
|
89
|
+
</Box>
|
|
64
90
|
{environments.map((environment, index) => (
|
|
65
91
|
<Box
|
|
66
92
|
key={environment.id}
|
|
@@ -75,31 +101,44 @@ export function NcfPreviewLauncher(props: ToolMenuProps) {
|
|
|
75
101
|
</Text>
|
|
76
102
|
</Box>
|
|
77
103
|
{PERSPECTIVES.map((option) => (
|
|
78
|
-
<Flex
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
event
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
event
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
<
|
|
98
|
-
|
|
99
|
-
{
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
104
|
+
<Flex key={option.id} align="center" gap={1}>
|
|
105
|
+
<Flex
|
|
106
|
+
align="center"
|
|
107
|
+
flex={1}
|
|
108
|
+
padding={2}
|
|
109
|
+
onClick={() => {
|
|
110
|
+
openInStudio(environment, option.id)
|
|
111
|
+
close()
|
|
112
|
+
}}
|
|
113
|
+
style={{cursor: 'pointer', borderRadius: 3, minWidth: 0}}
|
|
114
|
+
title="Ouvrir dans le Studio (Presentation)"
|
|
115
|
+
onMouseEnter={(event) => {
|
|
116
|
+
event.currentTarget.style.background =
|
|
117
|
+
'var(--card-muted-bg-color, rgba(127,127,127,0.12))'
|
|
118
|
+
}}
|
|
119
|
+
onMouseLeave={(event) => {
|
|
120
|
+
event.currentTarget.style.background = 'transparent'
|
|
121
|
+
}}
|
|
122
|
+
>
|
|
123
|
+
<Stack gap={1} style={{minWidth: 0}}>
|
|
124
|
+
<Text size={1}>{option.label}</Text>
|
|
125
|
+
<Text size={0} muted textOverflow="ellipsis">
|
|
126
|
+
{option.hint}
|
|
127
|
+
</Text>
|
|
128
|
+
</Stack>
|
|
129
|
+
</Flex>
|
|
130
|
+
<Button
|
|
131
|
+
mode="bleed"
|
|
132
|
+
padding={2}
|
|
133
|
+
fontSize={1}
|
|
134
|
+
icon={LaunchIcon}
|
|
135
|
+
title="Ouvrir le site en ligne, dans un nouvel onglet"
|
|
136
|
+
aria-label={`Ouvrir ${environment.label} en ligne (${option.label})`}
|
|
137
|
+
onClick={() => {
|
|
138
|
+
openOnline(environment, option.id)
|
|
139
|
+
close()
|
|
140
|
+
}}
|
|
141
|
+
/>
|
|
103
142
|
</Flex>
|
|
104
143
|
))}
|
|
105
144
|
</Box>
|
package/src/version.ts
CHANGED