@nocode-factory/sanity-kit 2.14.0 → 2.14.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -1
- package/dist/index.js +40 -26
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/actions/BatchPublish.tsx +35 -12
- package/src/version.ts +1 -1
package/package.json
CHANGED
|
@@ -34,29 +34,49 @@ function useDrafts(currentId: string | null) {
|
|
|
34
34
|
const schema = useSchema()
|
|
35
35
|
const [rows, setRows] = useState<{drafts: DraftRow[]; published: string[]} | null>(null)
|
|
36
36
|
|
|
37
|
+
const [reloadKey, setReloadKey] = useState(0)
|
|
38
|
+
|
|
37
39
|
useEffect(() => {
|
|
38
40
|
let cancelled = false
|
|
39
41
|
let timer: ReturnType<typeof setTimeout> | undefined
|
|
42
|
+
let retry: ReturnType<typeof setTimeout> | undefined
|
|
43
|
+
let subscription: {unsubscribe: () => void} | undefined
|
|
40
44
|
const load = async () => {
|
|
41
45
|
const drafts = await client.fetch<DraftRow[]>(DRAFTS_QUERY)
|
|
42
46
|
const published = await client.fetch<string[]>(PUBLISHED_IDS_QUERY, {ids: drafts.map((d) => publishedIdOf(d._id))})
|
|
43
47
|
if (!cancelled) setRows({drafts, published})
|
|
44
48
|
}
|
|
45
49
|
load().catch(() => {
|
|
46
|
-
if (!cancelled) setRows({drafts: [], published: []})
|
|
50
|
+
if (!cancelled) setRows((prev) => prev ?? {drafts: [], published: []})
|
|
47
51
|
})
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
52
|
+
// Écoute en direct, best effort : si le service temps réel de Sanity tombe, on ne
|
|
53
|
+
// fait JAMAIS remonter l'erreur (elle s'afficherait en « Uncaught error » dans tout
|
|
54
|
+
// le Studio). On réessaie plus tard, et la liste est relue à l'ouverture de la
|
|
55
|
+
// fenêtre de toute façon.
|
|
56
|
+
const listen = (delay: number) => {
|
|
57
|
+
subscription = client
|
|
58
|
+
.listen(`*[_id in path("drafts.**") || _id in path("*")]`, {}, {visibility: 'query', includeResult: false, events: ['mutation']})
|
|
59
|
+
.subscribe({
|
|
60
|
+
next: () => {
|
|
61
|
+
clearTimeout(timer)
|
|
62
|
+
timer = setTimeout(() => void load().catch(() => {}), 500)
|
|
63
|
+
},
|
|
64
|
+
error: () => {
|
|
65
|
+
if (cancelled) return
|
|
66
|
+
retry = setTimeout(() => listen(Math.min(delay * 2, 120_000)), delay)
|
|
67
|
+
},
|
|
68
|
+
})
|
|
69
|
+
}
|
|
70
|
+
listen(15_000)
|
|
54
71
|
return () => {
|
|
55
72
|
cancelled = true
|
|
56
73
|
clearTimeout(timer)
|
|
57
|
-
|
|
74
|
+
clearTimeout(retry)
|
|
75
|
+
subscription?.unsubscribe()
|
|
58
76
|
}
|
|
59
|
-
}, [client])
|
|
77
|
+
}, [client, reloadKey])
|
|
78
|
+
|
|
79
|
+
const reload = useCallback(() => setReloadKey((k) => k + 1), [])
|
|
60
80
|
|
|
61
81
|
const drafts = useMemo(
|
|
62
82
|
() =>
|
|
@@ -69,7 +89,7 @@ function useDrafts(currentId: string | null) {
|
|
|
69
89
|
: null,
|
|
70
90
|
[rows, schema, currentId],
|
|
71
91
|
)
|
|
72
|
-
return drafts
|
|
92
|
+
return {drafts, reload}
|
|
73
93
|
}
|
|
74
94
|
|
|
75
95
|
function isDocumentSchema(type: unknown): boolean {
|
|
@@ -93,7 +113,7 @@ function isDocumentSchema(type: unknown): boolean {
|
|
|
93
113
|
export function createBatchPublishAction(original: DocumentActionComponent): DocumentActionComponent {
|
|
94
114
|
const BatchPublishAction: DocumentActionComponent = (props: DocumentActionProps) => {
|
|
95
115
|
const base = original(props)
|
|
96
|
-
const drafts = useDrafts(props.id)
|
|
116
|
+
const {drafts, reload} = useDrafts(props.id)
|
|
97
117
|
const [open, setOpen] = useState(false)
|
|
98
118
|
|
|
99
119
|
if (!base || props.release || props.version || !drafts) return base
|
|
@@ -107,7 +127,10 @@ export function createBatchPublishAction(original: DocumentActionComponent): Doc
|
|
|
107
127
|
title: 'Choisir les brouillons à publier, ensemble',
|
|
108
128
|
icon: PublishIcon,
|
|
109
129
|
disabled: false,
|
|
110
|
-
onHandle: () =>
|
|
130
|
+
onHandle: () => {
|
|
131
|
+
reload()
|
|
132
|
+
setOpen(true)
|
|
133
|
+
},
|
|
111
134
|
dialog: open
|
|
112
135
|
? {
|
|
113
136
|
type: 'dialog',
|
package/src/version.ts
CHANGED