@nocode-factory/sanity-kit 2.54.0 → 2.56.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/README.md +23 -5
- package/dist/index.d.ts +48 -4
- package/dist/index.js +287 -69
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/hooks/useCollection.ts +19 -3
- package/src/lib/collectionQuery.ts +10 -2
- package/src/structure/collections.tsx +35 -5
- package/src/tools/collections/CollectionTable.tsx +257 -78
- package/src/tools/collections/CollectionToolbar.tsx +53 -4
- package/src/version.ts +1 -1
package/package.json
CHANGED
|
@@ -17,12 +17,28 @@ export interface CollectionState {
|
|
|
17
17
|
refresh: () => void
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
+
export interface UseCollectionOptions {
|
|
21
|
+
/** Prédicat GROQ supplémentaire (ex. `language == $lang`) — niveau feuille. */
|
|
22
|
+
filter?: string
|
|
23
|
+
/** Valeurs des paramètres du `filter` (ex. `{lang: 'fr'}`). */
|
|
24
|
+
params?: Record<string, unknown>
|
|
25
|
+
}
|
|
26
|
+
|
|
20
27
|
/**
|
|
21
28
|
* Données d'une collection pour la vue table : colonnes tirées du schéma,
|
|
22
29
|
* lignes en direct. Seules les colonnes visibles sont projetées (comme
|
|
23
30
|
* `usePages`), pour ne pas tirer tout le schéma sur chaque document.
|
|
31
|
+
*
|
|
32
|
+
* `filter`/`params` restreignent la table au niveau feuille (une langue, une
|
|
33
|
+
* catégorie…). `useQueryData` stabilise lui-même les paramètres, donc l'objet
|
|
34
|
+
* peut être reconstruit à chaque rendu sans relancer la requête en boucle.
|
|
24
35
|
*/
|
|
25
|
-
export function useCollection(
|
|
36
|
+
export function useCollection(
|
|
37
|
+
type: string,
|
|
38
|
+
visibleKeys: readonly string[],
|
|
39
|
+
options: UseCollectionOptions = {},
|
|
40
|
+
): CollectionState {
|
|
41
|
+
const {filter, params: extraParams} = options
|
|
26
42
|
const schema = useSchema()
|
|
27
43
|
|
|
28
44
|
const allColumns = useMemo(
|
|
@@ -35,8 +51,8 @@ export function useCollection(type: string, visibleKeys: readonly string[]): Col
|
|
|
35
51
|
[allColumns, visibleKeys],
|
|
36
52
|
)
|
|
37
53
|
|
|
38
|
-
const query = useMemo(() => buildCollectionQuery(visibleColumns), [visibleColumns])
|
|
39
|
-
const params =
|
|
54
|
+
const query = useMemo(() => buildCollectionQuery(visibleColumns, filter), [visibleColumns, filter])
|
|
55
|
+
const params = {type, ...extraParams}
|
|
40
56
|
|
|
41
57
|
const {data, loading, error, refresh} = useQueryData<CollectionDoc[]>(query, params, {
|
|
42
58
|
listenTypes: [type],
|
|
@@ -10,14 +10,22 @@ import type {CollectionColumn} from './collectionColumns'
|
|
|
10
10
|
*
|
|
11
11
|
* Le type passe en paramètre (`$type`), jamais interpolé dans la requête.
|
|
12
12
|
*
|
|
13
|
+
* `filter` est un prédicat GROQ supplémentaire écrit par l'intégrateur (dans le
|
|
14
|
+
* résolveur de structure), par exemple `language == $lang` : il restreint la
|
|
15
|
+
* table au niveau feuille (une langue, une catégorie, une page…). Il est
|
|
16
|
+
* parenthésé puis combiné en `&&`, et ses valeurs passent par `params`, jamais
|
|
17
|
+
* interpolées. Même niveau de confiance que `S.documentList().filter(...)`.
|
|
18
|
+
*
|
|
13
19
|
* On ne filtre pas les brouillons : le client (perspective `raw`) renvoie
|
|
14
20
|
* `drafts.x` ET `x`, que `buildCollectionRows` fusionne par id de base — sans
|
|
15
21
|
* quoi un item jamais publié n'apparaîtrait pas.
|
|
16
22
|
*/
|
|
17
|
-
export function buildCollectionQuery(columns: readonly CollectionColumn[]): string {
|
|
23
|
+
export function buildCollectionQuery(columns: readonly CollectionColumn[], filter?: string): string {
|
|
18
24
|
const lines = columns
|
|
19
25
|
.filter((column) => isSafeColumnKey(column.key))
|
|
20
26
|
.map((column) => ` "${DATA_PREFIX}${column.key}": ${column.projection},`)
|
|
21
27
|
|
|
22
|
-
|
|
28
|
+
const predicate = filter && filter.trim() ? `_type == $type && (${filter.trim()})` : '_type == $type'
|
|
29
|
+
|
|
30
|
+
return [`*[${predicate}]{`, ' _id, _updatedAt,', ...lines, '}'].join('\n')
|
|
23
31
|
}
|
|
@@ -16,11 +16,16 @@ import type {ConfigIssue, NcfKitConfig, ResolvedKitConfig} from '../config/types
|
|
|
16
16
|
*/
|
|
17
17
|
function createCollectionPane(config: ResolvedKitConfig, issues: ConfigIssue[]): UserComponent {
|
|
18
18
|
return function NcfCollectionPane(props) {
|
|
19
|
-
const
|
|
19
|
+
const opts = (props.options ?? {}) as Record<string, unknown>
|
|
20
|
+
const type = typeof opts.type === 'string' ? opts.type : ''
|
|
21
|
+
const filter = typeof opts.filter === 'string' ? opts.filter : undefined
|
|
22
|
+
const params =
|
|
23
|
+
opts.params && typeof opts.params === 'object' ? (opts.params as Record<string, unknown>) : undefined
|
|
24
|
+
const newTemplate = typeof opts.newTemplate === 'string' ? opts.newTemplate : undefined
|
|
20
25
|
return (
|
|
21
26
|
<KitProvider config={config} issues={issues}>
|
|
22
27
|
<Box padding={3} height="fill" overflow="auto">
|
|
23
|
-
{type ? <CollectionTable type={type} /> : null}
|
|
28
|
+
{type ? <CollectionTable type={type} filter={filter} params={params} newTemplate={newTemplate} /> : null}
|
|
24
29
|
</Box>
|
|
25
30
|
</KitProvider>
|
|
26
31
|
)
|
|
@@ -34,20 +39,45 @@ function createCollectionPane(config: ResolvedKitConfig, issues: ConfigIssue[]):
|
|
|
34
39
|
* ```ts
|
|
35
40
|
* S.listItem().title('Articles').child(ncfCollectionTable(S, ncfOptions, 'article'))
|
|
36
41
|
* ```
|
|
42
|
+
*
|
|
43
|
+
* `filter`/`params` posent la table au **niveau feuille** (une langue, une
|
|
44
|
+
* catégorie…) sans mélanger le reste ; `newTemplate` garde la création
|
|
45
|
+
* pré-taguée de ce niveau ; `id` sert quand un même type a plusieurs tables
|
|
46
|
+
* (ex. une par langue), pour des identifiants de structure uniques.
|
|
47
|
+
*
|
|
48
|
+
* ```ts
|
|
49
|
+
* S.documentList() // ancien niveau feuille…
|
|
50
|
+
* ncfCollectionTable(S, ncfOptions, 'newsArticle', {
|
|
51
|
+
* id: `ncf-col-newsArticle-${lang}`,
|
|
52
|
+
* title: `News (${lang})`,
|
|
53
|
+
* filter: 'language == $lang',
|
|
54
|
+
* params: {lang},
|
|
55
|
+
* newTemplate: `newsArticle-${lang}`,
|
|
56
|
+
* })
|
|
57
|
+
* ```
|
|
37
58
|
*/
|
|
38
59
|
export function ncfCollectionTable(
|
|
39
60
|
S: StructureBuilder,
|
|
40
61
|
options: NcfKitConfig,
|
|
41
62
|
type: string,
|
|
42
|
-
opts: {
|
|
63
|
+
opts: {
|
|
64
|
+
title?: string
|
|
65
|
+
id?: string
|
|
66
|
+
filter?: string
|
|
67
|
+
params?: Record<string, unknown>
|
|
68
|
+
newTemplate?: string
|
|
69
|
+
} = {},
|
|
43
70
|
) {
|
|
44
71
|
const config = resolveConfig(options)
|
|
45
72
|
const issues = validateConfig(config)
|
|
46
73
|
const pane = createCollectionPane(config, issues)
|
|
47
74
|
return S.component(pane)
|
|
48
|
-
.id(`ncf-collection-${type}`)
|
|
75
|
+
.id(opts.id ?? `ncf-collection-${type}`)
|
|
49
76
|
.title(opts.title ?? type)
|
|
50
|
-
.options({type})
|
|
77
|
+
.options({type, filter: opts.filter, params: opts.params, newTemplate: opts.newTemplate})
|
|
78
|
+
// Cliquer une ligne ouvre le document comme VOLET ENFANT (à droite) : les
|
|
79
|
+
// volets précédents se réduisent sans se fermer, au lieu de les remplacer.
|
|
80
|
+
.child((childId: string) => S.document().schemaType(type).documentId(childId))
|
|
51
81
|
}
|
|
52
82
|
|
|
53
83
|
/** Libellé d'une entrée de collection : config, puis titre du schéma, puis nom humanisé. */
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import {useCallback, useMemo, useState} from 'react'
|
|
1
|
+
import {useCallback, useMemo, useRef, useState} from 'react'
|
|
2
2
|
import {Badge, Box, Flex, Spinner, Stack, Text} from '@sanity/ui'
|
|
3
3
|
import {ChevronDownIcon} from '@sanity/icons/ChevronDown'
|
|
4
4
|
import {ChevronUpIcon} from '@sanity/icons/ChevronUp'
|
|
5
|
+
import {usePaneRouter} from 'sanity/structure'
|
|
5
6
|
import {useRouter} from 'sanity/router'
|
|
6
7
|
import {useSchema} from 'sanity'
|
|
7
8
|
import {useCollection} from '../../hooks/useCollection'
|
|
@@ -13,10 +14,12 @@ import type {SchemaTypeLike} from '../../lib/schemaColumns'
|
|
|
13
14
|
import type {CollectionRow} from '../../lib/collectionRows'
|
|
14
15
|
import {BORDER, BORDER_SOFT, CELL, TABULAR} from '../../ui/tokens'
|
|
15
16
|
import {QueryError} from '../../ui/QueryError'
|
|
16
|
-
import {CollectionToolbar} from './CollectionToolbar'
|
|
17
|
+
import {CollectionToolbar, type CollectionView} from './CollectionToolbar'
|
|
17
18
|
|
|
18
19
|
/** Colonnes montrées par défaut : le titre d'abord, puis les premières. */
|
|
19
20
|
const DEFAULT_MAX = 6
|
|
21
|
+
/** Largeur minimale d'une colonne au redimensionnement. */
|
|
22
|
+
const MIN_WIDTH = 80
|
|
20
23
|
|
|
21
24
|
function defaultVisible(columns: readonly CollectionColumn[]): string[] {
|
|
22
25
|
const title = columns.find((c) => c.key === 'title') ?? columns.find((c) => c.kind === 'text')
|
|
@@ -24,18 +27,60 @@ function defaultVisible(columns: readonly CollectionColumn[]): string[] {
|
|
|
24
27
|
return ordered.slice(0, DEFAULT_MAX).map((c) => c.key)
|
|
25
28
|
}
|
|
26
29
|
|
|
30
|
+
/** Largeur par défaut d'une colonne : le titre large, le reste selon le type. */
|
|
31
|
+
function defaultWidth(column: CollectionColumn, isFirst: boolean): number {
|
|
32
|
+
if (isFirst) return 320
|
|
33
|
+
switch (column.kind) {
|
|
34
|
+
case 'number':
|
|
35
|
+
return 110
|
|
36
|
+
case 'boolean':
|
|
37
|
+
return 90
|
|
38
|
+
case 'date':
|
|
39
|
+
return 150
|
|
40
|
+
case 'reference':
|
|
41
|
+
return 200
|
|
42
|
+
default:
|
|
43
|
+
return 190
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Déplace `fromKey` à la position de `toKey` dans la liste des clés. */
|
|
48
|
+
function moveKey(keys: string[], fromKey: string, toKey: string): string[] {
|
|
49
|
+
const from = keys.indexOf(fromKey)
|
|
50
|
+
const to = keys.indexOf(toKey)
|
|
51
|
+
if (from < 0 || to < 0 || from === to) return keys
|
|
52
|
+
const next = [...keys]
|
|
53
|
+
next.splice(from, 1)
|
|
54
|
+
next.splice(to, 0, fromKey)
|
|
55
|
+
return next
|
|
56
|
+
}
|
|
57
|
+
|
|
27
58
|
export interface CollectionTableProps {
|
|
28
59
|
type: string
|
|
60
|
+
/** Prédicat GROQ supplémentaire (ex. `language == $lang`) — niveau feuille. */
|
|
61
|
+
filter?: string
|
|
62
|
+
/** Valeurs des paramètres du `filter` (ex. `{lang: 'fr'}`). */
|
|
63
|
+
params?: Record<string, unknown>
|
|
64
|
+
/** Template de création à appliquer au « + » (ex. `article-fr`) : garde la
|
|
65
|
+
* création pré-taguée (langue, catégorie…) du niveau feuille. Sans lui, le
|
|
66
|
+
* « + » crée un document nu du type. */
|
|
67
|
+
newTemplate?: string
|
|
29
68
|
}
|
|
30
69
|
|
|
31
|
-
export function CollectionTable({type}: CollectionTableProps) {
|
|
70
|
+
export function CollectionTable({type, filter, params, newTemplate}: CollectionTableProps) {
|
|
32
71
|
const router = useRouter()
|
|
72
|
+
// Ouverture par VOLET ENFANT : le document s'ouvre à droite, les volets
|
|
73
|
+
// précédents se réduisent sans se fermer (le pane déclare un résolveur
|
|
74
|
+
// d'enfant, cf. `ncfCollectionTable`).
|
|
75
|
+
const {ChildLink} = usePaneRouter()
|
|
33
76
|
|
|
77
|
+
const [view, setView] = useState<CollectionView>('table')
|
|
34
78
|
// `null` = pas encore de choix explicite → colonnes par défaut.
|
|
35
79
|
const [selected, setSelected] = useState<string[] | null>(null)
|
|
36
80
|
const [sort, setSort] = useState<CollectionSort>({key: null, direction: 'asc'})
|
|
37
81
|
const [search, setSearch] = useState('')
|
|
38
82
|
const [columnFilters, setColumnFilters] = useState<Record<string, string>>({})
|
|
83
|
+
const [widths, setWidths] = useState<Record<string, number>>({})
|
|
39
84
|
|
|
40
85
|
// Toutes les colonnes viennent du schéma (sans requête) : elles fixent le
|
|
41
86
|
// défaut et alimentent le sélecteur. Seules les visibles sont ensuite
|
|
@@ -47,13 +92,20 @@ export function CollectionTable({type}: CollectionTableProps) {
|
|
|
47
92
|
)
|
|
48
93
|
const visibleKeys = useMemo(() => selected ?? defaultVisible(allColumns), [selected, allColumns])
|
|
49
94
|
|
|
50
|
-
const {visibleColumns, rows, loading, error, refresh} = useCollection(type, visibleKeys)
|
|
95
|
+
const {visibleColumns, rows, loading, error, refresh} = useCollection(type, visibleKeys, {filter, params})
|
|
51
96
|
|
|
52
97
|
const visible = useMemo(() => {
|
|
53
98
|
const filtered = filterRows(rows, {search, columns: columnFilters}, visibleColumns)
|
|
54
99
|
return sortRows(filtered, sort, visibleColumns)
|
|
55
100
|
}, [rows, search, columnFilters, visibleColumns, sort])
|
|
56
101
|
|
|
102
|
+
// Largeur effective de chaque colonne visible (état ou défaut selon le type).
|
|
103
|
+
const cols = useMemo(
|
|
104
|
+
() => visibleColumns.map((column, index) => ({column, width: widths[column.key] ?? defaultWidth(column, index === 0)})),
|
|
105
|
+
[visibleColumns, widths],
|
|
106
|
+
)
|
|
107
|
+
const totalWidth = useMemo(() => cols.reduce((sum, c) => sum + c.width, 0), [cols])
|
|
108
|
+
|
|
57
109
|
const onToggleColumn = useCallback(
|
|
58
110
|
(key: string) => {
|
|
59
111
|
setSelected((current) => {
|
|
@@ -86,6 +138,45 @@ export function CollectionTable({type}: CollectionTableProps) {
|
|
|
86
138
|
setColumnFilters((current) => ({...current, [key]: value}))
|
|
87
139
|
}, [])
|
|
88
140
|
|
|
141
|
+
// Création : applique le template du niveau feuille (langue, catégorie…) s'il
|
|
142
|
+
// est fourni, sinon un document nu du type.
|
|
143
|
+
const onCreate = useCallback(() => {
|
|
144
|
+
router.navigateIntent('create', newTemplate ? {type, template: newTemplate} : {type})
|
|
145
|
+
}, [router, type, newTemplate])
|
|
146
|
+
|
|
147
|
+
// --- Réordonnancement des colonnes (glisser l'en-tête) ---
|
|
148
|
+
const dragKeyRef = useRef<string | null>(null)
|
|
149
|
+
const [dragOverKey, setDragOverKey] = useState<string | null>(null)
|
|
150
|
+
const onReorder = useCallback(
|
|
151
|
+
(toKey: string) => {
|
|
152
|
+
const fromKey = dragKeyRef.current
|
|
153
|
+
dragKeyRef.current = null
|
|
154
|
+
setDragOverKey(null)
|
|
155
|
+
if (!fromKey || fromKey === toKey) return
|
|
156
|
+
setSelected((current) => moveKey(current ?? defaultVisible(allColumns), fromKey, toKey))
|
|
157
|
+
},
|
|
158
|
+
[allColumns],
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
// --- Redimensionnement d'une colonne (glisser la poignée à droite) ---
|
|
162
|
+
const onResizeStart = useCallback(
|
|
163
|
+
(key: string, startW: number) => (event: React.PointerEvent) => {
|
|
164
|
+
event.preventDefault()
|
|
165
|
+
event.stopPropagation()
|
|
166
|
+
const startX = event.clientX
|
|
167
|
+
const onMove = (ev: PointerEvent) => {
|
|
168
|
+
setWidths((current) => ({...current, [key]: Math.max(MIN_WIDTH, startW + (ev.clientX - startX))}))
|
|
169
|
+
}
|
|
170
|
+
const onUp = () => {
|
|
171
|
+
window.removeEventListener('pointermove', onMove)
|
|
172
|
+
window.removeEventListener('pointerup', onUp)
|
|
173
|
+
}
|
|
174
|
+
window.addEventListener('pointermove', onMove)
|
|
175
|
+
window.addEventListener('pointerup', onUp)
|
|
176
|
+
},
|
|
177
|
+
[],
|
|
178
|
+
)
|
|
179
|
+
|
|
89
180
|
if (error) return <QueryError error={error} onRetry={refresh} />
|
|
90
181
|
|
|
91
182
|
const summary =
|
|
@@ -93,18 +184,13 @@ export function CollectionTable({type}: CollectionTableProps) {
|
|
|
93
184
|
? `${rows.length} item${rows.length > 1 ? 's' : ''}`
|
|
94
185
|
: `${visible.length} of ${rows.length}`
|
|
95
186
|
|
|
96
|
-
const
|
|
97
|
-
display: 'grid',
|
|
98
|
-
gridTemplateColumns: visibleColumns
|
|
99
|
-
.map((_, index) => (index === 0 ? 'minmax(200px, 1.6fr)' : 'minmax(150px, 1fr)'))
|
|
100
|
-
.join(' '),
|
|
101
|
-
gap: '0.875rem',
|
|
102
|
-
alignItems: 'center',
|
|
103
|
-
}
|
|
187
|
+
const subtitleKey = visibleColumns.find((c, i) => i > 0 && c.kind === 'text')?.key
|
|
104
188
|
|
|
105
189
|
return (
|
|
106
|
-
<Stack gap={
|
|
190
|
+
<Stack gap={3}>
|
|
107
191
|
<CollectionToolbar
|
|
192
|
+
view={view}
|
|
193
|
+
onView={setView}
|
|
108
194
|
search={search}
|
|
109
195
|
onSearch={setSearch}
|
|
110
196
|
columns={allColumns}
|
|
@@ -112,6 +198,7 @@ export function CollectionTable({type}: CollectionTableProps) {
|
|
|
112
198
|
onToggleColumn={onToggleColumn}
|
|
113
199
|
onExport={onExport}
|
|
114
200
|
onRefresh={refresh}
|
|
201
|
+
onCreate={onCreate}
|
|
115
202
|
busy={loading}
|
|
116
203
|
summary={summary}
|
|
117
204
|
/>
|
|
@@ -129,16 +216,34 @@ export function CollectionTable({type}: CollectionTableProps) {
|
|
|
129
216
|
No displayable field in this type. Pick columns above.
|
|
130
217
|
</Text>
|
|
131
218
|
</Box>
|
|
219
|
+
) : view === 'list' ? (
|
|
220
|
+
<Box style={{border: `1px solid ${BORDER_SOFT}`, borderRadius: 6}}>
|
|
221
|
+
<style>{`.ncf-coll-body a{display:contents;color:inherit;text-decoration:none}`}</style>
|
|
222
|
+
<Box className="ncf-coll-body">
|
|
223
|
+
{visible.map((row) => (
|
|
224
|
+
<ChildLink key={row.id} childId={row.id}>
|
|
225
|
+
<ListRowView row={row} titleKey={visibleColumns[0]?.key} subtitleKey={subtitleKey} />
|
|
226
|
+
</ChildLink>
|
|
227
|
+
))}
|
|
228
|
+
{visible.length === 0 && (
|
|
229
|
+
<Box padding={5}>
|
|
230
|
+
<Text size={1} muted align="center">
|
|
231
|
+
{rows.length === 0 ? 'No item yet.' : 'No match.'}
|
|
232
|
+
</Text>
|
|
233
|
+
</Box>
|
|
234
|
+
)}
|
|
235
|
+
</Box>
|
|
236
|
+
</Box>
|
|
132
237
|
) : (
|
|
133
238
|
<Box style={{overflowX: 'auto', border: `1px solid ${BORDER_SOFT}`, borderRadius: 6}}>
|
|
134
|
-
<
|
|
135
|
-
|
|
136
|
-
|
|
239
|
+
<style>{`.ncf-coll-body a{display:contents;color:inherit;text-decoration:none}`}</style>
|
|
240
|
+
<Box style={{width: totalWidth, minWidth: '100%'}}>
|
|
241
|
+
{/* En-tête : libellé triable + déplaçable, poignée de largeur, filtre. */}
|
|
242
|
+
<Flex
|
|
137
243
|
paddingX={3}
|
|
138
244
|
paddingY={2}
|
|
245
|
+
align="flex-end"
|
|
139
246
|
style={{
|
|
140
|
-
...grid,
|
|
141
|
-
alignItems: 'end',
|
|
142
247
|
borderBottom: `1px solid ${BORDER}`,
|
|
143
248
|
position: 'sticky',
|
|
144
249
|
top: 0,
|
|
@@ -146,62 +251,102 @@ export function CollectionTable({type}: CollectionTableProps) {
|
|
|
146
251
|
zIndex: 2,
|
|
147
252
|
}}
|
|
148
253
|
>
|
|
149
|
-
{
|
|
254
|
+
{cols.map(({column, width}) => {
|
|
150
255
|
const active = sort.key === column.key
|
|
151
256
|
const Arrow = sort.direction === 'asc' ? ChevronUpIcon : ChevronDownIcon
|
|
152
257
|
return (
|
|
153
|
-
<
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
258
|
+
<Box
|
|
259
|
+
key={column.key}
|
|
260
|
+
paddingRight={2}
|
|
261
|
+
onDragOver={(e) => {
|
|
262
|
+
if (dragKeyRef.current && dragKeyRef.current !== column.key) {
|
|
263
|
+
e.preventDefault()
|
|
264
|
+
setDragOverKey(column.key)
|
|
265
|
+
}
|
|
266
|
+
}}
|
|
267
|
+
onDrop={() => onReorder(column.key)}
|
|
268
|
+
style={{
|
|
269
|
+
width,
|
|
270
|
+
flex: 'none',
|
|
271
|
+
position: 'relative',
|
|
272
|
+
boxSizing: 'border-box',
|
|
273
|
+
borderLeft: dragOverKey === column.key ? `2px solid var(--card-focus-ring-color, #2276fc)` : '2px solid transparent',
|
|
274
|
+
}}
|
|
275
|
+
>
|
|
276
|
+
<Stack gap={2} style={CELL}>
|
|
277
|
+
<Flex
|
|
278
|
+
align="center"
|
|
279
|
+
gap={1}
|
|
280
|
+
draggable
|
|
281
|
+
onDragStart={() => {
|
|
282
|
+
dragKeyRef.current = column.key
|
|
283
|
+
}}
|
|
284
|
+
onDragEnd={() => {
|
|
285
|
+
dragKeyRef.current = null
|
|
286
|
+
setDragOverKey(null)
|
|
287
|
+
}}
|
|
288
|
+
onClick={() => onSort(column.key)}
|
|
289
|
+
style={{cursor: 'grab', userSelect: 'none'}}
|
|
290
|
+
title={`Sort by ${column.label.toLowerCase()} — drag to reorder`}
|
|
291
|
+
>
|
|
292
|
+
<Text size={0} weight="medium" muted={!active} textOverflow="ellipsis">
|
|
293
|
+
{column.label}
|
|
294
|
+
</Text>
|
|
295
|
+
{active && <Arrow style={{fontSize: 12, opacity: 0.7, flex: 'none'}} />}
|
|
296
|
+
</Flex>
|
|
297
|
+
<input
|
|
298
|
+
type="text"
|
|
299
|
+
value={columnFilters[column.key] ?? ''}
|
|
300
|
+
placeholder="Filter…"
|
|
301
|
+
onChange={(event) => onColumnFilter(column.key, event.currentTarget.value)}
|
|
302
|
+
style={{
|
|
303
|
+
width: '100%',
|
|
304
|
+
boxSizing: 'border-box',
|
|
305
|
+
font: 'inherit',
|
|
306
|
+
fontSize: 11,
|
|
307
|
+
padding: '3px 6px',
|
|
308
|
+
borderRadius: 3,
|
|
309
|
+
border: `1px solid ${BORDER_SOFT}`,
|
|
310
|
+
background: 'var(--card-bg-color)',
|
|
311
|
+
color: 'var(--card-fg-color)',
|
|
312
|
+
outline: 'none',
|
|
313
|
+
}}
|
|
314
|
+
/>
|
|
315
|
+
</Stack>
|
|
316
|
+
{/* Poignée de redimensionnement (bord droit). */}
|
|
317
|
+
<div
|
|
318
|
+
onPointerDown={onResizeStart(column.key, width)}
|
|
319
|
+
title="Drag to resize"
|
|
171
320
|
style={{
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
color: 'var(--card-fg-color)',
|
|
180
|
-
outline: 'none',
|
|
321
|
+
position: 'absolute',
|
|
322
|
+
top: 0,
|
|
323
|
+
right: 0,
|
|
324
|
+
width: 9,
|
|
325
|
+
height: '100%',
|
|
326
|
+
cursor: 'col-resize',
|
|
327
|
+
touchAction: 'none',
|
|
181
328
|
}}
|
|
182
329
|
/>
|
|
183
|
-
</
|
|
330
|
+
</Box>
|
|
184
331
|
)
|
|
185
332
|
})}
|
|
186
|
-
</
|
|
333
|
+
</Flex>
|
|
187
334
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
key={row.id}
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
onOpen={() => router.navigateIntent('edit', {id: row.id, type})}
|
|
195
|
-
/>
|
|
196
|
-
))}
|
|
335
|
+
<Box className="ncf-coll-body">
|
|
336
|
+
{visible.map((row) => (
|
|
337
|
+
<ChildLink key={row.id} childId={row.id}>
|
|
338
|
+
<TableRowView row={row} cols={cols} />
|
|
339
|
+
</ChildLink>
|
|
340
|
+
))}
|
|
197
341
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
342
|
+
{visible.length === 0 && (
|
|
343
|
+
<Box padding={5}>
|
|
344
|
+
<Text size={1} muted align="center">
|
|
345
|
+
{rows.length === 0 ? 'No item yet.' : 'No match.'}
|
|
346
|
+
</Text>
|
|
347
|
+
</Box>
|
|
348
|
+
)}
|
|
349
|
+
</Box>
|
|
205
350
|
</Box>
|
|
206
351
|
</Box>
|
|
207
352
|
)}
|
|
@@ -209,36 +354,30 @@ export function CollectionTable({type}: CollectionTableProps) {
|
|
|
209
354
|
)
|
|
210
355
|
}
|
|
211
356
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
grid: React.CSSProperties
|
|
216
|
-
onOpen: () => void
|
|
217
|
-
}) {
|
|
218
|
-
const {row, columns, grid, onOpen} = props
|
|
357
|
+
/** Ligne de la vue TABLE : une cellule par colonne, largeurs fixes. */
|
|
358
|
+
function TableRowView(props: {row: CollectionRow; cols: readonly {column: CollectionColumn; width: number}[]}) {
|
|
359
|
+
const {row, cols} = props
|
|
219
360
|
const [hovered, setHovered] = useState(false)
|
|
220
361
|
|
|
221
362
|
return (
|
|
222
|
-
<
|
|
363
|
+
<Flex
|
|
223
364
|
paddingX={3}
|
|
224
365
|
paddingY={2}
|
|
225
|
-
onClick={onOpen}
|
|
226
366
|
onMouseEnter={() => setHovered(true)}
|
|
227
367
|
onMouseLeave={() => setHovered(false)}
|
|
228
368
|
style={{
|
|
229
|
-
...grid,
|
|
230
369
|
cursor: 'pointer',
|
|
231
370
|
borderBottom: `1px solid ${BORDER_SOFT}`,
|
|
232
371
|
background: hovered ? 'var(--card-muted-bg-color, rgba(127,127,127,0.08))' : 'transparent',
|
|
233
372
|
}}
|
|
234
373
|
>
|
|
235
|
-
{
|
|
374
|
+
{cols.map(({column, width}, index) => {
|
|
236
375
|
const cell = row.cells[column.key]
|
|
237
376
|
const empty = !cell || cell.raw === null || cell.text === ''
|
|
238
377
|
return (
|
|
239
|
-
<Flex key={column.key} align="center" gap={2} style={
|
|
378
|
+
<Flex key={column.key} align="center" gap={2} paddingRight={2} style={{width, flex: 'none', boxSizing: 'border-box'}}>
|
|
240
379
|
<Text
|
|
241
|
-
size={
|
|
380
|
+
size={0}
|
|
242
381
|
muted={empty}
|
|
243
382
|
weight={index === 0 ? 'medium' : 'regular'}
|
|
244
383
|
textOverflow="ellipsis"
|
|
@@ -254,6 +393,46 @@ function CollectionRowView(props: {
|
|
|
254
393
|
</Flex>
|
|
255
394
|
)
|
|
256
395
|
})}
|
|
257
|
-
</
|
|
396
|
+
</Flex>
|
|
397
|
+
)
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/** Ligne de la vue LISTE : titre + sous-titre, façon liste native. */
|
|
401
|
+
function ListRowView(props: {row: CollectionRow; titleKey?: string; subtitleKey?: string}) {
|
|
402
|
+
const {row, titleKey, subtitleKey} = props
|
|
403
|
+
const [hovered, setHovered] = useState(false)
|
|
404
|
+
const title = titleKey ? row.cells[titleKey]?.text : ''
|
|
405
|
+
const subtitle = subtitleKey ? row.cells[subtitleKey]?.text : ''
|
|
406
|
+
|
|
407
|
+
return (
|
|
408
|
+
<Flex
|
|
409
|
+
align="center"
|
|
410
|
+
gap={2}
|
|
411
|
+
paddingX={3}
|
|
412
|
+
paddingY={2}
|
|
413
|
+
onMouseEnter={() => setHovered(true)}
|
|
414
|
+
onMouseLeave={() => setHovered(false)}
|
|
415
|
+
style={{
|
|
416
|
+
cursor: 'pointer',
|
|
417
|
+
borderBottom: `1px solid ${BORDER_SOFT}`,
|
|
418
|
+
background: hovered ? 'var(--card-muted-bg-color, rgba(127,127,127,0.08))' : 'transparent',
|
|
419
|
+
}}
|
|
420
|
+
>
|
|
421
|
+
<Stack gap={2} flex={1} style={{minWidth: 0}}>
|
|
422
|
+
<Text size={1} weight="medium" textOverflow="ellipsis">
|
|
423
|
+
{title || 'Untitled'}
|
|
424
|
+
</Text>
|
|
425
|
+
{subtitle ? (
|
|
426
|
+
<Text size={0} muted textOverflow="ellipsis">
|
|
427
|
+
{subtitle}
|
|
428
|
+
</Text>
|
|
429
|
+
) : null}
|
|
430
|
+
</Stack>
|
|
431
|
+
{row.draftOnly && (
|
|
432
|
+
<Badge tone="caution" fontSize={0} style={{flex: 'none'}}>
|
|
433
|
+
Draft
|
|
434
|
+
</Badge>
|
|
435
|
+
)}
|
|
436
|
+
</Flex>
|
|
258
437
|
)
|
|
259
438
|
}
|