@nocode-factory/sanity-kit 2.24.0 → 2.25.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nocode-factory/sanity-kit",
3
- "version": "2.24.0",
3
+ "version": "2.25.0",
4
4
  "description": "Outils Studio de NoCode Factory : Pages (liste + board), Composants, Historique",
5
5
  "license": "UNLICENSED",
6
6
  "private": false,
@@ -0,0 +1,85 @@
1
+ import {createElement, useEffect, useState, type ComponentType} from 'react'
2
+ import {useFormValue, type InputProps} from 'sanity'
3
+ import {usePresentationNavigate, usePresentationParams} from 'sanity/presentation'
4
+ import {Button, Card, Flex, Stack, Text} from '@sanity/ui'
5
+ import type {ResolvedKitConfig} from '../config/types'
6
+ import {useKitClient} from '../hooks/useKitClient'
7
+ import {localeOfPreview} from '../lib/previewLocale'
8
+
9
+ /**
10
+ * Presentation : le panneau d'édition DOIT montrer la langue de l'aperçu. Si le
11
+ * document édité n'est pas dans la langue de la page affichée (ex. aperçu EN, panneau
12
+ * FR), un bandeau le dit en haut du formulaire, avec un bouton qui ouvre la bonne
13
+ * version. Évite de modifier le français en croyant modifier l'anglais.
14
+ * Structure stable : le formulaire garde sa place, que le bandeau soit là ou non.
15
+ */
16
+ export function withLanguageMismatch(Inner: ComponentType<InputProps>, config: ResolvedKitConfig): ComponentType<InputProps> {
17
+ const i18n = config.i18n
18
+ function LanguageMismatch(props: InputProps) {
19
+ const inner = createElement(Inner, props)
20
+ if (!i18n || props.path.length > 0) return inner
21
+ return (
22
+ <Stack gap={4}>
23
+ <MismatchCheck />
24
+ {inner}
25
+ </Stack>
26
+ )
27
+ }
28
+
29
+ function MismatchCheck() {
30
+ const params = usePresentationParams(false)
31
+ const language = useFormValue([i18n?.field ?? 'language']) as string | undefined
32
+ const shown = localeOfPreview(params?.preview, i18n)
33
+ if (!params || !language || !shown || language === shown) return null
34
+ return <MismatchBanner editing={language} shown={shown} />
35
+ }
36
+
37
+ function MismatchBanner(props: {editing: string; shown: string}) {
38
+ const {editing, shown} = props
39
+ const navigate = usePresentationNavigate()
40
+ const client = useKitClient()
41
+ const id = (useFormValue(['_id']) as string | undefined)?.replace(/^drafts\./, '')
42
+ const type = useFormValue(['_type']) as string | undefined
43
+ const [target, setTarget] = useState<string | null | undefined>(undefined)
44
+
45
+ useEffect(() => {
46
+ if (!id) return
47
+ let alive = true
48
+ client
49
+ .fetch<string | null>(
50
+ `*[_type == "translation.metadata" && references($id)][0].translations[_key == $lang || language == $lang][0].value._ref`,
51
+ {id, lang: shown},
52
+ )
53
+ .then((ref) => alive && setTarget(ref ?? null))
54
+ .catch(() => alive && setTarget(null))
55
+ return () => {
56
+ alive = false
57
+ }
58
+ }, [client, id, shown])
59
+
60
+ return (
61
+ <Card tone="caution" padding={3} radius={2} border>
62
+ <Flex align="center" gap={3} wrap="wrap">
63
+ <Text size={1} weight="semibold" style={{flex: 1, minWidth: 200}}>
64
+ You are editing the {editing.toUpperCase()} version. The preview shows {shown.toUpperCase()}.
65
+ </Text>
66
+ {target && type ? (
67
+ <Button
68
+ tone="caution"
69
+ fontSize={1}
70
+ padding={2}
71
+ text={`Edit the ${shown.toUpperCase()} version`}
72
+ onClick={() => navigate(undefined, {type, id: target})}
73
+ />
74
+ ) : target === null ? (
75
+ <Text size={1} muted>
76
+ No {shown.toUpperCase()} version of this document yet.
77
+ </Text>
78
+ ) : null}
79
+ </Flex>
80
+ </Card>
81
+ )
82
+ }
83
+
84
+ return LanguageMismatch
85
+ }
package/src/index.ts CHANGED
@@ -45,6 +45,7 @@ import {RemoveFromSiteAction} from './actions/RemoveFromSite'
45
45
  import {createSlugUrlInput, isSlugInput} from './form/SlugUrlInput'
46
46
  import {withTranslationTodo} from './form/TranslationTodoInput'
47
47
  import {withPublishedHint} from './form/PublishedHint'
48
+ import {withLanguageMismatch} from './form/LanguageMismatch'
48
49
  import {withPropagation} from './actions/PropagatePublish'
49
50
 
50
51
  /** Champs du formulaire : adresse sous le Slug d'une page, pastille sur les sections. */
@@ -56,7 +57,8 @@ function createKitInput(config: Parameters<typeof createOverrideInput>[0]): Comp
56
57
  }
57
58
  // Traductions : pastille « À traduire » sur les champs modifiés dans la version principale.
58
59
  // Vue « Published » : « Switch to Drafts to edit » au survol d'un champ.
59
- return withPublishedHint(config.i18n ? withTranslationTodo(KitInput, config) : KitInput)
60
+ // Presentation : bandeau si le document édité n'est pas dans la langue de l'aperçu.
61
+ return withPublishedHint(config.i18n ? withLanguageMismatch(withTranslationTodo(KitInput, config), config) : KitInput)
60
62
  }
61
63
 
62
64
  export const ncfKit = definePlugin<NcfKitConfig>((options) => {
@@ -0,0 +1,17 @@
1
+ import type {ResolvedI18n} from '../config/types'
2
+
3
+ /**
4
+ * Langue de la page affichée dans l'aperçu, d'après son adresse : premier segment
5
+ * s'il est une langue autre que la principale (/en/...), sinon la langue principale.
6
+ */
7
+ export function localeOfPreview(preview: string | null | undefined, i18n: ResolvedI18n | null): string | null {
8
+ if (!i18n || !preview) return null
9
+ let pathname: string
10
+ try {
11
+ pathname = new URL(preview, 'http://preview.local').pathname
12
+ } catch {
13
+ return null
14
+ }
15
+ const first = pathname.split('/').filter(Boolean)[0]
16
+ return first && first !== i18n.defaultLocale && i18n.locales.includes(first) ? first : i18n.defaultLocale
17
+ }
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.24.0'
6
+ export const KIT_VERSION = '2.25.0'