@nocode-factory/sanity-kit 2.0.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/bin/ncf-kit.mjs +201 -0
- package/dist/index.d.ts +1643 -0
- package/dist/index.js +6632 -0
- package/dist/index.js.map +1 -0
- package/dist/web-server.d.ts +46 -0
- package/dist/web-server.js +51 -0
- package/dist/web-server.js.map +1 -0
- package/dist/web.d.ts +27 -0
- package/dist/web.js +117 -0
- package/dist/web.js.map +1 -0
- package/package.json +90 -0
- package/scripts/use-client.mjs +20 -0
- package/src/config/defaults.ts +74 -0
- package/src/config/resolve.ts +153 -0
- package/src/config/types.ts +290 -0
- package/src/config/validate.ts +193 -0
- package/src/context/BoardLayoutContext.tsx +24 -0
- package/src/context/KitContext.tsx +30 -0
- package/src/context/PreviewTargetContext.tsx +27 -0
- package/src/hooks/useAnnotations.ts +60 -0
- package/src/hooks/useBoardLayout.ts +398 -0
- package/src/hooks/useColumnOptions.ts +47 -0
- package/src/hooks/useDocumentColumns.ts +61 -0
- package/src/hooks/useHistory.ts +89 -0
- package/src/hooks/useInternalColumns.ts +19 -0
- package/src/hooks/useKitClient.ts +15 -0
- package/src/hooks/useOccurrences.ts +62 -0
- package/src/hooks/usePageContents.ts +65 -0
- package/src/hooks/usePages.ts +41 -0
- package/src/hooks/usePreviewTarget.ts +162 -0
- package/src/hooks/useQueryData.ts +103 -0
- package/src/hooks/useRegistration.ts +50 -0
- package/src/index.ts +280 -0
- package/src/lib/blocks.ts +38 -0
- package/src/lib/colors.ts +103 -0
- package/src/lib/columns.ts +137 -0
- package/src/lib/diff.ts +200 -0
- package/src/lib/elkLayout.ts +124 -0
- package/src/lib/graph.ts +238 -0
- package/src/lib/grouping.ts +116 -0
- package/src/lib/links.ts +70 -0
- package/src/lib/occurrences.ts +93 -0
- package/src/lib/pageThumbnail.ts +43 -0
- package/src/lib/pages.ts +199 -0
- package/src/lib/paths.ts +68 -0
- package/src/lib/query.ts +113 -0
- package/src/lib/schemaColumns.ts +162 -0
- package/src/lib/schemaTypes.ts +75 -0
- package/src/lib/sectionsMap.ts +83 -0
- package/src/lib/service.ts +88 -0
- package/src/lib/siteRoutes.ts +43 -0
- package/src/lib/status.ts +122 -0
- package/src/lib/studioLinks.ts +137 -0
- package/src/schema/boardLayout.ts +190 -0
- package/src/tools/components/BlockCard.tsx +79 -0
- package/src/tools/components/ComponentsTool.tsx +171 -0
- package/src/tools/components/map/SectionNode.tsx +109 -0
- package/src/tools/components/map/SectionsMap.tsx +339 -0
- package/src/tools/createToolComponent.tsx +67 -0
- package/src/tools/history/ChangeRow.tsx +87 -0
- package/src/tools/history/DiffPanel.tsx +227 -0
- package/src/tools/history/HistoryTool.tsx +230 -0
- package/src/tools/more/MoreTool.tsx +82 -0
- package/src/tools/pages/ColumnHeader.tsx +216 -0
- package/src/tools/pages/ColumnVisibility.tsx +173 -0
- package/src/tools/pages/FieldValue.tsx +327 -0
- package/src/tools/pages/PagesTable.tsx +394 -0
- package/src/tools/pages/PagesTool.tsx +333 -0
- package/src/tools/pages/PagesToolbar.tsx +53 -0
- package/src/tools/pages/PathCell.tsx +71 -0
- package/src/tools/pages/PreviewTarget.tsx +226 -0
- package/src/tools/pages/TagCell.tsx +124 -0
- package/src/tools/pages/ViewTabs.tsx +57 -0
- package/src/tools/pages/board/BoardView.tsx +661 -0
- package/src/tools/pages/board/PageNode.tsx +306 -0
- package/src/tools/pages/board/PagePanel.tsx +228 -0
- package/src/tools/presentation/PreviewHeader.tsx +75 -0
- package/src/tools/shared/AnnotationNode.tsx +126 -0
- package/src/ui/ActorName.tsx +20 -0
- package/src/ui/ConfigIssues.tsx +41 -0
- package/src/ui/InlineCode.tsx +22 -0
- package/src/ui/Popover.tsx +85 -0
- package/src/ui/ProjectStatusNotice.tsx +58 -0
- package/src/ui/QueryError.tsx +16 -0
- package/src/ui/QuietInput.tsx +64 -0
- package/src/ui/StatusBadge.tsx +11 -0
- package/src/ui/ToolPlaceholder.tsx +23 -0
- package/src/ui/formatDate.ts +14 -0
- package/src/ui/tokens.ts +54 -0
- package/src/version.ts +5 -0
- package/src/web/PreviewBanner.tsx +115 -0
- package/src/web/draftModeToggle.ts +56 -0
- package/src/web/environment.ts +21 -0
- package/src/web/index.ts +15 -0
- package/src/web/server.ts +11 -0
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import {useMemo} from 'react'
|
|
2
|
+
import {useKitConfig} from '../context/KitContext'
|
|
3
|
+
import {buildContentsQuery, PROJECTION} from '../lib/query'
|
|
4
|
+
import {mergeVersions} from '../lib/status'
|
|
5
|
+
import {useQueryData} from './useQueryData'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Plafond du balayage. La requête rapatrie les documents entiers : c'est le
|
|
9
|
+
* seul moyen générique de compter les blocs imbriqués et de relever les liens
|
|
10
|
+
* internes, mais ça se borne. 1000 documents couvrent la cible des 500 pages.
|
|
11
|
+
*/
|
|
12
|
+
export const CONTENT_SCAN_LIMIT = 1000
|
|
13
|
+
|
|
14
|
+
export interface PageContent {
|
|
15
|
+
_id: string
|
|
16
|
+
_type: string
|
|
17
|
+
_updatedAt?: string
|
|
18
|
+
[key: string]: unknown
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface PageContentsState {
|
|
22
|
+
/** Contenu par identifiant de page publiée, brouillon prioritaire. */
|
|
23
|
+
contents: Map<string, PageContent>
|
|
24
|
+
/** Vrai quand le plafond est atteint : les chiffres sont alors partiels. */
|
|
25
|
+
truncated: boolean
|
|
26
|
+
scanned: number
|
|
27
|
+
loading: boolean
|
|
28
|
+
error: Error | null
|
|
29
|
+
refresh: () => void
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Contenu des pages, partagé par l'outil Composants et par le board. Une
|
|
34
|
+
* page « modifiée » existe en double (publiée + brouillon) : la fusion garde
|
|
35
|
+
* la version de référence, sinon ses blocs et ses liens compteraient deux fois.
|
|
36
|
+
*/
|
|
37
|
+
export function usePageContents(): PageContentsState {
|
|
38
|
+
const config = useKitConfig()
|
|
39
|
+
|
|
40
|
+
const query = useMemo(() => buildContentsQuery(config, CONTENT_SCAN_LIMIT), [config])
|
|
41
|
+
const params = useMemo(() => ({types: config.pageTypeNames}), [config.pageTypeNames])
|
|
42
|
+
|
|
43
|
+
const {data, loading, error, refresh} = useQueryData<PageContent[]>(query, params, {
|
|
44
|
+
listenTypes: config.pageTypeNames,
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
const contents = useMemo(() => {
|
|
48
|
+
const map = new Map<string, PageContent>()
|
|
49
|
+
for (const entry of mergeVersions(data ?? [])) {
|
|
50
|
+
map.set(entry.id, entry.doc)
|
|
51
|
+
}
|
|
52
|
+
return map
|
|
53
|
+
}, [data])
|
|
54
|
+
|
|
55
|
+
return {
|
|
56
|
+
contents,
|
|
57
|
+
truncated: (data?.length ?? 0) >= CONTENT_SCAN_LIMIT,
|
|
58
|
+
scanned: data?.length ?? 0,
|
|
59
|
+
loading,
|
|
60
|
+
error,
|
|
61
|
+
refresh,
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export {PROJECTION}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import {useMemo} from 'react'
|
|
2
|
+
import {useKitConfig} from '../context/KitContext'
|
|
3
|
+
import {buildPagesQuery} from '../lib/query'
|
|
4
|
+
import {buildPageRows, type PageDoc, type PageRow} from '../lib/pages'
|
|
5
|
+
import {useQueryData} from './useQueryData'
|
|
6
|
+
import type {ResolvedDocumentColumn} from '../config/types'
|
|
7
|
+
|
|
8
|
+
export interface PagesState {
|
|
9
|
+
rows: PageRow[]
|
|
10
|
+
loading: boolean
|
|
11
|
+
error: Error | null
|
|
12
|
+
refresh: () => void
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Toutes les pages du site, brouillons compris, tenues à jour.
|
|
17
|
+
*
|
|
18
|
+
* Seules les colonnes de données affichées sont projetées : sur 500 pages, en
|
|
19
|
+
* remonter tout le document coûterait cher pour des valeurs que personne ne
|
|
20
|
+
* regarde.
|
|
21
|
+
*/
|
|
22
|
+
export function usePages(documentColumns: readonly ResolvedDocumentColumn[] = []): PagesState {
|
|
23
|
+
const config = useKitConfig()
|
|
24
|
+
// La signature de la projection suffit à distinguer deux requêtes : deux
|
|
25
|
+
// rendus avec les mêmes colonnes ne doivent pas en reconstruire une.
|
|
26
|
+
const signature = documentColumns.map((column) => `${column.key}:${column.paths.join('|')}`).join(',')
|
|
27
|
+
const columns = useMemo(() => documentColumns, [signature]) // eslint-disable-line react-hooks/exhaustive-deps
|
|
28
|
+
const query = useMemo(() => buildPagesQuery(config, columns), [config, columns])
|
|
29
|
+
const params = useMemo(() => ({types: config.pageTypeNames}), [config.pageTypeNames])
|
|
30
|
+
|
|
31
|
+
const {data, loading, error, refresh} = useQueryData<PageDoc[]>(query, params, {
|
|
32
|
+
listenTypes: config.pageTypeNames,
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
const rows = useMemo(
|
|
36
|
+
() => (data ? buildPageRows(data, config, columns) : []),
|
|
37
|
+
[data, config, columns],
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
return {rows, loading, error, refresh}
|
|
41
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import {useCallback, useMemo, useState} from 'react'
|
|
2
|
+
import {useKitConfig} from '../context/KitContext'
|
|
3
|
+
import {useKitClient} from './useKitClient'
|
|
4
|
+
import {
|
|
5
|
+
customEnvironment,
|
|
6
|
+
CUSTOM_ENVIRONMENT_ID,
|
|
7
|
+
presentationPath,
|
|
8
|
+
urlOn,
|
|
9
|
+
type PreviewPerspective,
|
|
10
|
+
} from '../lib/studioLinks'
|
|
11
|
+
import type {NcfEnvironment} from '../config/types'
|
|
12
|
+
|
|
13
|
+
export interface PreviewTargetState {
|
|
14
|
+
environments: NcfEnvironment[]
|
|
15
|
+
environment: NcfEnvironment | null
|
|
16
|
+
environmentId: string | null
|
|
17
|
+
perspective: PreviewPerspective
|
|
18
|
+
setEnvironment: (id: string) => void
|
|
19
|
+
/**
|
|
20
|
+
* Cible une adresse collée à la volée. Chaîne vide : retour au premier
|
|
21
|
+
* environnement déclaré.
|
|
22
|
+
*/
|
|
23
|
+
setCustomUrl: (url: string) => boolean
|
|
24
|
+
customUrl: string | null
|
|
25
|
+
setPerspective: (perspective: PreviewPerspective) => void
|
|
26
|
+
/** Chemin Studio vers Presentation pour une page, selon la cible choisie. */
|
|
27
|
+
pathFor: (url: string | null) => string | null
|
|
28
|
+
/** URL de la page sur l'environnement choisi, pour l'ouvrir hors du Studio. */
|
|
29
|
+
siteUrlFor: (url: string | null) => string | null
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const STORAGE_PREFIX = 'ncf-kit:preview-target:'
|
|
33
|
+
|
|
34
|
+
/** Lit une préférence locale sans jamais faire échouer le montage. */
|
|
35
|
+
function read(key: string): string | null {
|
|
36
|
+
try {
|
|
37
|
+
return window.localStorage.getItem(key)
|
|
38
|
+
} catch {
|
|
39
|
+
// Navigation privée, stockage bloqué, aperçu de vignette : ce n'est pas
|
|
40
|
+
// une erreur, c'est simplement une préférence qu'on ne retrouve pas.
|
|
41
|
+
return null
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function write(key: string, value: string): void {
|
|
46
|
+
try {
|
|
47
|
+
window.localStorage.setItem(key, value)
|
|
48
|
+
} catch {
|
|
49
|
+
// Sans mémoire, le choix vaut pour la session : acceptable pour un
|
|
50
|
+
// confort d'affichage.
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Cible de l'aperçu : quelle version du site, et quel état du contenu.
|
|
56
|
+
*
|
|
57
|
+
* Retenue par navigateur et par projet, pas dans le document de disposition :
|
|
58
|
+
* c'est un confort personnel, pas un réglage d'équipe. Deux personnes
|
|
59
|
+
* relisant le même site peuvent vouloir regarder deux environnements
|
|
60
|
+
* différents sans se déranger.
|
|
61
|
+
*/
|
|
62
|
+
export function usePreviewTargetState(): PreviewTargetState {
|
|
63
|
+
const config = useKitConfig()
|
|
64
|
+
const client = useKitClient()
|
|
65
|
+
const scope = `${STORAGE_PREFIX}${client.config().projectId ?? 'sans-projet'}`
|
|
66
|
+
|
|
67
|
+
const environments = config.environments
|
|
68
|
+
const fallback = environments[0]?.id ?? null
|
|
69
|
+
|
|
70
|
+
const [environmentId, setStoredEnvironment] = useState<string | null>(() => {
|
|
71
|
+
const saved = read(`${scope}:environment`)
|
|
72
|
+
// Un environnement retiré de la configuration ne doit pas rester
|
|
73
|
+
// sélectionné : on retombe sur le premier déclaré.
|
|
74
|
+
return saved && environments.some((e) => e.id === saved) ? saved : fallback
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
// Les préversions sont innombrables et éphémères : on retient la dernière
|
|
78
|
+
// adresse collée plutôt que de demander de la redéclarer.
|
|
79
|
+
const [customUrl, setStoredCustomUrl] = useState<string | null>(() => read(`${scope}:custom`))
|
|
80
|
+
|
|
81
|
+
const [perspective, setStoredPerspective] = useState<PreviewPerspective>(() =>
|
|
82
|
+
read(`${scope}:perspective`) === 'published' ? 'published' : 'drafts',
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
const setEnvironment = useCallback(
|
|
86
|
+
(id: string) => {
|
|
87
|
+
setStoredEnvironment(id)
|
|
88
|
+
write(`${scope}:environment`, id)
|
|
89
|
+
},
|
|
90
|
+
[scope],
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
const setPerspective = useCallback(
|
|
94
|
+
(next: PreviewPerspective) => {
|
|
95
|
+
setStoredPerspective(next)
|
|
96
|
+
write(`${scope}:perspective`, next)
|
|
97
|
+
},
|
|
98
|
+
[scope],
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
const custom = useMemo(() => (customUrl ? customEnvironment(customUrl) : null), [customUrl])
|
|
102
|
+
|
|
103
|
+
const allEnvironments = useMemo(
|
|
104
|
+
() => (custom ? [...environments, custom] : environments),
|
|
105
|
+
[environments, custom],
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
const environment = useMemo(
|
|
109
|
+
() => allEnvironments.find((item) => item.id === environmentId) ?? allEnvironments[0] ?? null,
|
|
110
|
+
[allEnvironments, environmentId],
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
const setCustomUrl = useCallback(
|
|
114
|
+
(url: string) => {
|
|
115
|
+
if (!url.trim()) {
|
|
116
|
+
setStoredCustomUrl(null)
|
|
117
|
+
write(`${scope}:custom`, '')
|
|
118
|
+
setEnvironment(fallback ?? '')
|
|
119
|
+
return true
|
|
120
|
+
}
|
|
121
|
+
// Une adresse inexploitable n'est pas retenue : mieux vaut le dire tout
|
|
122
|
+
// de suite que de charger une page blanche.
|
|
123
|
+
if (!customEnvironment(url)) return false
|
|
124
|
+
setStoredCustomUrl(url)
|
|
125
|
+
write(`${scope}:custom`, url)
|
|
126
|
+
setEnvironment(CUSTOM_ENVIRONMENT_ID)
|
|
127
|
+
return true
|
|
128
|
+
},
|
|
129
|
+
[scope, setEnvironment, fallback],
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
const pathFor = useCallback(
|
|
133
|
+
(url: string | null) => presentationPath(url, {environment, perspective}),
|
|
134
|
+
[environment, perspective],
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
const siteUrlFor = useCallback(
|
|
138
|
+
(url: string | null) => {
|
|
139
|
+
if (!url) return null
|
|
140
|
+
if (!environment) return url
|
|
141
|
+
try {
|
|
142
|
+
return urlOn(environment, new URL(url).pathname)
|
|
143
|
+
} catch {
|
|
144
|
+
return urlOn(environment, url)
|
|
145
|
+
}
|
|
146
|
+
},
|
|
147
|
+
[environment],
|
|
148
|
+
)
|
|
149
|
+
|
|
150
|
+
return {
|
|
151
|
+
environments: allEnvironments,
|
|
152
|
+
customUrl,
|
|
153
|
+
setCustomUrl,
|
|
154
|
+
environment,
|
|
155
|
+
environmentId: environment?.id ?? null,
|
|
156
|
+
perspective,
|
|
157
|
+
setEnvironment,
|
|
158
|
+
setPerspective,
|
|
159
|
+
pathFor,
|
|
160
|
+
siteUrlFor,
|
|
161
|
+
}
|
|
162
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import {useCallback, useEffect, useMemo, useRef, useState} from 'react'
|
|
2
|
+
import {useKitClient} from './useKitClient'
|
|
3
|
+
|
|
4
|
+
export interface QueryState<T> {
|
|
5
|
+
data: T | null
|
|
6
|
+
loading: boolean
|
|
7
|
+
error: Error | null
|
|
8
|
+
refresh: () => void
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface UseQueryDataOptions {
|
|
12
|
+
/** Types écoutés pour rafraîchir automatiquement. Vide = pas d'écoute. */
|
|
13
|
+
listenTypes?: readonly string[]
|
|
14
|
+
/** `false` pour différer la requête (onglet pas encore ouvert). */
|
|
15
|
+
enabled?: boolean
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Exécute une requête GROQ et la rejoue quand le contenu change.
|
|
20
|
+
*
|
|
21
|
+
* Le rafraîchissement passe par un `listen` puis une nouvelle requête, plutôt
|
|
22
|
+
* que par l'application des mutations reçues : c'est plus lent mais toujours
|
|
23
|
+
* cohérent, y compris quand une publication modifie plusieurs documents.
|
|
24
|
+
*/
|
|
25
|
+
export function useQueryData<T>(
|
|
26
|
+
query: string,
|
|
27
|
+
params: Record<string, unknown>,
|
|
28
|
+
options: UseQueryDataOptions = {},
|
|
29
|
+
): QueryState<T> {
|
|
30
|
+
const client = useKitClient()
|
|
31
|
+
const {listenTypes, enabled = true} = options
|
|
32
|
+
|
|
33
|
+
const [data, setData] = useState<T | null>(null)
|
|
34
|
+
const [loading, setLoading] = useState(enabled)
|
|
35
|
+
const [error, setError] = useState<Error | null>(null)
|
|
36
|
+
const [tick, setTick] = useState(0)
|
|
37
|
+
|
|
38
|
+
const refresh = useCallback(() => setTick((n) => n + 1), [])
|
|
39
|
+
|
|
40
|
+
// Stabilise les paramètres : un objet recréé à chaque rendu relancerait
|
|
41
|
+
// la requête en boucle.
|
|
42
|
+
const paramsKey = JSON.stringify(params)
|
|
43
|
+
const stableParams = useMemo(() => JSON.parse(paramsKey) as Record<string, unknown>, [paramsKey])
|
|
44
|
+
|
|
45
|
+
useEffect(() => {
|
|
46
|
+
if (!enabled) return undefined
|
|
47
|
+
|
|
48
|
+
let cancelled = false
|
|
49
|
+
setLoading(true)
|
|
50
|
+
|
|
51
|
+
client
|
|
52
|
+
.fetch<T>(query, stableParams)
|
|
53
|
+
.then((result) => {
|
|
54
|
+
if (cancelled) return
|
|
55
|
+
setData(result)
|
|
56
|
+
setError(null)
|
|
57
|
+
})
|
|
58
|
+
.catch((cause: unknown) => {
|
|
59
|
+
if (cancelled) return
|
|
60
|
+
setError(cause instanceof Error ? cause : new Error(String(cause)))
|
|
61
|
+
})
|
|
62
|
+
.finally(() => {
|
|
63
|
+
if (!cancelled) setLoading(false)
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
return () => {
|
|
67
|
+
cancelled = true
|
|
68
|
+
}
|
|
69
|
+
}, [client, query, stableParams, enabled, tick])
|
|
70
|
+
|
|
71
|
+
/* --- Rafraîchissement sur mutation --- */
|
|
72
|
+
const refreshRef = useRef(refresh)
|
|
73
|
+
refreshRef.current = refresh
|
|
74
|
+
const typesKey = (listenTypes ?? []).join(',')
|
|
75
|
+
|
|
76
|
+
useEffect(() => {
|
|
77
|
+
if (!enabled || !typesKey) return undefined
|
|
78
|
+
|
|
79
|
+
const types = typesKey.split(',')
|
|
80
|
+
// Fenêtre courte : une publication émet plusieurs événements d'affilée,
|
|
81
|
+
// on ne veut pas relancer la requête à chacun.
|
|
82
|
+
let timer: ReturnType<typeof setTimeout> | undefined
|
|
83
|
+
const subscription = client
|
|
84
|
+
.listen('*[_type in $types]', {types}, {visibility: 'query', events: ['mutation']})
|
|
85
|
+
.subscribe({
|
|
86
|
+
next: () => {
|
|
87
|
+
if (timer) clearTimeout(timer)
|
|
88
|
+
timer = setTimeout(() => refreshRef.current(), 600)
|
|
89
|
+
},
|
|
90
|
+
error: () => {
|
|
91
|
+
// Une écoute qui tombe ne doit pas vider la liste déjà affichée :
|
|
92
|
+
// l'utilisateur garde ses données et le bouton de rafraîchissement.
|
|
93
|
+
},
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
return () => {
|
|
97
|
+
if (timer) clearTimeout(timer)
|
|
98
|
+
subscription.unsubscribe()
|
|
99
|
+
}
|
|
100
|
+
}, [client, typesKey, enabled])
|
|
101
|
+
|
|
102
|
+
return {data, loading, error, refresh}
|
|
103
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import {useEffect, useState} from 'react'
|
|
2
|
+
import {useKitConfig} from '../context/KitContext'
|
|
3
|
+
import {useKitClient} from './useKitClient'
|
|
4
|
+
import {announce, type Registration} from '../lib/service'
|
|
5
|
+
import {KIT_VERSION} from '../version'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Signale ce Studio au service du kit, une fois par montage.
|
|
9
|
+
*
|
|
10
|
+
* C'est ce qui permet de savoir quels projets utilisent le kit. Ce qui part :
|
|
11
|
+
* identifiant de projet Sanity, dataset, hôte du Studio, version du kit.
|
|
12
|
+
* Aucun contenu, aucune identité d'éditeur.
|
|
13
|
+
*
|
|
14
|
+
* `serviceEndpoint: false` en configuration coupe complètement cet appel.
|
|
15
|
+
*
|
|
16
|
+
* En cas d'échec — réseau coupé, service indisponible — on rend `null` et
|
|
17
|
+
* rien ne change : le Studio d'un client ne doit pas dépendre de notre
|
|
18
|
+
* disponibilité.
|
|
19
|
+
*/
|
|
20
|
+
export function useRegistration(): Registration | null {
|
|
21
|
+
const config = useKitConfig()
|
|
22
|
+
const client = useKitClient()
|
|
23
|
+
const [registration, setRegistration] = useState<Registration | null>(null)
|
|
24
|
+
|
|
25
|
+
const endpoint = config.serviceEndpoint
|
|
26
|
+
const projectId = client.config().projectId
|
|
27
|
+
const dataset = client.config().dataset
|
|
28
|
+
|
|
29
|
+
useEffect(() => {
|
|
30
|
+
if (!endpoint || !projectId || !dataset) return undefined
|
|
31
|
+
|
|
32
|
+
const controller = new AbortController()
|
|
33
|
+
announce(
|
|
34
|
+
endpoint,
|
|
35
|
+
{
|
|
36
|
+
projectId,
|
|
37
|
+
dataset,
|
|
38
|
+
host: typeof window === 'undefined' ? 'inconnu' : window.location.host,
|
|
39
|
+
version: KIT_VERSION,
|
|
40
|
+
},
|
|
41
|
+
controller.signal,
|
|
42
|
+
).then((result) => {
|
|
43
|
+
if (!controller.signal.aborted) setRegistration(result)
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
return () => controller.abort()
|
|
47
|
+
}, [endpoint, projectId, dataset])
|
|
48
|
+
|
|
49
|
+
return registration
|
|
50
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import {definePlugin, type Tool} from 'sanity'
|
|
2
|
+
import {ClockIcon} from '@sanity/icons/Clock'
|
|
3
|
+
import {ComponentIcon} from '@sanity/icons/Component'
|
|
4
|
+
import {DocumentsIcon} from '@sanity/icons/Documents'
|
|
5
|
+
|
|
6
|
+
import {resolveConfig} from './config/resolve'
|
|
7
|
+
import {validateConfig} from './config/validate'
|
|
8
|
+
import {createToolComponent} from './tools/createToolComponent'
|
|
9
|
+
import {PagesTool} from './tools/pages/PagesTool'
|
|
10
|
+
import {ComponentsTool} from './tools/components/ComponentsTool'
|
|
11
|
+
import {HistoryTool} from './tools/history/HistoryTool'
|
|
12
|
+
import {BOARD_LAYOUT_TYPE, isKitType, kitSchemaTypes} from './schema/boardLayout'
|
|
13
|
+
import type {NcfKitConfig, NcfToolName} from './config/types'
|
|
14
|
+
|
|
15
|
+
/** Noms des outils dans la barre du Studio. Exportés pour filtrer `tools`. */
|
|
16
|
+
export const NCF_PAGES_TOOL = 'ncf-pages'
|
|
17
|
+
export const NCF_COMPONENTS_TOOL = 'ncf-components'
|
|
18
|
+
export const NCF_HISTORY_TOOL = 'ncf-history'
|
|
19
|
+
|
|
20
|
+
export const NCF_TOOL_NAMES = [
|
|
21
|
+
NCF_PAGES_TOOL,
|
|
22
|
+
NCF_COMPONENTS_TOOL,
|
|
23
|
+
NCF_HISTORY_TOOL,
|
|
24
|
+
] as const
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Plugin Studio de NoCode Factory : Pages, Composants, Historique.
|
|
28
|
+
*
|
|
29
|
+
* ```ts
|
|
30
|
+
* plugins: [ncfKit({pageTypes: [{type: 'homePage', path: '/'}, 'page']})]
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* La configuration est validée au démarrage. Une option invalide n'empêche
|
|
34
|
+
* pas le Studio de démarrer : l'onglet concerné affiche le détail à corriger.
|
|
35
|
+
*/
|
|
36
|
+
export const ncfKit = definePlugin<NcfKitConfig>((options) => {
|
|
37
|
+
const config = resolveConfig(options)
|
|
38
|
+
const issues = validateConfig(config)
|
|
39
|
+
|
|
40
|
+
const definitions: {name: NcfToolName; tool: Tool}[] = [
|
|
41
|
+
{
|
|
42
|
+
name: 'pages',
|
|
43
|
+
tool: {
|
|
44
|
+
name: NCF_PAGES_TOOL,
|
|
45
|
+
title: 'Pages',
|
|
46
|
+
icon: DocumentsIcon,
|
|
47
|
+
component: createToolComponent({
|
|
48
|
+
name: 'pages',
|
|
49
|
+
title: 'Pages',
|
|
50
|
+
config,
|
|
51
|
+
issues,
|
|
52
|
+
Component: PagesTool,
|
|
53
|
+
}),
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
name: 'components',
|
|
58
|
+
tool: {
|
|
59
|
+
name: NCF_COMPONENTS_TOOL,
|
|
60
|
+
title: 'Composants',
|
|
61
|
+
icon: ComponentIcon,
|
|
62
|
+
component: createToolComponent({
|
|
63
|
+
name: 'components',
|
|
64
|
+
title: 'Composants',
|
|
65
|
+
config,
|
|
66
|
+
issues,
|
|
67
|
+
Component: ComponentsTool,
|
|
68
|
+
}),
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
name: 'history',
|
|
73
|
+
tool: {
|
|
74
|
+
name: NCF_HISTORY_TOOL,
|
|
75
|
+
title: 'Historique',
|
|
76
|
+
icon: ClockIcon,
|
|
77
|
+
component: createToolComponent({
|
|
78
|
+
name: 'history',
|
|
79
|
+
title: 'Historique',
|
|
80
|
+
config,
|
|
81
|
+
issues,
|
|
82
|
+
Component: HistoryTool,
|
|
83
|
+
}),
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
]
|
|
87
|
+
|
|
88
|
+
return {
|
|
89
|
+
name: '@nocode-factory/sanity-kit',
|
|
90
|
+
|
|
91
|
+
tools: definitions.filter((d) => config.tools[d.name]).map((d) => d.tool),
|
|
92
|
+
|
|
93
|
+
schema: {
|
|
94
|
+
types: kitSchemaTypes,
|
|
95
|
+
// La disposition du board est écrite par l'outil, pas créée à la main.
|
|
96
|
+
templates: (prev) => prev.filter((template) => !isKitType(template.schemaType)),
|
|
97
|
+
},
|
|
98
|
+
|
|
99
|
+
document: {
|
|
100
|
+
newDocumentOptions: (prev) =>
|
|
101
|
+
prev.filter((option) => option.templateId !== BOARD_LAYOUT_TYPE),
|
|
102
|
+
},
|
|
103
|
+
}
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
/* --- API publique --- */
|
|
107
|
+
|
|
108
|
+
export {resolveConfig} from './config/resolve'
|
|
109
|
+
export {validateConfig, issuesForTool, hasBlockingIssue} from './config/validate'
|
|
110
|
+
export {normalizePath} from './config/resolve'
|
|
111
|
+
export {
|
|
112
|
+
DEFAULT_COMPONENT_PREVIEW,
|
|
113
|
+
DEFAULT_I18N,
|
|
114
|
+
DEFAULT_PAGE_BUILDER_FIELD,
|
|
115
|
+
DEFAULT_SLUG_FIELD,
|
|
116
|
+
DEFAULT_TITLE_FIELD,
|
|
117
|
+
} from './config/defaults'
|
|
118
|
+
|
|
119
|
+
export {
|
|
120
|
+
BOARD_LAYOUT_ID,
|
|
121
|
+
BOARD_LAYOUT_TYPE,
|
|
122
|
+
SECTION_NODE_TYPE,
|
|
123
|
+
NODE_FIELD_TYPE,
|
|
124
|
+
COLUMN_TYPE,
|
|
125
|
+
ANNOTATION_TYPE,
|
|
126
|
+
NCF_HIDDEN_TYPES,
|
|
127
|
+
isKitType,
|
|
128
|
+
kitSchemaTypes,
|
|
129
|
+
} from './schema/boardLayout'
|
|
130
|
+
|
|
131
|
+
export {useKitConfig, useKitContext} from './context/KitContext'
|
|
132
|
+
|
|
133
|
+
export {toPath, toUrl, segments, depth, parentPath, pageTypeFor} from './lib/paths'
|
|
134
|
+
export {pageTypesFromSiteRoutes, type SiteRoutesRegistry} from './lib/siteRoutes'
|
|
135
|
+
export {withMoreTool, NCF_MORE_TOOL, DEFAULT_MORE_TOOLS} from './tools/more/MoreTool'
|
|
136
|
+
export {
|
|
137
|
+
buildPageRows,
|
|
138
|
+
formatDocumentValue,
|
|
139
|
+
filterRows,
|
|
140
|
+
localesInRows,
|
|
141
|
+
sortRows,
|
|
142
|
+
} from './lib/pages'
|
|
143
|
+
export {
|
|
144
|
+
collectOccurrences,
|
|
145
|
+
occurrencesForTypes,
|
|
146
|
+
topLevelBlockCount,
|
|
147
|
+
} from './lib/occurrences'
|
|
148
|
+
export {
|
|
149
|
+
blockTypesFromSchema,
|
|
150
|
+
missingPageTypes,
|
|
151
|
+
pageTypesWithoutBuilder,
|
|
152
|
+
} from './lib/schemaTypes'
|
|
153
|
+
export {
|
|
154
|
+
buildPagesQuery,
|
|
155
|
+
buildContentsQuery,
|
|
156
|
+
isSafeFieldPath,
|
|
157
|
+
isSafeColumnKey,
|
|
158
|
+
PROJECTION,
|
|
159
|
+
DATA_PREFIX,
|
|
160
|
+
} from './lib/query'
|
|
161
|
+
export {deriveDocumentColumns, keyForPath} from './lib/schemaColumns'
|
|
162
|
+
export {diffDocuments, diffWords, flatten, isTextChange, summarize} from './lib/diff'
|
|
163
|
+
export {
|
|
164
|
+
presentationPath,
|
|
165
|
+
editIntentPath,
|
|
166
|
+
ncfAllowedOrigins,
|
|
167
|
+
urlOn,
|
|
168
|
+
PERSPECTIVES,
|
|
169
|
+
customEnvironment,
|
|
170
|
+
CUSTOM_ENVIRONMENT_ID,
|
|
171
|
+
} from './lib/studioLinks'
|
|
172
|
+
export type {PreviewPerspective, PresentationOptions} from './lib/studioLinks'
|
|
173
|
+
export {usePreviewTarget} from './context/PreviewTargetContext'
|
|
174
|
+
export {NcfPreviewHeader, ncfPreviewComponents} from './tools/presentation/PreviewHeader'
|
|
175
|
+
export type {PreviewTargetState} from './hooks/usePreviewTarget'
|
|
176
|
+
export type {Change, ChangeKind, Segment} from './lib/diff'
|
|
177
|
+
export {announce, fetchTimeline, fetchDetail} from './lib/service'
|
|
178
|
+
export type {HistoryEvent, HistoryDetail, ProjectStatus, Registration} from './lib/service'
|
|
179
|
+
export {useHistory} from './hooks/useHistory'
|
|
180
|
+
export {useRegistration} from './hooks/useRegistration'
|
|
181
|
+
export {ActorName} from './ui/ActorName'
|
|
182
|
+
export {KIT_VERSION} from './version'
|
|
183
|
+
export type {SchemaFieldLike, SchemaTypeLike, DeriveOptions} from './lib/schemaColumns'
|
|
184
|
+
export {useDocumentColumns} from './hooks/useDocumentColumns'
|
|
185
|
+
export {
|
|
186
|
+
applySavedPositions,
|
|
187
|
+
buildLinkGraph,
|
|
188
|
+
pruneSavedPositions,
|
|
189
|
+
upsertLayout,
|
|
190
|
+
} from './lib/graph'
|
|
191
|
+
export {collectLinks, normalizeInternalPath} from './lib/links'
|
|
192
|
+
export {layoutSectionsMap} from './lib/sectionsMap'
|
|
193
|
+
export {pageThumbnailUrl, slugKey} from './lib/pageThumbnail'
|
|
194
|
+
export {groupPageRows, groupValue, UNSET_LABEL} from './lib/grouping'
|
|
195
|
+
export {colorForRow, colorFromString, resolveColorBy, withAlpha, STATUS_COLORS} from './lib/colors'
|
|
196
|
+
export {useInternalColumns} from './hooks/useInternalColumns'
|
|
197
|
+
export {
|
|
198
|
+
addColumn,
|
|
199
|
+
addColumnOption,
|
|
200
|
+
columnId,
|
|
201
|
+
moveColumn,
|
|
202
|
+
removeColumn,
|
|
203
|
+
renameColumn,
|
|
204
|
+
uniqueColumnId,
|
|
205
|
+
} from './lib/columns'
|
|
206
|
+
export {useAnnotations} from './hooks/useAnnotations'
|
|
207
|
+
export {DEFAULT_INTERNAL_FIELDS} from './config/defaults'
|
|
208
|
+
export {
|
|
209
|
+
BRAIN_NODE_SIZE,
|
|
210
|
+
defaultNodeSize,
|
|
211
|
+
fallbackLayout,
|
|
212
|
+
layoutGraph,
|
|
213
|
+
NODE_HEIGHT,
|
|
214
|
+
NODE_WIDTH,
|
|
215
|
+
} from './lib/elkLayout'
|
|
216
|
+
export {useBoardLayout, BoardLayoutProvider} from './context/BoardLayoutContext'
|
|
217
|
+
export {usePageContents, CONTENT_SCAN_LIMIT} from './hooks/usePageContents'
|
|
218
|
+
export {DEFAULT_BOARD_TAGS} from './config/defaults'
|
|
219
|
+
export {usePages} from './hooks/usePages'
|
|
220
|
+
export {useOccurrences, OCCURRENCE_SCAN_LIMIT} from './hooks/useOccurrences'
|
|
221
|
+
export {useKitClient, KIT_API_VERSION} from './hooks/useKitClient'
|
|
222
|
+
export {toKebabCase, blockName, fillTemplate, humanizeBlockName} from './lib/blocks'
|
|
223
|
+
export {
|
|
224
|
+
mergeVersions,
|
|
225
|
+
baseId,
|
|
226
|
+
isDraftId,
|
|
227
|
+
isVersionId,
|
|
228
|
+
releaseId,
|
|
229
|
+
STATUS_LABELS,
|
|
230
|
+
} from './lib/status'
|
|
231
|
+
|
|
232
|
+
export type {
|
|
233
|
+
ConfigIssue,
|
|
234
|
+
IssueLevel,
|
|
235
|
+
KitContextValue,
|
|
236
|
+
NcfComponentPreviewConfig,
|
|
237
|
+
NcfHistoryConfig,
|
|
238
|
+
NcfI18nConfig,
|
|
239
|
+
NcfKitConfig,
|
|
240
|
+
NcfPageTypeConfig,
|
|
241
|
+
NcfPageTypeInput,
|
|
242
|
+
NcfToolName,
|
|
243
|
+
NcfToolsConfig,
|
|
244
|
+
ResolvedComponentPreview,
|
|
245
|
+
ResolvedI18n,
|
|
246
|
+
ResolvedKitConfig,
|
|
247
|
+
ResolvedPageType,
|
|
248
|
+
NcfDocumentColumn,
|
|
249
|
+
ResolvedDocumentColumn,
|
|
250
|
+
ResolvedHistory,
|
|
251
|
+
NcfEnvironment,
|
|
252
|
+
} from './config/types'
|
|
253
|
+
export type {MergedDoc, PageStatus, VersionedDoc} from './lib/status'
|
|
254
|
+
export type {PageDoc, PageRow, PageFilters, SortKey, SortDirection} from './lib/pages'
|
|
255
|
+
export type {BlockOccurrence, OccurrenceInput} from './lib/occurrences'
|
|
256
|
+
export type {BlockTypeInfo} from './lib/schemaTypes'
|
|
257
|
+
export type {OccurrencePage, OccurrencesState} from './hooks/useOccurrences'
|
|
258
|
+
export type {PagesState} from './hooks/usePages'
|
|
259
|
+
export type {
|
|
260
|
+
BoardEdge,
|
|
261
|
+
BoardGraph,
|
|
262
|
+
BoardNode,
|
|
263
|
+
NodeLayout,
|
|
264
|
+
NodePosition,
|
|
265
|
+
Positioned,
|
|
266
|
+
} from './lib/graph'
|
|
267
|
+
export type {CollectedLinks} from './lib/links'
|
|
268
|
+
export type {LayoutMode} from './lib/elkLayout'
|
|
269
|
+
export type {SectionMapNode, SectionOrder, SectionsMapOptions} from './lib/sectionsMap'
|
|
270
|
+
export type {ThumbnailInput} from './lib/pageThumbnail'
|
|
271
|
+
export type {SectionLayout} from './lib/graph'
|
|
272
|
+
export type {BoardLayoutState} from './hooks/useBoardLayout'
|
|
273
|
+
export type {PageContent, PageContentsState} from './hooks/usePageContents'
|
|
274
|
+
export type {NcfBoardTag, NcfInternalField, NcfInternalFieldOption} from './config/types'
|
|
275
|
+
export type {GroupBy, GroupOptions, RowGroup} from './lib/grouping'
|
|
276
|
+
export type {ColorBy, ColorOptions, RowColor} from './lib/colors'
|
|
277
|
+
export type {Annotation} from './lib/graph'
|
|
278
|
+
export type {AddColumnInput} from './lib/columns'
|
|
279
|
+
export type {AnnotationsState} from './hooks/useAnnotations'
|
|
280
|
+
export type {PathInput} from './lib/paths'
|