@nocode-factory/sanity-kit 2.53.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 +49 -0
- package/dist/{finalUrl-CaFXmYvm.js → finalUrl-IXF3zbT9.js} +7 -2
- package/dist/finalUrl-IXF3zbT9.js.map +1 -0
- package/dist/{fixCrossLanguageReferences-w3whO9xM.d.ts → fixCrossLanguageReferences-C1VsmGsJ.d.ts} +21 -2
- package/dist/{fixCrossLanguageReferences-BeXywAAU.js → fixCrossLanguageReferences-CDczoic9.js} +2 -2
- package/dist/{fixCrossLanguageReferences-BeXywAAU.js.map → fixCrossLanguageReferences-CDczoic9.js.map} +1 -1
- package/dist/index.d.ts +217 -7
- package/dist/index.js +933 -26
- package/dist/index.js.map +1 -1
- package/dist/jsonld.js +1 -1
- package/dist/schema.d.ts +1 -1
- package/dist/schema.js +2 -2
- package/package.json +1 -1
- package/src/config/resolve.ts +5 -0
- package/src/config/types.ts +21 -0
- package/src/hooks/useCollection.ts +67 -0
- package/src/index.ts +18 -0
- package/src/lib/collectionColumns.ts +114 -0
- package/src/lib/collectionQuery.ts +31 -0
- package/src/lib/collectionRows.ts +92 -0
- package/src/lib/collectionTable.ts +72 -0
- package/src/lib/collectionTypes.ts +49 -0
- package/src/lib/csv.ts +24 -0
- package/src/lib/download.ts +24 -0
- package/src/lib/schemaColumns.ts +18 -5
- package/src/structure/collections.tsx +133 -0
- package/src/tools/collections/CollectionColumnPicker.tsx +122 -0
- package/src/tools/collections/CollectionTable.tsx +438 -0
- package/src/tools/collections/CollectionToolbar.tsx +117 -0
- package/src/version.ts +1 -1
- package/dist/finalUrl-CaFXmYvm.js.map +0 -1
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import {Box} from '@sanity/ui'
|
|
2
|
+
import type {StructureBuilder, StructureResolverContext, ListItemBuilder, UserComponent} from 'sanity/structure'
|
|
3
|
+
import {KitProvider} from '../context/KitContext'
|
|
4
|
+
import {resolveConfig} from '../config/resolve'
|
|
5
|
+
import {validateConfig} from '../config/validate'
|
|
6
|
+
import {CollectionTable} from '../tools/collections/CollectionTable'
|
|
7
|
+
import {discoverCollectionTypes, type SchemaTypeInfo} from '../lib/collectionTypes'
|
|
8
|
+
import {humanise} from '../lib/schemaColumns'
|
|
9
|
+
import {NCF_HIDDEN_TYPES} from '../schema/boardLayout'
|
|
10
|
+
import type {ConfigIssue, NcfKitConfig, ResolvedKitConfig} from '../config/types'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Composant de panneau pour une collection. Le type arrive par les `options`
|
|
14
|
+
* du pane de structure ; la configuration est fermée par la fabrique (elle
|
|
15
|
+
* n'est pas sérialisable dans les options).
|
|
16
|
+
*/
|
|
17
|
+
function createCollectionPane(config: ResolvedKitConfig, issues: ConfigIssue[]): UserComponent {
|
|
18
|
+
return function NcfCollectionPane(props) {
|
|
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
|
|
25
|
+
return (
|
|
26
|
+
<KitProvider config={config} issues={issues}>
|
|
27
|
+
<Box padding={3} height="fill" overflow="auto">
|
|
28
|
+
{type ? <CollectionTable type={type} filter={filter} params={params} newTemplate={newTemplate} /> : null}
|
|
29
|
+
</Box>
|
|
30
|
+
</KitProvider>
|
|
31
|
+
)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Vue table d'UNE collection, à utiliser comme enfant d'une entrée de structure
|
|
37
|
+
* pour que cliquer la collection ouvre la table (au lieu de la liste native) :
|
|
38
|
+
*
|
|
39
|
+
* ```ts
|
|
40
|
+
* S.listItem().title('Articles').child(ncfCollectionTable(S, ncfOptions, 'article'))
|
|
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
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
export function ncfCollectionTable(
|
|
60
|
+
S: StructureBuilder,
|
|
61
|
+
options: NcfKitConfig,
|
|
62
|
+
type: string,
|
|
63
|
+
opts: {
|
|
64
|
+
title?: string
|
|
65
|
+
id?: string
|
|
66
|
+
filter?: string
|
|
67
|
+
params?: Record<string, unknown>
|
|
68
|
+
newTemplate?: string
|
|
69
|
+
} = {},
|
|
70
|
+
) {
|
|
71
|
+
const config = resolveConfig(options)
|
|
72
|
+
const issues = validateConfig(config)
|
|
73
|
+
const pane = createCollectionPane(config, issues)
|
|
74
|
+
return S.component(pane)
|
|
75
|
+
.id(opts.id ?? `ncf-collection-${type}`)
|
|
76
|
+
.title(opts.title ?? 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))
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Libellé d'une entrée de collection : config, puis titre du schéma, puis nom humanisé. */
|
|
84
|
+
function labelFor(type: string, config: ResolvedKitConfig, schemaTitle?: string): string {
|
|
85
|
+
return config.collections.labels[type] ?? schemaTitle ?? humanise(type)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Types toujours écartés de la découverte, en plus de l'`exclude` du projet. */
|
|
89
|
+
function reservedExcludes(config: ResolvedKitConfig): string[] {
|
|
90
|
+
return [
|
|
91
|
+
...config.pageTypeNames,
|
|
92
|
+
config.redirectType,
|
|
93
|
+
config.componentPreview.defaultsType,
|
|
94
|
+
...NCF_HIDDEN_TYPES,
|
|
95
|
+
...config.collections.exclude,
|
|
96
|
+
].filter(Boolean)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Entrées « collection » pour la structure du Studio, une par type découvert,
|
|
101
|
+
* chacune ouvrant la vue table. À épandre dans la liste racine :
|
|
102
|
+
*
|
|
103
|
+
* ```ts
|
|
104
|
+
* structure: (S, context) =>
|
|
105
|
+
* S.list().title('Contenu').items([..., ...ncfCollections(S, context, ncfOptions)])
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
export function ncfCollections(
|
|
109
|
+
S: StructureBuilder,
|
|
110
|
+
context: StructureResolverContext,
|
|
111
|
+
options: NcfKitConfig,
|
|
112
|
+
): ListItemBuilder[] {
|
|
113
|
+
const config = resolveConfig(options)
|
|
114
|
+
|
|
115
|
+
const types: SchemaTypeInfo[] = context.schema
|
|
116
|
+
.getTypeNames()
|
|
117
|
+
.map((name) => ({name, jsonType: context.schema.get(name)?.jsonType}))
|
|
118
|
+
|
|
119
|
+
const discovered = discoverCollectionTypes(types, {
|
|
120
|
+
include: config.collections.include,
|
|
121
|
+
exclude: reservedExcludes(config),
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
return discovered.map((type) => {
|
|
125
|
+
const schemaType = context.schema.get(type)
|
|
126
|
+
const label = labelFor(type, config, schemaType?.title)
|
|
127
|
+
const item = S.listItem()
|
|
128
|
+
.id(`ncf-collection-${type}`)
|
|
129
|
+
.title(label)
|
|
130
|
+
.child(ncfCollectionTable(S, options, type, {title: label}))
|
|
131
|
+
return schemaType?.icon ? item.icon(schemaType.icon) : item
|
|
132
|
+
})
|
|
133
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
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 {CollectionColumn} from '../../lib/collectionColumns'
|
|
8
|
+
|
|
9
|
+
export interface CollectionColumnPickerProps {
|
|
10
|
+
columns: readonly CollectionColumn[]
|
|
11
|
+
/** Clés visibles, dans l'ordre d'affichage. */
|
|
12
|
+
visible: readonly string[]
|
|
13
|
+
onToggle: (key: string) => void
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Choix des colonnes affichées, sans quitter la table (façon tableur). */
|
|
17
|
+
export function CollectionColumnPicker(props: CollectionColumnPickerProps) {
|
|
18
|
+
const {columns, visible, onToggle} = props
|
|
19
|
+
const visibleSet = useMemo(() => new Set(visible), [visible])
|
|
20
|
+
|
|
21
|
+
return (
|
|
22
|
+
<InlinePopover
|
|
23
|
+
width={264}
|
|
24
|
+
trigger={({toggle}) => (
|
|
25
|
+
<Button
|
|
26
|
+
mode="bleed"
|
|
27
|
+
fontSize={1}
|
|
28
|
+
padding={2}
|
|
29
|
+
icon={ControlsIcon}
|
|
30
|
+
text={`Columns · ${visible.length}`}
|
|
31
|
+
onClick={toggle}
|
|
32
|
+
/>
|
|
33
|
+
)}
|
|
34
|
+
>
|
|
35
|
+
{() => <ColumnList columns={columns} visibleSet={visibleSet} onToggle={onToggle} />}
|
|
36
|
+
</InlinePopover>
|
|
37
|
+
)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function Row(props: {label: string; checked: boolean; onClick: () => void}) {
|
|
41
|
+
return (
|
|
42
|
+
<Flex
|
|
43
|
+
align="center"
|
|
44
|
+
gap={2}
|
|
45
|
+
padding={2}
|
|
46
|
+
onClick={props.onClick}
|
|
47
|
+
style={{cursor: 'pointer', borderRadius: 3}}
|
|
48
|
+
onMouseEnter={(event) => {
|
|
49
|
+
event.currentTarget.style.background = 'var(--card-muted-bg-color, rgba(127,127,127,0.12))'
|
|
50
|
+
}}
|
|
51
|
+
onMouseLeave={(event) => {
|
|
52
|
+
event.currentTarget.style.background = 'transparent'
|
|
53
|
+
}}
|
|
54
|
+
>
|
|
55
|
+
<Box style={{width: 14, flex: 'none'}}>
|
|
56
|
+
{props.checked && <CheckmarkIcon style={{fontSize: 14, opacity: 0.7}} />}
|
|
57
|
+
</Box>
|
|
58
|
+
<Text size={1} muted={!props.checked} textOverflow="ellipsis">
|
|
59
|
+
{props.label}
|
|
60
|
+
</Text>
|
|
61
|
+
</Flex>
|
|
62
|
+
)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Au-delà d'une poignée de champs, un filtre apparaît pour en chercher un. */
|
|
66
|
+
function ColumnList(props: {
|
|
67
|
+
columns: readonly CollectionColumn[]
|
|
68
|
+
visibleSet: ReadonlySet<string>
|
|
69
|
+
onToggle: (key: string) => void
|
|
70
|
+
}) {
|
|
71
|
+
const {columns, visibleSet, onToggle} = props
|
|
72
|
+
const [filter, setFilter] = useState('')
|
|
73
|
+
|
|
74
|
+
const matches = useMemo(() => {
|
|
75
|
+
const needle = filter.trim().toLowerCase()
|
|
76
|
+
if (!needle) return columns
|
|
77
|
+
return columns.filter((column) => column.label.toLowerCase().includes(needle))
|
|
78
|
+
}, [columns, filter])
|
|
79
|
+
|
|
80
|
+
return (
|
|
81
|
+
<Stack gap={0}>
|
|
82
|
+
{columns.length > 8 && (
|
|
83
|
+
<Box paddingX={1} paddingBottom={1}>
|
|
84
|
+
<input
|
|
85
|
+
type="text"
|
|
86
|
+
value={filter}
|
|
87
|
+
placeholder="Filter…"
|
|
88
|
+
onChange={(event) => setFilter(event.currentTarget.value)}
|
|
89
|
+
style={{
|
|
90
|
+
width: '100%',
|
|
91
|
+
font: 'inherit',
|
|
92
|
+
fontSize: 12,
|
|
93
|
+
padding: '6px',
|
|
94
|
+
borderRadius: 3,
|
|
95
|
+
border: `1px solid ${BORDER}`,
|
|
96
|
+
background: 'var(--card-bg-color)',
|
|
97
|
+
color: 'var(--card-fg-color)',
|
|
98
|
+
outline: 'none',
|
|
99
|
+
}}
|
|
100
|
+
/>
|
|
101
|
+
</Box>
|
|
102
|
+
)}
|
|
103
|
+
|
|
104
|
+
{matches.map((column) => (
|
|
105
|
+
<Row
|
|
106
|
+
key={column.key}
|
|
107
|
+
label={column.label}
|
|
108
|
+
checked={visibleSet.has(column.key)}
|
|
109
|
+
onClick={() => onToggle(column.key)}
|
|
110
|
+
/>
|
|
111
|
+
))}
|
|
112
|
+
|
|
113
|
+
{matches.length === 0 && (
|
|
114
|
+
<Box padding={2}>
|
|
115
|
+
<Text size={0} muted>
|
|
116
|
+
No matching fields.
|
|
117
|
+
</Text>
|
|
118
|
+
</Box>
|
|
119
|
+
)}
|
|
120
|
+
</Stack>
|
|
121
|
+
)
|
|
122
|
+
}
|
|
@@ -0,0 +1,438 @@
|
|
|
1
|
+
import {useCallback, useMemo, useRef, useState} from 'react'
|
|
2
|
+
import {Badge, Box, Flex, Spinner, Stack, Text} from '@sanity/ui'
|
|
3
|
+
import {ChevronDownIcon} from '@sanity/icons/ChevronDown'
|
|
4
|
+
import {ChevronUpIcon} from '@sanity/icons/ChevronUp'
|
|
5
|
+
import {usePaneRouter} from 'sanity/structure'
|
|
6
|
+
import {useRouter} from 'sanity/router'
|
|
7
|
+
import {useSchema} from 'sanity'
|
|
8
|
+
import {useCollection} from '../../hooks/useCollection'
|
|
9
|
+
import {filterRows, sortRows, type CollectionSort} from '../../lib/collectionTable'
|
|
10
|
+
import {toCsv} from '../../lib/csv'
|
|
11
|
+
import {downloadText} from '../../lib/download'
|
|
12
|
+
import {deriveCollectionColumns, type CollectionColumn} from '../../lib/collectionColumns'
|
|
13
|
+
import type {SchemaTypeLike} from '../../lib/schemaColumns'
|
|
14
|
+
import type {CollectionRow} from '../../lib/collectionRows'
|
|
15
|
+
import {BORDER, BORDER_SOFT, CELL, TABULAR} from '../../ui/tokens'
|
|
16
|
+
import {QueryError} from '../../ui/QueryError'
|
|
17
|
+
import {CollectionToolbar, type CollectionView} from './CollectionToolbar'
|
|
18
|
+
|
|
19
|
+
/** Colonnes montrées par défaut : le titre d'abord, puis les premières. */
|
|
20
|
+
const DEFAULT_MAX = 6
|
|
21
|
+
/** Largeur minimale d'une colonne au redimensionnement. */
|
|
22
|
+
const MIN_WIDTH = 80
|
|
23
|
+
|
|
24
|
+
function defaultVisible(columns: readonly CollectionColumn[]): string[] {
|
|
25
|
+
const title = columns.find((c) => c.key === 'title') ?? columns.find((c) => c.kind === 'text')
|
|
26
|
+
const ordered = title ? [title, ...columns.filter((c) => c !== title)] : [...columns]
|
|
27
|
+
return ordered.slice(0, DEFAULT_MAX).map((c) => c.key)
|
|
28
|
+
}
|
|
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
|
+
|
|
58
|
+
export interface CollectionTableProps {
|
|
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
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function CollectionTable({type, filter, params, newTemplate}: CollectionTableProps) {
|
|
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()
|
|
76
|
+
|
|
77
|
+
const [view, setView] = useState<CollectionView>('table')
|
|
78
|
+
// `null` = pas encore de choix explicite → colonnes par défaut.
|
|
79
|
+
const [selected, setSelected] = useState<string[] | null>(null)
|
|
80
|
+
const [sort, setSort] = useState<CollectionSort>({key: null, direction: 'asc'})
|
|
81
|
+
const [search, setSearch] = useState('')
|
|
82
|
+
const [columnFilters, setColumnFilters] = useState<Record<string, string>>({})
|
|
83
|
+
const [widths, setWidths] = useState<Record<string, number>>({})
|
|
84
|
+
|
|
85
|
+
// Toutes les colonnes viennent du schéma (sans requête) : elles fixent le
|
|
86
|
+
// défaut et alimentent le sélecteur. Seules les visibles sont ensuite
|
|
87
|
+
// projetées par le hook de données.
|
|
88
|
+
const schema = useSchema()
|
|
89
|
+
const allColumns = useMemo(
|
|
90
|
+
() => deriveCollectionColumns(schema.get(type) as SchemaTypeLike | undefined),
|
|
91
|
+
[schema, type],
|
|
92
|
+
)
|
|
93
|
+
const visibleKeys = useMemo(() => selected ?? defaultVisible(allColumns), [selected, allColumns])
|
|
94
|
+
|
|
95
|
+
const {visibleColumns, rows, loading, error, refresh} = useCollection(type, visibleKeys, {filter, params})
|
|
96
|
+
|
|
97
|
+
const visible = useMemo(() => {
|
|
98
|
+
const filtered = filterRows(rows, {search, columns: columnFilters}, visibleColumns)
|
|
99
|
+
return sortRows(filtered, sort, visibleColumns)
|
|
100
|
+
}, [rows, search, columnFilters, visibleColumns, sort])
|
|
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
|
+
|
|
109
|
+
const onToggleColumn = useCallback(
|
|
110
|
+
(key: string) => {
|
|
111
|
+
setSelected((current) => {
|
|
112
|
+
const base = current ?? defaultVisible(allColumns)
|
|
113
|
+
return base.includes(key) ? base.filter((k) => k !== key) : [...base, key]
|
|
114
|
+
})
|
|
115
|
+
// Un filtre sur une colonne qu'on masque n'a plus de sens.
|
|
116
|
+
setColumnFilters((current) => {
|
|
117
|
+
if (!current[key]) return current
|
|
118
|
+
const {[key]: _removed, ...rest} = current
|
|
119
|
+
return rest
|
|
120
|
+
})
|
|
121
|
+
},
|
|
122
|
+
[allColumns],
|
|
123
|
+
)
|
|
124
|
+
|
|
125
|
+
const onSort = useCallback((key: string) => {
|
|
126
|
+
setSort((current) =>
|
|
127
|
+
current.key === key
|
|
128
|
+
? {key, direction: current.direction === 'asc' ? 'desc' : 'asc'}
|
|
129
|
+
: {key, direction: 'asc'},
|
|
130
|
+
)
|
|
131
|
+
}, [])
|
|
132
|
+
|
|
133
|
+
const onExport = useCallback(() => {
|
|
134
|
+
downloadText(`${type}.csv`, toCsv(visible, visibleColumns))
|
|
135
|
+
}, [type, visible, visibleColumns])
|
|
136
|
+
|
|
137
|
+
const onColumnFilter = useCallback((key: string, value: string) => {
|
|
138
|
+
setColumnFilters((current) => ({...current, [key]: value}))
|
|
139
|
+
}, [])
|
|
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
|
+
|
|
180
|
+
if (error) return <QueryError error={error} onRetry={refresh} />
|
|
181
|
+
|
|
182
|
+
const summary =
|
|
183
|
+
visible.length === rows.length
|
|
184
|
+
? `${rows.length} item${rows.length > 1 ? 's' : ''}`
|
|
185
|
+
: `${visible.length} of ${rows.length}`
|
|
186
|
+
|
|
187
|
+
const subtitleKey = visibleColumns.find((c, i) => i > 0 && c.kind === 'text')?.key
|
|
188
|
+
|
|
189
|
+
return (
|
|
190
|
+
<Stack gap={3}>
|
|
191
|
+
<CollectionToolbar
|
|
192
|
+
view={view}
|
|
193
|
+
onView={setView}
|
|
194
|
+
search={search}
|
|
195
|
+
onSearch={setSearch}
|
|
196
|
+
columns={allColumns}
|
|
197
|
+
visible={visibleKeys}
|
|
198
|
+
onToggleColumn={onToggleColumn}
|
|
199
|
+
onExport={onExport}
|
|
200
|
+
onRefresh={refresh}
|
|
201
|
+
onCreate={onCreate}
|
|
202
|
+
busy={loading}
|
|
203
|
+
summary={summary}
|
|
204
|
+
/>
|
|
205
|
+
|
|
206
|
+
{loading && rows.length === 0 ? (
|
|
207
|
+
<Flex align="center" justify="center" padding={6} gap={3}>
|
|
208
|
+
<Spinner muted />
|
|
209
|
+
<Text size={1} muted>
|
|
210
|
+
Loading…
|
|
211
|
+
</Text>
|
|
212
|
+
</Flex>
|
|
213
|
+
) : visibleColumns.length === 0 ? (
|
|
214
|
+
<Box padding={5}>
|
|
215
|
+
<Text size={1} muted align="center">
|
|
216
|
+
No displayable field in this type. Pick columns above.
|
|
217
|
+
</Text>
|
|
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>
|
|
237
|
+
) : (
|
|
238
|
+
<Box style={{overflowX: 'auto', border: `1px solid ${BORDER_SOFT}`, borderRadius: 6}}>
|
|
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
|
|
243
|
+
paddingX={3}
|
|
244
|
+
paddingY={2}
|
|
245
|
+
align="flex-end"
|
|
246
|
+
style={{
|
|
247
|
+
borderBottom: `1px solid ${BORDER}`,
|
|
248
|
+
position: 'sticky',
|
|
249
|
+
top: 0,
|
|
250
|
+
background: 'var(--card-bg-color, inherit)',
|
|
251
|
+
zIndex: 2,
|
|
252
|
+
}}
|
|
253
|
+
>
|
|
254
|
+
{cols.map(({column, width}) => {
|
|
255
|
+
const active = sort.key === column.key
|
|
256
|
+
const Arrow = sort.direction === 'asc' ? ChevronUpIcon : ChevronDownIcon
|
|
257
|
+
return (
|
|
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"
|
|
320
|
+
style={{
|
|
321
|
+
position: 'absolute',
|
|
322
|
+
top: 0,
|
|
323
|
+
right: 0,
|
|
324
|
+
width: 9,
|
|
325
|
+
height: '100%',
|
|
326
|
+
cursor: 'col-resize',
|
|
327
|
+
touchAction: 'none',
|
|
328
|
+
}}
|
|
329
|
+
/>
|
|
330
|
+
</Box>
|
|
331
|
+
)
|
|
332
|
+
})}
|
|
333
|
+
</Flex>
|
|
334
|
+
|
|
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
|
+
))}
|
|
341
|
+
|
|
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>
|
|
350
|
+
</Box>
|
|
351
|
+
</Box>
|
|
352
|
+
)}
|
|
353
|
+
</Stack>
|
|
354
|
+
)
|
|
355
|
+
}
|
|
356
|
+
|
|
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
|
|
360
|
+
const [hovered, setHovered] = useState(false)
|
|
361
|
+
|
|
362
|
+
return (
|
|
363
|
+
<Flex
|
|
364
|
+
paddingX={3}
|
|
365
|
+
paddingY={2}
|
|
366
|
+
onMouseEnter={() => setHovered(true)}
|
|
367
|
+
onMouseLeave={() => setHovered(false)}
|
|
368
|
+
style={{
|
|
369
|
+
cursor: 'pointer',
|
|
370
|
+
borderBottom: `1px solid ${BORDER_SOFT}`,
|
|
371
|
+
background: hovered ? 'var(--card-muted-bg-color, rgba(127,127,127,0.08))' : 'transparent',
|
|
372
|
+
}}
|
|
373
|
+
>
|
|
374
|
+
{cols.map(({column, width}, index) => {
|
|
375
|
+
const cell = row.cells[column.key]
|
|
376
|
+
const empty = !cell || cell.raw === null || cell.text === ''
|
|
377
|
+
return (
|
|
378
|
+
<Flex key={column.key} align="center" gap={2} paddingRight={2} style={{width, flex: 'none', boxSizing: 'border-box'}}>
|
|
379
|
+
<Text
|
|
380
|
+
size={0}
|
|
381
|
+
muted={empty}
|
|
382
|
+
weight={index === 0 ? 'medium' : 'regular'}
|
|
383
|
+
textOverflow="ellipsis"
|
|
384
|
+
style={{flex: 1, minWidth: 0, ...(column.kind === 'number' || column.kind === 'date' ? TABULAR : null)}}
|
|
385
|
+
>
|
|
386
|
+
{empty ? '—' : cell.text}
|
|
387
|
+
</Text>
|
|
388
|
+
{index === 0 && row.draftOnly && (
|
|
389
|
+
<Badge tone="caution" fontSize={0} style={{flex: 'none'}}>
|
|
390
|
+
Draft
|
|
391
|
+
</Badge>
|
|
392
|
+
)}
|
|
393
|
+
</Flex>
|
|
394
|
+
)
|
|
395
|
+
})}
|
|
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>
|
|
437
|
+
)
|
|
438
|
+
}
|