@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.
Files changed (95) hide show
  1. package/bin/ncf-kit.mjs +201 -0
  2. package/dist/index.d.ts +1643 -0
  3. package/dist/index.js +6632 -0
  4. package/dist/index.js.map +1 -0
  5. package/dist/web-server.d.ts +46 -0
  6. package/dist/web-server.js +51 -0
  7. package/dist/web-server.js.map +1 -0
  8. package/dist/web.d.ts +27 -0
  9. package/dist/web.js +117 -0
  10. package/dist/web.js.map +1 -0
  11. package/package.json +90 -0
  12. package/scripts/use-client.mjs +20 -0
  13. package/src/config/defaults.ts +74 -0
  14. package/src/config/resolve.ts +153 -0
  15. package/src/config/types.ts +290 -0
  16. package/src/config/validate.ts +193 -0
  17. package/src/context/BoardLayoutContext.tsx +24 -0
  18. package/src/context/KitContext.tsx +30 -0
  19. package/src/context/PreviewTargetContext.tsx +27 -0
  20. package/src/hooks/useAnnotations.ts +60 -0
  21. package/src/hooks/useBoardLayout.ts +398 -0
  22. package/src/hooks/useColumnOptions.ts +47 -0
  23. package/src/hooks/useDocumentColumns.ts +61 -0
  24. package/src/hooks/useHistory.ts +89 -0
  25. package/src/hooks/useInternalColumns.ts +19 -0
  26. package/src/hooks/useKitClient.ts +15 -0
  27. package/src/hooks/useOccurrences.ts +62 -0
  28. package/src/hooks/usePageContents.ts +65 -0
  29. package/src/hooks/usePages.ts +41 -0
  30. package/src/hooks/usePreviewTarget.ts +162 -0
  31. package/src/hooks/useQueryData.ts +103 -0
  32. package/src/hooks/useRegistration.ts +50 -0
  33. package/src/index.ts +280 -0
  34. package/src/lib/blocks.ts +38 -0
  35. package/src/lib/colors.ts +103 -0
  36. package/src/lib/columns.ts +137 -0
  37. package/src/lib/diff.ts +200 -0
  38. package/src/lib/elkLayout.ts +124 -0
  39. package/src/lib/graph.ts +238 -0
  40. package/src/lib/grouping.ts +116 -0
  41. package/src/lib/links.ts +70 -0
  42. package/src/lib/occurrences.ts +93 -0
  43. package/src/lib/pageThumbnail.ts +43 -0
  44. package/src/lib/pages.ts +199 -0
  45. package/src/lib/paths.ts +68 -0
  46. package/src/lib/query.ts +113 -0
  47. package/src/lib/schemaColumns.ts +162 -0
  48. package/src/lib/schemaTypes.ts +75 -0
  49. package/src/lib/sectionsMap.ts +83 -0
  50. package/src/lib/service.ts +88 -0
  51. package/src/lib/siteRoutes.ts +43 -0
  52. package/src/lib/status.ts +122 -0
  53. package/src/lib/studioLinks.ts +137 -0
  54. package/src/schema/boardLayout.ts +190 -0
  55. package/src/tools/components/BlockCard.tsx +79 -0
  56. package/src/tools/components/ComponentsTool.tsx +171 -0
  57. package/src/tools/components/map/SectionNode.tsx +109 -0
  58. package/src/tools/components/map/SectionsMap.tsx +339 -0
  59. package/src/tools/createToolComponent.tsx +67 -0
  60. package/src/tools/history/ChangeRow.tsx +87 -0
  61. package/src/tools/history/DiffPanel.tsx +227 -0
  62. package/src/tools/history/HistoryTool.tsx +230 -0
  63. package/src/tools/more/MoreTool.tsx +82 -0
  64. package/src/tools/pages/ColumnHeader.tsx +216 -0
  65. package/src/tools/pages/ColumnVisibility.tsx +173 -0
  66. package/src/tools/pages/FieldValue.tsx +327 -0
  67. package/src/tools/pages/PagesTable.tsx +394 -0
  68. package/src/tools/pages/PagesTool.tsx +333 -0
  69. package/src/tools/pages/PagesToolbar.tsx +53 -0
  70. package/src/tools/pages/PathCell.tsx +71 -0
  71. package/src/tools/pages/PreviewTarget.tsx +226 -0
  72. package/src/tools/pages/TagCell.tsx +124 -0
  73. package/src/tools/pages/ViewTabs.tsx +57 -0
  74. package/src/tools/pages/board/BoardView.tsx +661 -0
  75. package/src/tools/pages/board/PageNode.tsx +306 -0
  76. package/src/tools/pages/board/PagePanel.tsx +228 -0
  77. package/src/tools/presentation/PreviewHeader.tsx +75 -0
  78. package/src/tools/shared/AnnotationNode.tsx +126 -0
  79. package/src/ui/ActorName.tsx +20 -0
  80. package/src/ui/ConfigIssues.tsx +41 -0
  81. package/src/ui/InlineCode.tsx +22 -0
  82. package/src/ui/Popover.tsx +85 -0
  83. package/src/ui/ProjectStatusNotice.tsx +58 -0
  84. package/src/ui/QueryError.tsx +16 -0
  85. package/src/ui/QuietInput.tsx +64 -0
  86. package/src/ui/StatusBadge.tsx +11 -0
  87. package/src/ui/ToolPlaceholder.tsx +23 -0
  88. package/src/ui/formatDate.ts +14 -0
  89. package/src/ui/tokens.ts +54 -0
  90. package/src/version.ts +5 -0
  91. package/src/web/PreviewBanner.tsx +115 -0
  92. package/src/web/draftModeToggle.ts +56 -0
  93. package/src/web/environment.ts +21 -0
  94. package/src/web/index.ts +15 -0
  95. package/src/web/server.ts +11 -0
@@ -0,0 +1,216 @@
1
+ import {useState} from 'react'
2
+ import {Box, Button, Card, Flex, Stack, Text, TextInput} from '@sanity/ui'
3
+ import {AddIcon} from '@sanity/icons/Add'
4
+ import {ChevronLeftIcon} from '@sanity/icons/ChevronLeft'
5
+ import {ChevronRightIcon} from '@sanity/icons/ChevronRight'
6
+ import {CloseIcon} from '@sanity/icons/Close'
7
+ import {UlistIcon} from '@sanity/icons/Ulist'
8
+ import {InlinePopover} from '../../ui/Popover'
9
+ import {OptionEditor, type OptionHandlers} from './FieldValue'
10
+ import type {NcfInternalField} from '../../config/types'
11
+
12
+ export interface ColumnHeaderProps {
13
+ column: NcfInternalField
14
+ onRename: (id: string, label: string) => void
15
+ onRemove: (id: string) => void
16
+ onMove: (id: string, delta: number) => void
17
+ /** Gestion des choix, pour une colonne de type liste. */
18
+ options: OptionHandlers
19
+ }
20
+
21
+ /** En-tête d'une colonne ajoutée : renommage sur place, ordre, suppression. */
22
+ export function ColumnHeader(props: ColumnHeaderProps) {
23
+ const {column, onRename, onRemove, onMove, options} = props
24
+ const [editing, setEditing] = useState(false)
25
+ const [confirming, setConfirming] = useState(false)
26
+ const [hovered, setHovered] = useState(false)
27
+ const [managing, setManaging] = useState(false)
28
+
29
+ if (editing) {
30
+ return (
31
+ <TextInput
32
+ fontSize={0}
33
+ padding={1}
34
+ radius={2}
35
+ autoFocus
36
+ defaultValue={column.label}
37
+ onBlur={(event) => {
38
+ onRename(column.id, event.currentTarget.value)
39
+ setEditing(false)
40
+ }}
41
+ onKeyDown={(event) => {
42
+ if (event.key === 'Enter') event.currentTarget.blur()
43
+ if (event.key === 'Escape') setEditing(false)
44
+ }}
45
+ />
46
+ )
47
+ }
48
+
49
+ if (confirming) {
50
+ return (
51
+ <Flex align="center" gap={1}>
52
+ <Text size={0} muted>
53
+ Supprimer ?
54
+ </Text>
55
+ <Button
56
+ mode="bleed"
57
+ tone="critical"
58
+ padding={1}
59
+ fontSize={0}
60
+ text="Oui"
61
+ onClick={() => onRemove(column.id)}
62
+ />
63
+ <Button mode="bleed" padding={1} fontSize={0} text="Non" onClick={() => setConfirming(false)} />
64
+ </Flex>
65
+ )
66
+ }
67
+
68
+ return (
69
+ <Flex
70
+ align="center"
71
+ gap={1}
72
+ style={{minWidth: 0}}
73
+ onMouseEnter={() => setHovered(true)}
74
+ onMouseLeave={() => setHovered(false)}
75
+ >
76
+ <Box flex={1} style={{minWidth: 0, cursor: 'text'}} onClick={() => setEditing(true)}>
77
+ <Text size={0} weight="medium" muted textOverflow="ellipsis" title="Cliquer pour renommer">
78
+ {column.label || column.id}
79
+ </Text>
80
+ </Box>
81
+ {/* Les commandes n'apparaissent qu'au survol : un en-tête de tableau
82
+ doit d'abord se lire. */}
83
+ {(hovered || managing) && (
84
+ <Flex gap={0} style={{flex: 'none'}}>
85
+ {/* Une colonne liste se crée vide : c'est ici, et pas dans une
86
+ cellule au hasard, qu'on vient en définir les choix. */}
87
+ {column.type === 'select' && (
88
+ <InlinePopover
89
+ width={240}
90
+ onOpenChange={setManaging}
91
+ trigger={({toggle}) => (
92
+ <Button
93
+ mode="bleed"
94
+ padding={1}
95
+ fontSize={0}
96
+ icon={UlistIcon}
97
+ title="Gérer les choix"
98
+ aria-label="Gérer les choix de la colonne"
99
+ onClick={toggle}
100
+ />
101
+ )}
102
+ >
103
+ {() => <OptionEditor options={column.options ?? []} {...options} />}
104
+ </InlinePopover>
105
+ )}
106
+ <Button
107
+ mode="bleed"
108
+ padding={1}
109
+ fontSize={0}
110
+ icon={ChevronLeftIcon}
111
+ title="Déplacer à gauche"
112
+ aria-label="Déplacer la colonne à gauche"
113
+ onClick={() => onMove(column.id, -1)}
114
+ />
115
+ <Button
116
+ mode="bleed"
117
+ padding={1}
118
+ fontSize={0}
119
+ icon={ChevronRightIcon}
120
+ title="Déplacer à droite"
121
+ aria-label="Déplacer la colonne à droite"
122
+ onClick={() => onMove(column.id, 1)}
123
+ />
124
+ <Button
125
+ mode="bleed"
126
+ padding={1}
127
+ fontSize={0}
128
+ icon={CloseIcon}
129
+ tone="critical"
130
+ title="Supprimer la colonne"
131
+ aria-label="Supprimer la colonne"
132
+ onClick={() => setConfirming(true)}
133
+ />
134
+ </Flex>
135
+ )}
136
+ </Flex>
137
+ )
138
+ }
139
+
140
+ export interface AddColumnProps {
141
+ onAdd: (label: string, type: 'text' | 'select') => void
142
+ }
143
+
144
+ /** Création d'une colonne, à la manière d'un tableur. */
145
+ export function AddColumn(props: AddColumnProps) {
146
+ const [open, setOpen] = useState(false)
147
+ const [label, setLabel] = useState('')
148
+ const [type, setType] = useState<'text' | 'select'>('text')
149
+
150
+ if (!open) {
151
+ return (
152
+ <Button
153
+ mode="bleed"
154
+ padding={1}
155
+ fontSize={0}
156
+ icon={AddIcon}
157
+ text="Colonne"
158
+ onClick={() => setOpen(true)}
159
+ />
160
+ )
161
+ }
162
+
163
+ const submit = () => {
164
+ if (label.trim()) props.onAdd(label, type)
165
+ setLabel('')
166
+ setType('text')
167
+ setOpen(false)
168
+ }
169
+
170
+ return (
171
+ <Card padding={2} radius={2} shadow={2} style={{position: 'relative', zIndex: 3}}>
172
+ <Stack gap={2}>
173
+ <TextInput
174
+ fontSize={0}
175
+ padding={1}
176
+ radius={2}
177
+ autoFocus
178
+ placeholder="Nom de la colonne"
179
+ value={label}
180
+ onChange={(event) => setLabel(event.currentTarget.value)}
181
+ onKeyDown={(event) => {
182
+ if (event.key === 'Enter') submit()
183
+ if (event.key === 'Escape') setOpen(false)
184
+ }}
185
+ />
186
+ <Flex gap={1}>
187
+ <Button
188
+ mode={type === 'text' ? 'default' : 'bleed'}
189
+ padding={1}
190
+ fontSize={0}
191
+ text="Texte"
192
+ onClick={() => setType('text')}
193
+ />
194
+ <Button
195
+ mode={type === 'select' ? 'default' : 'bleed'}
196
+ padding={1}
197
+ fontSize={0}
198
+ text="Liste"
199
+ onClick={() => setType('select')}
200
+ />
201
+ </Flex>
202
+ <Flex gap={1}>
203
+ <Button
204
+ mode="default"
205
+ tone="primary"
206
+ padding={1}
207
+ fontSize={0}
208
+ text="Ajouter"
209
+ onClick={submit}
210
+ />
211
+ <Button mode="bleed" padding={1} fontSize={0} text="Annuler" onClick={() => setOpen(false)} />
212
+ </Flex>
213
+ </Stack>
214
+ </Card>
215
+ )
216
+ }
@@ -0,0 +1,173 @@
1
+ import {useMemo, useState} from 'react'
2
+ import {Box, Button, Flex, Stack, Text} from '@sanity/ui'
3
+ import {CheckmarkIcon} from '@sanity/icons/Checkmark'
4
+ import {ControlsIcon} from '@sanity/icons/Controls'
5
+ import {InlinePopover} from '../../ui/Popover'
6
+ import {BORDER} from '../../ui/tokens'
7
+ import type {ResolvedDocumentColumn} from '../../config/types'
8
+
9
+ /** Colonnes qu'on peut masquer. Titre et chemin restent toujours visibles. */
10
+ export const HIDEABLE = [
11
+ {key: 'status', label: 'Statut'},
12
+ {key: 'blockCount', label: 'Blocs'},
13
+ {key: 'updatedAt', label: 'Modifiée'},
14
+ {key: 'tag', label: 'Étiquette'},
15
+ ] as const
16
+
17
+ export type HideableKey = (typeof HIDEABLE)[number]['key']
18
+
19
+ export interface ColumnVisibilityProps {
20
+ hidden: ReadonlySet<string>
21
+ onToggle: (key: HideableKey) => void
22
+ /** Données du document déclarées en configuration. */
23
+ dataColumns: readonly ResolvedDocumentColumn[]
24
+ /** Celles qu'on a demandé à voir. Vide au départ, contrairement aux autres. */
25
+ shownData: ReadonlySet<string>
26
+ onToggleData: (key: string) => void
27
+ }
28
+
29
+ /** Choix des colonnes affichées, sans quitter le tableau. */
30
+ export function ColumnVisibility(props: ColumnVisibilityProps) {
31
+ const {hidden, onToggle, dataColumns, shownData, onToggleData} = props
32
+ const hiddenCount = HIDEABLE.filter((column) => hidden.has(column.key)).length
33
+ const shownCount = dataColumns.filter((column) => shownData.has(column.key)).length
34
+ const label =
35
+ hiddenCount > 0
36
+ ? `Colonnes · ${hiddenCount} masquée${hiddenCount > 1 ? 's' : ''}`
37
+ : shownCount > 0
38
+ ? `Colonnes · +${shownCount}`
39
+ : 'Colonnes'
40
+
41
+ return (
42
+ <InlinePopover
43
+ width={264}
44
+ trigger={({toggle}) => (
45
+ <Button
46
+ mode="bleed"
47
+ fontSize={1}
48
+ padding={2}
49
+ icon={ControlsIcon}
50
+ text={label}
51
+ onClick={toggle}
52
+ />
53
+ )}
54
+ >
55
+ {() => (
56
+ <DataColumnList
57
+ hidden={hidden}
58
+ onToggle={onToggle}
59
+ dataColumns={dataColumns}
60
+ shownData={shownData}
61
+ onToggleData={onToggleData}
62
+ />
63
+ )}
64
+ </InlinePopover>
65
+ )
66
+ }
67
+
68
+ function Row(props: {label: string; checked: boolean; onClick: () => void}) {
69
+ return (
70
+ <Flex
71
+ align="center"
72
+ gap={2}
73
+ padding={2}
74
+ onClick={props.onClick}
75
+ style={{cursor: 'pointer', borderRadius: 3}}
76
+ onMouseEnter={(event) => {
77
+ event.currentTarget.style.background =
78
+ 'var(--card-muted-bg-color, rgba(127,127,127,0.12))'
79
+ }}
80
+ onMouseLeave={(event) => {
81
+ event.currentTarget.style.background = 'transparent'
82
+ }}
83
+ >
84
+ <Box style={{width: 14, flex: 'none'}}>
85
+ {props.checked && <CheckmarkIcon style={{fontSize: 14, opacity: 0.7}} />}
86
+ </Box>
87
+ <Text size={1} muted={!props.checked} textOverflow="ellipsis">
88
+ {props.label}
89
+ </Text>
90
+ </Flex>
91
+ )
92
+ }
93
+
94
+ /**
95
+ * Le schéma d'un client peut porter des dizaines de champs. Au-delà d'une
96
+ * poignée, on cherche un nom plutôt qu'on ne parcourt une liste : le filtre
97
+ * apparaît quand il commence à servir.
98
+ */
99
+ function DataColumnList(props: ColumnVisibilityProps) {
100
+ const {hidden, onToggle, dataColumns, shownData, onToggleData} = props
101
+ const [filter, setFilter] = useState('')
102
+
103
+ const matches = useMemo(() => {
104
+ const needle = filter.trim().toLowerCase()
105
+ if (!needle) return dataColumns
106
+ return dataColumns.filter((column) => column.label.toLowerCase().includes(needle))
107
+ }, [dataColumns, filter])
108
+
109
+ return (
110
+ <Stack gap={0}>
111
+ {HIDEABLE.map((column) => (
112
+ <Row
113
+ key={column.key}
114
+ label={column.label}
115
+ checked={!hidden.has(column.key)}
116
+ onClick={() => onToggle(column.key)}
117
+ />
118
+ ))}
119
+
120
+ {/* Les données du document viennent du contenu, pas du suivi : les
121
+ mélanger aux colonnes de gestion ferait croire qu'on peut les
122
+ écrire ici. */}
123
+ {dataColumns.length > 0 && (
124
+ <>
125
+ <Box paddingX={2} paddingTop={3} paddingBottom={1}>
126
+ <Text size={0} weight="semibold" muted>
127
+ Données de la page
128
+ </Text>
129
+ </Box>
130
+
131
+ {dataColumns.length > 8 && (
132
+ <Box paddingX={1} paddingBottom={1}>
133
+ <input
134
+ type="text"
135
+ value={filter}
136
+ placeholder="Filtrer…"
137
+ onChange={(event) => setFilter(event.currentTarget.value)}
138
+ style={{
139
+ width: '100%',
140
+ font: 'inherit',
141
+ fontSize: 12,
142
+ padding: '6px',
143
+ borderRadius: 3,
144
+ border: `1px solid ${BORDER}`,
145
+ background: 'var(--card-bg-color)',
146
+ color: 'var(--card-fg-color)',
147
+ outline: 'none',
148
+ }}
149
+ />
150
+ </Box>
151
+ )}
152
+
153
+ {matches.map((column) => (
154
+ <Row
155
+ key={column.key}
156
+ label={column.label}
157
+ checked={shownData.has(column.key)}
158
+ onClick={() => onToggleData(column.key)}
159
+ />
160
+ ))}
161
+
162
+ {matches.length === 0 && (
163
+ <Box padding={2}>
164
+ <Text size={0} muted>
165
+ Aucun champ ne correspond.
166
+ </Text>
167
+ </Box>
168
+ )}
169
+ </>
170
+ )}
171
+ </Stack>
172
+ )
173
+ }
@@ -0,0 +1,327 @@
1
+ import {useState} from 'react'
2
+ import {Box, Button, Flex, Stack, Text} from '@sanity/ui'
3
+ import {AddIcon} from '@sanity/icons/Add'
4
+ import {CheckmarkIcon} from '@sanity/icons/Checkmark'
5
+ import {CloseIcon} from '@sanity/icons/Close'
6
+ import {InlinePopover} from '../../ui/Popover'
7
+ import {QuietInput} from '../../ui/QuietInput'
8
+ import {BORDER, CELL, OPTION_COLORS} from '../../ui/tokens'
9
+ import type {NcfInternalField, NcfInternalFieldOption} from '../../config/types'
10
+
11
+ export interface OptionHandlers {
12
+ onAddOption: (label: string) => void
13
+ onRemoveOption: (optionId: string) => void
14
+ onOptionColor: (optionId: string, color: string | null) => void
15
+ }
16
+
17
+ export interface FieldValueProps extends OptionHandlers {
18
+ column: NcfInternalField
19
+ value: string
20
+ onChange: (value: string) => void
21
+ /** Hors tableau, le champ montre son cadre au repos. */
22
+ bordered?: boolean
23
+ }
24
+
25
+ /**
26
+ * Valeur d'une colonne de gestion interne, éditable sur place.
27
+ *
28
+ * Partagée par le tableau et par la fiche du board : une même donnée doit
29
+ * s'éditer de la même façon partout, sinon on apprend deux interfaces.
30
+ */
31
+ export function FieldValue(props: FieldValueProps) {
32
+ const {column, value, onChange, bordered, ...options} = props
33
+
34
+ if (column.type !== 'select') {
35
+ return <QuietInput value={value} bordered={bordered} onCommit={onChange} />
36
+ }
37
+
38
+ const current = (column.options ?? []).find((option) => option.id === value)
39
+
40
+ return (
41
+ <InlinePopover
42
+ width={240}
43
+ trigger={({toggle}) => (
44
+ <Flex
45
+ align="center"
46
+ gap={2}
47
+ onClick={toggle}
48
+ padding={bordered ? 2 : 0}
49
+ style={{
50
+ ...CELL,
51
+ cursor: 'pointer',
52
+ minHeight: 24,
53
+ borderRadius: 3,
54
+ border: bordered ? `1px solid ${BORDER}` : undefined,
55
+ background: bordered ? 'var(--card-bg-color)' : undefined,
56
+ }}
57
+ title={`Changer ${(column.label || column.id).toLowerCase()}`}
58
+ >
59
+ {current ? (
60
+ <>
61
+ {current.color && <Dot color={current.color} />}
62
+ <Text size={0} textOverflow="ellipsis">
63
+ {current.label}
64
+ </Text>
65
+ </>
66
+ ) : (
67
+ <Text size={0} muted>
68
+ —
69
+ </Text>
70
+ )}
71
+ </Flex>
72
+ )}
73
+ >
74
+ {({close}) => (
75
+ <OptionEditor
76
+ options={column.options ?? []}
77
+ selectedId={value}
78
+ onSelect={(next) => {
79
+ onChange(next)
80
+ close()
81
+ }}
82
+ {...options}
83
+ />
84
+ )}
85
+ </InlinePopover>
86
+ )
87
+ }
88
+
89
+ export interface OptionEditorProps extends OptionHandlers {
90
+ options: readonly NcfInternalFieldOption[]
91
+ /** Absent hors d'une cellule : on gère alors les choix sans en retenir un. */
92
+ selectedId?: string
93
+ onSelect?: (optionId: string) => void
94
+ }
95
+
96
+ /**
97
+ * Liste des choix d'une colonne : en retenir un, en ajouter, en supprimer, les
98
+ * colorer.
99
+ *
100
+ * Le même panneau sert dans une cellule et dans l'en-tête de colonne. Sans ça,
101
+ * définir les choix d'une nouvelle colonne obligerait à ouvrir une cellule au
102
+ * hasard, alors que c'est l'en-tête qu'on regarde en créant la colonne.
103
+ */
104
+ export function OptionEditor(props: OptionEditorProps) {
105
+ const {options, selectedId, onSelect, onAddOption, onRemoveOption, onOptionColor} = props
106
+
107
+ return (
108
+ <Stack gap={1}>
109
+ {onSelect && (
110
+ <Option
111
+ label="Aucune valeur"
112
+ selected={!options.some((option) => option.id === selectedId)}
113
+ onSelect={() => onSelect('')}
114
+ />
115
+ )}
116
+
117
+ {options.length === 0 && !onSelect && (
118
+ <Box padding={2}>
119
+ <Text size={0} muted>
120
+ Aucun choix pour l'instant.
121
+ </Text>
122
+ </Box>
123
+ )}
124
+
125
+ {options.map((option) => (
126
+ <Option
127
+ key={option.id}
128
+ label={option.label}
129
+ color={option.color}
130
+ selected={option.id === selectedId}
131
+ onSelect={onSelect ? () => onSelect(option.id) : undefined}
132
+ onRemove={() => {
133
+ // La page ouverte ne doit pas garder une valeur qui n'existe plus.
134
+ if (onSelect && option.id === selectedId) onSelect('')
135
+ onRemoveOption(option.id)
136
+ }}
137
+ onColor={(color) => onOptionColor(option.id, color)}
138
+ />
139
+ ))}
140
+
141
+ <NewOption onAdd={onAddOption} />
142
+ </Stack>
143
+ )
144
+ }
145
+
146
+ function Dot(props: {color: string}) {
147
+ return (
148
+ <Box
149
+ style={{
150
+ width: 6,
151
+ height: 6,
152
+ borderRadius: '50%',
153
+ background: props.color,
154
+ flex: 'none',
155
+ }}
156
+ />
157
+ )
158
+ }
159
+
160
+ function Option(props: {
161
+ label: string
162
+ color?: string
163
+ selected: boolean
164
+ onSelect?: () => void
165
+ onRemove?: () => void
166
+ onColor?: (color: string | null) => void
167
+ }) {
168
+ const [hovered, setHovered] = useState(false)
169
+ const [picking, setPicking] = useState(false)
170
+
171
+ return (
172
+ <Stack gap={1}>
173
+ <Flex
174
+ align="center"
175
+ gap={2}
176
+ padding={2}
177
+ onMouseEnter={() => setHovered(true)}
178
+ onMouseLeave={() => setHovered(false)}
179
+ style={{
180
+ borderRadius: 3,
181
+ background: hovered
182
+ ? 'var(--card-muted-bg-color, rgba(127,127,127,0.12))'
183
+ : 'transparent',
184
+ }}
185
+ >
186
+ <Box
187
+ onClick={() => props.onColor && setPicking((value) => !value)}
188
+ title={props.onColor ? 'Couleur' : undefined}
189
+ style={{
190
+ width: 10,
191
+ height: 10,
192
+ borderRadius: '50%',
193
+ flex: 'none',
194
+ background: props.color ?? 'transparent',
195
+ border: props.color ? 'none' : `1px solid ${BORDER}`,
196
+ cursor: props.onColor ? 'pointer' : 'default',
197
+ }}
198
+ />
199
+ <Box
200
+ flex={1}
201
+ style={{minWidth: 0, cursor: props.onSelect ? 'pointer' : 'default'}}
202
+ onClick={props.onSelect}
203
+ >
204
+ <Text size={1} textOverflow="ellipsis">
205
+ {props.label}
206
+ </Text>
207
+ </Box>
208
+ {props.selected && <CheckmarkIcon style={{fontSize: 14, opacity: 0.7}} />}
209
+ {hovered && props.onRemove && (
210
+ <Button
211
+ mode="bleed"
212
+ tone="critical"
213
+ padding={1}
214
+ fontSize={0}
215
+ icon={CloseIcon}
216
+ title="Supprimer ce choix"
217
+ aria-label="Supprimer ce choix"
218
+ onClick={props.onRemove}
219
+ />
220
+ )}
221
+ </Flex>
222
+
223
+ {picking && props.onColor && (
224
+ <Flex gap={1} paddingX={2} paddingBottom={2} wrap="wrap" align="center">
225
+ {OPTION_COLORS.map((color) => (
226
+ <Box
227
+ key={color}
228
+ onClick={() => {
229
+ props.onColor?.(color)
230
+ setPicking(false)
231
+ }}
232
+ style={{
233
+ width: 14,
234
+ height: 14,
235
+ borderRadius: '50%',
236
+ background: color,
237
+ cursor: 'pointer',
238
+ outline: color === props.color ? '2px solid var(--card-fg-color)' : 'none',
239
+ }}
240
+ />
241
+ ))}
242
+ <Button
243
+ mode="bleed"
244
+ padding={1}
245
+ fontSize={0}
246
+ text="Aucune"
247
+ onClick={() => {
248
+ props.onColor?.(null)
249
+ setPicking(false)
250
+ }}
251
+ />
252
+ </Flex>
253
+ )}
254
+ </Stack>
255
+ )
256
+ }
257
+
258
+ /**
259
+ * Ajout d'un choix.
260
+ *
261
+ * Le bouton d'abord, le champ ensuite : un champ vide en permanence au bas
262
+ * d'une liste se lit comme un choix existant, et on ne sait pas qu'il attend
263
+ * une validation.
264
+ */
265
+ function NewOption(props: {onAdd: (label: string) => void}) {
266
+ const [open, setOpen] = useState(false)
267
+ const [label, setLabel] = useState('')
268
+
269
+ const submit = () => {
270
+ if (label.trim()) props.onAdd(label)
271
+ setLabel('')
272
+ setOpen(false)
273
+ }
274
+
275
+ if (!open) {
276
+ return (
277
+ <Button
278
+ mode="bleed"
279
+ padding={2}
280
+ fontSize={1}
281
+ icon={AddIcon}
282
+ text="Ajouter un choix"
283
+ style={{width: '100%', justifyContent: 'flex-start'}}
284
+ onClick={() => setOpen(true)}
285
+ />
286
+ )
287
+ }
288
+
289
+ return (
290
+ <Flex gap={1} padding={1} align="center">
291
+ <input
292
+ type="text"
293
+ autoFocus
294
+ value={label}
295
+ placeholder="Nom du choix"
296
+ onChange={(event) => setLabel(event.currentTarget.value)}
297
+ onKeyDown={(event) => {
298
+ if (event.key === 'Enter') submit()
299
+ if (event.key === 'Escape') {
300
+ setLabel('')
301
+ setOpen(false)
302
+ }
303
+ }}
304
+ style={{
305
+ flex: 1,
306
+ minWidth: 0,
307
+ font: 'inherit',
308
+ fontSize: 12,
309
+ padding: '6px',
310
+ borderRadius: 3,
311
+ border: `1px solid ${BORDER}`,
312
+ background: 'var(--card-bg-color)',
313
+ color: 'var(--card-fg-color)',
314
+ outline: 'none',
315
+ }}
316
+ />
317
+ <Button
318
+ mode="default"
319
+ tone="primary"
320
+ padding={2}
321
+ fontSize={0}
322
+ text="OK"
323
+ onClick={submit}
324
+ />
325
+ </Flex>
326
+ )
327
+ }