@nocode-factory/sanity-kit 2.68.0-rc.1 → 2.70.0-rc.2
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/CHANGELOG.md +37 -18
- package/dist/index.d.ts +48 -16
- package/dist/index.js +610 -383
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/hooks/useAssets.ts +1 -1
- package/src/hooks/useCollection.ts +5 -3
- package/src/hooks/useComponentDefaults.ts +1 -1
- package/src/hooks/useComponentNames.ts +1 -1
- package/src/hooks/usePageContents.ts +0 -1
- package/src/hooks/usePages.ts +5 -3
- package/src/hooks/useQueryData.ts +27 -59
- package/src/hooks/useRedirectMap.ts +1 -1
- package/src/hooks/useScheduledReleases.ts +27 -0
- package/src/hooks/useTranslationTodo.ts +1 -1
- package/src/hooks/useTranslationTodos.ts +0 -1
- package/src/lib/collectionRows.ts +26 -26
- package/src/lib/colors.ts +3 -6
- package/src/lib/pages.ts +16 -3
- package/src/lib/status.ts +118 -22
- package/src/tools/assets/AssetDetail.tsx +4 -4
- package/src/tools/assets/AssetTable.tsx +3 -3
- package/src/tools/collections/CollectionTable.tsx +53 -30
- package/src/tools/components/ComponentsTool.tsx +25 -2
- package/src/tools/pages/FieldValue.tsx +2 -2
- package/src/tools/pages/PagesTable.tsx +2 -2
- package/src/tools/pages/PagesTool.tsx +25 -2
- package/src/tools/pages/PathCell.tsx +1 -1
- package/src/tools/pages/TagCell.tsx +2 -2
- package/src/tools/pages/board/BoardView.tsx +2 -6
- package/src/tools/pages/board/PageNode.tsx +1 -1
- package/src/tools/pages/board/PagePanel.tsx +2 -2
- package/src/tools/presentation/PagesNavigator.tsx +1 -1
- package/src/ui/ActorName.tsx +1 -1
- package/src/ui/QuietInput.tsx +1 -1
- package/src/ui/StatusBadge.tsx +93 -7
- package/src/ui/StatusFilter.tsx +68 -0
- package/src/ui/formatDate.ts +2 -2
- package/src/version.ts +1 -1
- package/src/hooks/sharedListener.ts +0 -101
package/package.json
CHANGED
package/src/hooks/useAssets.ts
CHANGED
|
@@ -5,6 +5,7 @@ import {deriveCollectionColumns, type CollectionColumn} from '../lib/collectionC
|
|
|
5
5
|
import {buildCollectionQuery} from '../lib/collectionQuery'
|
|
6
6
|
import {buildCollectionRows, type CollectionDoc, type CollectionRow} from '../lib/collectionRows'
|
|
7
7
|
import type {SchemaTypeLike} from '../lib/schemaColumns'
|
|
8
|
+
import {useScheduledReleases} from './useScheduledReleases'
|
|
8
9
|
|
|
9
10
|
export interface CollectionState {
|
|
10
11
|
/** Toutes les colonnes possibles du type (pour le sélecteur). */
|
|
@@ -56,12 +57,13 @@ export function useCollection(
|
|
|
56
57
|
|
|
57
58
|
const {data, loading, error, refresh} = useQueryData<CollectionDoc[]>(query, params, {
|
|
58
59
|
listenTypes: [type],
|
|
59
|
-
tag: `collection.${type}`,
|
|
60
60
|
})
|
|
61
61
|
|
|
62
|
+
// Publications programmées : statut « Scheduled » et sa date.
|
|
63
|
+
const schedules = useScheduledReleases()
|
|
62
64
|
const rows = useMemo(
|
|
63
|
-
() => buildCollectionRows(data ?? [], visibleColumns),
|
|
64
|
-
[data, visibleColumns],
|
|
65
|
+
() => buildCollectionRows(data ?? [], visibleColumns, schedules),
|
|
66
|
+
[data, visibleColumns, schedules],
|
|
65
67
|
)
|
|
66
68
|
|
|
67
69
|
return {allColumns, visibleColumns, rows, loading, error, refresh}
|
|
@@ -37,7 +37,7 @@ function entryFor(client: SanityClient, type: string, languageField: string, lan
|
|
|
37
37
|
})
|
|
38
38
|
.catch(() => {})
|
|
39
39
|
void load()
|
|
40
|
-
const sub = client.listen(`*[${filter}]`, params, {visibility: 'query', includeResult: false
|
|
40
|
+
const sub = client.listen(`*[${filter}]`, params, {visibility: 'query', includeResult: false}).subscribe({next: () => void load()})
|
|
41
41
|
e.stop = () => sub.unsubscribe()
|
|
42
42
|
}
|
|
43
43
|
return [key, e]
|
|
@@ -27,7 +27,7 @@ export function useComponentNames(): Map<string, string> {
|
|
|
27
27
|
.catch(() => {})
|
|
28
28
|
void load()
|
|
29
29
|
const subscription = client
|
|
30
|
-
.listen(`*[_id == $id]`, {id: COMPONENT_META_ID}, {visibility: 'query', events: ['mutation']
|
|
30
|
+
.listen(`*[_id == $id]`, {id: COMPONENT_META_ID}, {visibility: 'query', events: ['mutation']})
|
|
31
31
|
.subscribe({next: () => void load(), error: () => {}})
|
|
32
32
|
return () => {
|
|
33
33
|
cancelled = true
|
package/src/hooks/usePages.ts
CHANGED
|
@@ -5,6 +5,7 @@ import {buildPagesQuery} from '../lib/query'
|
|
|
5
5
|
import {buildPageRows, type PageDoc, type PageRow} from '../lib/pages'
|
|
6
6
|
import {useQueryData} from './useQueryData'
|
|
7
7
|
import {useRedirectMap} from './useRedirectMap'
|
|
8
|
+
import {useScheduledReleases} from './useScheduledReleases'
|
|
8
9
|
import type {ResolvedDocumentColumn} from '../config/types'
|
|
9
10
|
|
|
10
11
|
export interface PagesState {
|
|
@@ -35,14 +36,15 @@ export function usePages(documentColumns: readonly ResolvedDocumentColumn[] = []
|
|
|
35
36
|
|
|
36
37
|
const {data, loading, error, refresh} = useQueryData<PageDoc[]>(query, params, {
|
|
37
38
|
listenTypes: config.pageTypeNames,
|
|
38
|
-
tag: 'pages',
|
|
39
39
|
})
|
|
40
40
|
|
|
41
41
|
// Redirections du Studio : l'adresse affichée est l'URL finale.
|
|
42
42
|
const redirects = useRedirectMap(config.redirectType)
|
|
43
|
+
// Publications programmées : statut « Scheduled » et sa date.
|
|
44
|
+
const schedules = useScheduledReleases()
|
|
43
45
|
const rows = useMemo(
|
|
44
|
-
() => (data ? buildPageRows(data, config, columns, redirects) : []),
|
|
45
|
-
[data, config, columns, redirects],
|
|
46
|
+
() => (data ? buildPageRows(data, config, columns, redirects, schedules) : []),
|
|
47
|
+
[data, config, columns, redirects, schedules],
|
|
46
48
|
)
|
|
47
49
|
|
|
48
50
|
return {rows, loading, error, refresh}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import {useCallback, useEffect, useMemo, useRef, useState} from 'react'
|
|
2
|
-
import {subscribeShared} from './sharedListener'
|
|
3
2
|
import {useKitClient} from './useKitClient'
|
|
4
3
|
|
|
5
4
|
export interface QueryState<T> {
|
|
@@ -12,35 +11,19 @@ export interface QueryState<T> {
|
|
|
12
11
|
export interface UseQueryDataOptions {
|
|
13
12
|
/** Types écoutés pour rafraîchir automatiquement. Vide = pas d'écoute. */
|
|
14
13
|
listenTypes?: readonly string[]
|
|
15
|
-
/** `false` pour différer la requête (onglet pas encore ouvert). */
|
|
16
|
-
enabled?: boolean
|
|
17
|
-
/**
|
|
18
|
-
* La liste n'affiche que du publié : les brouillons en cours de frappe ne la font pas
|
|
19
|
-
* recharger.
|
|
20
|
-
*/
|
|
21
|
-
publishedOnly?: boolean
|
|
22
14
|
/**
|
|
23
|
-
*
|
|
24
|
-
*
|
|
15
|
+
* Requête d'écoute à la place de `*[_type in $types]`, pour des documents
|
|
16
|
+
* qui ne se repèrent pas par leur type (releases : `_.releases.**`).
|
|
25
17
|
*/
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
/** Au plus un rechargement par intervalle pendant une saisie. */
|
|
30
|
-
const REFRESH_EVERY_MS = 3_000
|
|
31
|
-
/** Petite attente : une publication émet plusieurs événements d'affilée. */
|
|
32
|
-
const FIRST_DELAY_MS = 600
|
|
33
|
-
|
|
34
|
-
/** Étiquette acceptée par Sanity : lettres, chiffres, . _ - (75 caractères au plus). */
|
|
35
|
-
export function kitTag(tag: string | undefined): string {
|
|
36
|
-
const clean = (tag ?? 'query').replace(/[^a-zA-Z0-9._-]/g, '-').slice(0, 66)
|
|
37
|
-
return `ncf-kit.${clean}`
|
|
18
|
+
listenQuery?: string
|
|
19
|
+
/** `false` pour différer la requête (onglet pas encore ouvert). */
|
|
20
|
+
enabled?: boolean
|
|
38
21
|
}
|
|
39
22
|
|
|
40
23
|
/**
|
|
41
24
|
* Exécute une requête GROQ et la rejoue quand le contenu change.
|
|
42
25
|
*
|
|
43
|
-
* Le rafraîchissement passe par un
|
|
26
|
+
* Le rafraîchissement passe par un `listen` puis une nouvelle requête, plutôt
|
|
44
27
|
* que par l'application des mutations reçues : c'est plus lent mais toujours
|
|
45
28
|
* cohérent, y compris quand une publication modifie plusieurs documents.
|
|
46
29
|
*/
|
|
@@ -50,8 +33,7 @@ export function useQueryData<T>(
|
|
|
50
33
|
options: UseQueryDataOptions = {},
|
|
51
34
|
): QueryState<T> {
|
|
52
35
|
const client = useKitClient()
|
|
53
|
-
const {listenTypes, enabled = true
|
|
54
|
-
const tag = kitTag(options.tag)
|
|
36
|
+
const {listenTypes, listenQuery, enabled = true} = options
|
|
55
37
|
|
|
56
38
|
const [data, setData] = useState<T | null>(null)
|
|
57
39
|
const [loading, setLoading] = useState(enabled)
|
|
@@ -72,7 +54,7 @@ export function useQueryData<T>(
|
|
|
72
54
|
setLoading(true)
|
|
73
55
|
|
|
74
56
|
client
|
|
75
|
-
.fetch<T>(query, stableParams
|
|
57
|
+
.fetch<T>(query, stableParams)
|
|
76
58
|
.then((result) => {
|
|
77
59
|
if (cancelled) return
|
|
78
60
|
setData(result)
|
|
@@ -91,50 +73,36 @@ export function useQueryData<T>(
|
|
|
91
73
|
}
|
|
92
74
|
}, [client, query, stableParams, enabled, tick])
|
|
93
75
|
|
|
94
|
-
/* --- Rafraîchissement sur
|
|
95
|
-
// Une écoute partagée par types (sharedListener.ts), signal seulement. Rechargement au
|
|
96
|
-
// plus toutes les 3 s pendant une saisie (la liste avance, sans relancer la requête à
|
|
97
|
-
// chaque frappe) ; onglet caché : rien, rechargement au retour sur l'onglet.
|
|
76
|
+
/* --- Rafraîchissement sur mutation --- */
|
|
98
77
|
const refreshRef = useRef(refresh)
|
|
99
78
|
refreshRef.current = refresh
|
|
100
79
|
const typesKey = (listenTypes ?? []).join(',')
|
|
101
80
|
|
|
102
81
|
useEffect(() => {
|
|
103
|
-
if (!enabled || !typesKey) return undefined
|
|
82
|
+
if (!enabled || (!typesKey && !listenQuery)) return undefined
|
|
104
83
|
|
|
84
|
+
const types = typesKey ? typesKey.split(',') : []
|
|
85
|
+
// Fenêtre courte : une publication émet plusieurs événements d'affilée,
|
|
86
|
+
// on ne veut pas relancer la requête à chacun.
|
|
105
87
|
let timer: ReturnType<typeof setTimeout> | undefined
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
if (timer) return
|
|
119
|
-
const wait = Math.max(FIRST_DELAY_MS, REFRESH_EVERY_MS - (Date.now() - lastRefresh))
|
|
120
|
-
timer = setTimeout(run, wait)
|
|
121
|
-
}
|
|
122
|
-
const onVisible = () => {
|
|
123
|
-
if (!document.hidden && staleWhileHidden) {
|
|
124
|
-
staleWhileHidden = false
|
|
125
|
-
lastRefresh = Date.now()
|
|
126
|
-
refreshRef.current()
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
const unsubscribe = subscribeShared(client, typesKey.split(','), publishedOnly, onChange, tag)
|
|
130
|
-
document.addEventListener('visibilitychange', onVisible)
|
|
88
|
+
const subscription = client
|
|
89
|
+
.listen(listenQuery ?? '*[_type in $types]', listenQuery ? {} : {types}, {visibility: 'query', events: ['mutation']})
|
|
90
|
+
.subscribe({
|
|
91
|
+
next: () => {
|
|
92
|
+
if (timer) clearTimeout(timer)
|
|
93
|
+
timer = setTimeout(() => refreshRef.current(), 600)
|
|
94
|
+
},
|
|
95
|
+
error: () => {
|
|
96
|
+
// Une écoute qui tombe ne doit pas vider la liste déjà affichée :
|
|
97
|
+
// l'utilisateur garde ses données et le bouton de rafraîchissement.
|
|
98
|
+
},
|
|
99
|
+
})
|
|
131
100
|
|
|
132
101
|
return () => {
|
|
133
102
|
if (timer) clearTimeout(timer)
|
|
134
|
-
unsubscribe()
|
|
135
|
-
document.removeEventListener('visibilitychange', onVisible)
|
|
103
|
+
subscription.unsubscribe()
|
|
136
104
|
}
|
|
137
|
-
}, [client, typesKey,
|
|
105
|
+
}, [client, typesKey, listenQuery, enabled])
|
|
138
106
|
|
|
139
107
|
return {data, loading, error, refresh}
|
|
140
108
|
}
|
|
@@ -8,6 +8,6 @@ const QUERY = `*[_type == $type && defined(from) && defined(to) && !(_id in path
|
|
|
8
8
|
export function useRedirectMap(redirectType: string): RedirectMap {
|
|
9
9
|
const params = useMemo(() => ({type: redirectType}), [redirectType])
|
|
10
10
|
const listenTypes = useMemo(() => [redirectType], [redirectType])
|
|
11
|
-
const {data} = useQueryData<{from?: string; to?: string}[]>(QUERY, params, {listenTypes
|
|
11
|
+
const {data} = useQueryData<{from?: string; to?: string}[]>(QUERY, params, {listenTypes})
|
|
12
12
|
return useMemo(() => redirectMapOf(data ?? []), [data])
|
|
13
13
|
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import {useMemo} from 'react'
|
|
2
|
+
import {useQueryData} from './useQueryData'
|
|
3
|
+
import type {ScheduleMap} from '../lib/status'
|
|
4
|
+
|
|
5
|
+
/** Releases programmées (Scheduled drafts et Releases : même mécanisme). */
|
|
6
|
+
export const SCHEDULED_RELEASES_QUERY =
|
|
7
|
+
'*[_id in path("_.releases.**") && state == "scheduled"]{name, "at": coalesce(publishAt, metadata.intendedPublishAt)}'
|
|
8
|
+
|
|
9
|
+
const EMPTY: ScheduleMap = new Map()
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Publications programmées, tenues à jour : nom de la release -> date.
|
|
13
|
+
* Sert à afficher le statut « Scheduled » dans les listes du kit.
|
|
14
|
+
*/
|
|
15
|
+
export function useScheduledReleases(): ScheduleMap {
|
|
16
|
+
const {data} = useQueryData<{name?: string; at?: string}[]>(
|
|
17
|
+
SCHEDULED_RELEASES_QUERY,
|
|
18
|
+
{},
|
|
19
|
+
{listenQuery: '*[_id in path("_.releases.**")]'},
|
|
20
|
+
)
|
|
21
|
+
return useMemo(() => {
|
|
22
|
+
if (!data?.length) return EMPTY
|
|
23
|
+
return new Map(
|
|
24
|
+
data.filter((r): r is {name: string; at: string} => Boolean(r.name && r.at)).map((r) => [r.name, r.at]),
|
|
25
|
+
)
|
|
26
|
+
}, [data])
|
|
27
|
+
}
|
|
@@ -39,7 +39,7 @@ function entryFor(client: SanityClient, documentId: string): Entry {
|
|
|
39
39
|
})
|
|
40
40
|
.catch(() => {})
|
|
41
41
|
void load()
|
|
42
|
-
const sub = client.listen(`*[_id == $id]`, {id}, {visibility: 'query', includeResult: false
|
|
42
|
+
const sub = client.listen(`*[_id == $id]`, {id}, {visibility: 'query', includeResult: false}).subscribe({next: () => void load()})
|
|
43
43
|
e.stop = () => sub.unsubscribe()
|
|
44
44
|
}
|
|
45
45
|
return e
|
|
@@ -14,7 +14,6 @@ export function useTranslationTodos(): ReadonlyMap<string, number> {
|
|
|
14
14
|
const params = useMemo(() => ({type: TRANSLATION_TODO_TYPE}), [])
|
|
15
15
|
const {data} = useQueryData<{documentId: string; count: number}[]>(QUERY, params, {
|
|
16
16
|
listenTypes: [TRANSLATION_TODO_TYPE],
|
|
17
|
-
tag: 'translation-todos',
|
|
18
17
|
enabled: Boolean(config.i18n),
|
|
19
18
|
})
|
|
20
19
|
return useMemo(() => new Map((data ?? []).map((t) => [t.documentId, t.count])), [data])
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {DATA_PREFIX} from './query'
|
|
2
2
|
import type {CollectionColumn} from './collectionColumns'
|
|
3
|
+
import {mergeVersions, type PageStatus, type ScheduleMap} from './status'
|
|
3
4
|
|
|
4
5
|
export type CellValue = string | number | boolean | null
|
|
5
6
|
|
|
@@ -14,8 +15,14 @@ export interface CollectionRow {
|
|
|
14
15
|
/** Id de base (sans `drafts.`), pour ouvrir le document. */
|
|
15
16
|
id: string
|
|
16
17
|
updatedAt: string | null
|
|
17
|
-
/**
|
|
18
|
-
|
|
18
|
+
/** Statut de publication (Draft, Modified, Scheduled, Published). */
|
|
19
|
+
status: PageStatus
|
|
20
|
+
/** Date de publication programmée (statut `scheduled`). */
|
|
21
|
+
scheduledAt?: string | null
|
|
22
|
+
/** Une version publiée existe (l'item est en ligne). */
|
|
23
|
+
live?: boolean
|
|
24
|
+
/** Un brouillon existe (modifications non publiées). */
|
|
25
|
+
hasDraft?: boolean
|
|
19
26
|
cells: Record<string, CollectionCell>
|
|
20
27
|
}
|
|
21
28
|
|
|
@@ -26,8 +33,6 @@ export interface CollectionDoc {
|
|
|
26
33
|
[key: string]: unknown
|
|
27
34
|
}
|
|
28
35
|
|
|
29
|
-
const baseId = (id: string): string => (id.startsWith('drafts.') ? id.slice('drafts.'.length) : id)
|
|
30
|
-
const isDraft = (id: string): boolean => id.startsWith('drafts.')
|
|
31
36
|
|
|
32
37
|
const dateFormatter = new Intl.DateTimeFormat('en-US', {day: '2-digit', month: '2-digit', year: 'numeric'})
|
|
33
38
|
|
|
@@ -57,36 +62,31 @@ function cellOf(raw: unknown, kind: CollectionColumn['kind']): CollectionCell {
|
|
|
57
62
|
|
|
58
63
|
/**
|
|
59
64
|
* Documents → lignes, une par item. Le client (perspective `raw`) renvoie le
|
|
60
|
-
* brouillon
|
|
61
|
-
*
|
|
65
|
+
* brouillon, la version publiée et les versions de release : on les fusionne
|
|
66
|
+
* par id de base (`mergeVersions`, même règle que les pages), en gardant le
|
|
67
|
+
* brouillon s'il existe (valeurs les plus fraîches). Une version de release non
|
|
68
|
+
* programmée ne crée jamais de ligne à part.
|
|
62
69
|
*/
|
|
63
70
|
export function buildCollectionRows(
|
|
64
71
|
docs: readonly CollectionDoc[],
|
|
65
72
|
columns: readonly CollectionColumn[],
|
|
73
|
+
schedules?: ScheduleMap,
|
|
66
74
|
): CollectionRow[] {
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
const entry = byId.get(baseId(doc._id)) ?? {}
|
|
71
|
-
if (isDraft(doc._id)) entry.draft = doc
|
|
72
|
-
else entry.published = doc
|
|
73
|
-
byId.set(baseId(doc._id), entry)
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
const rows: CollectionRow[] = []
|
|
77
|
-
for (const [id, {draft, published}] of byId) {
|
|
78
|
-
const source = draft ?? published
|
|
79
|
-
if (!source) continue
|
|
75
|
+
const valid = docs.filter((doc) => Boolean(doc?._id)).map((doc) => ({...doc, _type: '', _updatedAt: doc._updatedAt ?? undefined}))
|
|
76
|
+
return mergeVersions(valid, schedules).map((merged) => {
|
|
77
|
+
const source = merged.doc as unknown as CollectionDoc
|
|
80
78
|
const cells: Record<string, CollectionCell> = {}
|
|
81
79
|
for (const column of columns) {
|
|
82
80
|
cells[column.key] = cellOf(source[`${DATA_PREFIX}${column.key}`], column.kind)
|
|
83
81
|
}
|
|
84
|
-
|
|
85
|
-
id,
|
|
86
|
-
updatedAt:
|
|
87
|
-
|
|
82
|
+
return {
|
|
83
|
+
id: merged.id,
|
|
84
|
+
updatedAt: merged.updatedAt ?? null,
|
|
85
|
+
status: merged.status,
|
|
86
|
+
scheduledAt: merged.scheduledAt ?? null,
|
|
87
|
+
live: merged.live,
|
|
88
|
+
hasDraft: merged.hasDraft,
|
|
88
89
|
cells,
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
return rows
|
|
90
|
+
}
|
|
91
|
+
})
|
|
92
92
|
}
|
package/src/lib/colors.ts
CHANGED
|
@@ -1,16 +1,13 @@
|
|
|
1
1
|
import type {NcfBoardTag, NcfInternalField} from '../config/types'
|
|
2
2
|
import type {NodeLayout} from './graph'
|
|
3
3
|
import type {PageRow} from './pages'
|
|
4
|
+
import {STATUS_COLORS} from './status'
|
|
4
5
|
|
|
5
6
|
/** Critère qui donne sa couleur à une page. */
|
|
6
7
|
export type ColorBy = 'tag' | 'status' | `field:${string}`
|
|
7
8
|
|
|
8
|
-
/** Couleurs des statuts
|
|
9
|
-
export
|
|
10
|
-
published: '#43d675',
|
|
11
|
-
modified: '#f5a623',
|
|
12
|
-
draft: '#8a8a8a',
|
|
13
|
-
}
|
|
9
|
+
/** Couleurs des statuts : source unique dans `lib/status`. */
|
|
10
|
+
export {STATUS_COLORS}
|
|
14
11
|
|
|
15
12
|
/**
|
|
16
13
|
* Applique une opacité à une couleur hexadécimale.
|
package/src/lib/pages.ts
CHANGED
|
@@ -3,7 +3,7 @@ import {orderByIds} from './pageOrder'
|
|
|
3
3
|
import type {ResolvedDocumentColumn, ResolvedKitConfig} from '../config/types'
|
|
4
4
|
import {pageTypeFor, toPath, toUrl} from './paths'
|
|
5
5
|
import {DATA_PREFIX} from './query'
|
|
6
|
-
import {mergeVersions, type PageStatus, type VersionedDoc} from './status'
|
|
6
|
+
import {mergeVersions, STATUS_ORDER as STATUS_SEQUENCE, type PageStatus, type ScheduleMap, type VersionedDoc} from './status'
|
|
7
7
|
|
|
8
8
|
/** Document tel que le renvoie `buildPagesQuery`. */
|
|
9
9
|
export interface PageDoc extends VersionedDoc {
|
|
@@ -26,6 +26,12 @@ export interface PageRow {
|
|
|
26
26
|
url: string | null
|
|
27
27
|
locale: string | null
|
|
28
28
|
status: PageStatus
|
|
29
|
+
/** Date de publication programmée (statut `scheduled`). */
|
|
30
|
+
scheduledAt?: string | null
|
|
31
|
+
/** Une version publiée existe (l'item est en ligne). */
|
|
32
|
+
live?: boolean
|
|
33
|
+
/** Un brouillon existe (modifications non publiées). */
|
|
34
|
+
hasDraft?: boolean
|
|
29
35
|
updatedAt: string | null
|
|
30
36
|
blockCount: number
|
|
31
37
|
/** Marquée non indexable dans le CMS. */
|
|
@@ -59,8 +65,9 @@ export function buildPageRows(
|
|
|
59
65
|
config: ResolvedKitConfig,
|
|
60
66
|
documentColumns: readonly ResolvedDocumentColumn[] = [],
|
|
61
67
|
redirects: RedirectMap | null = null,
|
|
68
|
+
schedules?: ScheduleMap,
|
|
62
69
|
): PageRow[] {
|
|
63
|
-
return mergeVersions(docs).map((merged) => {
|
|
70
|
+
return mergeVersions(docs, schedules).map((merged) => {
|
|
64
71
|
const doc = merged.doc
|
|
65
72
|
const pageType = pageTypeFor(doc._type, config.pageTypes)
|
|
66
73
|
// URL FINALE (page codée, dossier de la langue, redirection du Studio suivie).
|
|
@@ -74,6 +81,9 @@ export function buildPageRows(
|
|
|
74
81
|
url: toUrl(config.previewUrl, path),
|
|
75
82
|
locale: doc.ncfLocale ?? null,
|
|
76
83
|
status: merged.status,
|
|
84
|
+
scheduledAt: merged.scheduledAt ?? null,
|
|
85
|
+
live: merged.live,
|
|
86
|
+
hasDraft: merged.hasDraft,
|
|
77
87
|
updatedAt: merged.updatedAt ?? null,
|
|
78
88
|
blockCount: doc.ncfBlockCount ?? 0,
|
|
79
89
|
noindex: Boolean(doc.ncfNoindex),
|
|
@@ -110,7 +120,10 @@ export type SortKey =
|
|
|
110
120
|
export type SortDirection = 'asc' | 'desc'
|
|
111
121
|
|
|
112
122
|
/** Ordre d'affichage des statuts : du plus « à traiter » au plus stable. */
|
|
113
|
-
const STATUS_ORDER
|
|
123
|
+
const STATUS_ORDER = Object.fromEntries(STATUS_SEQUENCE.map((status, index) => [status, index])) as Record<
|
|
124
|
+
PageStatus,
|
|
125
|
+
number
|
|
126
|
+
>
|
|
114
127
|
|
|
115
128
|
export interface SortOptions {
|
|
116
129
|
/** Ordre de déclaration des types, pour le tri « structure ». */
|
package/src/lib/status.ts
CHANGED
|
@@ -3,7 +3,17 @@
|
|
|
3
3
|
* release) pour en déduire un statut unique affichable.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
export type PageStatus = 'published' | '
|
|
6
|
+
export type PageStatus = 'published' | 'modified' | 'scheduled' | 'draft'
|
|
7
|
+
|
|
8
|
+
/** Ordre d'affichage et de tri : du plus urgent au plus calme. */
|
|
9
|
+
export const STATUS_ORDER: readonly PageStatus[] = ['scheduled', 'modified', 'draft', 'published']
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Publications programmées en cours : nom de la release -> date de publication
|
|
13
|
+
* (ISO). Seules les releases à l'état `scheduled` y figurent (« Scheduled
|
|
14
|
+
* drafts » comme Releases : c'est le même mécanisme dans Sanity).
|
|
15
|
+
*/
|
|
16
|
+
export type ScheduleMap = ReadonlyMap<string, string>
|
|
7
17
|
|
|
8
18
|
export interface VersionedDoc {
|
|
9
19
|
_id: string
|
|
@@ -49,6 +59,12 @@ export interface MergedDoc<T extends VersionedDoc> {
|
|
|
49
59
|
versions: Record<string, T>
|
|
50
60
|
/** Date de la modification la plus récente, toutes variantes confondues. */
|
|
51
61
|
updatedAt?: string
|
|
62
|
+
/** Date de publication programmée la plus proche (statut `scheduled`). */
|
|
63
|
+
scheduledAt?: string
|
|
64
|
+
/** Une version publiée existe : l'item est sur le site. */
|
|
65
|
+
live: boolean
|
|
66
|
+
/** Un brouillon existe (modifications non publiées). */
|
|
67
|
+
hasDraft: boolean
|
|
52
68
|
}
|
|
53
69
|
|
|
54
70
|
const mostRecent = (a?: string, b?: string): string | undefined => {
|
|
@@ -60,14 +76,20 @@ const mostRecent = (a?: string, b?: string): string | undefined => {
|
|
|
60
76
|
/**
|
|
61
77
|
* Regroupe les documents par identifiant de base.
|
|
62
78
|
*
|
|
63
|
-
* -
|
|
64
|
-
* - brouillon seul -> 'draft' (jamais publié, absent du site)
|
|
79
|
+
* - publication programmée -> 'scheduled' (prime sur tout : c'est ce qui va changer)
|
|
65
80
|
* - publié + brouillon -> 'modified' (modifications non publiées)
|
|
81
|
+
* - brouillon seul -> 'draft' (jamais publié ou retiré du site)
|
|
82
|
+
* - publié seul -> 'published'
|
|
66
83
|
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
84
|
+
* Une version de release ne compte que si sa release est programmée
|
|
85
|
+
* (`schedules`) : une release en préparation n'est ni en ligne ni attendue.
|
|
86
|
+
* Un item qui n'existe QUE dans une release programmée (brouillon programmé,
|
|
87
|
+
* jamais publié) est bien listé, en `scheduled`.
|
|
69
88
|
*/
|
|
70
|
-
export function mergeVersions<T extends VersionedDoc>(
|
|
89
|
+
export function mergeVersions<T extends VersionedDoc>(
|
|
90
|
+
docs: readonly T[],
|
|
91
|
+
schedules: ScheduleMap = new Map(),
|
|
92
|
+
): MergedDoc<T>[] {
|
|
71
93
|
const groups = new Map<string, {published?: T; draft?: T; versions: Record<string, T>}>()
|
|
72
94
|
|
|
73
95
|
for (const doc of docs) {
|
|
@@ -89,15 +111,25 @@ export function mergeVersions<T extends VersionedDoc>(docs: readonly T[]): Merge
|
|
|
89
111
|
const merged: MergedDoc<T>[] = []
|
|
90
112
|
|
|
91
113
|
for (const [id, group] of groups) {
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
114
|
+
const scheduled = Object.keys(group.versions)
|
|
115
|
+
.map((release) => ({release, at: schedules.get(release)}))
|
|
116
|
+
.filter((entry): entry is {release: string; at: string} => Boolean(entry.at))
|
|
117
|
+
.sort((a, b) => (a.at < b.at ? -1 : a.at > b.at ? 1 : 0))[0]
|
|
118
|
+
|
|
119
|
+
const reference =
|
|
120
|
+
group.draft ?? group.published ?? (scheduled ? group.versions[scheduled.release] : undefined)
|
|
121
|
+
// Une version de release non programmée, seule, n'est pas un item du site.
|
|
122
|
+
if (!reference) continue
|
|
123
|
+
|
|
124
|
+
const status: PageStatus = scheduled
|
|
125
|
+
? 'scheduled'
|
|
126
|
+
: group.published
|
|
127
|
+
? group.draft
|
|
128
|
+
? 'modified'
|
|
129
|
+
: 'published'
|
|
130
|
+
: 'draft'
|
|
131
|
+
|
|
132
|
+
const updatedAt = mostRecent(group.published?._updatedAt, group.draft?._updatedAt) ?? reference._updatedAt
|
|
101
133
|
|
|
102
134
|
merged.push({
|
|
103
135
|
id,
|
|
@@ -106,17 +138,81 @@ export function mergeVersions<T extends VersionedDoc>(docs: readonly T[]): Merge
|
|
|
106
138
|
...(group.published ? {published: group.published} : {}),
|
|
107
139
|
...(group.draft ? {draft: group.draft} : {}),
|
|
108
140
|
versions: group.versions,
|
|
109
|
-
...(
|
|
110
|
-
|
|
111
|
-
|
|
141
|
+
...(updatedAt ? {updatedAt} : {}),
|
|
142
|
+
...(scheduled ? {scheduledAt: scheduled.at} : {}),
|
|
143
|
+
live: Boolean(group.published),
|
|
144
|
+
hasDraft: Boolean(group.draft),
|
|
112
145
|
})
|
|
113
146
|
}
|
|
114
147
|
|
|
115
148
|
return merged
|
|
116
149
|
}
|
|
117
150
|
|
|
118
|
-
export const STATUS_LABELS: Record<
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
151
|
+
export const STATUS_LABELS: Record<
|
|
152
|
+
PageStatus,
|
|
153
|
+
{label: string; tone: 'positive' | 'caution' | 'primary' | 'default'; help: string}
|
|
154
|
+
> = {
|
|
155
|
+
published: {label: 'Published', tone: 'positive', help: 'Live, up to date.'},
|
|
156
|
+
modified: {label: 'Modified', tone: 'caution', help: 'Live, with changes not published yet.'},
|
|
157
|
+
scheduled: {label: 'Scheduled', tone: 'primary', help: 'Publication scheduled: it goes live automatically on the date shown.'},
|
|
158
|
+
draft: {label: 'Draft', tone: 'default', help: 'Not on the site: never published, or removed from the site (content kept in the draft).'},
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Couleur de chaque statut, SOURCE UNIQUE pour le kit (pastilles, filtres,
|
|
163
|
+
* minimap et couleurs du board) : gris, orange, bleu, vert.
|
|
164
|
+
*/
|
|
165
|
+
export const STATUS_COLORS: Record<PageStatus, string> = {
|
|
166
|
+
draft: '#8b93a1',
|
|
167
|
+
modified: '#f08a24',
|
|
168
|
+
scheduled: '#2f7bf5',
|
|
169
|
+
published: '#2fb466',
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Données utiles à l'infobulle d'un statut (toutes facultatives). */
|
|
173
|
+
export interface StatusDetails {
|
|
174
|
+
status: PageStatus
|
|
175
|
+
scheduledAt?: string | null
|
|
176
|
+
live?: boolean
|
|
177
|
+
hasDraft?: boolean
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const scheduleFormat = new Intl.DateTimeFormat(undefined, {
|
|
181
|
+
day: 'numeric',
|
|
182
|
+
month: 'short',
|
|
183
|
+
year: 'numeric',
|
|
184
|
+
hour: '2-digit',
|
|
185
|
+
minute: '2-digit',
|
|
186
|
+
})
|
|
187
|
+
const shortFormat = new Intl.DateTimeFormat(undefined, {day: 'numeric', month: 'short', hour: '2-digit', minute: '2-digit'})
|
|
188
|
+
|
|
189
|
+
/** Date de programmation lisible (« 1 Mar 2027, 10:00 », fuseau du navigateur). */
|
|
190
|
+
export function formatSchedule(iso: string | null | undefined, short = false): string {
|
|
191
|
+
if (!iso) return ''
|
|
192
|
+
const date = new Date(iso)
|
|
193
|
+
if (Number.isNaN(date.getTime())) return ''
|
|
194
|
+
return (short ? shortFormat : scheduleFormat).format(date)
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** Lignes de l'infobulle : ce qu'est le statut, puis ce qu'il faut savoir en plus. */
|
|
198
|
+
export function statusHelp(details: StatusDetails): string[] {
|
|
199
|
+
const {status, scheduledAt, live, hasDraft} = details
|
|
200
|
+
if (status === 'scheduled') {
|
|
201
|
+
const lines = [
|
|
202
|
+
live
|
|
203
|
+
? 'Currently live: this update goes live automatically on the date shown.'
|
|
204
|
+
: 'Not on the site yet: it goes live automatically on the date shown.',
|
|
205
|
+
]
|
|
206
|
+
if (hasDraft) lines.push('Also has other changes not scheduled (draft).')
|
|
207
|
+
lines.push('Change the date in Scheduled drafts / Releases.')
|
|
208
|
+
return scheduledAt ? [`Scheduled for ${formatSchedule(scheduledAt)}.`, ...lines] : lines
|
|
209
|
+
}
|
|
210
|
+
return [STATUS_LABELS[status].help]
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** Nombre d'items par statut (filtres). */
|
|
214
|
+
export function countStatuses(rows: readonly {status: PageStatus}[]): Record<PageStatus, number> {
|
|
215
|
+
const counts: Record<PageStatus, number> = {published: 0, modified: 0, scheduled: 0, draft: 0}
|
|
216
|
+
for (const row of rows) counts[row.status] += 1
|
|
217
|
+
return counts
|
|
122
218
|
}
|