@nocode-factory/sanity-kit 2.50.0 → 2.51.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.
@@ -1,16 +1,23 @@
1
1
  import {useMemo, useState} from 'react'
2
- import {Box, Button, Card, Flex, Grid, Spinner, Stack, Text} from '@sanity/ui'
2
+ import {Box, Button, Card, Dialog, Flex, Grid, Spinner, Stack, Text, TextInput} from '@sanity/ui'
3
+ import {EditIcon} from '@sanity/icons/Edit'
4
+ import {TrashIcon} from '@sanity/icons/Trash'
3
5
  import {useKitConfig} from '../../context/KitContext'
4
6
  import {useOccurrences, OCCURRENCE_SCAN_LIMIT} from '../../hooks/useOccurrences'
5
7
  import {fillTemplate} from '../../lib/blocks'
8
+ import {groupComponents, withCustomNames, type ComponentGroup} from '../../lib/componentMeta'
6
9
  import {useOpenComponent} from '../../hooks/useOpenComponent'
10
+ import {ComponentMetaProvider, useComponentMeta} from '../../context/ComponentMetaContext'
7
11
  import {QueryError} from '../../ui/QueryError'
8
12
  import {BlockCard} from './BlockCard'
13
+ import {ComponentEditDialog} from './ComponentEditDialog'
9
14
  import {SectionsMap} from './map/SectionsMap'
15
+ import type {BlockOccurrence} from '../../lib/occurrences'
16
+ import type {BlockTypeInfo} from '../../lib/schemaTypes'
10
17
 
11
18
  type View = 'grid' | 'map'
12
-
13
19
  type Order = 'type' | 'schema' | 'usage' | 'unused'
20
+ type Row = {occurrence: BlockOccurrence; block: BlockTypeInfo}
14
21
 
15
22
  const ORDERS: {value: Order; label: string}[] = [
16
23
  {value: 'type', label: 'By type'},
@@ -19,39 +26,60 @@ const ORDERS: {value: Order; label: string}[] = [
19
26
  {value: 'schema', label: 'Schema order'},
20
27
  ]
21
28
 
29
+ /** Outil Composants, sous son provider d'organisation (groupes + noms). */
22
30
  export function ComponentsTool() {
31
+ return (
32
+ <ComponentMetaProvider>
33
+ <ComponentsToolInner />
34
+ </ComponentMetaProvider>
35
+ )
36
+ }
37
+
38
+ function ComponentsToolInner() {
23
39
  const config = useKitConfig()
24
- const {blockTypes, occurrences, pagesById, truncated, scanned, loading, error, refresh} =
25
- useOccurrences()
40
+ const meta = useComponentMeta()
41
+ const {blockTypes, occurrences, pagesById, truncated, scanned, loading, error, refresh} = useOccurrences()
26
42
  const [order, setOrder] = useState<Order>('type')
27
43
  const [view, setView] = useState<View>('grid')
44
+ const [grouped, setGrouped] = useState(false)
45
+ const [editing, setEditing] = useState<string | null>(null)
46
+ const [groupDialog, setGroupDialog] = useState<{mode: 'create' | 'rename'; id?: string; name: string} | null>(null)
47
+ const [deletingGroup, setDeletingGroup] = useState<ComponentGroup | null>(null)
28
48
 
29
49
  const preview = config.componentPreview
30
50
  const canPreview = preview.provider === 'designSystemPage' && Boolean(config.previewUrl)
31
51
  const openComponent = useOpenComponent()
32
52
 
33
- const cards = useMemo(() => {
34
- const byName = new Map(blockTypes.map((b) => [b.name, b]))
53
+ // Noms marketing appliqués au titre ; le type (block.name) reste la clé technique.
54
+ const named = useMemo(() => withCustomNames(blockTypes, meta.names), [blockTypes, meta.names])
55
+
56
+ const cards = useMemo<Row[]>(() => {
57
+ const byName = new Map(named.map((b) => [b.name, b]))
35
58
  const rows = occurrences
36
59
  .map((occurrence) => ({occurrence, block: byName.get(occurrence.type)}))
37
- .filter((row): row is {occurrence: (typeof occurrences)[number]; block: (typeof blockTypes)[number]} =>
38
- Boolean(row.block),
39
- )
40
-
60
+ .filter((row): row is Row => Boolean(row.block))
41
61
  if (order === 'schema') return rows
42
- if (order === 'type') {
43
- return [...rows].sort((a, b) => a.block.name.localeCompare(b.block.name, undefined, {sensitivity: 'base'}))
44
- }
45
- if (order === 'unused') {
46
- return [...rows].sort((a, b) => a.occurrence.count - b.occurrence.count)
47
- }
62
+ if (order === 'type') return [...rows].sort((a, b) => a.block.title.localeCompare(b.block.title, 'fr', {sensitivity: 'base'}))
63
+ if (order === 'unused') return [...rows].sort((a, b) => a.occurrence.count - b.occurrence.count)
48
64
  return [...rows].sort((a, b) => b.occurrence.count - a.occurrence.count)
49
- }, [occurrences, blockTypes, order])
65
+ }, [occurrences, named, order])
50
66
 
51
67
  const unusedCount = occurrences.filter((o) => o.count === 0).length
68
+ const editingBlock = editing ? named.find((b) => b.name === editing) : undefined
52
69
 
53
- if (error) return <QueryError error={error} onRetry={refresh} />
70
+ const renderCard = (card: Row) => (
71
+ <BlockCard
72
+ key={card.block.name}
73
+ block={card.block}
74
+ occurrence={card.occurrence}
75
+ namespace={config.blockNamespace}
76
+ thumbnailUrl={canPreview ? `${config.previewUrl}${fillTemplate(preview.thumbnail, card.block.name, config.blockNamespace)}` : null}
77
+ onOpen={openComponent ? () => openComponent(card.block.name) : undefined}
78
+ onEdit={() => setEditing(card.block.name)}
79
+ />
80
+ )
54
81
 
82
+ if (error) return <QueryError error={error} onRetry={refresh} />
55
83
  if (loading && blockTypes.length === 0) {
56
84
  return (
57
85
  <Flex align="center" justify="center" padding={5} gap={3}>
@@ -62,39 +90,40 @@ export function ComponentsTool() {
62
90
  </Flex>
63
91
  )
64
92
  }
65
-
66
93
  if (blockTypes.length === 0) {
67
94
  return (
68
95
  <Card padding={4} radius={3} shadow={1} tone="caution">
69
96
  <Stack gap={3}>
70
97
  <Text weight="semibold">No blocks found.</Text>
71
98
  <Text size={1}>
72
- The field "{config.pageBuilderField}" does not exist on any of the types{' '}
73
- {config.pageTypeNames.join(', ')}, or it is not an array. Check
74
- `pageBuilderField` in the ncfKit() call.
99
+ The field "{config.pageBuilderField}" does not exist on any of the types {config.pageTypeNames.join(', ')}, or it is not an array.
75
100
  </Text>
76
101
  </Stack>
77
102
  </Card>
78
103
  )
79
104
  }
80
105
 
106
+ const grouping = grouped ? groupComponents(cards.map((c) => c.block), meta.groupOf, meta.groups) : null
107
+ const cardByName = new Map(cards.map((c) => [c.block.name, c]))
108
+
81
109
  return (
82
110
  <Stack gap={4}>
83
- <Flex gap={2}>
84
- <Button
85
- text="Grid"
86
- mode={view === 'grid' ? 'default' : 'ghost'}
87
- fontSize={1}
88
- padding={2}
89
- onClick={() => setView('grid')}
90
- />
91
- <Button
92
- text="Map"
93
- mode={view === 'map' ? 'default' : 'ghost'}
94
- fontSize={1}
95
- padding={2}
96
- onClick={() => setView('map')}
97
- />
111
+ <Flex gap={2} align="center" wrap="wrap">
112
+ <Button text="Grid" mode={view === 'grid' ? 'default' : 'ghost'} fontSize={1} padding={2} onClick={() => setView('grid')} />
113
+ <Button text="Map" mode={view === 'map' ? 'default' : 'ghost'} fontSize={1} padding={2} onClick={() => setView('map')} />
114
+ {view === 'grid' ? (
115
+ <Button
116
+ text="By group"
117
+ mode={grouped ? 'default' : 'ghost'}
118
+ tone={grouped ? 'primary' : 'default'}
119
+ fontSize={1}
120
+ padding={2}
121
+ onClick={() => setGrouped((value) => !value)}
122
+ />
123
+ ) : null}
124
+ {grouped && view === 'grid' ? (
125
+ <Button text="New group" mode="ghost" fontSize={1} padding={2} onClick={() => setGroupDialog({mode: 'create', name: ''})} />
126
+ ) : null}
98
127
  </Flex>
99
128
 
100
129
  <Flex align="center" justify="space-between" gap={3} wrap="wrap">
@@ -102,73 +131,155 @@ export function ComponentsTool() {
102
131
  {blockTypes.length} blocks · {scanned} pages analyzed
103
132
  {unusedCount > 0 ? ` · ${unusedCount} never used` : ''}
104
133
  </Text>
105
-
106
134
  <Flex gap={2} wrap="wrap">
107
135
  {ORDERS.filter((option) => view === 'grid' || option.value !== 'schema').map((option) => (
108
- <Button
109
- key={option.value}
110
- mode={order === option.value ? 'default' : 'ghost'}
111
- fontSize={1}
112
- padding={2}
113
- text={option.label}
114
- onClick={() => setOrder(option.value)}
115
- />
136
+ <Button key={option.value} mode={order === option.value ? 'default' : 'ghost'} fontSize={1} padding={2} text={option.label} onClick={() => setOrder(option.value)} />
116
137
  ))}
117
- <Button
118
- text="Refresh"
119
- mode="ghost"
120
- fontSize={1}
121
- padding={2}
122
- onClick={refresh}
123
- disabled={loading}
124
- />
138
+ <Button text="Refresh" mode="ghost" fontSize={1} padding={2} onClick={refresh} disabled={loading} />
125
139
  </Flex>
126
140
  </Flex>
127
141
 
128
- {/* Un comptage tronqué est un chiffre faux : il faut le dire. */}
129
142
  {truncated && (
130
143
  <Card padding={3} radius={2} tone="caution">
131
- <Text size={1}>
132
- Analysis limited to the first {OCCURRENCE_SCAN_LIMIT} pages: the numbers
133
- below are a minimum, not a total.
134
- </Text>
144
+ <Text size={1}>Analysis limited to the first {OCCURRENCE_SCAN_LIMIT} pages: the numbers below are a minimum, not a total.</Text>
135
145
  </Card>
136
146
  )}
137
-
138
147
  {!canPreview && (
139
148
  <Card padding={3} radius={2} tone="transparent" border>
140
149
  <Text size={1} muted>
141
- Thumbnails disabled: set `previewUrl` in ncfKit() to show the design system
142
- screenshots.
150
+ Thumbnails disabled: set `previewUrl` in ncfKit() to show the design system screenshots.
143
151
  </Text>
144
152
  </Card>
145
153
  )}
146
154
 
147
155
  {view === 'map' ? (
148
- <SectionsMap
149
- blockTypes={blockTypes}
150
- occurrences={occurrences}
151
- pagesById={pagesById}
152
- order={order === 'schema' ? 'type' : order}
153
- />
156
+ <SectionsMap blockTypes={named} occurrences={occurrences} pagesById={pagesById} order={order === 'schema' ? 'type' : order} />
157
+ ) : grouping ? (
158
+ <Stack gap={5}>
159
+ {grouping.grouped.map(({group, blocks}) => (
160
+ <Stack key={group.id} gap={3}>
161
+ <Flex align="center" gap={2}>
162
+ <Text size={2} weight="semibold">
163
+ {group.name}
164
+ </Text>
165
+ <Text size={1} muted>
166
+ {blocks.length}
167
+ </Text>
168
+ <Box flex={1} />
169
+ <Button icon={EditIcon} mode="bleed" fontSize={1} padding={2} title="Renommer le groupe" aria-label="Renommer le groupe" onClick={() => setGroupDialog({mode: 'rename', id: group.id, name: group.name})} />
170
+ <Button icon={TrashIcon} mode="bleed" tone="critical" fontSize={1} padding={2} title="Supprimer le groupe" aria-label="Supprimer le groupe" onClick={() => setDeletingGroup(group)} />
171
+ </Flex>
172
+ {blocks.length ? (
173
+ <Grid gridTemplateColumns={[1, 2, 3, 4]} gap={3}>
174
+ {blocks.map((block) => cardByName.get(block.name)).filter((c): c is Row => Boolean(c)).map(renderCard)}
175
+ </Grid>
176
+ ) : (
177
+ <Text size={1} muted>
178
+ Aucun composant dans ce groupe.
179
+ </Text>
180
+ )}
181
+ </Stack>
182
+ ))}
183
+ <Stack gap={3}>
184
+ <Text size={2} weight="semibold" muted>
185
+ Sans groupe
186
+ </Text>
187
+ <Grid gridTemplateColumns={[1, 2, 3, 4]} gap={3}>
188
+ {grouping.ungrouped.map((block) => cardByName.get(block.name)).filter((c): c is Row => Boolean(c)).map(renderCard)}
189
+ </Grid>
190
+ </Stack>
191
+ </Stack>
154
192
  ) : (
155
- <Grid gridTemplateColumns={[1, 2, 3, 4]} gap={3}>
156
- {cards.map(({block, occurrence}) => (
157
- <BlockCard
158
- key={block.name}
159
- block={block}
160
- occurrence={occurrence}
161
- namespace={config.blockNamespace}
162
- thumbnailUrl={
163
- canPreview
164
- ? `${config.previewUrl}${fillTemplate(preview.thumbnail, block.name, config.blockNamespace)}`
165
- : null
166
- }
167
- onOpen={openComponent ? () => openComponent(block.name) : undefined}
168
- />
169
- ))}
170
- </Grid>
193
+ <Grid gridTemplateColumns={[1, 2, 3, 4]} gap={3}>
194
+ {cards.map(renderCard)}
195
+ </Grid>
171
196
  )}
197
+
198
+ {editingBlock ? (
199
+ <ComponentEditDialog
200
+ block={editingBlock}
201
+ namespace={config.blockNamespace}
202
+ currentName={meta.names.get(editingBlock.name) ?? ''}
203
+ currentGroup={meta.groupOf.get(editingBlock.name) ?? null}
204
+ groups={meta.groups}
205
+ onSave={(name, group) => {
206
+ meta.renameComponent(editingBlock.name, name)
207
+ meta.setGroup(editingBlock.name, group)
208
+ }}
209
+ onClose={() => setEditing(null)}
210
+ />
211
+ ) : null}
212
+
213
+ {groupDialog ? (
214
+ <Dialog
215
+ id="component-group-dialog"
216
+ header={groupDialog.mode === 'rename' ? 'Renommer le groupe' : 'Nouveau groupe'}
217
+ onClose={() => setGroupDialog(null)}
218
+ width={0}
219
+ footer={
220
+ <Flex justify="flex-end" gap={2} padding={2}>
221
+ <Button text="Annuler" mode="bleed" onClick={() => setGroupDialog(null)} />
222
+ <Button
223
+ text={groupDialog.mode === 'rename' ? 'Renommer' : 'Créer'}
224
+ tone="primary"
225
+ disabled={!groupDialog.name.trim()}
226
+ onClick={() => {
227
+ if (groupDialog.mode === 'rename' && groupDialog.id) meta.renameGroup(groupDialog.id, groupDialog.name)
228
+ else meta.createGroup(groupDialog.name)
229
+ setGroupDialog(null)
230
+ }}
231
+ />
232
+ </Flex>
233
+ }
234
+ >
235
+ <Box padding={4}>
236
+ <TextInput
237
+ value={groupDialog.name}
238
+ autoFocus
239
+ placeholder="Nom du groupe"
240
+ onChange={(event) => {
241
+ // `currentTarget` n'est lisible que pendant l'événement : on capture
242
+ // la valeur AVANT l'updater d'état (sinon il vaut null → crash).
243
+ const value = event.currentTarget.value
244
+ setGroupDialog((d) => (d ? {...d, name: value} : d))
245
+ }}
246
+ onKeyDown={(event) => {
247
+ if (event.key === 'Enter' && groupDialog.name.trim()) {
248
+ if (groupDialog.mode === 'rename' && groupDialog.id) meta.renameGroup(groupDialog.id, groupDialog.name)
249
+ else meta.createGroup(groupDialog.name)
250
+ setGroupDialog(null)
251
+ }
252
+ }}
253
+ />
254
+ </Box>
255
+ </Dialog>
256
+ ) : null}
257
+
258
+ {deletingGroup ? (
259
+ <Dialog
260
+ id="component-group-delete"
261
+ header="Supprimer le groupe"
262
+ onClose={() => setDeletingGroup(null)}
263
+ width={0}
264
+ footer={
265
+ <Flex justify="flex-end" gap={2} padding={2}>
266
+ <Button text="Annuler" mode="bleed" onClick={() => setDeletingGroup(null)} />
267
+ <Button
268
+ text="Supprimer"
269
+ tone="critical"
270
+ onClick={() => {
271
+ meta.deleteGroup(deletingGroup.id)
272
+ setDeletingGroup(null)
273
+ }}
274
+ />
275
+ </Flex>
276
+ }
277
+ >
278
+ <Box padding={4}>
279
+ <Text size={1}>Supprimer « {deletingGroup.name} » ? Les composants ne sont pas supprimés : ils repassent « Sans groupe ».</Text>
280
+ </Box>
281
+ </Dialog>
282
+ ) : null}
172
283
  </Stack>
173
284
  )
174
285
  }
package/src/version.ts CHANGED
@@ -3,4 +3,4 @@
3
3
  * quelle version. Recopiée de `package.json` par `npm version` (scripts/sync-version.mjs) ; un test
4
4
  * le vérifie.
5
5
  */
6
- export const KIT_VERSION = '2.50.0'
6
+ export const KIT_VERSION = '2.51.1'