@proveanything/smartlinks 2.2.3 → 2.2.5

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.
@@ -25,6 +25,9 @@ export declare function channel(scope: RealtimeScope & {
25
25
  * records, products). Filter by `type` ('product' | 'proof' | 'asset' | 'record' | 'collection' |
26
26
  * 'collection.apps' | 'settings'), `id`, `appId`, `recordType`, `productId`, `proofId`.
27
27
  *
28
+ * A change with no `id` means many things of that type changed (an import): a `{ type, id }` filter
29
+ * hears it too, so check `change.id` before assuming it is yours.
30
+ *
28
31
  * ```ts
29
32
  * const off = realtime.onChange({ collectionId }, { type: "record", appId, recordType: "entry" }, () => refetch())
30
33
  * ```
@@ -194,8 +194,13 @@ export function channel(scope, name) {
194
194
  }
195
195
  function matches(change, filter) {
196
196
  for (const k of ["type", "id", "appId", "recordType", "productId", "proofId", "kind"]) {
197
- if (filter[k] !== undefined && change[k] !== filter[k])
198
- return false;
197
+ if (filter[k] === undefined || change[k] === filter[k])
198
+ continue;
199
+ // A change that names no id is about many things of its type (an import sends one
200
+ // { type: 'product' } in place of thousands): whoever follows one of them by type and id hears it.
201
+ if (k === "id" && change.id == null && filter.type !== undefined)
202
+ continue;
203
+ return false;
199
204
  }
200
205
  return true;
201
206
  }
@@ -206,6 +211,9 @@ function matches(change, filter) {
206
211
  * records, products). Filter by `type` ('product' | 'proof' | 'asset' | 'record' | 'collection' |
207
212
  * 'collection.apps' | 'settings'), `id`, `appId`, `recordType`, `productId`, `proofId`.
208
213
  *
214
+ * A change with no `id` means many things of that type changed (an import): a `{ type, id }` filter
215
+ * hears it too, so check `change.id` before assuming it is yours.
216
+ *
209
217
  * ```ts
210
218
  * const off = realtime.onChange({ collectionId }, { type: "record", appId, recordType: "entry" }, () => refetch())
211
219
  * ```
@@ -1,6 +1,6 @@
1
1
  # Smartlinks API Summary
2
2
 
3
- Version: 2.2.3 | Generated: 2026-12-02T13:03:18.253Z
3
+ Version: 2.2.5 | Generated: 2026-12-04T13:13:18.143Z
4
4
 
5
5
  This is a concise summary of all available API functions and types.
6
6
 
@@ -16547,7 +16547,7 @@ Reset rate limit for a user
16547
16547
  One of your app's declared channels (manifest `realtime.channels`). ```ts const draw = realtime.channel({ collectionId, appId }, "draw") const off = draw.subscribe("status", (m) => setStatus(m.data)) await draw.publish("status", { phase: "spinning" }) // if the channel lets this viewer publish ```
16548
16548
 
16549
16549
  **onChange**(scope: RealtimeScope, filter: RealtimeChangeFilter, onChangeFn: (change: RealtimeChangeEvent) → `void`
16550
- Something changed: refetch it. IDs only — never data. Admins of the collection see every change; everyone else sees changes to public things (public app records, products). Filter by `type` ('product' | 'proof' | 'asset' | 'record' | 'collection' | 'collection.apps' | 'settings'), `id`, `appId`, `recordType`, `productId`, `proofId`. ```ts const off = realtime.onChange({ collectionId }, { type: "record", appId, recordType: "entry" }, () => refetch()) ```
16550
+ Something changed: refetch it. IDs only — never data. Admins of the collection see every change; everyone else sees changes to public things (public app records, products). Filter by `type` ('product' | 'proof' | 'asset' | 'record' | 'collection' | 'collection.apps' | 'settings'), `id`, `appId`, `recordType`, `productId`, `proofId`. A change with no `id` means many things of that type changed (an import): a `{ type, id }` filter hears it too, so check `change.id` before assuming it is yours. ```ts const off = realtime.onChange({ collectionId }, { type: "record", appId, recordType: "entry" }, () => refetch()) ```
16551
16551
 
16552
16552
  **onStaffChange**(scope: RealtimeScope & { appId: string }, filter: RealtimeChangeFilter, onChangeFn: (change: RealtimeChangeEvent) → `void`
16553
16553
  Something this APP works on changed: one of its work orders, a container or stock it moved, or one of its own records. For the people who use the app, admins or not: someone hears these when they are in one of the app's user groups (the ones its server functions are limited to, or `realtime.staff` in its manifest), and so does a collection admin. IDs only — refetch through your own API. Types: 'workOrder' (`id`, `appId`), 'container' (`id`, `kind` = its container type), 'stock' (`id` = the container, `productId`), 'record' (`id`, `recordType`). Filter as with `onChange`; an empty filter hears them all. ```ts // A production app's screens refetch when anything on the floor changes. useEffect(() => realtime.onStaffChange({ collectionId, appId }, {}, () => queryClient.invalidateQueries()), [collectionId, appId]) ```
@@ -220,7 +220,7 @@ All standard widget props apply:
220
220
  | `tag` | `TagContext` | ❌ | **Deprecated** — use `itemContext.tag` instead. Kept for one release. |
221
221
  | `onNavigate` | function | ❌ | Navigation callback (accepts `NavigationRequest` or legacy string) |
222
222
  | `size` | string | ❌ | `"compact"`, `"standard"`, or `"large"` |
223
- | `lang` | string | ❌ | Language code (e.g., `"en"`) |
223
+ | `lang` | string | ❌ | Language code (e.g., `"en"`). Your own words: `i18n.forApp` (see [i18n.md](i18n.md)), never `i18n.init` or `i18n.t`, which are the host's |
224
224
  | `translations` | object | ❌ | Translation overrides |
225
225
  | `className` | string | ❌ | CSS class for the wrapper element |
226
226
 
@@ -1,91 +1,91 @@
1
- # Data changed: an open screen reads again
2
-
3
- Since 2.2.3.
4
-
5
- An admin screen is not the only thing that changes records. The assistant applies a change, another app
6
- saves, another person edits. The screen that is open keeps showing what it loaded, and the person has to
7
- reload to see the change. **Data changed** is the host telling the app, so it can read again.
8
-
9
- It is one message, host to app:
10
-
11
- ```js
12
- { type: 'smartlinks:data-changed', collectionId: 'acme', changes: [{ type: 'product', id: 'p_1', status: 'updated' }], source: 'assistant' }
13
- ```
14
-
15
- Ids only, never data. The app reads again through the normal API, with the normal permissions.
16
-
17
- ## In an app
18
-
19
- ```tsx
20
- import * as SL from '@proveanything/smartlinks'
21
-
22
- useEffect(() => SL.onDataChanged((e) => {
23
- if (e.collectionId && e.collectionId !== collectionId) return
24
- if (e.touches({ type: 'product' })) queryClient.invalidateQueries({ queryKey: ['products'] })
25
- if (e.touches({ type: 'product', id: productId })) queryClient.invalidateQueries({ queryKey: ['product', collectionId, productId] })
26
- }), [collectionId, productId])
27
- ```
28
-
29
- - `SL.onDataChanged(listener)` returns a function that stops listening. It does nothing outside a frame.
30
- - Before your listener runs, the SDK has already forgotten the reads it kept for what changed, so the
31
- next read returns what the server has now. This happens even in an app that never calls `onDataChanged`.
32
- - `e.changes` is what changed. It is **empty when the host can't say**: treat everything you show from
33
- the collection as possibly changed.
34
- - `e.touches({ type, id })` answers "might this be about my record?". It is true when a change matches
35
- every field you give, and always true when `e.changes` is empty.
36
- - `e.source` is who made the change when the host knows (`'assistant'`, `'app'`, `'person'`).
37
-
38
- ### A form the person is filling in
39
-
40
- Reading again must not throw away typing.
41
-
42
- - **Untouched form:** fill it again from the new record.
43
- - **Form with changes in it:** keep what the person typed. Say plainly that the record was changed
44
- elsewhere, and offer to load the new version. Do not save the old copy over the new one without asking.
45
-
46
- Compare the record you loaded with the one you just read (its `updatedAt`, or the fields the form shows)
47
- before saying anything: "something changed" is not always about the record on screen.
48
-
49
- ### One change word per kind of record
50
-
51
- `type` uses the same words as realtime changes (`docs/realtime.md`): `product`, `proof`, `asset`, `record`,
52
- `collection`, `collection.apps`, `settings`. A host may also send its own words for other records
53
- (`contact`, `appConfig`). `id` is the record's id; `productId`, `proofId`, `appId` and `recordType` say
54
- where it lives when that helps.
55
-
56
- ## In a host
57
-
58
- Send it after a change the app did not make itself.
59
-
60
- ```ts
61
- // Any host
62
- SL.postDataChanged(iframe.contentWindow, { collectionId, changes: [{ type: 'product', id }], source: 'assistant' })
63
-
64
- // With IframeResponder
65
- responder.dataChanged({ collectionId, changes: [{ type: 'product', id }] })
66
-
67
- // When you can't say what changed
68
- SL.postDataChanged(iframe.contentWindow, { collectionId })
69
- ```
70
-
71
- Studio sends it to every open app frame when an assistant change set is applied or undone, when the
72
- assistant finishes a step that writes, and when its turn ends.
73
-
74
- ## What it is not
75
-
76
- - It is not the realtime feed. `SL.realtime.onChange` (`docs/realtime.md`) is the server telling a page
77
- that something changed, over a live connection. Data changed is the host passing a change it already
78
- knows about into a frame, with no connection of the app's own. A host that listens to the realtime
79
- feed can pass those changes on with the same message, so an app needs only `onDataChanged`.
80
- - It does not carry data and it is not a save. Nothing is written by sending or hearing it.
81
-
82
- ## What the SDK forgets
83
-
84
- | The host says | The SDK forgets |
85
- |---|---|
86
- | Only products and proofs changed | Everything kept under that collection's products (the product, its lists, its proofs) |
87
- | Anything else, or nothing specific | Everything kept for that collection |
88
- | No collection given | Everything kept |
89
-
90
- Admin reads are never kept in the first place (`docs/caching.md`), so this matters for public reads an
91
- admin screen makes, and for apps on an SDK before 2.2.2.
1
+ # Data changed: an open screen reads again
2
+
3
+ Since 2.2.3.
4
+
5
+ An admin screen is not the only thing that changes records. The assistant applies a change, another app
6
+ saves, another person edits. The screen that is open keeps showing what it loaded, and the person has to
7
+ reload to see the change. **Data changed** is the host telling the app, so it can read again.
8
+
9
+ It is one message, host to app:
10
+
11
+ ```js
12
+ { type: 'smartlinks:data-changed', collectionId: 'acme', changes: [{ type: 'product', id: 'p_1', status: 'updated' }], source: 'assistant' }
13
+ ```
14
+
15
+ Ids only, never data. The app reads again through the normal API, with the normal permissions.
16
+
17
+ ## In an app
18
+
19
+ ```tsx
20
+ import * as SL from '@proveanything/smartlinks'
21
+
22
+ useEffect(() => SL.onDataChanged((e) => {
23
+ if (e.collectionId && e.collectionId !== collectionId) return
24
+ if (e.touches({ type: 'product' })) queryClient.invalidateQueries({ queryKey: ['products'] })
25
+ if (e.touches({ type: 'product', id: productId })) queryClient.invalidateQueries({ queryKey: ['product', collectionId, productId] })
26
+ }), [collectionId, productId])
27
+ ```
28
+
29
+ - `SL.onDataChanged(listener)` returns a function that stops listening. It does nothing outside a frame.
30
+ - Before your listener runs, the SDK has already forgotten the reads it kept for what changed, so the
31
+ next read returns what the server has now. This happens even in an app that never calls `onDataChanged`.
32
+ - `e.changes` is what changed. It is **empty when the host can't say**: treat everything you show from
33
+ the collection as possibly changed.
34
+ - `e.touches({ type, id })` answers "might this be about my record?". It is true when a change matches
35
+ every field you give, and always true when `e.changes` is empty.
36
+ - `e.source` is who made the change when the host knows (`'assistant'`, `'app'`, `'person'`).
37
+
38
+ ### A form the person is filling in
39
+
40
+ Reading again must not throw away typing.
41
+
42
+ - **Untouched form:** fill it again from the new record.
43
+ - **Form with changes in it:** keep what the person typed. Say plainly that the record was changed
44
+ elsewhere, and offer to load the new version. Do not save the old copy over the new one without asking.
45
+
46
+ Compare the record you loaded with the one you just read (its `updatedAt`, or the fields the form shows)
47
+ before saying anything: "something changed" is not always about the record on screen.
48
+
49
+ ### One change word per kind of record
50
+
51
+ `type` uses the same words as realtime changes (`docs/realtime.md`): `product`, `proof`, `asset`, `record`,
52
+ `collection`, `collection.apps`, `settings`. A host may also send its own words for other records
53
+ (`contact`, `appConfig`). `id` is the record's id; `productId`, `proofId`, `appId` and `recordType` say
54
+ where it lives when that helps.
55
+
56
+ ## In a host
57
+
58
+ Send it after a change the app did not make itself.
59
+
60
+ ```ts
61
+ // Any host
62
+ SL.postDataChanged(iframe.contentWindow, { collectionId, changes: [{ type: 'product', id }], source: 'assistant' })
63
+
64
+ // With IframeResponder
65
+ responder.dataChanged({ collectionId, changes: [{ type: 'product', id }] })
66
+
67
+ // When you can't say what changed
68
+ SL.postDataChanged(iframe.contentWindow, { collectionId })
69
+ ```
70
+
71
+ Studio sends it to every open app frame when an assistant change set is applied or undone, when the
72
+ assistant finishes a step that writes, and when its turn ends.
73
+
74
+ ## What it is not
75
+
76
+ - It is not the realtime feed. `SL.realtime.onChange` (`docs/realtime.md`) is the server telling a page
77
+ that something changed, over a live connection. Data changed is the host passing a change it already
78
+ knows about into a frame, with no connection of the app's own. A host that listens to the realtime
79
+ feed can pass those changes on with the same message, so an app needs only `onDataChanged`.
80
+ - It does not carry data and it is not a save. Nothing is written by sending or hearing it.
81
+
82
+ ## What the SDK forgets
83
+
84
+ | The host says | The SDK forgets |
85
+ |---|---|
86
+ | Only products and proofs changed | Everything kept under that collection's products (the product, its lists, its proofs) |
87
+ | Anything else, or nothing specific | Everything kept for that collection |
88
+ | No collection given | Everything kept |
89
+
90
+ Admin reads are never kept in the first place (`docs/caching.md`), so this matters for public reads an
91
+ admin screen makes, and for apps on an SDK before 2.2.2.
package/dist/docs/i18n.md CHANGED
@@ -16,6 +16,45 @@ Forge's step 21 moves an app off it.
16
16
  3. **The host says which language.** Studio passes the person's own Studio language as `?lang=`, and later
17
17
  changes arrive live (`smartlinks:root-state` with `lang`). A key with no translation shows its English.
18
18
 
19
+ **Where the other languages come from at run time.** Only `en.json` is in your source. The other
20
+ languages are files the platform adds to the published bundle, and the SDK **downloads** the one in use
21
+ (`GET <folder>/i18n/fr.json`) when the app starts or the language changes. It is a static file fetch, not
22
+ an API call. `<folder>` is beside the page by default, or what you gave as `baseUrl` / `moduleUrl`. So when
23
+ a key that has a translation still shows English, ask: **which `fr.json` was fetched, from where, and was it
24
+ yours?** (In the browser's network list, filter on `i18n/`.) A local dev server or stand-in usually can't
25
+ show this fault: there the page and the bundle are the same folder.
26
+
27
+ ## Mounted inside another app's page: use `i18n.forApp`
28
+
29
+ A **container** or **widget** (mounted by Portal or Hub into their own page), or anything else that runs
30
+ inside another app's page, shares that page's SDK. There `i18n.t` reads the **host's** words, and
31
+ `i18n.init` fetches from beside the **host's** page (Portal's own `fr.json`, or its web page), so your keys
32
+ are never found and everything shows in English. Keep your own words instead:
33
+
34
+ ```ts
35
+ // src/i18n.ts — used by your pages, containers and widgets alike
36
+ import { useSyncExternalStore } from 'react'
37
+ import { i18n } from '@proveanything/smartlinks'
38
+ import en from '../public/i18n/en.json'
39
+
40
+ // Your English bundled in; other languages fetched from i18n/ beside YOUR bundle (import.meta.url).
41
+ export const words = i18n.forApp({ catalogs: { en }, moduleUrl: import.meta.url })
42
+ export function useT() {
43
+ useSyncExternalStore(words.subscribe, words.snapshot)
44
+ return words.t
45
+ }
46
+ ```
47
+
48
+ - The **language** stays the page's: the host sets it, and every app's words follow (each loads its own
49
+ catalog before the switch shows). In a container, set it from the host's `lang` prop with
50
+ `i18n.setLanguage(lang)`.
51
+ - `words.ready` resolves once the words for the current language are loaded.
52
+ - `moduleUrl: import.meta.url` finds `i18n/` beside the bundle (`containers.esm.js` at the bundle root; a
53
+ page chunk under `assets/` looks one folder up), then beside the page (a dev server). Pass `baseUrl`
54
+ instead when you know the folder exactly.
55
+ - Don't call `i18n.init` from a container or widget: it changes the host's settings.
56
+ - `forApp` works for a standalone page too, so one `src/i18n.ts` can serve every way the app is shown.
57
+
19
58
  In an admin app, `lang` is the person's **Studio language** (buttons and labels). The site's content
20
59
  language arrives separately as `contentLang`: use it for the language your product data is written in.
21
60
 
@@ -31,6 +31,10 @@ off()
31
31
  installed, removed or switched), `settings`.
32
32
  - **Filter fields:** `type`, `id`, `appId`, `recordType`, `productId`, `proofId`. Every field you give
33
33
  must match.
34
+ - **Many at once:** a large import doesn't send thousands of product changes. After the first few it
35
+ sends one `{ type: 'product' }` with no `id`, meaning "many products changed". A listener that follows
36
+ one thing, `{ type: 'product', id }`, hears that too (from SDK 2.2.4), since its product may be among
37
+ them. Read again as usual; `change.id` is empty, so don't rely on it.
34
38
  - **Who hears what:**
35
39
  - a collection's **admins** hear every change;
36
40
  - **everyone else** hears changes to public things (public app records, products).
package/dist/i18n.d.ts CHANGED
@@ -63,5 +63,52 @@ export declare const format: {
63
63
  /** Minor units as money in the current language: `money(129950, 'EUR')` → "1.299,50 €" in German. */
64
64
  money: (minor: number, currency: string) => string;
65
65
  };
66
+ export interface AppWordsOptions {
67
+ /** This app's English (and any other languages it bundles), keyed by language. */
68
+ catalogs?: Record<string, Catalog>;
69
+ /**
70
+ * The URL of the app's own code: pass `import.meta.url`. Its catalogs are fetched from `i18n/` beside
71
+ * the bundle (a page chunk under `assets/` looks one folder up), so a container mounted in Portal reads
72
+ * its own `fr.json`, not Portal's. In a dev server, where the code isn't in a bundle, the page's own
73
+ * `i18n/` folder is tried next.
74
+ */
75
+ moduleUrl?: string;
76
+ /** Where the catalogs are, when you know exactly (overrides moduleUrl). */
77
+ baseUrl?: string;
78
+ /** The app's source language. Default 'en'. */
79
+ sourceLanguage?: string;
80
+ }
81
+ export interface AppWords {
82
+ /** Loads this app's words for the language in use. Await it (or wait on `snapshot`) before first render. */
83
+ ready: Promise<void>;
84
+ /** Translate with this app's words, in the page's language. Same rules as `i18n.t`. */
85
+ t: typeof t;
86
+ /** Is there a translation for `key` in the language in use (not just the English fallback)? */
87
+ has: (key: string) => boolean;
88
+ /** The page's language (shared by every app on the page). */
89
+ language: () => string;
90
+ /** Language or catalog changes. For useSyncExternalStore. */
91
+ subscribe: (fn: () => void) => () => void;
92
+ /** Changes on every language or catalog change. */
93
+ snapshot: () => number;
94
+ }
95
+ /**
96
+ * One app's own words, kept apart from the page's. Use it in any code that may run inside another app's
97
+ * page: a container or widget mounted by Portal or Hub, a panel in Studio. There the SDK is the host's, so
98
+ * `i18n.t` reads the host's words and `i18n.init` would fetch from beside the host's page.
99
+ *
100
+ * // src/i18n.ts
101
+ * import en from '../public/i18n/en.json'
102
+ * export const words = i18n.forApp({ catalogs: { en }, moduleUrl: import.meta.url })
103
+ * export function useT() {
104
+ * useSyncExternalStore(words.subscribe, words.snapshot)
105
+ * return words.t
106
+ * }
107
+ *
108
+ * The language stays the page's: the host sets it (`i18n.setLanguage`, or its `smartlinks:language`
109
+ * message), and every app's words follow, each loading its own catalog first. Never throws: with nothing
110
+ * found, `t` returns the app's English, then the key.
111
+ */
112
+ export declare function forApp(options?: AppWordsOptions): AppWords;
66
113
  /** Forget loaded catalogs and listeners (tests). */
67
114
  export declare function _reset(): void;
package/dist/i18n.js CHANGED
@@ -23,6 +23,11 @@
23
23
  // }
24
24
  // const t = useT(); t('contacts.add') t('contacts.count', { count: 3 })
25
25
  //
26
+ // An app mounted INSIDE another app's page (a container or widget in Portal, a panel in Studio) shares
27
+ // that page's SDK, so `i18n.t` there reads the HOST's words, fetched from beside the host's page. Such an
28
+ // app keeps its own words with `i18n.forApp` instead: its own catalogs, fetched from its own bundle,
29
+ // following the page's language. See forApp below.
30
+ //
26
31
  // Catalog format (the same shape i18next uses, so files move between tools):
27
32
  // { "contacts": { "add": "Add contact", "count_one": "{{count}} contact", "count_other": "{{count}} contacts" } }
28
33
  // Nested keys are read with dots. `{{name}}` is replaced from the values. With `count`, the plural
@@ -194,7 +199,11 @@ export async function init(options = {}) {
194
199
  }
195
200
  current = normalizeLanguage(options.language) || detectLanguage(options.languages);
196
201
  listen();
197
- await Promise.all([fetchCatalog(source), current === source ? null : fetchCatalog(current)]);
202
+ await Promise.all([
203
+ fetchCatalog(source),
204
+ current === source ? null : fetchCatalog(current),
205
+ ...Array.from(appSets, (set) => fetchAppCatalog(set, current)),
206
+ ]);
198
207
  setPageLanguage(current);
199
208
  emit();
200
209
  }
@@ -203,7 +212,8 @@ export async function setLanguage(lang) {
203
212
  const next = normalizeLanguage(lang);
204
213
  if (!next || next === current)
205
214
  return;
206
- await fetchCatalog(next);
215
+ // The page's words and every app's own (forApp), so the whole page switches in one step.
216
+ await Promise.all([fetchCatalog(next), ...Array.from(appSets, (set) => fetchAppCatalog(set, next))]);
207
217
  current = next;
208
218
  setPageLanguage(current);
209
219
  emit();
@@ -313,6 +323,138 @@ export const format = {
313
323
  /** Minor units as money in the current language: `money(129950, 'EUR')` → "1.299,50 €" in German. */
314
324
  money: (minor, currency) => formatMinor(minor, currency, current),
315
325
  };
326
+ const appSets = new Set();
327
+ const withSlash = (u) => (u.endsWith('/') ? u : `${u}/`);
328
+ /** Where an app's catalogs may be, in order: an exact base, else beside its bundle, else beside the page. */
329
+ function appBases(options) {
330
+ if (options.baseUrl)
331
+ return [withSlash(options.baseUrl)];
332
+ const out = [];
333
+ if (options.moduleUrl) {
334
+ try {
335
+ const folder = new URL('.', options.moduleUrl);
336
+ // Vite puts a page's chunks in assets/; the catalogs are beside the page, one folder up.
337
+ const home = /\/assets\/$/.test(folder.pathname) ? new URL('..', folder) : folder;
338
+ out.push(new URL('i18n/', home).toString());
339
+ }
340
+ catch ( /* not a URL */_a) { /* not a URL */ }
341
+ }
342
+ try {
343
+ out.push(new URL('i18n/', document.baseURI).toString());
344
+ }
345
+ catch (_b) {
346
+ if (!out.length)
347
+ out.push('i18n/');
348
+ }
349
+ return Array.from(new Set(out));
350
+ }
351
+ async function fetchAppCatalog(set, lang) {
352
+ const known = (name) => set.supplied.has(name) || set.loaded.has(name);
353
+ if (candidates(lang).some(known))
354
+ return;
355
+ const pending = set.fetching.get(lang);
356
+ if (pending)
357
+ return pending;
358
+ const job = (async () => {
359
+ for (const name of candidates(lang)) {
360
+ for (const at of set.bases) {
361
+ try {
362
+ const res = await fetch(`${at}${name}.json`, { cache: 'default' });
363
+ if (!res.ok)
364
+ continue;
365
+ const type = res.headers.get('content-type') || '';
366
+ if (type && !type.includes('json'))
367
+ continue; // a host's index page isn't a catalog
368
+ const data = (await res.json());
369
+ if (data && typeof data === 'object') {
370
+ set.loaded.set(name, data);
371
+ return;
372
+ }
373
+ }
374
+ catch ( /* not there, or offline: try the next place */_a) { /* not there, or offline: try the next place */ }
375
+ }
376
+ }
377
+ })().finally(() => { set.fetching.delete(lang); });
378
+ set.fetching.set(lang, job);
379
+ return job;
380
+ }
381
+ function appCatalogFor(set, lang) {
382
+ for (const name of candidates(lang)) {
383
+ const cat = set.supplied.get(name) || set.loaded.get(name);
384
+ if (cat)
385
+ return cat;
386
+ }
387
+ return undefined;
388
+ }
389
+ function appFind(set, lang, key, count) {
390
+ var _a;
391
+ const cat = appCatalogFor(set, lang);
392
+ if (typeof count === 'number') {
393
+ const form = (_a = lookup(cat, `${key}_${pluralCategory(lang, count)}`)) !== null && _a !== void 0 ? _a : lookup(cat, `${key}_other`);
394
+ if (form !== undefined)
395
+ return form;
396
+ }
397
+ return lookup(cat, key);
398
+ }
399
+ function fill(text, values) {
400
+ if (!values)
401
+ return text;
402
+ return text.replace(/\{\{\s*([\w.]+)\s*\}\}/g, (m, name) => {
403
+ const v = values[name];
404
+ if (v === undefined || v === null)
405
+ return m;
406
+ return typeof v === 'number' ? v.toLocaleString(current) : String(v);
407
+ });
408
+ }
409
+ /**
410
+ * One app's own words, kept apart from the page's. Use it in any code that may run inside another app's
411
+ * page: a container or widget mounted by Portal or Hub, a panel in Studio. There the SDK is the host's, so
412
+ * `i18n.t` reads the host's words and `i18n.init` would fetch from beside the host's page.
413
+ *
414
+ * // src/i18n.ts
415
+ * import en from '../public/i18n/en.json'
416
+ * export const words = i18n.forApp({ catalogs: { en }, moduleUrl: import.meta.url })
417
+ * export function useT() {
418
+ * useSyncExternalStore(words.subscribe, words.snapshot)
419
+ * return words.t
420
+ * }
421
+ *
422
+ * The language stays the page's: the host sets it (`i18n.setLanguage`, or its `smartlinks:language`
423
+ * message), and every app's words follow, each loading its own catalog first. Never throws: with nothing
424
+ * found, `t` returns the app's English, then the key.
425
+ */
426
+ export function forApp(options = {}) {
427
+ const set = {
428
+ bases: appBases(options),
429
+ source: normalizeLanguage(options.sourceLanguage) || 'en',
430
+ supplied: new Map(),
431
+ loaded: new Map(),
432
+ fetching: new Map(),
433
+ };
434
+ if (options.catalogs)
435
+ for (const [lang, cat] of Object.entries(options.catalogs)) {
436
+ set.supplied.set(normalizeLanguage(lang) || lang, cat);
437
+ }
438
+ appSets.add(set);
439
+ listen();
440
+ const ready = Promise.all([fetchAppCatalog(set, set.source), fetchAppCatalog(set, current)])
441
+ .then(() => { emit(); })
442
+ .catch(() => undefined);
443
+ const appT = (key, values) => {
444
+ var _a, _b, _c;
445
+ const count = values && typeof values.count === 'number' ? values.count : undefined;
446
+ const text = (_c = (_b = (_a = appFind(set, current, key, count)) !== null && _a !== void 0 ? _a : (current === set.source ? undefined : appFind(set, set.source, key, count))) !== null && _b !== void 0 ? _b : values === null || values === void 0 ? void 0 : values.defaultValue) !== null && _c !== void 0 ? _c : key;
447
+ return fill(text, values);
448
+ };
449
+ return {
450
+ ready,
451
+ t: appT,
452
+ has: (key) => appFind(set, current, key) !== undefined,
453
+ language,
454
+ subscribe,
455
+ snapshot,
456
+ };
457
+ }
316
458
  /** Forget loaded catalogs and listeners (tests). */
317
459
  export function _reset() {
318
460
  source = 'en';
@@ -321,5 +463,6 @@ export function _reset() {
321
463
  loaded.clear();
322
464
  supplied.clear();
323
465
  listeners.clear();
466
+ appSets.clear();
324
467
  version = 0;
325
468
  }
@@ -62,6 +62,7 @@ export interface RealtimeChangeEvent {
62
62
  * and, on an app's staff feed (`onStaffChange`): 'workOrder' | 'container' | 'stock'.
63
63
  */
64
64
  type: string;
65
+ /** The thing that changed. Absent when many of this type changed at once (an import): read them all again. */
65
66
  id?: string;
66
67
  appId?: string;
67
68
  recordType?: string;
@@ -73,7 +74,10 @@ export interface RealtimeChangeEvent {
73
74
  kind?: string;
74
75
  at: number;
75
76
  }
76
- /** Which changes to hear about — every field given must match. */
77
+ /**
78
+ * Which changes to hear about — every field given must match. One exception: with `type` and `id`
79
+ * given, a change of that type that names no id ("many changed") is heard too.
80
+ */
77
81
  export interface RealtimeChangeFilter {
78
82
  type?: string;
79
83
  id?: string;
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const SDK_VERSION = "2.2.3";
1
+ export declare const SDK_VERSION = "2.2.5";
package/dist/version.js CHANGED
@@ -1,2 +1,2 @@
1
1
  // Generated by scripts/write-version.mjs from package.json. Do not edit.
2
- export const SDK_VERSION = "2.2.3";
2
+ export const SDK_VERSION = "2.2.5";
@@ -1,6 +1,6 @@
1
1
  # Smartlinks API Summary
2
2
 
3
- Version: 2.2.3 | Generated: 2026-12-02T13:03:18.253Z
3
+ Version: 2.2.5 | Generated: 2026-12-04T13:13:18.143Z
4
4
 
5
5
  This is a concise summary of all available API functions and types.
6
6
 
@@ -16547,7 +16547,7 @@ Reset rate limit for a user
16547
16547
  One of your app's declared channels (manifest `realtime.channels`). ```ts const draw = realtime.channel({ collectionId, appId }, "draw") const off = draw.subscribe("status", (m) => setStatus(m.data)) await draw.publish("status", { phase: "spinning" }) // if the channel lets this viewer publish ```
16548
16548
 
16549
16549
  **onChange**(scope: RealtimeScope, filter: RealtimeChangeFilter, onChangeFn: (change: RealtimeChangeEvent) → `void`
16550
- Something changed: refetch it. IDs only — never data. Admins of the collection see every change; everyone else sees changes to public things (public app records, products). Filter by `type` ('product' | 'proof' | 'asset' | 'record' | 'collection' | 'collection.apps' | 'settings'), `id`, `appId`, `recordType`, `productId`, `proofId`. ```ts const off = realtime.onChange({ collectionId }, { type: "record", appId, recordType: "entry" }, () => refetch()) ```
16550
+ Something changed: refetch it. IDs only — never data. Admins of the collection see every change; everyone else sees changes to public things (public app records, products). Filter by `type` ('product' | 'proof' | 'asset' | 'record' | 'collection' | 'collection.apps' | 'settings'), `id`, `appId`, `recordType`, `productId`, `proofId`. A change with no `id` means many things of that type changed (an import): a `{ type, id }` filter hears it too, so check `change.id` before assuming it is yours. ```ts const off = realtime.onChange({ collectionId }, { type: "record", appId, recordType: "entry" }, () => refetch()) ```
16551
16551
 
16552
16552
  **onStaffChange**(scope: RealtimeScope & { appId: string }, filter: RealtimeChangeFilter, onChangeFn: (change: RealtimeChangeEvent) → `void`
16553
16553
  Something this APP works on changed: one of its work orders, a container or stock it moved, or one of its own records. For the people who use the app, admins or not: someone hears these when they are in one of the app's user groups (the ones its server functions are limited to, or `realtime.staff` in its manifest), and so does a collection admin. IDs only — refetch through your own API. Types: 'workOrder' (`id`, `appId`), 'container' (`id`, `kind` = its container type), 'stock' (`id` = the container, `productId`), 'record' (`id`, `recordType`). Filter as with `onChange`; an empty filter hears them all. ```ts // A production app's screens refetch when anything on the floor changes. useEffect(() => realtime.onStaffChange({ collectionId, appId }, {}, () => queryClient.invalidateQueries()), [collectionId, appId]) ```
@@ -220,7 +220,7 @@ All standard widget props apply:
220
220
  | `tag` | `TagContext` | ❌ | **Deprecated** — use `itemContext.tag` instead. Kept for one release. |
221
221
  | `onNavigate` | function | ❌ | Navigation callback (accepts `NavigationRequest` or legacy string) |
222
222
  | `size` | string | ❌ | `"compact"`, `"standard"`, or `"large"` |
223
- | `lang` | string | ❌ | Language code (e.g., `"en"`) |
223
+ | `lang` | string | ❌ | Language code (e.g., `"en"`). Your own words: `i18n.forApp` (see [i18n.md](i18n.md)), never `i18n.init` or `i18n.t`, which are the host's |
224
224
  | `translations` | object | ❌ | Translation overrides |
225
225
  | `className` | string | ❌ | CSS class for the wrapper element |
226
226
 
@@ -1,91 +1,91 @@
1
- # Data changed: an open screen reads again
2
-
3
- Since 2.2.3.
4
-
5
- An admin screen is not the only thing that changes records. The assistant applies a change, another app
6
- saves, another person edits. The screen that is open keeps showing what it loaded, and the person has to
7
- reload to see the change. **Data changed** is the host telling the app, so it can read again.
8
-
9
- It is one message, host to app:
10
-
11
- ```js
12
- { type: 'smartlinks:data-changed', collectionId: 'acme', changes: [{ type: 'product', id: 'p_1', status: 'updated' }], source: 'assistant' }
13
- ```
14
-
15
- Ids only, never data. The app reads again through the normal API, with the normal permissions.
16
-
17
- ## In an app
18
-
19
- ```tsx
20
- import * as SL from '@proveanything/smartlinks'
21
-
22
- useEffect(() => SL.onDataChanged((e) => {
23
- if (e.collectionId && e.collectionId !== collectionId) return
24
- if (e.touches({ type: 'product' })) queryClient.invalidateQueries({ queryKey: ['products'] })
25
- if (e.touches({ type: 'product', id: productId })) queryClient.invalidateQueries({ queryKey: ['product', collectionId, productId] })
26
- }), [collectionId, productId])
27
- ```
28
-
29
- - `SL.onDataChanged(listener)` returns a function that stops listening. It does nothing outside a frame.
30
- - Before your listener runs, the SDK has already forgotten the reads it kept for what changed, so the
31
- next read returns what the server has now. This happens even in an app that never calls `onDataChanged`.
32
- - `e.changes` is what changed. It is **empty when the host can't say**: treat everything you show from
33
- the collection as possibly changed.
34
- - `e.touches({ type, id })` answers "might this be about my record?". It is true when a change matches
35
- every field you give, and always true when `e.changes` is empty.
36
- - `e.source` is who made the change when the host knows (`'assistant'`, `'app'`, `'person'`).
37
-
38
- ### A form the person is filling in
39
-
40
- Reading again must not throw away typing.
41
-
42
- - **Untouched form:** fill it again from the new record.
43
- - **Form with changes in it:** keep what the person typed. Say plainly that the record was changed
44
- elsewhere, and offer to load the new version. Do not save the old copy over the new one without asking.
45
-
46
- Compare the record you loaded with the one you just read (its `updatedAt`, or the fields the form shows)
47
- before saying anything: "something changed" is not always about the record on screen.
48
-
49
- ### One change word per kind of record
50
-
51
- `type` uses the same words as realtime changes (`docs/realtime.md`): `product`, `proof`, `asset`, `record`,
52
- `collection`, `collection.apps`, `settings`. A host may also send its own words for other records
53
- (`contact`, `appConfig`). `id` is the record's id; `productId`, `proofId`, `appId` and `recordType` say
54
- where it lives when that helps.
55
-
56
- ## In a host
57
-
58
- Send it after a change the app did not make itself.
59
-
60
- ```ts
61
- // Any host
62
- SL.postDataChanged(iframe.contentWindow, { collectionId, changes: [{ type: 'product', id }], source: 'assistant' })
63
-
64
- // With IframeResponder
65
- responder.dataChanged({ collectionId, changes: [{ type: 'product', id }] })
66
-
67
- // When you can't say what changed
68
- SL.postDataChanged(iframe.contentWindow, { collectionId })
69
- ```
70
-
71
- Studio sends it to every open app frame when an assistant change set is applied or undone, when the
72
- assistant finishes a step that writes, and when its turn ends.
73
-
74
- ## What it is not
75
-
76
- - It is not the realtime feed. `SL.realtime.onChange` (`docs/realtime.md`) is the server telling a page
77
- that something changed, over a live connection. Data changed is the host passing a change it already
78
- knows about into a frame, with no connection of the app's own. A host that listens to the realtime
79
- feed can pass those changes on with the same message, so an app needs only `onDataChanged`.
80
- - It does not carry data and it is not a save. Nothing is written by sending or hearing it.
81
-
82
- ## What the SDK forgets
83
-
84
- | The host says | The SDK forgets |
85
- |---|---|
86
- | Only products and proofs changed | Everything kept under that collection's products (the product, its lists, its proofs) |
87
- | Anything else, or nothing specific | Everything kept for that collection |
88
- | No collection given | Everything kept |
89
-
90
- Admin reads are never kept in the first place (`docs/caching.md`), so this matters for public reads an
91
- admin screen makes, and for apps on an SDK before 2.2.2.
1
+ # Data changed: an open screen reads again
2
+
3
+ Since 2.2.3.
4
+
5
+ An admin screen is not the only thing that changes records. The assistant applies a change, another app
6
+ saves, another person edits. The screen that is open keeps showing what it loaded, and the person has to
7
+ reload to see the change. **Data changed** is the host telling the app, so it can read again.
8
+
9
+ It is one message, host to app:
10
+
11
+ ```js
12
+ { type: 'smartlinks:data-changed', collectionId: 'acme', changes: [{ type: 'product', id: 'p_1', status: 'updated' }], source: 'assistant' }
13
+ ```
14
+
15
+ Ids only, never data. The app reads again through the normal API, with the normal permissions.
16
+
17
+ ## In an app
18
+
19
+ ```tsx
20
+ import * as SL from '@proveanything/smartlinks'
21
+
22
+ useEffect(() => SL.onDataChanged((e) => {
23
+ if (e.collectionId && e.collectionId !== collectionId) return
24
+ if (e.touches({ type: 'product' })) queryClient.invalidateQueries({ queryKey: ['products'] })
25
+ if (e.touches({ type: 'product', id: productId })) queryClient.invalidateQueries({ queryKey: ['product', collectionId, productId] })
26
+ }), [collectionId, productId])
27
+ ```
28
+
29
+ - `SL.onDataChanged(listener)` returns a function that stops listening. It does nothing outside a frame.
30
+ - Before your listener runs, the SDK has already forgotten the reads it kept for what changed, so the
31
+ next read returns what the server has now. This happens even in an app that never calls `onDataChanged`.
32
+ - `e.changes` is what changed. It is **empty when the host can't say**: treat everything you show from
33
+ the collection as possibly changed.
34
+ - `e.touches({ type, id })` answers "might this be about my record?". It is true when a change matches
35
+ every field you give, and always true when `e.changes` is empty.
36
+ - `e.source` is who made the change when the host knows (`'assistant'`, `'app'`, `'person'`).
37
+
38
+ ### A form the person is filling in
39
+
40
+ Reading again must not throw away typing.
41
+
42
+ - **Untouched form:** fill it again from the new record.
43
+ - **Form with changes in it:** keep what the person typed. Say plainly that the record was changed
44
+ elsewhere, and offer to load the new version. Do not save the old copy over the new one without asking.
45
+
46
+ Compare the record you loaded with the one you just read (its `updatedAt`, or the fields the form shows)
47
+ before saying anything: "something changed" is not always about the record on screen.
48
+
49
+ ### One change word per kind of record
50
+
51
+ `type` uses the same words as realtime changes (`docs/realtime.md`): `product`, `proof`, `asset`, `record`,
52
+ `collection`, `collection.apps`, `settings`. A host may also send its own words for other records
53
+ (`contact`, `appConfig`). `id` is the record's id; `productId`, `proofId`, `appId` and `recordType` say
54
+ where it lives when that helps.
55
+
56
+ ## In a host
57
+
58
+ Send it after a change the app did not make itself.
59
+
60
+ ```ts
61
+ // Any host
62
+ SL.postDataChanged(iframe.contentWindow, { collectionId, changes: [{ type: 'product', id }], source: 'assistant' })
63
+
64
+ // With IframeResponder
65
+ responder.dataChanged({ collectionId, changes: [{ type: 'product', id }] })
66
+
67
+ // When you can't say what changed
68
+ SL.postDataChanged(iframe.contentWindow, { collectionId })
69
+ ```
70
+
71
+ Studio sends it to every open app frame when an assistant change set is applied or undone, when the
72
+ assistant finishes a step that writes, and when its turn ends.
73
+
74
+ ## What it is not
75
+
76
+ - It is not the realtime feed. `SL.realtime.onChange` (`docs/realtime.md`) is the server telling a page
77
+ that something changed, over a live connection. Data changed is the host passing a change it already
78
+ knows about into a frame, with no connection of the app's own. A host that listens to the realtime
79
+ feed can pass those changes on with the same message, so an app needs only `onDataChanged`.
80
+ - It does not carry data and it is not a save. Nothing is written by sending or hearing it.
81
+
82
+ ## What the SDK forgets
83
+
84
+ | The host says | The SDK forgets |
85
+ |---|---|
86
+ | Only products and proofs changed | Everything kept under that collection's products (the product, its lists, its proofs) |
87
+ | Anything else, or nothing specific | Everything kept for that collection |
88
+ | No collection given | Everything kept |
89
+
90
+ Admin reads are never kept in the first place (`docs/caching.md`), so this matters for public reads an
91
+ admin screen makes, and for apps on an SDK before 2.2.2.
package/docs/i18n.md CHANGED
@@ -16,6 +16,45 @@ Forge's step 21 moves an app off it.
16
16
  3. **The host says which language.** Studio passes the person's own Studio language as `?lang=`, and later
17
17
  changes arrive live (`smartlinks:root-state` with `lang`). A key with no translation shows its English.
18
18
 
19
+ **Where the other languages come from at run time.** Only `en.json` is in your source. The other
20
+ languages are files the platform adds to the published bundle, and the SDK **downloads** the one in use
21
+ (`GET <folder>/i18n/fr.json`) when the app starts or the language changes. It is a static file fetch, not
22
+ an API call. `<folder>` is beside the page by default, or what you gave as `baseUrl` / `moduleUrl`. So when
23
+ a key that has a translation still shows English, ask: **which `fr.json` was fetched, from where, and was it
24
+ yours?** (In the browser's network list, filter on `i18n/`.) A local dev server or stand-in usually can't
25
+ show this fault: there the page and the bundle are the same folder.
26
+
27
+ ## Mounted inside another app's page: use `i18n.forApp`
28
+
29
+ A **container** or **widget** (mounted by Portal or Hub into their own page), or anything else that runs
30
+ inside another app's page, shares that page's SDK. There `i18n.t` reads the **host's** words, and
31
+ `i18n.init` fetches from beside the **host's** page (Portal's own `fr.json`, or its web page), so your keys
32
+ are never found and everything shows in English. Keep your own words instead:
33
+
34
+ ```ts
35
+ // src/i18n.ts — used by your pages, containers and widgets alike
36
+ import { useSyncExternalStore } from 'react'
37
+ import { i18n } from '@proveanything/smartlinks'
38
+ import en from '../public/i18n/en.json'
39
+
40
+ // Your English bundled in; other languages fetched from i18n/ beside YOUR bundle (import.meta.url).
41
+ export const words = i18n.forApp({ catalogs: { en }, moduleUrl: import.meta.url })
42
+ export function useT() {
43
+ useSyncExternalStore(words.subscribe, words.snapshot)
44
+ return words.t
45
+ }
46
+ ```
47
+
48
+ - The **language** stays the page's: the host sets it, and every app's words follow (each loads its own
49
+ catalog before the switch shows). In a container, set it from the host's `lang` prop with
50
+ `i18n.setLanguage(lang)`.
51
+ - `words.ready` resolves once the words for the current language are loaded.
52
+ - `moduleUrl: import.meta.url` finds `i18n/` beside the bundle (`containers.esm.js` at the bundle root; a
53
+ page chunk under `assets/` looks one folder up), then beside the page (a dev server). Pass `baseUrl`
54
+ instead when you know the folder exactly.
55
+ - Don't call `i18n.init` from a container or widget: it changes the host's settings.
56
+ - `forApp` works for a standalone page too, so one `src/i18n.ts` can serve every way the app is shown.
57
+
19
58
  In an admin app, `lang` is the person's **Studio language** (buttons and labels). The site's content
20
59
  language arrives separately as `contentLang`: use it for the language your product data is written in.
21
60
 
package/docs/realtime.md CHANGED
@@ -31,6 +31,10 @@ off()
31
31
  installed, removed or switched), `settings`.
32
32
  - **Filter fields:** `type`, `id`, `appId`, `recordType`, `productId`, `proofId`. Every field you give
33
33
  must match.
34
+ - **Many at once:** a large import doesn't send thousands of product changes. After the first few it
35
+ sends one `{ type: 'product' }` with no `id`, meaning "many products changed". A listener that follows
36
+ one thing, `{ type: 'product', id }`, hears that too (from SDK 2.2.4), since its product may be among
37
+ them. Read again as usual; `change.id` is empty, so don't rely on it.
34
38
  - **Who hears what:**
35
39
  - a collection's **admins** hear every change;
36
40
  - **everyone else** hears changes to public things (public app records, products).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@proveanything/smartlinks",
3
- "version": "2.2.3",
3
+ "version": "2.2.5",
4
4
  "description": "Official JavaScript/TypeScript SDK for the Smartlinks API",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",