@nocode-factory/sanity-kit 2.65.0-rc.4 → 2.65.0-rc.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nocode-factory/sanity-kit",
3
- "version": "2.65.0-rc.4",
3
+ "version": "2.65.0-rc.6",
4
4
  "description": "Outils Studio de NoCode Factory : Pages (liste + board), Composants, Historique",
5
5
  "license": "UNLICENSED",
6
6
  "private": false,
@@ -1,4 +1,4 @@
1
- import {useState} from 'react'
1
+ import {useEffect, useState} from 'react'
2
2
  import {Box, Button, Card, Checkbox, Flex, Stack, Text} from '@sanity/ui'
3
3
  import {useToast} from '@sanity/ui/toast'
4
4
  import {useSchema, type DocumentActionComponent, type DocumentActionProps} from 'sanity'
@@ -11,6 +11,7 @@ import {translateValuesSettled} from '../lib/aiTranslate'
11
11
  import {createTranslatedVersion, translationsOf} from '../lib/translateDocument'
12
12
  import {isReplaceable, readTrace, recordTrace} from '../lib/translationTrace'
13
13
  import {localizeReferences} from '../lib/localizeReferences'
14
+ import {linkedWithoutVersion} from '../lib/linkedTranslations'
14
15
 
15
16
  type Doc = Record<string, unknown>
16
17
  type Client = ReturnType<typeof useKitClient>
@@ -150,6 +151,18 @@ function PropagateDialog(props: {
150
151
  const field = config.i18n?.field ?? 'language'
151
152
  const source = String(after[field] ?? '')
152
153
  const [targets, setTargets] = useState<Target[]>(props.targets)
154
+ // Pages liées sans version publiée dans une langue : la version de cette langue sera
155
+ // créée, mais ne pourra pas être publiée avant elles (dit AVANT de créer).
156
+ const [unlinked, setUnlinked] = useState<Map<string, string[]>>(new Map())
157
+ useEffect(() => {
158
+ let alive = true
159
+ linkedWithoutVersion(client, after, props.targets.map((t) => t.language), field)
160
+ .then((m) => alive && setUnlinked(m))
161
+ .catch(() => {})
162
+ return () => {
163
+ alive = false
164
+ }
165
+ }, [client, after, props.targets, field])
153
166
  const [busy, setBusy] = useState(false)
154
167
  const toast = useToast()
155
168
  const [error, setError] = useState<string | null>(null)
@@ -318,6 +331,17 @@ function PropagateDialog(props: {
318
331
  {mode === 'create' ? `New version (${t.result.toTranslate.length} texts)` : summary(t.result)}
319
332
  {t.edited.length ? ` · ${t.edited.length} edited by hand` : ''}
320
333
  </Text>
334
+ {unlinked.get(t.language)?.length ? (
335
+ <Card tone="caution" padding={2} radius={2}>
336
+ <Text size={1}>
337
+ Links to {unlinked.get(t.language)!.length} page{unlinked.get(t.language)!.length > 1 ? 's' : ''} with no published {t.language.toUpperCase()} version (
338
+ {unlinked.get(t.language)!.slice(0, 4).map((title) => `« ${title} »`).join(', ')}
339
+ {unlinked.get(t.language)!.length > 4 ? ', …' : ''}): this version can be saved as a draft, but not published until
340
+ {unlinked.get(t.language)!.length > 1 ? ' those pages are' : ' that page is'} translated and published. Not meant to be translated?
341
+ In the {t.language.toUpperCase()} version, link to its address directly (link type URL) or remove the link.
342
+ </Text>
343
+ </Card>
344
+ ) : null}
321
345
  </Stack>
322
346
  </Box>
323
347
  </Flex>
@@ -0,0 +1,51 @@
1
+ import type {SanityClient} from 'sanity'
2
+
3
+ type Doc = Record<string, unknown>
4
+
5
+ /** Identifiants des documents référencés par `doc` (liens, pages liées...), hors images et fichiers. */
6
+ export function referencedIds(doc: unknown): string[] {
7
+ const out = new Set<string>()
8
+ const walk = (node: unknown) => {
9
+ if (Array.isArray(node)) return node.forEach(walk)
10
+ if (!node || typeof node !== 'object') return
11
+ const ref = (node as {_ref?: unknown})._ref
12
+ if (typeof ref === 'string' && !/^(image|file)-/.test(ref)) out.add(ref.replace(/^drafts\./, ''))
13
+ for (const [k, v] of Object.entries(node)) if (!k.startsWith('_')) walk(v)
14
+ }
15
+ walk(doc)
16
+ return [...out]
17
+ }
18
+
19
+ /**
20
+ * Pages liées depuis `doc` qui n'ont pas de version PUBLIÉE dans chacune des `languages`
21
+ * (kit >= 2.65.0). La version traduite d'un document qui les vise ne peut pas être
22
+ * publiée (« Cette page est en FR et n'a pas de version EN », vu sur JAF) : on le dit
23
+ * AVANT de la créer, avec les pages à traduire d'abord.
24
+ */
25
+ export async function linkedWithoutVersion(
26
+ client: SanityClient,
27
+ doc: Doc,
28
+ languages: string[],
29
+ field = 'language',
30
+ ): Promise<Map<string, string[]>> {
31
+ const result = new Map<string, string[]>()
32
+ const ids = referencedIds(doc)
33
+ if (!ids.length || !languages.length) return result
34
+ const linked = await client.fetch<{_id: string; title: string; language?: string; versions: {language?: string; id?: string}[] | null}[]>(
35
+ `*[_id in $ids && defined(${field})]{
36
+ _id, "title": coalesce(title, name, slug.current, _id), "language": ${field},
37
+ "versions": *[_type == "translation.metadata" && references(^._id)][0].translations[]{"language": coalesce(language, _key), "id": value._ref}
38
+ }`,
39
+ {ids},
40
+ )
41
+ const versionIds = linked.flatMap((d) => (d.versions ?? []).map((v) => v.id ?? '')).filter(Boolean)
42
+ const published = new Set(versionIds.length ? await client.fetch<string[]>(`*[_id in $ids]._id`, {ids: versionIds}) : [])
43
+ for (const language of languages)
44
+ for (const d of linked) {
45
+ if (d.language === language) continue
46
+ const version = (d.versions ?? []).find((v) => v.language === language)
47
+ if (version?.id && published.has(version.id)) continue
48
+ result.set(language, [...(result.get(language) ?? []), d.title])
49
+ }
50
+ return result
51
+ }
package/src/version.ts CHANGED
@@ -3,4 +3,4 @@
3
3
  * quelle version. Recopiée de `package.json` par `npm version` (scripts/sync-version.mjs) ; un test
4
4
  * le vérifie.
5
5
  */
6
- export const KIT_VERSION = '2.65.0-rc.4'
6
+ export const KIT_VERSION = '2.65.0-rc.6'