@jsudc/react 0.0.0-stage → 0.1.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.
Files changed (253) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +228 -2
  3. package/dist/calendar/CalendarRenderer.d.ts +11 -0
  4. package/dist/calendar/CalendarRenderer.js +343 -0
  5. package/dist/calendar/CalendarRenderer.js.map +1 -0
  6. package/dist/calendar/EventBlock.d.ts +16 -0
  7. package/dist/calendar/EventBlock.js +57 -0
  8. package/dist/calendar/EventBlock.js.map +1 -0
  9. package/dist/calendar/EventDetail.d.ts +20 -0
  10. package/dist/calendar/EventDetail.js +186 -0
  11. package/dist/calendar/EventDetail.js.map +1 -0
  12. package/dist/calendar/MonthGrid.d.ts +19 -0
  13. package/dist/calendar/MonthGrid.js +120 -0
  14. package/dist/calendar/MonthGrid.js.map +1 -0
  15. package/dist/calendar/TimeGrid.d.ts +24 -0
  16. package/dist/calendar/TimeGrid.js +330 -0
  17. package/dist/calendar/TimeGrid.js.map +1 -0
  18. package/dist/calendar/model.d.ts +97 -0
  19. package/dist/calendar/model.js +279 -0
  20. package/dist/calendar/model.js.map +1 -0
  21. package/dist/cards/CardRenderer.d.ts +24 -0
  22. package/dist/cards/CardRenderer.js +274 -0
  23. package/dist/cards/CardRenderer.js.map +1 -0
  24. package/dist/cards/cardModel.d.ts +48 -0
  25. package/dist/cards/cardModel.js +86 -0
  26. package/dist/cards/cardModel.js.map +1 -0
  27. package/dist/chart/ChartRenderer.d.ts +16 -0
  28. package/dist/chart/ChartRenderer.js +529 -0
  29. package/dist/chart/ChartRenderer.js.map +1 -0
  30. package/dist/chart/ChartWindowControl.d.ts +13 -0
  31. package/dist/chart/ChartWindowControl.js +99 -0
  32. package/dist/chart/ChartWindowControl.js.map +1 -0
  33. package/dist/chart/model.d.ts +45 -0
  34. package/dist/chart/model.js +179 -0
  35. package/dist/chart/model.js.map +1 -0
  36. package/dist/chart/useChartWindow.d.ts +18 -0
  37. package/dist/chart/useChartWindow.js +70 -0
  38. package/dist/chart/useChartWindow.js.map +1 -0
  39. package/dist/chart/window.d.ts +50 -0
  40. package/dist/chart/window.js +130 -0
  41. package/dist/chart/window.js.map +1 -0
  42. package/dist/components/ui/button.d.ts +21 -0
  43. package/dist/components/ui/button.js +30 -0
  44. package/dist/components/ui/button.js.map +1 -0
  45. package/dist/components/ui/checkbox.d.ts +2 -0
  46. package/dist/components/ui/checkbox.js +23 -0
  47. package/dist/components/ui/checkbox.js.map +1 -0
  48. package/dist/components/ui/dialog.d.ts +14 -0
  49. package/dist/components/ui/dialog.js +52 -0
  50. package/dist/components/ui/dialog.js.map +1 -0
  51. package/dist/components/ui/dropdown-menu.d.ts +12 -0
  52. package/dist/components/ui/dropdown-menu.js +35 -0
  53. package/dist/components/ui/dropdown-menu.js.map +1 -0
  54. package/dist/components/ui/input.d.ts +4 -0
  55. package/dist/components/ui/input.js +24 -0
  56. package/dist/components/ui/input.js.map +1 -0
  57. package/dist/components/ui/misc.d.ts +6 -0
  58. package/dist/components/ui/misc.js +24 -0
  59. package/dist/components/ui/misc.js.map +1 -0
  60. package/dist/components/ui/popover.d.ts +6 -0
  61. package/dist/components/ui/popover.js +21 -0
  62. package/dist/components/ui/popover.js.map +1 -0
  63. package/dist/components/ui/segmented.d.ts +18 -0
  64. package/dist/components/ui/segmented.js +35 -0
  65. package/dist/components/ui/segmented.js.map +1 -0
  66. package/dist/components/ui/select.d.ts +14 -0
  67. package/dist/components/ui/select.js +39 -0
  68. package/dist/components/ui/select.js.map +1 -0
  69. package/dist/components/ui/toast.d.ts +14 -0
  70. package/dist/components/ui/toast.js +72 -0
  71. package/dist/components/ui/toast.js.map +1 -0
  72. package/dist/components/ui/tooltip.d.ts +9 -0
  73. package/dist/components/ui/tooltip.js +19 -0
  74. package/dist/components/ui/tooltip.js.map +1 -0
  75. package/dist/data-view/DocTab.d.ts +8 -0
  76. package/dist/data-view/DocTab.js +26 -0
  77. package/dist/data-view/DocTab.js.map +1 -0
  78. package/dist/data-view/FilePreview.d.ts +10 -0
  79. package/dist/data-view/FilePreview.js +71 -0
  80. package/dist/data-view/FilePreview.js.map +1 -0
  81. package/dist/data-view/FilterBuilder.d.ts +26 -0
  82. package/dist/data-view/FilterBuilder.js +350 -0
  83. package/dist/data-view/FilterBuilder.js.map +1 -0
  84. package/dist/data-view/FilterChips.d.ts +12 -0
  85. package/dist/data-view/FilterChips.js +295 -0
  86. package/dist/data-view/FilterChips.js.map +1 -0
  87. package/dist/data-view/FolderBar.d.ts +5 -0
  88. package/dist/data-view/FolderBar.js +46 -0
  89. package/dist/data-view/FolderBar.js.map +1 -0
  90. package/dist/data-view/FolderEntry.d.ts +13 -0
  91. package/dist/data-view/FolderEntry.js +37 -0
  92. package/dist/data-view/FolderEntry.js.map +1 -0
  93. package/dist/data-view/Freshness.d.ts +18 -0
  94. package/dist/data-view/Freshness.js +86 -0
  95. package/dist/data-view/Freshness.js.map +1 -0
  96. package/dist/data-view/SearchBox.d.ts +7 -0
  97. package/dist/data-view/SearchBox.js +65 -0
  98. package/dist/data-view/SearchBox.js.map +1 -0
  99. package/dist/data-view/SourceTab.d.ts +9 -0
  100. package/dist/data-view/SourceTab.js +103 -0
  101. package/dist/data-view/SourceTab.js.map +1 -0
  102. package/dist/data-view/UdcDataView.d.ts +8 -0
  103. package/dist/data-view/UdcDataView.js +366 -0
  104. package/dist/data-view/UdcDataView.js.map +1 -0
  105. package/dist/data-view/compose.d.ts +24 -0
  106. package/dist/data-view/compose.js +86 -0
  107. package/dist/data-view/compose.js.map +1 -0
  108. package/dist/data-view/folder.d.ts +13 -0
  109. package/dist/data-view/folder.js +52 -0
  110. package/dist/data-view/folder.js.map +1 -0
  111. package/dist/data-view/liveView.d.ts +20 -0
  112. package/dist/data-view/liveView.js +67 -0
  113. package/dist/data-view/liveView.js.map +1 -0
  114. package/dist/data-view/urlState.d.ts +44 -0
  115. package/dist/data-view/urlState.js +139 -0
  116. package/dist/data-view/urlState.js.map +1 -0
  117. package/dist/data-view/useDataViewState.d.ts +36 -0
  118. package/dist/data-view/useDataViewState.js +69 -0
  119. package/dist/data-view/useDataViewState.js.map +1 -0
  120. package/dist/i18n/i18n.d.ts +71 -0
  121. package/dist/i18n/i18n.js +151 -0
  122. package/dist/i18n/i18n.js.map +1 -0
  123. package/dist/i18n/translations.d.ts +16 -0
  124. package/dist/i18n/translations.js +959 -0
  125. package/dist/i18n/translations.js.map +1 -0
  126. package/dist/icons/lucide.generated.d.ts +2 -0
  127. package/dist/icons/lucide.generated.js +32 -0
  128. package/dist/icons/lucide.generated.js.map +1 -0
  129. package/dist/icons/resolve.d.ts +9 -0
  130. package/dist/icons/resolve.js +25 -0
  131. package/dist/icons/resolve.js.map +1 -0
  132. package/dist/index.d.ts +27 -0
  133. package/dist/index.js +28 -0
  134. package/dist/kanban/CardDetailDialog.d.ts +17 -0
  135. package/dist/kanban/CardDetailDialog.js +52 -0
  136. package/dist/kanban/CardDetailDialog.js.map +1 -0
  137. package/dist/kanban/KanbanBoardSwitcher.d.ts +6 -0
  138. package/dist/kanban/KanbanBoardSwitcher.js +27 -0
  139. package/dist/kanban/KanbanBoardSwitcher.js.map +1 -0
  140. package/dist/kanban/KanbanCard.d.ts +21 -0
  141. package/dist/kanban/KanbanCard.js +188 -0
  142. package/dist/kanban/KanbanCard.js.map +1 -0
  143. package/dist/kanban/KanbanColumn.d.ts +30 -0
  144. package/dist/kanban/KanbanColumn.js +227 -0
  145. package/dist/kanban/KanbanColumn.js.map +1 -0
  146. package/dist/kanban/KanbanRenderer.d.ts +3 -0
  147. package/dist/kanban/KanbanRenderer.js +472 -0
  148. package/dist/kanban/KanbanRenderer.js.map +1 -0
  149. package/dist/kanban/model.d.ts +89 -0
  150. package/dist/kanban/model.js +159 -0
  151. package/dist/kanban/model.js.map +1 -0
  152. package/dist/kanban/persist.d.ts +27 -0
  153. package/dist/kanban/persist.js +54 -0
  154. package/dist/kanban/persist.js.map +1 -0
  155. package/dist/kanban/useKanbanBoard.d.ts +21 -0
  156. package/dist/kanban/useKanbanBoard.js +47 -0
  157. package/dist/kanban/useKanbanBoard.js.map +1 -0
  158. package/dist/lib/theme.d.ts +11 -0
  159. package/dist/lib/theme.js +43 -0
  160. package/dist/lib/theme.js.map +1 -0
  161. package/dist/lib/utils.d.ts +9 -0
  162. package/dist/lib/utils.js +29 -0
  163. package/dist/lib/utils.js.map +1 -0
  164. package/dist/map/MapRenderer.d.ts +13 -0
  165. package/dist/map/MapRenderer.js +405 -0
  166. package/dist/map/MapRenderer.js.map +1 -0
  167. package/dist/map/model.d.ts +68 -0
  168. package/dist/map/model.js +140 -0
  169. package/dist/map/model.js.map +1 -0
  170. package/dist/renderers/AllRowsStatus.d.ts +8 -0
  171. package/dist/renderers/AllRowsStatus.js +34 -0
  172. package/dist/renderers/AllRowsStatus.js.map +1 -0
  173. package/dist/renderers/registry.d.ts +21 -0
  174. package/dist/renderers/registry.js +103 -0
  175. package/dist/renderers/registry.js.map +1 -0
  176. package/dist/renderers/types.d.ts +83 -0
  177. package/dist/renderers/types.js +8 -0
  178. package/dist/renderers/types.js.map +1 -0
  179. package/dist/schema-editor/ColumnHeaderMenu.d.ts +11 -0
  180. package/dist/schema-editor/ColumnHeaderMenu.js +74 -0
  181. package/dist/schema-editor/ColumnHeaderMenu.js.map +1 -0
  182. package/dist/schema-editor/DeleteFieldDialog.d.ts +8 -0
  183. package/dist/schema-editor/DeleteFieldDialog.js +79 -0
  184. package/dist/schema-editor/DeleteFieldDialog.js.map +1 -0
  185. package/dist/schema-editor/FieldFormDialog.d.ts +22 -0
  186. package/dist/schema-editor/FieldFormDialog.js +313 -0
  187. package/dist/schema-editor/FieldFormDialog.js.map +1 -0
  188. package/dist/schema-editor/TagInput.d.ts +9 -0
  189. package/dist/schema-editor/TagInput.js +46 -0
  190. package/dist/schema-editor/TagInput.js.map +1 -0
  191. package/dist/schema-editor/fieldForm.d.ts +52 -0
  192. package/dist/schema-editor/fieldForm.js +131 -0
  193. package/dist/schema-editor/fieldForm.js.map +1 -0
  194. package/dist/schema-editor/useSchemaEditor.d.ts +15 -0
  195. package/dist/schema-editor/useSchemaEditor.js +87 -0
  196. package/dist/schema-editor/useSchemaEditor.js.map +1 -0
  197. package/dist/styles.css +2 -0
  198. package/dist/table/CellEditor.d.ts +31 -0
  199. package/dist/table/CellEditor.js +144 -0
  200. package/dist/table/CellEditor.js.map +1 -0
  201. package/dist/table/Pager.d.ts +8 -0
  202. package/dist/table/Pager.js +90 -0
  203. package/dist/table/Pager.js.map +1 -0
  204. package/dist/table/TableRenderer.d.ts +27 -0
  205. package/dist/table/TableRenderer.js +458 -0
  206. package/dist/table/TableRenderer.js.map +1 -0
  207. package/dist/table/cells.d.ts +38 -0
  208. package/dist/table/cells.js +218 -0
  209. package/dist/table/cells.js.map +1 -0
  210. package/dist/table/sparkline.d.ts +31 -0
  211. package/dist/table/sparkline.js +105 -0
  212. package/dist/table/sparkline.js.map +1 -0
  213. package/dist/table/states.d.ts +10 -0
  214. package/dist/table/states.js +71 -0
  215. package/dist/table/states.js.map +1 -0
  216. package/dist/table/style.d.ts +15 -0
  217. package/dist/table/style.js +53 -0
  218. package/dist/table/style.js.map +1 -0
  219. package/dist/table/transforms.d.ts +17 -0
  220. package/dist/table/transforms.js +95 -0
  221. package/dist/table/transforms.js.map +1 -0
  222. package/dist/table/useInfiniteSentinel.d.ts +8 -0
  223. package/dist/table/useInfiniteSentinel.js +34 -0
  224. package/dist/table/useInfiniteSentinel.js.map +1 -0
  225. package/dist/time/timezone.d.ts +27 -0
  226. package/dist/time/timezone.js +69 -0
  227. package/dist/time/timezone.js.map +1 -0
  228. package/dist/time/zoned.d.ts +65 -0
  229. package/dist/time/zoned.js +208 -0
  230. package/dist/time/zoned.js.map +1 -0
  231. package/dist/tokens.css +454 -0
  232. package/dist/tree/TreeNode.d.ts +9 -0
  233. package/dist/tree/TreeNode.js +291 -0
  234. package/dist/tree/TreeNode.js.map +1 -0
  235. package/dist/tree/TreeRenderer.d.ts +54 -0
  236. package/dist/tree/TreeRenderer.js +446 -0
  237. package/dist/tree/TreeRenderer.js.map +1 -0
  238. package/dist/tree/expand.d.ts +31 -0
  239. package/dist/tree/expand.js +80 -0
  240. package/dist/tree/expand.js.map +1 -0
  241. package/dist/tree/model.d.ts +108 -0
  242. package/dist/tree/model.js +294 -0
  243. package/dist/tree/model.js.map +1 -0
  244. package/dist/udc/hooks.d.ts +644 -0
  245. package/dist/udc/hooks.js +353 -0
  246. package/dist/udc/hooks.js.map +1 -0
  247. package/dist/udc/keys.d.ts +19 -0
  248. package/dist/udc/keys.js +30 -0
  249. package/dist/udc/keys.js.map +1 -0
  250. package/dist/udc/provider.d.ts +14 -0
  251. package/dist/udc/provider.js +36 -0
  252. package/dist/udc/provider.js.map +1 -0
  253. package/package.json +113 -4
@@ -0,0 +1,71 @@
1
+ import type { SchemaDoc } from "@jsudc/core";
2
+ import { type ReactNode } from "react";
3
+ import { type BuiltinLocale, type Locale, type Term } from "./translations";
4
+ export declare const LOCALE_STORAGE_KEY = "udc-locale";
5
+ /** The BCP 47 tag `Intl` formats numbers and dates with, per built-in locale. */
6
+ export declare const INTL_LOCALE: Record<BuiltinLocale, string>;
7
+ /** `Intl`'s tag for a locale: the built-in ones are mapped, any other is used as it is. */
8
+ export declare function intlLocaleOf(locale: Locale): string;
9
+ export type Vars = Record<string, string | number>;
10
+ /** Sections of terms where a locale may be missing: runtime terms (field labels from the schema), a host's messages. */
11
+ export type Overlay = Record<string, Record<string, Partial<Term>>>;
12
+ /**
13
+ * What a host passes to `<UdcI18nProvider messages>`: its own sections
14
+ * (`COLLECTIONS`, `TBL_<COLLECTION>`, its chrome), overrides of library terms
15
+ * (`APP.RETRY.en`), and strings in locales the library does not ship (`de`).
16
+ */
17
+ export type Messages = Overlay;
18
+ /** `base` with `layers` over it, term by term and locale by locale: the later layer wins. */
19
+ export declare function mergeMessages(base: Overlay, ...layers: (Overlay | undefined)[]): Overlay;
20
+ /** Every locale a dictionary has at least one string in. */
21
+ export declare function messageLocales(messages: Overlay): Locale[];
22
+ /**
23
+ * The translation of `SECTION.TERM`, or `null` when neither the locale nor
24
+ * `en` has one. An `overlay` entry wins over the static map, locale by locale.
25
+ */
26
+ export declare function lookup(translations: Overlay, locale: Locale, section: string, term: string, overlay?: Overlay): string | null;
27
+ /** `t` for a fixed locale; a missing term falls back to the humanised term (`revenue_eur` → `Revenue eur`). */
28
+ export declare function makeT(locale: Locale, translations?: Overlay, overlay?: Overlay): (section: string, term: string, vars?: Vars) => string;
29
+ /**
30
+ * The labels a schema's fields carry, as overlay terms. `fielddef.label` is
31
+ * `{en, sr}` (what the field form writes) or a plain string for every locale.
32
+ */
33
+ export declare function schemaLabelTerms(schema: SchemaDoc, locales?: readonly Locale[]): Record<string, Partial<Term>>;
34
+ export declare function isLocale(v: unknown, locales?: readonly Locale[]): v is Locale;
35
+ interface I18n {
36
+ locale: Locale;
37
+ intlLocale: string;
38
+ /** The locales on offer: the built-in ones and those the host's messages add. */
39
+ locales: readonly Locale[];
40
+ setLocale: (l: Locale) => void;
41
+ t: (section: string, term: string, vars?: Vars) => string;
42
+ /** The translation, or `null` when there is none (so a caller can use its own fallback). */
43
+ tMaybe: (section: string, term: string, vars?: Vars) => string | null;
44
+ /** Replace the runtime terms of one section (the field labels of a collection's schema). */
45
+ setSectionTerms: (section: string, terms: Record<string, Partial<Term>>) => void;
46
+ }
47
+ export interface UdcI18nProviderProps {
48
+ children: ReactNode;
49
+ /**
50
+ * The locale to show. Leave it out and the provider keeps the locale
51
+ * itself, remembered in localStorage (`udc-locale`).
52
+ */
53
+ locale?: Locale;
54
+ /** Called with every locale `setLocale` is asked for; a host that passes `locale` stores it here. */
55
+ onLocaleChange?: (locale: Locale) => void;
56
+ /** Layered over the library's dictionary: the host's own sections, overrides, other locales. */
57
+ messages?: Messages;
58
+ /** The locales on offer. Default: `en`, `sr` and every locale `messages` has a string in. */
59
+ locales?: readonly Locale[];
60
+ }
61
+ /**
62
+ * Strings for everything under it. `t(section, term)` looks in the runtime
63
+ * terms (a schema's field labels), then `messages`, then the library's
64
+ * dictionary, in the current locale and then in `en`; a term found nowhere
65
+ * is humanised (`revenue_eur` → `Revenue eur`).
66
+ */
67
+ export declare function UdcI18nProvider({ children, locale: controlled, onLocaleChange, messages, locales: localesProp, }: UdcI18nProviderProps): import("react").JSX.Element;
68
+ export declare function useI18n(): I18n;
69
+ /** The translation section of a collection's own strings: `companies` → `TBL_COMPANIES`. */
70
+ export declare function tableSection(collection: string): string;
71
+ export {};
@@ -0,0 +1,151 @@
1
+ import { humanize, storage } from "../lib/utils.js";
2
+ import { LOCALES, TRANSLATIONS } from "./translations.js";
3
+ import { createContext, useCallback, useContext, useMemo, useState } from "react";
4
+ import { jsx } from "react/jsx-runtime";
5
+ // src/i18n/i18n.tsx
6
+ var LOCALE_STORAGE_KEY = "udc-locale";
7
+ /** The BCP 47 tag `Intl` formats numbers and dates with, per built-in locale. */
8
+ var INTL_LOCALE = {
9
+ en: "en-US",
10
+ sr: "sr-Latn-RS"
11
+ };
12
+ /** `Intl`'s tag for a locale: the built-in ones are mapped, any other is used as it is. */
13
+ function intlLocaleOf(locale) {
14
+ return INTL_LOCALE[locale] ?? locale;
15
+ }
16
+ function fill(s, vars) {
17
+ return vars ? s.replace(/\{(\w+)\}/g, (m, k) => String(vars[k] ?? m)) : s;
18
+ }
19
+ /** `base` with `layers` over it, term by term and locale by locale: the later layer wins. */
20
+ function mergeMessages(base, ...layers) {
21
+ const out = { ...base };
22
+ for (const layer of layers) {
23
+ if (!layer) continue;
24
+ for (const [section, terms] of Object.entries(layer)) {
25
+ const s = section.toUpperCase();
26
+ const merged = { ...out[s] };
27
+ for (const [term, entry] of Object.entries(terms)) {
28
+ const k = term.toUpperCase();
29
+ merged[k] = {
30
+ ...merged[k],
31
+ ...entry
32
+ };
33
+ }
34
+ out[s] = merged;
35
+ }
36
+ }
37
+ return out;
38
+ }
39
+ /** Every locale a dictionary has at least one string in. */
40
+ function messageLocales(messages) {
41
+ const found = /* @__PURE__ */ new Set();
42
+ for (const terms of Object.values(messages)) for (const entry of Object.values(terms)) for (const l of Object.keys(entry)) found.add(l);
43
+ return [...found];
44
+ }
45
+ /**
46
+ * The translation of `SECTION.TERM`, or `null` when neither the locale nor
47
+ * `en` has one. An `overlay` entry wins over the static map, locale by locale.
48
+ */
49
+ function lookup(translations, locale, section, term, overlay) {
50
+ const s = section.toUpperCase();
51
+ const k = term.toUpperCase();
52
+ const extra = overlay?.[s]?.[k];
53
+ const entry = translations[s]?.[k];
54
+ return extra?.[locale] || entry?.[locale] || extra?.en || entry?.en || null;
55
+ }
56
+ /** `t` for a fixed locale; a missing term falls back to the humanised term (`revenue_eur` → `Revenue eur`). */
57
+ function makeT(locale, translations = TRANSLATIONS, overlay) {
58
+ return (section, term, vars) => fill(lookup(translations, locale, section, term, overlay) ?? humanize(term), vars);
59
+ }
60
+ /**
61
+ * The labels a schema's fields carry, as overlay terms. `fielddef.label` is
62
+ * `{en, sr}` (what the field form writes) or a plain string for every locale.
63
+ */
64
+ function schemaLabelTerms(schema, locales = LOCALES) {
65
+ const out = {};
66
+ for (const [name, def] of Object.entries(schema.fields)) {
67
+ const label = def.label;
68
+ const term = {};
69
+ if (typeof label === "string" && label.trim()) for (const l of locales) term[l] = label.trim();
70
+ else if (label && typeof label === "object") for (const l of locales) {
71
+ const v = label[l];
72
+ if (typeof v === "string" && v.trim()) term[l] = v.trim();
73
+ }
74
+ if (Object.keys(term).length) out[name.toUpperCase()] = term;
75
+ }
76
+ return out;
77
+ }
78
+ function isLocale(v, locales = LOCALES) {
79
+ return locales.includes(v);
80
+ }
81
+ function initialLocale(locales) {
82
+ const stored = storage.get(LOCALE_STORAGE_KEY);
83
+ return isLocale(stored, locales) ? stored : "en";
84
+ }
85
+ var I18nContext = createContext(null);
86
+ /**
87
+ * Strings for everything under it. `t(section, term)` looks in the runtime
88
+ * terms (a schema's field labels), then `messages`, then the library's
89
+ * dictionary, in the current locale and then in `en`; a term found nowhere
90
+ * is humanised (`revenue_eur` → `Revenue eur`).
91
+ */
92
+ function UdcI18nProvider({ children, locale: controlled, onLocaleChange, messages, locales: localesProp }) {
93
+ const dictionary = useMemo(() => mergeMessages(TRANSLATIONS, messages), [messages]);
94
+ const locales = useMemo(() => localesProp ?? [.../* @__PURE__ */ new Set([...LOCALES, ...messageLocales(messages ?? {})])], [localesProp, messages]);
95
+ const [own, setOwn] = useState(() => initialLocale(locales));
96
+ const [overlay, setOverlay] = useState({});
97
+ const locale = controlled ?? own;
98
+ const setLocale = useCallback((l) => {
99
+ if (controlled === void 0) {
100
+ storage.set(LOCALE_STORAGE_KEY, l);
101
+ setOwn(l);
102
+ }
103
+ onLocaleChange?.(l);
104
+ }, [controlled, onLocaleChange]);
105
+ const setSectionTerms = useCallback((section, terms) => setOverlay((prev) => {
106
+ const key = section.toUpperCase();
107
+ if (JSON.stringify(prev[key] ?? {}) === JSON.stringify(terms)) return prev;
108
+ return {
109
+ ...prev,
110
+ [key]: terms
111
+ };
112
+ }), []);
113
+ const value = useMemo(() => {
114
+ document.documentElement.lang = locale;
115
+ return {
116
+ locale,
117
+ intlLocale: intlLocaleOf(locale),
118
+ locales,
119
+ setLocale,
120
+ t: makeT(locale, dictionary, overlay),
121
+ tMaybe: (section, term, vars) => {
122
+ const s = lookup(dictionary, locale, section, term, overlay);
123
+ return s === null ? null : fill(s, vars);
124
+ },
125
+ setSectionTerms
126
+ };
127
+ }, [
128
+ locale,
129
+ locales,
130
+ setLocale,
131
+ dictionary,
132
+ overlay,
133
+ setSectionTerms
134
+ ]);
135
+ return /* @__PURE__ */ jsx(I18nContext.Provider, {
136
+ value,
137
+ children
138
+ });
139
+ }
140
+ function useI18n() {
141
+ const ctx = useContext(I18nContext);
142
+ if (!ctx) throw new Error("useI18n needs a <UdcI18nProvider> above it");
143
+ return ctx;
144
+ }
145
+ /** The translation section of a collection's own strings: `companies` → `TBL_COMPANIES`. */
146
+ function tableSection(collection) {
147
+ return `TBL_${collection.toUpperCase()}`;
148
+ }
149
+ export { INTL_LOCALE, LOCALE_STORAGE_KEY, UdcI18nProvider, intlLocaleOf, isLocale, lookup, makeT, mergeMessages, messageLocales, schemaLabelTerms, tableSection, useI18n };
150
+
151
+ //# sourceMappingURL=i18n.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"i18n.js","names":[],"sources":["../../src/i18n/i18n.tsx"],"sourcesContent":["import type { SchemaDoc } from \"@jsudc/core\";\nimport { createContext, type ReactNode, useCallback, useContext, useMemo, useState } from \"react\";\nimport { humanize, storage } from \"../lib/utils\";\nimport { type BuiltinLocale, LOCALES, type Locale, type Term, TRANSLATIONS } from \"./translations\";\n\nexport const LOCALE_STORAGE_KEY = \"udc-locale\";\n\n/** The BCP 47 tag `Intl` formats numbers and dates with, per built-in locale. */\nexport const INTL_LOCALE: Record<BuiltinLocale, string> = { en: \"en-US\", sr: \"sr-Latn-RS\" };\n\n/** `Intl`'s tag for a locale: the built-in ones are mapped, any other is used as it is. */\nexport function intlLocaleOf(locale: Locale): string {\n return (INTL_LOCALE as Record<string, string>)[locale] ?? locale;\n}\n\nexport type Vars = Record<string, string | number>;\n\nfunction fill(s: string, vars: Vars | undefined): string {\n return vars ? s.replace(/\\{(\\w+)\\}/g, (m, k: string) => String(vars[k] ?? m)) : s;\n}\n\n/** Sections of terms where a locale may be missing: runtime terms (field labels from the schema), a host's messages. */\nexport type Overlay = Record<string, Record<string, Partial<Term>>>;\n\n/**\n * What a host passes to `<UdcI18nProvider messages>`: its own sections\n * (`COLLECTIONS`, `TBL_<COLLECTION>`, its chrome), overrides of library terms\n * (`APP.RETRY.en`), and strings in locales the library does not ship (`de`).\n */\nexport type Messages = Overlay;\n\n/** `base` with `layers` over it, term by term and locale by locale: the later layer wins. */\nexport function mergeMessages(base: Overlay, ...layers: (Overlay | undefined)[]): Overlay {\n const out: Overlay = { ...base };\n for (const layer of layers) {\n if (!layer) continue;\n for (const [section, terms] of Object.entries(layer)) {\n const s = section.toUpperCase();\n const merged = { ...out[s] };\n for (const [term, entry] of Object.entries(terms)) {\n const k = term.toUpperCase();\n merged[k] = { ...merged[k], ...entry };\n }\n out[s] = merged;\n }\n }\n return out;\n}\n\n/** Every locale a dictionary has at least one string in. */\nexport function messageLocales(messages: Overlay): Locale[] {\n const found = new Set<string>();\n for (const terms of Object.values(messages))\n for (const entry of Object.values(terms)) for (const l of Object.keys(entry)) found.add(l);\n return [...found];\n}\n\n/**\n * The translation of `SECTION.TERM`, or `null` when neither the locale nor\n * `en` has one. An `overlay` entry wins over the static map, locale by locale.\n */\nexport function lookup(\n translations: Overlay,\n locale: Locale,\n section: string,\n term: string,\n overlay?: Overlay,\n): string | null {\n const s = section.toUpperCase();\n const k = term.toUpperCase();\n const extra = overlay?.[s]?.[k];\n const entry = translations[s]?.[k];\n return extra?.[locale] || entry?.[locale] || extra?.en || entry?.en || null;\n}\n\n/** `t` for a fixed locale; a missing term falls back to the humanised term (`revenue_eur` → `Revenue eur`). */\nexport function makeT(locale: Locale, translations: Overlay = TRANSLATIONS, overlay?: Overlay) {\n return (section: string, term: string, vars?: Vars): string =>\n fill(lookup(translations, locale, section, term, overlay) ?? humanize(term), vars);\n}\n\n/**\n * The labels a schema's fields carry, as overlay terms. `fielddef.label` is\n * `{en, sr}` (what the field form writes) or a plain string for every locale.\n */\nexport function schemaLabelTerms(\n schema: SchemaDoc,\n locales: readonly Locale[] = LOCALES,\n): Record<string, Partial<Term>> {\n const out: Record<string, Partial<Term>> = {};\n for (const [name, def] of Object.entries(schema.fields)) {\n const label = (def as { label?: unknown }).label;\n const term: Partial<Term> = {};\n if (typeof label === \"string\" && label.trim()) {\n for (const l of locales) term[l] = label.trim();\n } else if (label && typeof label === \"object\") {\n for (const l of locales) {\n const v = (label as Record<string, unknown>)[l];\n if (typeof v === \"string\" && v.trim()) term[l] = v.trim();\n }\n }\n if (Object.keys(term).length) out[name.toUpperCase()] = term;\n }\n return out;\n}\n\nexport function isLocale(v: unknown, locales: readonly Locale[] = LOCALES): v is Locale {\n return (locales as readonly unknown[]).includes(v);\n}\n\nfunction initialLocale(locales: readonly Locale[]): Locale {\n const stored = storage.get(LOCALE_STORAGE_KEY);\n return isLocale(stored, locales) ? stored : \"en\";\n}\n\ninterface I18n {\n locale: Locale;\n intlLocale: string;\n /** The locales on offer: the built-in ones and those the host's messages add. */\n locales: readonly Locale[];\n setLocale: (l: Locale) => void;\n t: (section: string, term: string, vars?: Vars) => string;\n /** The translation, or `null` when there is none (so a caller can use its own fallback). */\n tMaybe: (section: string, term: string, vars?: Vars) => string | null;\n /** Replace the runtime terms of one section (the field labels of a collection's schema). */\n setSectionTerms: (section: string, terms: Record<string, Partial<Term>>) => void;\n}\n\nconst I18nContext = createContext<I18n | null>(null);\n\nexport interface UdcI18nProviderProps {\n children: ReactNode;\n /**\n * The locale to show. Leave it out and the provider keeps the locale\n * itself, remembered in localStorage (`udc-locale`).\n */\n locale?: Locale;\n /** Called with every locale `setLocale` is asked for; a host that passes `locale` stores it here. */\n onLocaleChange?: (locale: Locale) => void;\n /** Layered over the library's dictionary: the host's own sections, overrides, other locales. */\n messages?: Messages;\n /** The locales on offer. Default: `en`, `sr` and every locale `messages` has a string in. */\n locales?: readonly Locale[];\n}\n\n/**\n * Strings for everything under it. `t(section, term)` looks in the runtime\n * terms (a schema's field labels), then `messages`, then the library's\n * dictionary, in the current locale and then in `en`; a term found nowhere\n * is humanised (`revenue_eur` → `Revenue eur`).\n */\nexport function UdcI18nProvider({\n children,\n locale: controlled,\n onLocaleChange,\n messages,\n locales: localesProp,\n}: UdcI18nProviderProps) {\n const dictionary = useMemo(() => mergeMessages(TRANSLATIONS, messages), [messages]);\n const locales = useMemo<readonly Locale[]>(\n () => localesProp ?? [...new Set<Locale>([...LOCALES, ...messageLocales(messages ?? {})])],\n [localesProp, messages],\n );\n const [own, setOwn] = useState<Locale>(() => initialLocale(locales));\n const [overlay, setOverlay] = useState<Overlay>({});\n const locale = controlled ?? own;\n\n const setLocale = useCallback(\n (l: Locale) => {\n if (controlled === undefined) {\n storage.set(LOCALE_STORAGE_KEY, l);\n setOwn(l);\n }\n onLocaleChange?.(l);\n },\n [controlled, onLocaleChange],\n );\n\n const setSectionTerms = useCallback(\n (section: string, terms: Record<string, Partial<Term>>) =>\n setOverlay((prev) => {\n const key = section.toUpperCase();\n if (JSON.stringify(prev[key] ?? {}) === JSON.stringify(terms)) return prev;\n return { ...prev, [key]: terms };\n }),\n [],\n );\n\n const value = useMemo<I18n>(() => {\n document.documentElement.lang = locale;\n return {\n locale,\n intlLocale: intlLocaleOf(locale),\n locales,\n setLocale,\n t: makeT(locale, dictionary, overlay),\n tMaybe: (section, term, vars) => {\n const s = lookup(dictionary, locale, section, term, overlay);\n return s === null ? null : fill(s, vars);\n },\n setSectionTerms,\n };\n }, [locale, locales, setLocale, dictionary, overlay, setSectionTerms]);\n\n return <I18nContext.Provider value={value}>{children}</I18nContext.Provider>;\n}\n\nexport function useI18n(): I18n {\n const ctx = useContext(I18nContext);\n if (!ctx) throw new Error(\"useI18n needs a <UdcI18nProvider> above it\");\n return ctx;\n}\n\n/** The translation section of a collection's own strings: `companies` → `TBL_COMPANIES`. */\nexport function tableSection(collection: string): string {\n return `TBL_${collection.toUpperCase()}`;\n}\n"],"mappings":";;;;;AAKA,IAAa,qBAAqB;;AAGlC,IAAa,cAA6C;CAAE,IAAI;CAAS,IAAI;AAAa;;AAG1F,SAAgB,aAAa,QAAwB;CACnD,OAAQ,YAAuC,WAAW;AAC5D;AAIA,SAAS,KAAK,GAAW,MAAgC;CACvD,OAAO,OAAO,EAAE,QAAQ,eAAe,GAAG,MAAc,OAAO,KAAK,MAAM,CAAC,CAAC,IAAI;AAClF;;AAaA,SAAgB,cAAc,MAAe,GAAG,QAA0C;CACxF,MAAM,MAAe,EAAE,GAAG,KAAK;CAC/B,KAAK,MAAM,SAAS,QAAQ;EAC1B,IAAI,CAAC,OAAO;EACZ,KAAK,MAAM,CAAC,SAAS,UAAU,OAAO,QAAQ,KAAK,GAAG;GACp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AAG,OAAO;EACtE,OAAO;GAAE,GAAG;IAAO,MAAM;EAAM;CACjC,CAAC,GACH,CAAC,CACH;CAEA,MAAM,QAAQ,cAAoB;EAChC,SAAS,gBAAgB,OAAO;EAChC,OAAO;GACL;GACA,YAAY,aAAa,MAAM;GAC/B;GACA;GACA,GAAG,MAAM,QAAQ,YAAY,OAAO;GACpC,SAAS,SAAS,MAAM,SAAS;IAC/B,MAAM,IAAI,OAAO,YAAY,QAAQ,SAAS,MAAM,OAAO;IAC3D,OAAO,MAAM,OAAO,OAAO,KAAK,GAAG,IAAI;GACzC;GACA;EACF;CACF,GAAG;EAAC;EAAQ;EAAS;EAAW;EAAY;EAAS;CAAe,CAAC;CAErE,OAAO,oBAAC,YAAY,UAAb;EAA6B;EAAQ;CAA+B,CAAA;AAC7E;AAEA,SAAgB,UAAgB;CAC9B,MAAM,MAAM,WAAW,WAAW;CAClC,IAAI,CAAC,KAAK,MAAM,IAAI,MAAM,4CAA4C;CACtE,OAAO;AACT;;AAGA,SAAgB,aAAa,YAA4B;CACvD,OAAO,OAAO,WAAW,YAAY;AACvC"}
@@ -0,0 +1,16 @@
1
+ /** The locales the library's dictionary ships. A host adds others through `messages`. */
2
+ export declare const LOCALES: readonly ["en", "sr"];
3
+ export type BuiltinLocale = (typeof LOCALES)[number];
4
+ /** A locale tag: `en` or `sr`, or any other a host supplies strings for. */
5
+ export type Locale = BuiltinLocale | (string & {});
6
+ /** One term: every built-in locale, plus any a host adds. */
7
+ export type Term = Record<BuiltinLocale, string> & Partial<Record<string, string>>;
8
+ export type Translations = Record<string, Record<string, Term>>;
9
+ /**
10
+ * The library's dictionary, `{SECTION: {TERM: {en, sr}}}`. `APP` is the
11
+ * strings of the data view and its renderers. A host's own sections (its
12
+ * collection names, `TBL_<COLLECTION>` field labels, its chrome) come in
13
+ * through `<UdcI18nProvider messages>`. `{name}` placeholders are filled from
14
+ * `t`'s third argument.
15
+ */
16
+ export declare const TRANSLATIONS: Translations;