@colixsystems/widget-sdk 0.332.0 → 0.334.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/README.md CHANGED
@@ -91,7 +91,13 @@ See the design reference for the full architecture: [`docs/architecture/widget-m
91
91
 
92
92
  ## Status
93
93
 
94
- `v0.332.0` — pre-publish. The package surface (types, function names, export paths) is the v1 contract; runtime behaviour for some hooks is stubbed (each hook documents what's wired and what isn't). It is **not yet published to npm**.
94
+ `v0.334.0` — pre-publish. The package surface (types, function names, export paths) is the v1 contract; runtime behaviour for some hooks is stubbed (each hook documents what's wired and what isn't). It is **not yet published to npm**.
95
+
96
+ ### What's new in 0.334.0 (contract 1.272.0)
97
+
98
+ **A widget that sells access plans must let a buyer cancel.** A held monthly plan renews until `useAccessPlans().cancel(planId)` stops it, so a widget that calls `purchase` but never `cancel` left its subscribers no way to stop paying. The linter now refuses it with `access-plan-cancel-missing` (an error, so it blocks the publish). For a held plan with `renews: true`, render a "Cancel renewal" control that calls `cancel(plan.id)`, and once it resolves show `access_ends_at`, the day access ends. A one-time plan never renews, so the control never shows for it. The check reads the whole file, so one component may buy and another cancel; `cancel` counts only where it is called or passed on, never in text such as "Cancel any time". A widget that hands the whole hook result to a child component (`<PlanList plans={ap} />`) is left to that child.
99
+
100
+ **A plan taken off sale stays listed to its holders.** `plans` now also holds a plan the signed-in user holds after the workspace stopped selling it, with `purchase_available: false`, so they can still see it and cancel it. Show the buy control only where `purchase_available` is true.
95
101
 
96
102
  ### What's new in 0.332.0 (contract 1.271.0)
97
103
 
package/dist/contract.cjs CHANGED
@@ -2484,7 +2484,7 @@ const HOOKS = [
2484
2484
  signature: "useAccessPlans()",
2485
2485
  returnShape: {
2486
2486
  plans:
2487
- "Array<{ id, name, price_cents, currency, billing: \"one_time\" | \"monthly\", owned, renews, access_ends_at, purchase_available }> // owned: the user holds it; renews: a held monthly plan will charge again; access_ends_at: end of held access without a renewal (null for one-time); purchase_available: this plan can be bought here — false in an iOS/Android build for a plan the workspace does not sell in-app",
2487
+ "Array<{ id, name, price_cents, currency, billing: \"one_time\" | \"monthly\", owned, renews, access_ends_at, purchase_available }> // owned: the user holds it; renews: a held monthly plan will charge again; access_ends_at: end of held access without a renewal (null for one-time); purchase_available: this plan can be bought here — false in an iOS/Android build for a plan the workspace does not sell in-app, and for a plan the user holds that is no longer on sale (listed so they can still cancel it)",
2488
2488
  loading: "boolean",
2489
2489
  error: "PaymentError | null",
2490
2490
  purchaseAvailable:
@@ -2492,7 +2492,7 @@ const HOOKS = [
2492
2492
  purchase:
2493
2493
  "(planId, { return_path? }?) => Promise<{ plan_id, status: \"ACTIVE\" | \"PENDING\" }> // pass ONLY the plan id; the server sets price and group; the host opens hosted Checkout on the web and the store's in-app purchase sheet in an iOS/Android build; access arrives with the payment confirmation, so refresh() a moment later",
2494
2494
  cancel:
2495
- "(planId) => Promise<{ plan_id, renews: false, access_ends_at }> // stop a renewing monthly plan; access runs to access_ends_at — offer it beside every plan with renews: true",
2495
+ "(planId) => Promise<{ plan_id, renews: false, access_ends_at }> // stop a renewing monthly plan; access runs to access_ends_at — offer it beside every plan with renews: true; a widget that calls purchase without it is refused (access-plan-cancel-missing)",
2496
2496
  refresh: "() => Promise<void> // re-read after returning from Checkout",
2497
2497
  },
2498
2498
  requiredContextSlice: [
@@ -6470,7 +6470,7 @@ const CONTRACT = deepFreeze({
6470
6470
  // it. The app's global mobile quick bar is retired: it was a SELECTION
6471
6471
  // over the global menu, and the rail and drawer list every menu page
6472
6472
  // regardless, so nothing was reachable only through it. The bottom edge
6473
- // belongs to a PAGE's own bottom navigation bar (`local_nav = BOTTOM_BAR`,
6473
+ // belongs to a section menu's bottom navigation bar (`layout = BOTTOM_BAR`,
6474
6474
  // REQ-NAV-LOCAL), chosen per page. `resolveFooterTokens` is unchanged —
6475
6475
  // it is the bottom edge's token set and the surviving bar reads it.
6476
6476
  // 1.113.0: additive (sc-7951) — `resolveFooterTokens` gains the bottom
@@ -7073,7 +7073,10 @@ const CONTRACT = deepFreeze({
7073
7073
  // value, and an update may still change it.
7074
7074
  // 1.271.0: additive (sc-17551) — `useDatastoreAggregate` groups by a
7075
7075
  // relation column; new warning lint `aggregate-error-not-read`.
7076
- version: "1.271.0",
7076
+ // 1.272.0: stricter lint (sc-17552) — a widget that calls
7077
+ // useAccessPlans().purchase without cancel is the publish-blocking
7078
+ // `access-plan-cancel-missing` finding.
7079
+ version: "1.272.0",
7077
7080
  sharedTranslationKeys: SHARED_TRANSLATION_KEYS,
7078
7081
  hooks: HOOKS,
7079
7082
  primitives: PRIMITIVES,
package/dist/contract.js CHANGED
@@ -2479,7 +2479,7 @@ const HOOKS = [
2479
2479
  signature: "useAccessPlans()",
2480
2480
  returnShape: {
2481
2481
  plans:
2482
- "Array<{ id, name, price_cents, currency, billing: \"one_time\" | \"monthly\", owned, renews, access_ends_at, purchase_available }> // owned: the user holds it; renews: a held monthly plan will charge again; access_ends_at: end of held access without a renewal (null for one-time); purchase_available: this plan can be bought here — false in an iOS/Android build for a plan the workspace does not sell in-app",
2482
+ "Array<{ id, name, price_cents, currency, billing: \"one_time\" | \"monthly\", owned, renews, access_ends_at, purchase_available }> // owned: the user holds it; renews: a held monthly plan will charge again; access_ends_at: end of held access without a renewal (null for one-time); purchase_available: this plan can be bought here — false in an iOS/Android build for a plan the workspace does not sell in-app, and for a plan the user holds that is no longer on sale (listed so they can still cancel it)",
2483
2483
  loading: "boolean",
2484
2484
  error: "PaymentError | null",
2485
2485
  purchaseAvailable:
@@ -2487,7 +2487,7 @@ const HOOKS = [
2487
2487
  purchase:
2488
2488
  "(planId, { return_path? }?) => Promise<{ plan_id, status: \"ACTIVE\" | \"PENDING\" }> // pass ONLY the plan id; the server sets price and group; the host opens hosted Checkout on the web and the store's in-app purchase sheet in an iOS/Android build; access arrives with the payment confirmation, so refresh() a moment later",
2489
2489
  cancel:
2490
- "(planId) => Promise<{ plan_id, renews: false, access_ends_at }> // stop a renewing monthly plan; access runs to access_ends_at — offer it beside every plan with renews: true",
2490
+ "(planId) => Promise<{ plan_id, renews: false, access_ends_at }> // stop a renewing monthly plan; access runs to access_ends_at — offer it beside every plan with renews: true; a widget that calls purchase without it is refused (access-plan-cancel-missing)",
2491
2491
  refresh: "() => Promise<void> // re-read after returning from Checkout",
2492
2492
  },
2493
2493
  requiredContextSlice: [
@@ -6465,7 +6465,7 @@ const CONTRACT = deepFreeze({
6465
6465
  // it. The app's global mobile quick bar is retired: it was a SELECTION
6466
6466
  // over the global menu, and the rail and drawer list every menu page
6467
6467
  // regardless, so nothing was reachable only through it. The bottom edge
6468
- // belongs to a PAGE's own bottom navigation bar (`local_nav = BOTTOM_BAR`,
6468
+ // belongs to a section menu's bottom navigation bar (`layout = BOTTOM_BAR`,
6469
6469
  // REQ-NAV-LOCAL), chosen per page. `resolveFooterTokens` is unchanged —
6470
6470
  // it is the bottom edge's token set and the surviving bar reads it.
6471
6471
  // 1.113.0: additive (sc-7951) — `resolveFooterTokens` gains the bottom
@@ -7068,7 +7068,10 @@ const CONTRACT = deepFreeze({
7068
7068
  // value, and an update may still change it.
7069
7069
  // 1.271.0: additive (sc-17551) — `useDatastoreAggregate` groups by a
7070
7070
  // relation column; new warning lint `aggregate-error-not-read`.
7071
- version: "1.271.0",
7071
+ // 1.272.0: stricter lint (sc-17552) — a widget that calls
7072
+ // useAccessPlans().purchase without cancel is the publish-blocking
7073
+ // `access-plan-cancel-missing` finding.
7074
+ version: "1.272.0",
7072
7075
  sharedTranslationKeys: SHARED_TRANSLATION_KEYS,
7073
7076
  hooks: HOOKS,
7074
7077
  primitives: PRIMITIVES,
package/dist/host.d.ts CHANGED
@@ -395,45 +395,18 @@ export interface SidebarTokens {
395
395
  */
396
396
  export function resolveSidebarTokens(theme: unknown): SidebarTokens;
397
397
 
398
- /** One destination of a page's local menu, as a host hands it over. `key`
399
- * identifies the target in the caller's own vocabulary (a page slug on web, a
400
- * screen name in the export); a URL item carries none. */
401
- export interface SidebarUnfoldItem {
402
- key?: string | null;
403
- [field: string]: unknown;
404
- }
405
-
406
- /** One section's local menu and the sidebar entry it hangs beneath. `inside`
407
- * marks the section the visitor is in — the one that starts unfolded. */
408
- export interface SidebarUnfoldTree {
409
- under: string;
410
- items: Array<SidebarUnfoldItem & { current: boolean }>;
411
- standalone: boolean;
412
- inside: boolean;
413
- }
414
-
415
- /** Where a page's `SIDEBAR` local menu unfolds (REQ-NAV-LOCAL, sc-7696).
416
- * `under` is the global menu entry the visitor's own section hangs beneath,
417
- * or null when they are in none. `standalone` marks the case where the global
418
- * menu does not list the page, so the page itself is the entry. `trees` is
419
- * every section an entry carries, in menu order, so an entry can offer its
420
- * menu from any page; a `standalone` tree comes last. */
421
- export interface SidebarUnfold {
422
- under: string | null;
423
- items: Array<SidebarUnfoldItem & { current: boolean }>;
424
- standalone: boolean;
425
- trees: SidebarUnfoldTree[];
426
- }
427
-
428
- /** Resolve where the viewed page's local menu unfolds in the sidebar and the
429
- * drawer. The one derivation both hosts read: the Player calls it per render,
430
- * the compiler once per screen, so the rail and the exported drawer cannot
431
- * disagree. */
432
- export function resolveSidebarUnfold(input: {
433
- entries?: readonly string[];
434
- active?: string | null;
435
- sections?: Record<string, { menu?: unknown; items?: readonly unknown[] }>;
436
- }): SidebarUnfold;
398
+ /** A stored menu item with neither a page nor a link: a sub-menu (sc-14098). */
399
+ export function isSubMenuItem(item: unknown): boolean;
400
+
401
+ /** A menu's flat entries as both hosts draw them (sc-14098): the top-level
402
+ * ones in order, a sub-menu returned with its own entries as `children`.
403
+ * An empty sub-menu is dropped; an entry naming no sub-menu of the list
404
+ * stands at the top level. `of` reads `id`, `parent_id` and `submenu` when
405
+ * they live inside the entry. */
406
+ export function menuTree<T extends object>(
407
+ entries: readonly T[],
408
+ options?: { of?: (entry: T) => { id?: unknown; parent_id?: unknown; submenu?: unknown } },
409
+ ): Array<T | (T & { children: T[] })>;
437
410
 
438
411
 
439
412
  /** The top app bar's resolved tokens. `tintColor` and `titleColor` are separate
package/dist/host.js CHANGED
@@ -93,13 +93,9 @@ export { bannerScopeDefaults } from "./banner-tokens.js";
93
93
  // row above the side panel holds, in what order, and the box it sits in. The
94
94
  // same bar is drawn by the desktop top bar, the phone header and the exported
95
95
  // app's screen header, and each hard-coded its own row before this.
96
- // REQ-NAV-LOCAL (sc-7696): `resolveSidebarUnfold` answers where a page's
97
- // SIDEBAR local menu hangs in the rail and the drawer. The compiler asks it
98
- // once per screen at build time and the Player once per render, so the two
99
- // chromes derive the same tree from the same code.
100
- // REQ-NAV-LOCAL (sc-8901): `resolveLocalNavModel` answers WHICH model a page
101
- // wears in one device view, so the same menu can be a bottom bar on a phone and
102
- // unfold in the rail on a desktop.
96
+ // REQ-NAV-LOCAL (sc-8901, sc-12397): `resolveLocalNavModel` answers WHICH
97
+ // model a menu wears in one device view, so the same menu can be a bottom
98
+ // bar on a phone and a tab row on a desktop.
103
99
  export {
104
100
  normaliseNavigation,
105
101
  menuItemCap,
@@ -122,9 +118,17 @@ export {
122
118
  MAX_TOP_STRIPE_OFFSET_Y_PX,
123
119
  DEFAULT_TOP_STRIPE_HEIGHT_PX,
124
120
  DEFAULT_TOP_STRIPE_PADDING_X_PX,
125
- resolveSidebarUnfold,
121
+ // sc-14098: a menu's sub-menus, grouped the one way both hosts draw them.
122
+ isSubMenuItem,
123
+ menuTree,
126
124
  resolveLocalNavModel,
127
125
  drawsLocalNavAnywhere,
126
+ mainMenuAppearsOn,
127
+ LOCAL_NAV_POSITIONS,
128
+ localNavPosition,
129
+ resolvePageBars,
130
+ drawsTopNavigation,
131
+ topNavigationPageIds,
128
132
  isDarkSurface,
129
133
  resolveAppLogo,
130
134
  } from "./navigation.js";
package/dist/index.d.ts CHANGED
@@ -1676,7 +1676,11 @@ export interface AccessPlan {
1676
1676
  renews: boolean;
1677
1677
  /** ISO time held access ends without a renewal; null for one-time access. */
1678
1678
  access_ends_at: string | null;
1679
- /** Whether this plan can be bought here (false in a store build that does not sell it in-app). */
1679
+ /**
1680
+ * Whether this plan can be bought here: false in a store build that does not
1681
+ * sell it in-app, and for a plan the user holds that is no longer on sale
1682
+ * (listed so they can still cancel it).
1683
+ */
1680
1684
  purchase_available: boolean;
1681
1685
  }
1682
1686
 
package/dist/linter.cjs CHANGED
@@ -1925,34 +1925,51 @@ function _destructuredLocals(pattern) {
1925
1925
  return locals;
1926
1926
  }
1927
1927
 
1928
- // Which reads of a binding count: `idn.qr` on an object, the local name on a destructure.
1929
- function _bankIdOrderReads(binding) {
1930
- const escaped = (name) => name.replace(/\$/g, "\\$");
1931
- if (!binding.startsWith("{")) {
1932
- const name = escaped(binding);
1933
- const member = (field) => new RegExp(String.raw`(?<![\w$])${name}\s*\??\.\s*${field}(?![\w$])`);
1934
- // Handed whole to a child component, which draws the order itself.
1935
- const handedOn = new RegExp(String.raw`=\{\s*${name}\s*\}|\.\.\.\s*${name}(?![\w$])`);
1936
- return { read: member, handedOn };
1937
- }
1938
- const locals = _destructuredLocals(binding);
1939
- const word = (local) => new RegExp(String.raw`(?<![\w$.])${escaped(local)}(?![\w$])`);
1940
- return { read: (field) => (locals[field] ? word(locals[field]) : null), handedOn: null };
1928
+ // What the code reads, scanned once: bare identifiers, `name.field` member
1929
+ // reads, names handed whole to a child (`={ap}`, `...ap`), which does the work,
1930
+ // and names called or passed on: JSX text ("cancel any time") is neither.
1931
+ function _codeReads(code) {
1932
+ const members = new Set();
1933
+ for (const m of code.matchAll(/(?<![\w$])([\w$]+)(?=\s*\??\.\s*([\w$]+))/g)) members.add(`${m[1]}.${m[2]}`);
1934
+ const handedOn = new Set();
1935
+ for (const m of code.matchAll(/=\{\s*([\w$]+)\s*\}|\.\.\.\s*([\w$]+)/g)) handedOn.add(m[1] || m[2]);
1936
+ const used = new Set(handedOn);
1937
+ // No variable-length lookbehind: masked strings are long space runs.
1938
+ for (const m of code.matchAll(/(?<![\w$.])([\w$]+)(?=\s*\()|[,(]\s*([\w$]+)(?=\s*[,)])/g)) used.add(m[1] || m[2]);
1939
+ return { words: new Set(code.match(/(?<![\w$.])[\w$]+/g)), members, handedOn, used };
1940
+ }
1941
+
1942
+ // Each `const x = hook(` binding: its line, and which of its fields the code
1943
+ // reads (any read) or uses (calls or passes on). One scan per hook: a
1944
+ // per-binding scan was quadratic in the bindings.
1945
+ function _hookBindings(code, hook) {
1946
+ const bindings = new RegExp(String.raw`\b(?:const|let|var)\s+([\w$]+|\{[^{}]*\})\s*=\s*${hook}\s*\(`, "g");
1947
+ const calls = [...code.matchAll(bindings)];
1948
+ if (calls.length === 0) return [];
1949
+ // A destructure names every field; only reads elsewhere count.
1950
+ const { words, members, handedOn, used } = _codeReads(code.replace(bindings, (m) => " ".repeat(m.length)));
1951
+ const lineAt = _lineCounter(code);
1952
+ return calls.map((call) => {
1953
+ const binding = call[1];
1954
+ const locals = binding.startsWith("{") ? _destructuredLocals(binding) : null;
1955
+ const local = (field, seen) => (locals ? !!locals[field] && seen.has(locals[field]) : members.has(`${binding}.${field}`));
1956
+ return {
1957
+ line: lineAt(call.index),
1958
+ reads: (field) => local(field, words),
1959
+ uses: (field) => local(field, used),
1960
+ handedOn: !locals && handedOn.has(binding),
1961
+ };
1962
+ });
1941
1963
  }
1942
1964
 
1943
1965
  function _bankIdOrderRules(source) {
1944
1966
  const code = _stripNonCode(source);
1967
+ const lines = source.split(/\r?\n/);
1945
1968
  const findings = [];
1946
1969
  for (const { hook, verb, sameDevice } of BANKID_ORDER_HOOKS) {
1947
- const bindings = new RegExp(String.raw`\b(?:const|let|var)\s+([\w$]+|\{[^{}]*\})\s*=\s*${hook}\s*\(`, "g");
1948
- for (const call of code.matchAll(bindings)) {
1949
- // The destructure itself names every field; only reads elsewhere count.
1950
- const rest = code.slice(0, call.index) + " ".repeat(call[0].length) + code.slice(call.index + call[0].length);
1951
- const { read, handedOn } = _bankIdOrderReads(call[1]);
1952
- const reads = (field) => !!read(field) && read(field).test(rest);
1953
- if (!reads(verb) || (handedOn && handedOn.test(rest))) continue;
1970
+ for (const { line, reads, handedOn } of _hookBindings(code, hook)) {
1971
+ if (!reads(verb) || handedOn) continue;
1954
1972
  if (reads("qr") && (!sameDevice || reads("autoStartToken"))) continue;
1955
- const line = code.slice(0, call.index).split(/\r?\n/).length;
1956
1973
  findings.push({
1957
1974
  rule: "bankid-order-not-shown",
1958
1975
  severity: "error",
@@ -1962,13 +1979,34 @@ function _bankIdOrderRules(source) {
1962
1979
  (sameDevice ? ` and a button calling Linking.openURL(bankIdAppUrl(autoStartToken))` : "") +
1963
1980
  `, or BankID ends it unopened.`,
1964
1981
  line,
1965
- snippet: (source.split(/\r?\n/)[line - 1] || "").trim().slice(0, 200),
1982
+ snippet: (lines[line - 1] || "").trim().slice(0, 200),
1966
1983
  });
1967
1984
  }
1968
1985
  }
1969
1986
  return findings;
1970
1987
  }
1971
1988
 
1989
+ // sc-17552 — a plan a widget sells, its holder can stop renewing there.
1990
+ // One-time plans never renew, so the control never shows for them. Judged per
1991
+ // file: one component may buy and another cancel.
1992
+ function _accessPlanCancelRules(source) {
1993
+ const code = _stripNonCode(source);
1994
+ const lines = source.split(/\r?\n/);
1995
+ const bindings = _hookBindings(code, "useAccessPlans");
1996
+ if (bindings.some(({ uses, handedOn }) => uses("cancel") || handedOn)) return [];
1997
+ return bindings
1998
+ .filter(({ uses }) => uses("purchase"))
1999
+ .map(({ line }) => ({
2000
+ rule: "access-plan-cancel-missing",
2001
+ severity: "error",
2002
+ label:
2003
+ "useAccessPlans().purchase() sells a plan its buyer can never cancel. For a held plan with renews: true, " +
2004
+ "render a \"Cancel renewal\" control that calls cancel(plan.id), and show access_ends_at once it is cancelled.",
2005
+ line,
2006
+ snippet: (lines[line - 1] || "").trim().slice(0, 200),
2007
+ }));
2008
+ }
2009
+
1972
2010
  // sc-16511 — soft warning, the read twin of `datastore-error-not-branched`.
1973
2011
  // A read refused because the viewer may not see the table (a signed-out
1974
2012
  // visitor gets NOT_FOUND, a signed-in one FORBIDDEN) never clears on a retry,
@@ -3802,6 +3840,7 @@ function lintSource(source, options) {
3802
3840
  findings.push(..._readErrorHandlingRules(source));
3803
3841
  findings.push(..._aggregateErrorRules(source));
3804
3842
  findings.push(..._bankIdOrderRules(source));
3843
+ findings.push(..._accessPlanCancelRules(source));
3805
3844
  // sc-5206 — soft warnings steering a widget toward the two new hooks.
3806
3845
  findings.push(..._rawUseMemoIntoDatastoreQueryRules(source));
3807
3846
  findings.push(..._handRolledWriteGateRules(source));
package/dist/linter.js CHANGED
@@ -1920,34 +1920,51 @@ function _destructuredLocals(pattern) {
1920
1920
  return locals;
1921
1921
  }
1922
1922
 
1923
- // Which reads of a binding count: `idn.qr` on an object, the local name on a destructure.
1924
- function _bankIdOrderReads(binding) {
1925
- const escaped = (name) => name.replace(/\$/g, "\\$");
1926
- if (!binding.startsWith("{")) {
1927
- const name = escaped(binding);
1928
- const member = (field) => new RegExp(String.raw`(?<![\w$])${name}\s*\??\.\s*${field}(?![\w$])`);
1929
- // Handed whole to a child component, which draws the order itself.
1930
- const handedOn = new RegExp(String.raw`=\{\s*${name}\s*\}|\.\.\.\s*${name}(?![\w$])`);
1931
- return { read: member, handedOn };
1932
- }
1933
- const locals = _destructuredLocals(binding);
1934
- const word = (local) => new RegExp(String.raw`(?<![\w$.])${escaped(local)}(?![\w$])`);
1935
- return { read: (field) => (locals[field] ? word(locals[field]) : null), handedOn: null };
1923
+ // What the code reads, scanned once: bare identifiers, `name.field` member
1924
+ // reads, names handed whole to a child (`={ap}`, `...ap`), which does the work,
1925
+ // and names called or passed on: JSX text ("cancel any time") is neither.
1926
+ function _codeReads(code) {
1927
+ const members = new Set();
1928
+ for (const m of code.matchAll(/(?<![\w$])([\w$]+)(?=\s*\??\.\s*([\w$]+))/g)) members.add(`${m[1]}.${m[2]}`);
1929
+ const handedOn = new Set();
1930
+ for (const m of code.matchAll(/=\{\s*([\w$]+)\s*\}|\.\.\.\s*([\w$]+)/g)) handedOn.add(m[1] || m[2]);
1931
+ const used = new Set(handedOn);
1932
+ // No variable-length lookbehind: masked strings are long space runs.
1933
+ for (const m of code.matchAll(/(?<![\w$.])([\w$]+)(?=\s*\()|[,(]\s*([\w$]+)(?=\s*[,)])/g)) used.add(m[1] || m[2]);
1934
+ return { words: new Set(code.match(/(?<![\w$.])[\w$]+/g)), members, handedOn, used };
1935
+ }
1936
+
1937
+ // Each `const x = hook(` binding: its line, and which of its fields the code
1938
+ // reads (any read) or uses (calls or passes on). One scan per hook: a
1939
+ // per-binding scan was quadratic in the bindings.
1940
+ function _hookBindings(code, hook) {
1941
+ const bindings = new RegExp(String.raw`\b(?:const|let|var)\s+([\w$]+|\{[^{}]*\})\s*=\s*${hook}\s*\(`, "g");
1942
+ const calls = [...code.matchAll(bindings)];
1943
+ if (calls.length === 0) return [];
1944
+ // A destructure names every field; only reads elsewhere count.
1945
+ const { words, members, handedOn, used } = _codeReads(code.replace(bindings, (m) => " ".repeat(m.length)));
1946
+ const lineAt = _lineCounter(code);
1947
+ return calls.map((call) => {
1948
+ const binding = call[1];
1949
+ const locals = binding.startsWith("{") ? _destructuredLocals(binding) : null;
1950
+ const local = (field, seen) => (locals ? !!locals[field] && seen.has(locals[field]) : members.has(`${binding}.${field}`));
1951
+ return {
1952
+ line: lineAt(call.index),
1953
+ reads: (field) => local(field, words),
1954
+ uses: (field) => local(field, used),
1955
+ handedOn: !locals && handedOn.has(binding),
1956
+ };
1957
+ });
1936
1958
  }
1937
1959
 
1938
1960
  function _bankIdOrderRules(source) {
1939
1961
  const code = _stripNonCode(source);
1962
+ const lines = source.split(/\r?\n/);
1940
1963
  const findings = [];
1941
1964
  for (const { hook, verb, sameDevice } of BANKID_ORDER_HOOKS) {
1942
- const bindings = new RegExp(String.raw`\b(?:const|let|var)\s+([\w$]+|\{[^{}]*\})\s*=\s*${hook}\s*\(`, "g");
1943
- for (const call of code.matchAll(bindings)) {
1944
- // The destructure itself names every field; only reads elsewhere count.
1945
- const rest = code.slice(0, call.index) + " ".repeat(call[0].length) + code.slice(call.index + call[0].length);
1946
- const { read, handedOn } = _bankIdOrderReads(call[1]);
1947
- const reads = (field) => !!read(field) && read(field).test(rest);
1948
- if (!reads(verb) || (handedOn && handedOn.test(rest))) continue;
1965
+ for (const { line, reads, handedOn } of _hookBindings(code, hook)) {
1966
+ if (!reads(verb) || handedOn) continue;
1949
1967
  if (reads("qr") && (!sameDevice || reads("autoStartToken"))) continue;
1950
- const line = code.slice(0, call.index).split(/\r?\n/).length;
1951
1968
  findings.push({
1952
1969
  rule: "bankid-order-not-shown",
1953
1970
  severity: "error",
@@ -1957,13 +1974,34 @@ function _bankIdOrderRules(source) {
1957
1974
  (sameDevice ? ` and a button calling Linking.openURL(bankIdAppUrl(autoStartToken))` : "") +
1958
1975
  `, or BankID ends it unopened.`,
1959
1976
  line,
1960
- snippet: (source.split(/\r?\n/)[line - 1] || "").trim().slice(0, 200),
1977
+ snippet: (lines[line - 1] || "").trim().slice(0, 200),
1961
1978
  });
1962
1979
  }
1963
1980
  }
1964
1981
  return findings;
1965
1982
  }
1966
1983
 
1984
+ // sc-17552 — a plan a widget sells, its holder can stop renewing there.
1985
+ // One-time plans never renew, so the control never shows for them. Judged per
1986
+ // file: one component may buy and another cancel.
1987
+ function _accessPlanCancelRules(source) {
1988
+ const code = _stripNonCode(source);
1989
+ const lines = source.split(/\r?\n/);
1990
+ const bindings = _hookBindings(code, "useAccessPlans");
1991
+ if (bindings.some(({ uses, handedOn }) => uses("cancel") || handedOn)) return [];
1992
+ return bindings
1993
+ .filter(({ uses }) => uses("purchase"))
1994
+ .map(({ line }) => ({
1995
+ rule: "access-plan-cancel-missing",
1996
+ severity: "error",
1997
+ label:
1998
+ "useAccessPlans().purchase() sells a plan its buyer can never cancel. For a held plan with renews: true, " +
1999
+ "render a \"Cancel renewal\" control that calls cancel(plan.id), and show access_ends_at once it is cancelled.",
2000
+ line,
2001
+ snippet: (lines[line - 1] || "").trim().slice(0, 200),
2002
+ }));
2003
+ }
2004
+
1967
2005
  // sc-16511 — soft warning, the read twin of `datastore-error-not-branched`.
1968
2006
  // A read refused because the viewer may not see the table (a signed-out
1969
2007
  // visitor gets NOT_FOUND, a signed-in one FORBIDDEN) never clears on a retry,
@@ -3797,6 +3835,7 @@ export function lintSource(source, options) {
3797
3835
  findings.push(..._readErrorHandlingRules(source));
3798
3836
  findings.push(..._aggregateErrorRules(source));
3799
3837
  findings.push(..._bankIdOrderRules(source));
3838
+ findings.push(..._accessPlanCancelRules(source));
3800
3839
  // sc-5206 — soft warnings steering a widget toward the two new hooks.
3801
3840
  findings.push(..._rawUseMemoIntoDatastoreQueryRules(source));
3802
3841
  findings.push(..._handRolledWriteGateRules(source));
@@ -67,7 +67,7 @@ function menuItemCap(menuType) {
67
67
  // navigation bar is drawn from.
68
68
  //
69
69
  // sc-7753: the global quick bar these tokens were introduced for is retired;
70
- // the bar that reads them now is the page's own (`local_nav = BOTTOM_BAR`).
70
+ // the bar that reads them now is a section menu's (`layout = BOTTOM_BAR`).
71
71
  //
72
72
  // sc-8033: the block is SELF-CONTAINED — it reads `theme.footer` and nothing
73
73
  // else. Every colour used to fall back to the rail's, so restyling the sidebar
@@ -688,121 +688,59 @@ function resolveTopBarTokens(theme) {
688
688
  }
689
689
 
690
690
  /**
691
- * REQ-NAV-LOCAL (sc-7696) — where a page's `SIDEBAR` local menu unfolds.
691
+ * sc-14098 — is this stored menu item a sub-menu? One with neither a page nor
692
+ * a link; it holds the items whose `parent_id` names it.
692
693
  *
693
- * The one derivation both hosts read, so the web rail, the web drawer and the
694
- * exported app's drawer cannot disagree about what hangs under what. Keys are
695
- * the caller's own (the web Player passes page slugs, the compiler passes
696
- * screen names); this only compares them.
697
- *
698
- * A page belongs to a section in either of two ways, and both keep the tree
699
- * open: it CARRIES the model itself, or the section's menu LISTS it. The
700
- * second is what makes the model usable — clicking a destination would
701
- * otherwise collapse the menu that led there unless the author repeated the
702
- * setting on every sub-page.
703
- *
704
- * The unfold point is the first GLOBAL menu entry carrying that section's
705
- * saved menu, so the tree hangs in the same place from every page of the
706
- * section. When the global menu lists no such page, the section's own page
707
- * becomes a top-level entry (`standalone`) for as long as the visitor is
708
- * inside it, so the menu is never lost — a section off the menu is exactly
709
- * the case that would otherwise render blank.
694
+ * @param {object|null|undefined} item — a menu item as the API serves it.
695
+ * @returns {boolean}
696
+ */
697
+ function isSubMenuItem(item) {
698
+ return isPlainObject(item) && !item.page_id && !item.url;
699
+ }
700
+
701
+ /**
702
+ * sc-14098 — a menu's flat entries as both hosts draw them: the top-level
703
+ * ones in order, each sub-menu carrying its own as `children`. The one
704
+ * grouping the web chrome and the compiler read, so a page cannot sit in a
705
+ * sub-menu on one host and at the top level on the other (§8).
710
706
  *
711
- * sc-10693: `trees` answers EVERY section, not only the visitor's own, so a
712
- * section's entry can offer its menu from any page. `under` / `items` /
713
- * `standalone` still name the one the visitor is inside.
707
+ * An entry naming no sub-menu of the list stands at the top level; a
708
+ * sub-menu left with nothing in it (its pages hidden from this visitor, say)
709
+ * is dropped rather than drawn as a heading over nothing.
714
710
  *
715
- * @param {{ entries?: unknown, active?: unknown, sections?: unknown }} input
716
- * `entries` — the global menu's keys, in menu order.
717
- * `active` — the key of the page being viewed.
718
- * `sections` — `{ [key]: { menu, items } }` for pages whose local nav is
719
- * `SIDEBAR`; `menu` identifies the saved menu, `items` carry their own key.
720
- * @returns {{ under: string|null, items: Array<object>, standalone: boolean, trees: Array<object> }}
721
- * `under` is null when the visitor is in no section; when `standalone` it
722
- * names the section's own page, which the host draws as the entry. Each
723
- * item is returned with a `current` flag rather than leaving each host to
724
- * recompute it. `trees` lists `{ under, items, standalone, inside }` in menu
725
- * order, a `standalone` one last; `inside` marks the visitor's own.
711
+ * @param {Array<object>} entries — in menu order, each with an `id`.
712
+ * @param {{ of?: (entry: object) => { id?: unknown, parent_id?: unknown, submenu?: unknown } }} [options]
713
+ * `of` reads an entry's `id`, `parent_id` and `submenu` flag when they
714
+ * live inside it (the web chrome's `{ page, isActive }` rows).
715
+ * @returns {Array<object>} the top-level entries; a sub-menu is returned as
716
+ * `{ ...entry, children }`, every other entry as it came.
726
717
  */
727
- function resolveSidebarUnfold(input) {
728
- const { entries, active, sections } = isPlainObject(input) ? input : {};
729
- const viewed = typeof active === "string" && active ? active : null;
730
- const bank = isPlainObject(sections) ? sections : {};
731
- const order = Array.isArray(entries) ? entries : [];
732
- const sectionAt = (key) => {
733
- const candidate = isPlainObject(bank[key]) ? bank[key] : null;
734
- const items = candidate && Array.isArray(candidate.items) ? candidate.items : [];
735
- return items.length ? { key, menu: candidate.menu, items } : null;
736
- };
737
- const lists = (key) => {
738
- const candidate = sectionAt(key);
739
- return (
740
- Boolean(candidate) &&
741
- candidate.items.some(
742
- (item) => isPlainObject(item) && item.key === viewed,
743
- )
744
- );
745
- };
746
- // The page's own section wins; otherwise the section whose menu lists it,
747
- // preferring one a global entry carries so the tree hangs where it always
748
- // does. Both are the same section from the visitor's side.
749
- const section = viewed
750
- ? sectionAt(viewed) ||
751
- sectionAt(order.find(lists)) ||
752
- sectionAt(Object.keys(bank).find(lists))
753
- : null;
754
- const mark = (items) =>
755
- items.map((item) => ({
756
- ...(isPlainObject(item) ? item : {}),
757
- current:
758
- viewed !== null &&
759
- isPlainObject(item) &&
760
- typeof item.key === "string" &&
761
- item.key === viewed,
762
- }));
763
- // A saved menu hangs under the FIRST global entry carrying it, so two
764
- // entries sharing one menu never draw it twice.
765
- const claimed = [];
766
- const trees = [];
767
- for (const key of order) {
768
- if (!isPlainObject(bank[key])) continue;
769
- const { menu } = bank[key];
770
- if (claimed.includes(menu)) continue;
771
- claimed.push(menu);
772
- // An unusable key still owns its menu: nothing may hang it elsewhere.
773
- if (typeof key !== "string" || !key) continue;
774
- const inside = Boolean(section) && menu === section.menu;
775
- const own = inside ? section : sectionAt(key);
776
- if (!own) continue;
777
- trees.push({ under: key, items: mark(own.items), standalone: false, inside });
778
- }
779
- if (!section) return { under: null, items: [], standalone: false, trees };
780
- const owned = trees.find((tree) => tree.inside);
781
- if (owned) {
782
- return { under: owned.under, items: owned.items, standalone: false, trees };
783
- }
784
- const alone = {
785
- under: section.key,
786
- items: mark(section.items),
787
- standalone: true,
788
- inside: true,
789
- };
790
- return {
791
- under: alone.under,
792
- items: alone.items,
793
- standalone: true,
794
- trees: [...trees, alone],
718
+ function menuTree(entries, { of = (entry) => entry } = {}) {
719
+ const list = Array.isArray(entries) ? entries.filter(isPlainObject) : [];
720
+ const read = (entry) => (isPlainObject(of(entry)) ? of(entry) : {});
721
+ const subMenuIds = new Set(
722
+ list.filter((entry) => read(entry).submenu === true).map((entry) => read(entry).id),
723
+ );
724
+ const parentOf = (entry) => {
725
+ const { parent_id: parent, submenu } = read(entry);
726
+ return submenu !== true && typeof parent === "string" && subMenuIds.has(parent) ? parent : null;
795
727
  };
728
+ return list
729
+ .filter((entry) => parentOf(entry) === null)
730
+ .flatMap((entry) => {
731
+ if (read(entry).submenu !== true) return [entry];
732
+ const children = list.filter((child) => parentOf(child) === read(entry).id);
733
+ return children.length ? [{ ...entry, children }] : [];
734
+ });
796
735
  }
797
736
 
798
737
  /**
799
- * REQ-NAV-LOCAL (sc-8901) — which local-navigation model a page wears in ONE
800
- * device view, so the same saved menu can be a bottom bar on a phone and unfold
801
- * in the rail on a desktop.
738
+ * REQ-NAV-LOCAL (sc-8901, sc-12397) — how a menu is drawn in ONE device
739
+ * view, so the same menu can be a bottom bar on a phone and a tab row on a
740
+ * desktop. Since sc-12397 the MENU owns this, not each page.
802
741
  *
803
- * `local_nav` is the MOBILE value and the base of the cascade: a page authored
804
- * before the wider views existed leaves both of them unset and therefore
805
- * renders identically at every width. The cascade is REQ-LAY-07's — the one the
742
+ * `layout` is the MOBILE value and the base of the cascade; a wider view left
743
+ * null follows the narrower one. The cascade is REQ-LAY-07's — the one the
806
744
  * layout grid and every responsive widget prop already use — because a page's
807
745
  * chrome and its content must not disagree about what a tablet is.
808
746
  *
@@ -810,21 +748,21 @@ function resolveSidebarUnfold(input) {
810
748
  * host's own switch decides what it can draw, so a kind a newer backend allows
811
749
  * cascades correctly here instead of being flattened to `NONE` on the way.
812
750
  *
813
- * @param {object|null|undefined} page — a page row carrying `local_nav` and,
814
- * optionally, `local_nav_tablet` / `local_nav_desktop`.
751
+ * @param {object|null|undefined} menu — a menu carrying `layout` and,
752
+ * optionally, `layout_tablet` / `layout_desktop`. No menu draws nothing.
815
753
  * @param {"mobile"|"tablet"|"desktop"} view — the device view being drawn.
816
- * @returns {string} the model for that view; `NONE` when the page sets none.
754
+ * @returns {string} the model for that view; `NONE` when the menu sets none.
817
755
  */
818
- function resolveLocalNavModel(page, view) {
756
+ function resolveLocalNavModel(menu, view) {
819
757
  const at = (key) => {
820
- const value = isPlainObject(page) ? page[key] : null;
758
+ const value = isPlainObject(menu) ? menu[key] : null;
821
759
  return typeof value === "string" && value ? value : null;
822
760
  };
823
- const mobile = at("local_nav") || "NONE";
761
+ const mobile = at("layout") || "NONE";
824
762
  if (view === "desktop") {
825
- return at("local_nav_desktop") || at("local_nav_tablet") || mobile;
763
+ return at("layout_desktop") || at("layout_tablet") || mobile;
826
764
  }
827
- if (view === "tablet") return at("local_nav_tablet") || mobile;
765
+ if (view === "tablet") return at("layout_tablet") || mobile;
828
766
  return mobile;
829
767
  }
830
768
 
@@ -834,25 +772,117 @@ function resolveLocalNavModel(page, view) {
834
772
  const LOCAL_NAV_VIEWS = ["mobile", "tablet", "desktop"];
835
773
 
836
774
  /**
837
- * REQ-NAV-LOCAL (sc-8901) — does this page draw a local bar in ANY device view?
775
+ * REQ-NAV-LOCAL (sc-8901) — does this menu draw a bar in ANY device view?
838
776
  *
839
- * The saved-menu reference is shared by all three views, so it is required — and
840
- * resolved onto the wire — as soon as one of them draws. Asking
841
- * `local_nav !== "NONE"` alone would miss a page that carries a bar only on the
842
- * wider views.
777
+ * Beside the cascade it is derived from, so the API, the export's screen bake
778
+ * and the Studio cannot disagree about which pages show a bar.
843
779
  *
844
- * Beside the cascade it is derived from, so the API rule, the export's screen
845
- * bake and Copy Workspace cannot disagree about which pages have a bar.
846
- *
847
- * @param {object|null|undefined} page — a page row.
780
+ * @param {object|null|undefined} menu — a menu row, or its layout fields.
848
781
  * @returns {boolean}
849
782
  */
850
- function drawsLocalNavAnywhere(page) {
783
+ function drawsLocalNavAnywhere(menu) {
851
784
  return LOCAL_NAV_VIEWS.some(
852
- (view) => resolveLocalNavModel(page, view) !== "NONE",
785
+ (view) => resolveLocalNavModel(menu, view) !== "NONE",
853
786
  );
854
787
  }
855
788
 
789
+ /**
790
+ * sc-12426 — does the app's main menu frame this page? A page it leaves out
791
+ * is full screen. Asked by the Player per render and by the compiler per
792
+ * screen, so both hosts frame the same pages (§8).
793
+ *
794
+ * @param {{appears_on?: string, page_ids?: string[]}|null|undefined} menu —
795
+ * the main menu; absent frames every page.
796
+ * @param {string} pageId
797
+ * @returns {boolean}
798
+ */
799
+ function mainMenuAppearsOn(menu, pageId) {
800
+ if (!isPlainObject(menu) || menu.appears_on !== "ALL_EXCEPT") return true;
801
+ return !(Array.isArray(menu.page_ids) && menu.page_ids.includes(pageId));
802
+ }
803
+
804
+ /**
805
+ * sc-12530 — the place on the page each layout draws in. Two menus in one
806
+ * place would stack, so a page shows at most one per place per view.
807
+ */
808
+ const LOCAL_NAV_POSITIONS = Object.freeze({
809
+ TOP_BAR: "top",
810
+ BOTTOM_BAR: "bottom",
811
+ SIDEBAR: "side",
812
+ HEADER: "header",
813
+ });
814
+
815
+ /**
816
+ * sc-12530 — where a model draws; null for `NONE`. A kind this SDK does not
817
+ * know is its own place, so two of them still never stack.
818
+ *
819
+ * @param {string} model — from resolveLocalNavModel.
820
+ * @returns {string|null}
821
+ */
822
+ function localNavPosition(model) {
823
+ if (!model || model === "NONE") return null;
824
+ return LOCAL_NAV_POSITIONS[model] || model;
825
+ }
826
+
827
+ /**
828
+ * sc-12530 — the section menus a page draws in ONE device view, at most one
829
+ * per position. A menu picked for this page (`appears_on: "SELECTED"`) wins
830
+ * its position over one shown on all pages; between equals the first listed
831
+ * wins. The server refuses a save that would leave a tie (409
832
+ * MENU_POSITION_CLASH), so the tie-break only steadies older data. Asked by
833
+ * the Player per render and by the compiler per screen (§8).
834
+ *
835
+ * @param {Array<object>|null|undefined} menus — the page's `local_nav_menus`.
836
+ * @param {"mobile"|"tablet"|"desktop"} view
837
+ * @returns {Object<string, {menu: object, model: string}>} by position.
838
+ */
839
+ function resolvePageBars(menus, view) {
840
+ const bars = {};
841
+ const specific = (menu) => menu.appears_on === "SELECTED";
842
+ for (const menu of Array.isArray(menus) ? menus : []) {
843
+ if (!isPlainObject(menu)) continue;
844
+ const model = resolveLocalNavModel(menu, view);
845
+ const position = localNavPosition(model);
846
+ if (!position) continue;
847
+ const held = bars[position];
848
+ if (held && (specific(held.menu) || !specific(menu))) continue;
849
+ bars[position] = { menu, model };
850
+ }
851
+ return bars;
852
+ }
853
+
854
+ /**
855
+ * sc-12427 — whether a menu is a Top navigation bar at any view. The Studio
856
+ * styles that bar only for an app that draws one.
857
+ *
858
+ * @param {object|null|undefined} menu
859
+ * @returns {boolean}
860
+ */
861
+ function drawsTopNavigation(menu) {
862
+ return ["mobile", "tablet", "desktop"].some(
863
+ (view) => resolveLocalNavModel(menu, view) === "HEADER",
864
+ );
865
+ }
866
+
867
+ /**
868
+ * sc-12427 — the pages a Top navigation bar links to, at any view: they are
869
+ * top-level destinations, as the main menu's are, so neither host offers a
870
+ * back control on them.
871
+ *
872
+ * @param {Array<object>|null|undefined} menus — every menu a page may draw.
873
+ * @returns {Set<string>} page ids.
874
+ */
875
+ function topNavigationPageIds(menus) {
876
+ const ids = new Set();
877
+ for (const menu of Array.isArray(menus) ? menus : []) {
878
+ if (!isPlainObject(menu) || !drawsTopNavigation(menu)) continue;
879
+ for (const item of Array.isArray(menu.items) ? menu.items : []) {
880
+ if (isPlainObject(item) && typeof item.page_id === "string") ids.add(item.page_id);
881
+ }
882
+ }
883
+ return ids;
884
+ }
885
+
856
886
  // REQ-THEME-LOOK: the divider's width in px, bounded HERE so both hosts inherit
857
887
  // one range. The export clamped it on its way into the StyleSheet
858
888
  // (`coerceBorderWidth(...) ?? 1`) where the Player passed any finite number
@@ -910,7 +940,14 @@ module.exports = {
910
940
  isDarkSurface,
911
941
  resolveAppLogo,
912
942
  resolveTopBarTokens,
913
- resolveSidebarUnfold,
943
+ isSubMenuItem,
944
+ menuTree,
914
945
  resolveLocalNavModel,
915
946
  drawsLocalNavAnywhere,
947
+ mainMenuAppearsOn,
948
+ LOCAL_NAV_POSITIONS,
949
+ localNavPosition,
950
+ resolvePageBars,
951
+ drawsTopNavigation,
952
+ topNavigationPageIds,
916
953
  };
@@ -62,7 +62,7 @@ export function menuItemCap(menuType) {
62
62
  // navigation bar is drawn from.
63
63
  //
64
64
  // sc-7753: the global quick bar these tokens were introduced for is retired;
65
- // the bar that reads them now is the page's own (`local_nav = BOTTOM_BAR`).
65
+ // the bar that reads them now is a section menu's (`layout = BOTTOM_BAR`).
66
66
  //
67
67
  // sc-8033: the block is SELF-CONTAINED — it reads `theme.footer` and nothing
68
68
  // else. Every colour used to fall back to the rail's, so restyling the sidebar
@@ -683,121 +683,59 @@ export function resolveTopBarTokens(theme) {
683
683
  }
684
684
 
685
685
  /**
686
- * REQ-NAV-LOCAL (sc-7696) — where a page's `SIDEBAR` local menu unfolds.
686
+ * sc-14098 — is this stored menu item a sub-menu? One with neither a page nor
687
+ * a link; it holds the items whose `parent_id` names it.
687
688
  *
688
- * The one derivation both hosts read, so the web rail, the web drawer and the
689
- * exported app's drawer cannot disagree about what hangs under what. Keys are
690
- * the caller's own (the web Player passes page slugs, the compiler passes
691
- * screen names); this only compares them.
692
- *
693
- * A page belongs to a section in either of two ways, and both keep the tree
694
- * open: it CARRIES the model itself, or the section's menu LISTS it. The
695
- * second is what makes the model usable — clicking a destination would
696
- * otherwise collapse the menu that led there unless the author repeated the
697
- * setting on every sub-page.
698
- *
699
- * The unfold point is the first GLOBAL menu entry carrying that section's
700
- * saved menu, so the tree hangs in the same place from every page of the
701
- * section. When the global menu lists no such page, the section's own page
702
- * becomes a top-level entry (`standalone`) for as long as the visitor is
703
- * inside it, so the menu is never lost — a section off the menu is exactly
704
- * the case that would otherwise render blank.
689
+ * @param {object|null|undefined} item — a menu item as the API serves it.
690
+ * @returns {boolean}
691
+ */
692
+ export function isSubMenuItem(item) {
693
+ return isPlainObject(item) && !item.page_id && !item.url;
694
+ }
695
+
696
+ /**
697
+ * sc-14098 — a menu's flat entries as both hosts draw them: the top-level
698
+ * ones in order, each sub-menu carrying its own as `children`. The one
699
+ * grouping the web chrome and the compiler read, so a page cannot sit in a
700
+ * sub-menu on one host and at the top level on the other (§8).
705
701
  *
706
- * sc-10693: `trees` answers EVERY section, not only the visitor's own, so a
707
- * section's entry can offer its menu from any page. `under` / `items` /
708
- * `standalone` still name the one the visitor is inside.
702
+ * An entry naming no sub-menu of the list stands at the top level; a
703
+ * sub-menu left with nothing in it (its pages hidden from this visitor, say)
704
+ * is dropped rather than drawn as a heading over nothing.
709
705
  *
710
- * @param {{ entries?: unknown, active?: unknown, sections?: unknown }} input
711
- * `entries` — the global menu's keys, in menu order.
712
- * `active` — the key of the page being viewed.
713
- * `sections` — `{ [key]: { menu, items } }` for pages whose local nav is
714
- * `SIDEBAR`; `menu` identifies the saved menu, `items` carry their own key.
715
- * @returns {{ under: string|null, items: Array<object>, standalone: boolean, trees: Array<object> }}
716
- * `under` is null when the visitor is in no section; when `standalone` it
717
- * names the section's own page, which the host draws as the entry. Each
718
- * item is returned with a `current` flag rather than leaving each host to
719
- * recompute it. `trees` lists `{ under, items, standalone, inside }` in menu
720
- * order, a `standalone` one last; `inside` marks the visitor's own.
706
+ * @param {Array<object>} entries — in menu order, each with an `id`.
707
+ * @param {{ of?: (entry: object) => { id?: unknown, parent_id?: unknown, submenu?: unknown } }} [options]
708
+ * `of` reads an entry's `id`, `parent_id` and `submenu` flag when they
709
+ * live inside it (the web chrome's `{ page, isActive }` rows).
710
+ * @returns {Array<object>} the top-level entries; a sub-menu is returned as
711
+ * `{ ...entry, children }`, every other entry as it came.
721
712
  */
722
- export function resolveSidebarUnfold(input) {
723
- const { entries, active, sections } = isPlainObject(input) ? input : {};
724
- const viewed = typeof active === "string" && active ? active : null;
725
- const bank = isPlainObject(sections) ? sections : {};
726
- const order = Array.isArray(entries) ? entries : [];
727
- const sectionAt = (key) => {
728
- const candidate = isPlainObject(bank[key]) ? bank[key] : null;
729
- const items = candidate && Array.isArray(candidate.items) ? candidate.items : [];
730
- return items.length ? { key, menu: candidate.menu, items } : null;
731
- };
732
- const lists = (key) => {
733
- const candidate = sectionAt(key);
734
- return (
735
- Boolean(candidate) &&
736
- candidate.items.some(
737
- (item) => isPlainObject(item) && item.key === viewed,
738
- )
739
- );
740
- };
741
- // The page's own section wins; otherwise the section whose menu lists it,
742
- // preferring one a global entry carries so the tree hangs where it always
743
- // does. Both are the same section from the visitor's side.
744
- const section = viewed
745
- ? sectionAt(viewed) ||
746
- sectionAt(order.find(lists)) ||
747
- sectionAt(Object.keys(bank).find(lists))
748
- : null;
749
- const mark = (items) =>
750
- items.map((item) => ({
751
- ...(isPlainObject(item) ? item : {}),
752
- current:
753
- viewed !== null &&
754
- isPlainObject(item) &&
755
- typeof item.key === "string" &&
756
- item.key === viewed,
757
- }));
758
- // A saved menu hangs under the FIRST global entry carrying it, so two
759
- // entries sharing one menu never draw it twice.
760
- const claimed = [];
761
- const trees = [];
762
- for (const key of order) {
763
- if (!isPlainObject(bank[key])) continue;
764
- const { menu } = bank[key];
765
- if (claimed.includes(menu)) continue;
766
- claimed.push(menu);
767
- // An unusable key still owns its menu: nothing may hang it elsewhere.
768
- if (typeof key !== "string" || !key) continue;
769
- const inside = Boolean(section) && menu === section.menu;
770
- const own = inside ? section : sectionAt(key);
771
- if (!own) continue;
772
- trees.push({ under: key, items: mark(own.items), standalone: false, inside });
773
- }
774
- if (!section) return { under: null, items: [], standalone: false, trees };
775
- const owned = trees.find((tree) => tree.inside);
776
- if (owned) {
777
- return { under: owned.under, items: owned.items, standalone: false, trees };
778
- }
779
- const alone = {
780
- under: section.key,
781
- items: mark(section.items),
782
- standalone: true,
783
- inside: true,
784
- };
785
- return {
786
- under: alone.under,
787
- items: alone.items,
788
- standalone: true,
789
- trees: [...trees, alone],
713
+ export function menuTree(entries, { of = (entry) => entry } = {}) {
714
+ const list = Array.isArray(entries) ? entries.filter(isPlainObject) : [];
715
+ const read = (entry) => (isPlainObject(of(entry)) ? of(entry) : {});
716
+ const subMenuIds = new Set(
717
+ list.filter((entry) => read(entry).submenu === true).map((entry) => read(entry).id),
718
+ );
719
+ const parentOf = (entry) => {
720
+ const { parent_id: parent, submenu } = read(entry);
721
+ return submenu !== true && typeof parent === "string" && subMenuIds.has(parent) ? parent : null;
790
722
  };
723
+ return list
724
+ .filter((entry) => parentOf(entry) === null)
725
+ .flatMap((entry) => {
726
+ if (read(entry).submenu !== true) return [entry];
727
+ const children = list.filter((child) => parentOf(child) === read(entry).id);
728
+ return children.length ? [{ ...entry, children }] : [];
729
+ });
791
730
  }
792
731
 
793
732
  /**
794
- * REQ-NAV-LOCAL (sc-8901) — which local-navigation model a page wears in ONE
795
- * device view, so the same saved menu can be a bottom bar on a phone and unfold
796
- * in the rail on a desktop.
733
+ * REQ-NAV-LOCAL (sc-8901, sc-12397) — how a menu is drawn in ONE device
734
+ * view, so the same menu can be a bottom bar on a phone and a tab row on a
735
+ * desktop. Since sc-12397 the MENU owns this, not each page.
797
736
  *
798
- * `local_nav` is the MOBILE value and the base of the cascade: a page authored
799
- * before the wider views existed leaves both of them unset and therefore
800
- * renders identically at every width. The cascade is REQ-LAY-07's — the one the
737
+ * `layout` is the MOBILE value and the base of the cascade; a wider view left
738
+ * null follows the narrower one. The cascade is REQ-LAY-07's — the one the
801
739
  * layout grid and every responsive widget prop already use — because a page's
802
740
  * chrome and its content must not disagree about what a tablet is.
803
741
  *
@@ -805,21 +743,21 @@ export function resolveSidebarUnfold(input) {
805
743
  * host's own switch decides what it can draw, so a kind a newer backend allows
806
744
  * cascades correctly here instead of being flattened to `NONE` on the way.
807
745
  *
808
- * @param {object|null|undefined} page — a page row carrying `local_nav` and,
809
- * optionally, `local_nav_tablet` / `local_nav_desktop`.
746
+ * @param {object|null|undefined} menu — a menu carrying `layout` and,
747
+ * optionally, `layout_tablet` / `layout_desktop`. No menu draws nothing.
810
748
  * @param {"mobile"|"tablet"|"desktop"} view — the device view being drawn.
811
- * @returns {string} the model for that view; `NONE` when the page sets none.
749
+ * @returns {string} the model for that view; `NONE` when the menu sets none.
812
750
  */
813
- export function resolveLocalNavModel(page, view) {
751
+ export function resolveLocalNavModel(menu, view) {
814
752
  const at = (key) => {
815
- const value = isPlainObject(page) ? page[key] : null;
753
+ const value = isPlainObject(menu) ? menu[key] : null;
816
754
  return typeof value === "string" && value ? value : null;
817
755
  };
818
- const mobile = at("local_nav") || "NONE";
756
+ const mobile = at("layout") || "NONE";
819
757
  if (view === "desktop") {
820
- return at("local_nav_desktop") || at("local_nav_tablet") || mobile;
758
+ return at("layout_desktop") || at("layout_tablet") || mobile;
821
759
  }
822
- if (view === "tablet") return at("local_nav_tablet") || mobile;
760
+ if (view === "tablet") return at("layout_tablet") || mobile;
823
761
  return mobile;
824
762
  }
825
763
 
@@ -829,25 +767,117 @@ export function resolveLocalNavModel(page, view) {
829
767
  const LOCAL_NAV_VIEWS = ["mobile", "tablet", "desktop"];
830
768
 
831
769
  /**
832
- * REQ-NAV-LOCAL (sc-8901) — does this page draw a local bar in ANY device view?
770
+ * REQ-NAV-LOCAL (sc-8901) — does this menu draw a bar in ANY device view?
833
771
  *
834
- * The saved-menu reference is shared by all three views, so it is required — and
835
- * resolved onto the wire — as soon as one of them draws. Asking
836
- * `local_nav !== "NONE"` alone would miss a page that carries a bar only on the
837
- * wider views.
772
+ * Beside the cascade it is derived from, so the API, the export's screen bake
773
+ * and the Studio cannot disagree about which pages show a bar.
838
774
  *
839
- * Beside the cascade it is derived from, so the API rule, the export's screen
840
- * bake and Copy Workspace cannot disagree about which pages have a bar.
841
- *
842
- * @param {object|null|undefined} page — a page row.
775
+ * @param {object|null|undefined} menu — a menu row, or its layout fields.
843
776
  * @returns {boolean}
844
777
  */
845
- export function drawsLocalNavAnywhere(page) {
778
+ export function drawsLocalNavAnywhere(menu) {
846
779
  return LOCAL_NAV_VIEWS.some(
847
- (view) => resolveLocalNavModel(page, view) !== "NONE",
780
+ (view) => resolveLocalNavModel(menu, view) !== "NONE",
848
781
  );
849
782
  }
850
783
 
784
+ /**
785
+ * sc-12426 — does the app's main menu frame this page? A page it leaves out
786
+ * is full screen. Asked by the Player per render and by the compiler per
787
+ * screen, so both hosts frame the same pages (§8).
788
+ *
789
+ * @param {{appears_on?: string, page_ids?: string[]}|null|undefined} menu —
790
+ * the main menu; absent frames every page.
791
+ * @param {string} pageId
792
+ * @returns {boolean}
793
+ */
794
+ export function mainMenuAppearsOn(menu, pageId) {
795
+ if (!isPlainObject(menu) || menu.appears_on !== "ALL_EXCEPT") return true;
796
+ return !(Array.isArray(menu.page_ids) && menu.page_ids.includes(pageId));
797
+ }
798
+
799
+ /**
800
+ * sc-12530 — the place on the page each layout draws in. Two menus in one
801
+ * place would stack, so a page shows at most one per place per view.
802
+ */
803
+ export const LOCAL_NAV_POSITIONS = Object.freeze({
804
+ TOP_BAR: "top",
805
+ BOTTOM_BAR: "bottom",
806
+ SIDEBAR: "side",
807
+ HEADER: "header",
808
+ });
809
+
810
+ /**
811
+ * sc-12530 — where a model draws; null for `NONE`. A kind this SDK does not
812
+ * know is its own place, so two of them still never stack.
813
+ *
814
+ * @param {string} model — from resolveLocalNavModel.
815
+ * @returns {string|null}
816
+ */
817
+ export function localNavPosition(model) {
818
+ if (!model || model === "NONE") return null;
819
+ return LOCAL_NAV_POSITIONS[model] || model;
820
+ }
821
+
822
+ /**
823
+ * sc-12530 — the section menus a page draws in ONE device view, at most one
824
+ * per position. A menu picked for this page (`appears_on: "SELECTED"`) wins
825
+ * its position over one shown on all pages; between equals the first listed
826
+ * wins. The server refuses a save that would leave a tie (409
827
+ * MENU_POSITION_CLASH), so the tie-break only steadies older data. Asked by
828
+ * the Player per render and by the compiler per screen (§8).
829
+ *
830
+ * @param {Array<object>|null|undefined} menus — the page's `local_nav_menus`.
831
+ * @param {"mobile"|"tablet"|"desktop"} view
832
+ * @returns {Object<string, {menu: object, model: string}>} by position.
833
+ */
834
+ export function resolvePageBars(menus, view) {
835
+ const bars = {};
836
+ const specific = (menu) => menu.appears_on === "SELECTED";
837
+ for (const menu of Array.isArray(menus) ? menus : []) {
838
+ if (!isPlainObject(menu)) continue;
839
+ const model = resolveLocalNavModel(menu, view);
840
+ const position = localNavPosition(model);
841
+ if (!position) continue;
842
+ const held = bars[position];
843
+ if (held && (specific(held.menu) || !specific(menu))) continue;
844
+ bars[position] = { menu, model };
845
+ }
846
+ return bars;
847
+ }
848
+
849
+ /**
850
+ * sc-12427 — whether a menu is a Top navigation bar at any view. The Studio
851
+ * styles that bar only for an app that draws one.
852
+ *
853
+ * @param {object|null|undefined} menu
854
+ * @returns {boolean}
855
+ */
856
+ export function drawsTopNavigation(menu) {
857
+ return ["mobile", "tablet", "desktop"].some(
858
+ (view) => resolveLocalNavModel(menu, view) === "HEADER",
859
+ );
860
+ }
861
+
862
+ /**
863
+ * sc-12427 — the pages a Top navigation bar links to, at any view: they are
864
+ * top-level destinations, as the main menu's are, so neither host offers a
865
+ * back control on them.
866
+ *
867
+ * @param {Array<object>|null|undefined} menus — every menu a page may draw.
868
+ * @returns {Set<string>} page ids.
869
+ */
870
+ export function topNavigationPageIds(menus) {
871
+ const ids = new Set();
872
+ for (const menu of Array.isArray(menus) ? menus : []) {
873
+ if (!isPlainObject(menu) || !drawsTopNavigation(menu)) continue;
874
+ for (const item of Array.isArray(menu.items) ? menu.items : []) {
875
+ if (isPlainObject(item) && typeof item.page_id === "string") ids.add(item.page_id);
876
+ }
877
+ }
878
+ return ids;
879
+ }
880
+
851
881
  // REQ-THEME-LOOK: the divider's width in px, bounded HERE so both hosts inherit
852
882
  // one range. The export clamped it on its way into the StyleSheet
853
883
  // (`coerceBorderWidth(...) ?? 1`) where the Player passed any finite number
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@colixsystems/widget-sdk",
3
- "version": "0.332.0",
3
+ "version": "0.334.0",
4
4
  "description": "Common widget interface for AppStudio. Implements WidgetManifest, WidgetContext, property schema, and helper hooks.",
5
5
  "repository": {
6
6
  "type": "git",
package/src/contract.js CHANGED
@@ -2479,7 +2479,7 @@ const HOOKS = [
2479
2479
  signature: "useAccessPlans()",
2480
2480
  returnShape: {
2481
2481
  plans:
2482
- "Array<{ id, name, price_cents, currency, billing: \"one_time\" | \"monthly\", owned, renews, access_ends_at, purchase_available }> // owned: the user holds it; renews: a held monthly plan will charge again; access_ends_at: end of held access without a renewal (null for one-time); purchase_available: this plan can be bought here — false in an iOS/Android build for a plan the workspace does not sell in-app",
2482
+ "Array<{ id, name, price_cents, currency, billing: \"one_time\" | \"monthly\", owned, renews, access_ends_at, purchase_available }> // owned: the user holds it; renews: a held monthly plan will charge again; access_ends_at: end of held access without a renewal (null for one-time); purchase_available: this plan can be bought here — false in an iOS/Android build for a plan the workspace does not sell in-app, and for a plan the user holds that is no longer on sale (listed so they can still cancel it)",
2483
2483
  loading: "boolean",
2484
2484
  error: "PaymentError | null",
2485
2485
  purchaseAvailable:
@@ -2487,7 +2487,7 @@ const HOOKS = [
2487
2487
  purchase:
2488
2488
  "(planId, { return_path? }?) => Promise<{ plan_id, status: \"ACTIVE\" | \"PENDING\" }> // pass ONLY the plan id; the server sets price and group; the host opens hosted Checkout on the web and the store's in-app purchase sheet in an iOS/Android build; access arrives with the payment confirmation, so refresh() a moment later",
2489
2489
  cancel:
2490
- "(planId) => Promise<{ plan_id, renews: false, access_ends_at }> // stop a renewing monthly plan; access runs to access_ends_at — offer it beside every plan with renews: true",
2490
+ "(planId) => Promise<{ plan_id, renews: false, access_ends_at }> // stop a renewing monthly plan; access runs to access_ends_at — offer it beside every plan with renews: true; a widget that calls purchase without it is refused (access-plan-cancel-missing)",
2491
2491
  refresh: "() => Promise<void> // re-read after returning from Checkout",
2492
2492
  },
2493
2493
  requiredContextSlice: [
@@ -6465,7 +6465,7 @@ const CONTRACT = deepFreeze({
6465
6465
  // it. The app's global mobile quick bar is retired: it was a SELECTION
6466
6466
  // over the global menu, and the rail and drawer list every menu page
6467
6467
  // regardless, so nothing was reachable only through it. The bottom edge
6468
- // belongs to a PAGE's own bottom navigation bar (`local_nav = BOTTOM_BAR`,
6468
+ // belongs to a section menu's bottom navigation bar (`layout = BOTTOM_BAR`,
6469
6469
  // REQ-NAV-LOCAL), chosen per page. `resolveFooterTokens` is unchanged —
6470
6470
  // it is the bottom edge's token set and the surviving bar reads it.
6471
6471
  // 1.113.0: additive (sc-7951) — `resolveFooterTokens` gains the bottom
@@ -7068,7 +7068,10 @@ const CONTRACT = deepFreeze({
7068
7068
  // value, and an update may still change it.
7069
7069
  // 1.271.0: additive (sc-17551) — `useDatastoreAggregate` groups by a
7070
7070
  // relation column; new warning lint `aggregate-error-not-read`.
7071
- version: "1.271.0",
7071
+ // 1.272.0: stricter lint (sc-17552) — a widget that calls
7072
+ // useAccessPlans().purchase without cancel is the publish-blocking
7073
+ // `access-plan-cancel-missing` finding.
7074
+ version: "1.272.0",
7072
7075
  sharedTranslationKeys: SHARED_TRANSLATION_KEYS,
7073
7076
  hooks: HOOKS,
7074
7077
  primitives: PRIMITIVES,