@meldgrove/ui 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/README.md +148 -3
  2. package/dist/components/app-frame.d.ts +82 -0
  3. package/dist/components/app-frame.js +129 -0
  4. package/dist/components/input-group.d.ts +1 -1
  5. package/dist/components/page.d.ts +14 -0
  6. package/dist/components/page.js +10 -0
  7. package/dist/components/settings-cards.d.ts +19 -0
  8. package/dist/components/settings-cards.js +62 -0
  9. package/dist/components/sidebar.js +3 -0
  10. package/dist/components/sign-in-page.d.ts +23 -0
  11. package/dist/components/sign-in-page.js +31 -0
  12. package/dist/frame/about-messages.d.ts +5 -0
  13. package/dist/frame/about-messages.js +12 -0
  14. package/dist/frame/crumbs.d.ts +18 -0
  15. package/dist/frame/crumbs.js +33 -0
  16. package/dist/frame/focus.d.ts +7 -0
  17. package/dist/frame/focus.js +22 -0
  18. package/dist/frame/keyboard.d.ts +20 -0
  19. package/dist/frame/keyboard.js +46 -0
  20. package/dist/frame/meldgrove-link.d.ts +6 -0
  21. package/dist/frame/meldgrove-link.js +7 -0
  22. package/dist/frame/messages.d.ts +38 -0
  23. package/dist/frame/messages.js +78 -0
  24. package/dist/frame/nav.d.ts +11 -0
  25. package/dist/frame/nav.js +24 -0
  26. package/dist/frame/part-of-meldgrove.d.ts +5 -0
  27. package/dist/frame/part-of-meldgrove.js +11 -0
  28. package/dist/frame/search.d.ts +47 -0
  29. package/dist/frame/search.js +104 -0
  30. package/dist/frame/shortcuts.d.ts +47 -0
  31. package/dist/frame/shortcuts.js +141 -0
  32. package/dist/lib/theme.d.ts +12 -0
  33. package/dist/lib/theme.js +60 -1
  34. package/dist/standalone/meldgrove-updates.js +3 -3
  35. package/dist/styles.css +1 -0
  36. package/dist/updates/controller.d.ts +41 -2
  37. package/dist/updates/controller.js +72 -7
  38. package/dist/updates/state.d.ts +10 -0
  39. package/dist/updates/state.js +15 -0
  40. package/package.json +4 -2
package/dist/styles.css CHANGED
@@ -9,3 +9,4 @@
9
9
 
10
10
  @source "./components";
11
11
  @source "./updates";
12
+ @source "./frame";
@@ -14,6 +14,11 @@ export declare const INSTALL_TIMEOUT_MS = 15000;
14
14
  export declare const POST_TIMEOUT_MS = 45000;
15
15
  /** The page reloads this long after the new version answered. */
16
16
  export declare const RELOAD_DELAY_MS = 1500;
17
+ /** What a page tells the app's other pages in this browser (BroadcastChannel `meldgrove.updates.<project>`). */
18
+ export type UpdatesMessage = {
19
+ type: "updated";
20
+ version: string;
21
+ };
17
22
  type LastResult = NonNullable<UpdaterStatus["last"]>;
18
23
  /** A message for the person: the app shows it as a toast. */
19
24
  export type Notice = {
@@ -44,7 +49,7 @@ export type UpdatesControllerOptions = {
44
49
  * (signed in, but no longer an app admin).
45
50
  */
46
51
  onSignedOut?: (reason: SignedOutReason) => void;
47
- /** Reloads the page once the new version answers. */
52
+ /** Reloads the page once the new version answers (on this page or another of the app). */
48
53
  reload?: () => void;
49
54
  /** The clock (epoch ms); for tests. */
50
55
  now?: () => number;
@@ -91,6 +96,11 @@ export type SignedOutReason = "signed-out" | "forbidden";
91
96
  * the page when the new version answers, and stops every automatic request
92
97
  * once the session ended. Never checks by itself. The React components and
93
98
  * the standalone element render its snapshot.
99
+ *
100
+ * No page keeps old code after an update: the page that saw an installation
101
+ * finish tells the app's other pages in this browser, and they reload too; a
102
+ * page that missed it (asleep, in another browser, an automatic install)
103
+ * reloads once a status names another app version than the page's first.
94
104
  */
95
105
  export declare class UpdatesController {
96
106
  appName: string;
@@ -112,6 +122,14 @@ export declare class UpdatesController {
112
122
  /** The request that asks for the status now, to cancel when an installation starts. */
113
123
  private statusAbort;
114
124
  private response;
125
+ /**
126
+ * The app's version in the page's first answer: the code this page runs.
127
+ * Never from `initialResponse`: a page served from a cache would reload
128
+ * again and again.
129
+ */
130
+ private pageVersion;
131
+ /** Tells the app's other pages in this browser that an installation is done, and hears theirs. */
132
+ private channel;
115
133
  /** When the latest status request ended, and whether it failed (no status, or not reachable). */
116
134
  private lastPoll;
117
135
  private pending;
@@ -158,6 +176,22 @@ export declare class UpdatesController {
158
176
  dispose(): void;
159
177
  private onFocus;
160
178
  private onVisibility;
179
+ /** The channel of the app's pages in this browser; without BroadcastChannel, the version check still reloads them. */
180
+ private openChannel;
181
+ /**
182
+ * Another page of the app saw an installation finish: reload once. Also a
183
+ * page whose first status already named the version: it may have loaded
184
+ * the old code just before the switch.
185
+ */
186
+ private onMessage;
187
+ /** Tells the app's other pages in this browser that it runs `version` now. */
188
+ private broadcast;
189
+ /**
190
+ * The app runs `version` now: says so and reloads the page once, after
191
+ * 1.5 s (a timer `dispose` clears). Not after `dispose`: an answer still
192
+ * on its way does nothing.
193
+ */
194
+ private reloadInto;
161
195
  /** Shows a message the way the page shows the controller's own (toast). */
162
196
  notify(notice: Notice): void;
163
197
  /** The next status request: every 2 s while following (also in the background), else every 5 min. */
@@ -182,7 +216,12 @@ export declare class UpdatesController {
182
216
  private pollFailed;
183
217
  /** A new status, from a status request (sent at `sentAt`) or from the answer of a POST. */
184
218
  private receive;
185
- /** Confirms this tab's attempt, adopts a reported installation, and ends it once its result is known. */
219
+ /**
220
+ * Confirms this tab's attempt, adopts a reported installation, and ends it
221
+ * once its result is known; done reloads every page of the app in this
222
+ * browser. Without an installation to follow, reloads a page whose code is
223
+ * older than the app's.
224
+ */
186
225
  private follow;
187
226
  /** A POST to `check` or `channel`: a status into the snapshot, or an Error with the route's reason. */
188
227
  private post;
@@ -1,6 +1,6 @@
1
1
  import { errorMessage, translate } from "../lib/i18n-core.js";
2
2
  import { messages } from "./messages.js";
3
- import { answerInstall, buttonState, followStatus, hasPanel, ignoreInstall, installOutcome, installStorage, noteOutage, panelState, reconcileDeadline, SLOW_AFTER_MS, startAttempt, statusTimeout, unconfirmedEnd, } from "./state.js";
3
+ import { answerInstall, buttonState, followStatus, hasPanel, ignoreInstall, installOutcome, installStorage, noteOutage, panelState, reconcileDeadline, SLOW_AFTER_MS, staleVersion, startAttempt, statusTimeout, unconfirmedEnd, } from "./state.js";
4
4
  import { SignedOutError, } from "./transport.js";
5
5
  import { parseUpdatesResponse, } from "./types.js";
6
6
  /** Status requests: every 5 minutes, every 2 seconds while following an installation. */
@@ -59,6 +59,11 @@ const signedOutReason = (status) => (status === 401 ? "signed-out" : "forbidden"
59
59
  * the page when the new version answers, and stops every automatic request
60
60
  * once the session ended. Never checks by itself. The React components and
61
61
  * the standalone element render its snapshot.
62
+ *
63
+ * No page keeps old code after an update: the page that saw an installation
64
+ * finish tells the app's other pages in this browser, and they reload too; a
65
+ * page that missed it (asleep, in another browser, an automatic install)
66
+ * reloads once a status names another app version than the page's first.
62
67
  */
63
68
  export class UpdatesController {
64
69
  appName;
@@ -80,6 +85,14 @@ export class UpdatesController {
80
85
  /** The request that asks for the status now, to cancel when an installation starts. */
81
86
  statusAbort = null;
82
87
  response;
88
+ /**
89
+ * The app's version in the page's first answer: the code this page runs.
90
+ * Never from `initialResponse`: a page served from a cache would reload
91
+ * again and again.
92
+ */
93
+ pageVersion = null;
94
+ /** Tells the app's other pages in this browser that an installation is done, and hears theirs. */
95
+ channel = null;
83
96
  /** When the latest status request ended, and whether it failed (no status, or not reachable). */
84
97
  lastPoll = null;
85
98
  pending = null;
@@ -220,12 +233,15 @@ export class UpdatesController {
220
233
  window.addEventListener("focus", this.onFocus);
221
234
  window.addEventListener("online", this.onFocus);
222
235
  document.addEventListener("visibilitychange", this.onVisibility);
236
+ this.openChannel();
223
237
  this.tickTimer = setInterval(() => this.emit(), TICK_MS);
224
238
  void this.fetchStatus();
225
239
  }
226
240
  /** Stops every timer and listener (also a pending reload); `start` resumes. */
227
241
  dispose() {
228
242
  this.started = false;
243
+ this.channel?.close();
244
+ this.channel = null;
229
245
  window.removeEventListener("focus", this.onFocus);
230
246
  window.removeEventListener("online", this.onFocus);
231
247
  document.removeEventListener("visibilitychange", this.onVisibility);
@@ -246,6 +262,47 @@ export class UpdatesController {
246
262
  if (document.visibilityState === "visible")
247
263
  this.onFocus();
248
264
  };
265
+ /** The channel of the app's pages in this browser; without BroadcastChannel, the version check still reloads them. */
266
+ openChannel() {
267
+ if (this.channel || typeof BroadcastChannel === "undefined")
268
+ return;
269
+ try {
270
+ this.channel = new BroadcastChannel(`meldgrove.updates.${this.project}`);
271
+ this.channel.onmessage = this.onMessage;
272
+ }
273
+ catch {
274
+ this.channel = null;
275
+ }
276
+ }
277
+ /**
278
+ * Another page of the app saw an installation finish: reload once. Also a
279
+ * page whose first status already named the version: it may have loaded
280
+ * the old code just before the switch.
281
+ */
282
+ onMessage = (event) => {
283
+ const message = event.data;
284
+ if (message?.type !== "updated" || typeof message.version !== "string")
285
+ return;
286
+ this.reloadInto(message.version);
287
+ };
288
+ /** Tells the app's other pages in this browser that it runs `version` now. */
289
+ broadcast(version) {
290
+ const message = { type: "updated", version };
291
+ this.channel?.postMessage(message);
292
+ }
293
+ /**
294
+ * The app runs `version` now: says so and reloads the page once, after
295
+ * 1.5 s (a timer `dispose` clears). Not after `dispose`: an answer still
296
+ * on its way does nothing.
297
+ */
298
+ reloadInto(version) {
299
+ if (this.reloading || !this.started)
300
+ return;
301
+ this.reloading = true;
302
+ this.setPending(null);
303
+ this.notify({ kind: "success", title: this.t("updated", { version }) });
304
+ this.reloadTimer = setTimeout(() => this.reloadFn(), RELOAD_DELAY_MS);
305
+ }
249
306
  /** Shows a message the way the page shows the controller's own (toast). */
250
307
  notify(notice) {
251
308
  this.notifyFn(notice);
@@ -406,25 +463,33 @@ export class UpdatesController {
406
463
  /** A new status, from a status request (sent at `sentAt`) or from the answer of a POST. */
407
464
  receive(response, sentAt) {
408
465
  this.response = response;
466
+ this.pageVersion ??= response.app.version;
409
467
  this.lastPoll = { at: this.clock(), failed: !response.updater, error: response.error };
410
468
  this.follow(response, sentAt);
411
469
  this.emit();
412
470
  }
413
- /** Confirms this tab's attempt, adopts a reported installation, and ends it once its result is known. */
471
+ /**
472
+ * Confirms this tab's attempt, adopts a reported installation, and ends it
473
+ * once its result is known; done reloads every page of the app in this
474
+ * browser. Without an installation to follow, reloads a page whose code is
475
+ * older than the app's.
476
+ */
414
477
  follow(response, sentAt) {
415
478
  if (this.reloading)
416
479
  return;
417
480
  const now = this.clock();
418
481
  const pending = this.pending;
419
482
  const followed = followStatus(pending, response, now, this.ignored, sentAt);
420
- if (!followed)
483
+ if (!followed) {
484
+ const version = staleVersion(this.pageVersion, response);
485
+ if (version)
486
+ this.reloadInto(version);
421
487
  return;
488
+ }
422
489
  const result = installOutcome(followed, response, now);
423
490
  if (result === "done") {
424
- this.reloading = true;
425
- this.setPending(null);
426
- this.notify({ kind: "success", title: this.t("updated", { version: followed.version }) });
427
- this.reloadTimer = setTimeout(() => this.reloadFn(), RELOAD_DELAY_MS);
491
+ this.broadcast(followed.version);
492
+ this.reloadInto(followed.version);
428
493
  }
429
494
  else if (result === "failed") {
430
495
  this.recent = response.updater?.last ?? null;
@@ -160,6 +160,16 @@ export type InstallOutcome = "done" | "failed" | "waiting" | "slow";
160
160
  * earlier, at `reconcileDeadline`, on a timer of its own.)
161
161
  */
162
162
  export declare function installOutcome(pending: PendingInstall, response: UpdatesResponse | undefined, now: number): InstallOutcome;
163
+ /**
164
+ * The version the app runs now, when it is not the one this page was loaded
165
+ * from (`pageVersion`: the app's version in the page's first status) and the
166
+ * update service reports no installation: the page runs old code and
167
+ * reloads. Another tab, another administrator or an automatic install
168
+ * updated the app. Null otherwise: in development (no app version), and
169
+ * without update service (it restarts during an installation, which may
170
+ * still roll back).
171
+ */
172
+ export declare function staleVersion(pageVersion: string | null, response: UpdatesResponse): string | null;
163
173
  /** How long a status request may take. */
164
174
  export declare const STATUS_TIMEOUT_MS = 10000;
165
175
  /**
@@ -271,6 +271,21 @@ export function installOutcome(pending, response, now) {
271
271
  }
272
272
  return now - pending.startedAt > SLOW_AFTER_MS ? "slow" : "waiting";
273
273
  }
274
+ /**
275
+ * The version the app runs now, when it is not the one this page was loaded
276
+ * from (`pageVersion`: the app's version in the page's first status) and the
277
+ * update service reports no installation: the page runs old code and
278
+ * reloads. Another tab, another administrator or an automatic install
279
+ * updated the app. Null otherwise: in development (no app version), and
280
+ * without update service (it restarts during an installation, which may
281
+ * still roll back).
282
+ */
283
+ export function staleVersion(pageVersion, response) {
284
+ const version = response.app.version;
285
+ if (pageVersion === null || version === null || version === pageVersion)
286
+ return null;
287
+ return response.updater && !response.updater.installing ? version : null;
288
+ }
274
289
  /** How long a status request may take. */
275
290
  export const STATUS_TIMEOUT_MS = 10_000;
276
291
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meldgrove/ui",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "The shared interface of the Meldgrove apps: theme, components and the update button.",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Simon Schubert",
@@ -84,7 +84,9 @@
84
84
  "test": "vitest run",
85
85
  "test:visual": "playwright test",
86
86
  "test:visual:update": "node scripts/visual-update.mjs",
87
- "pack:check": "node scripts/pack-check.mjs"
87
+ "pack:check": "node scripts/pack-check.mjs",
88
+ "dev:templates": "vite --config demo/templates/vite.config.ts",
89
+ "build:templates": "node scripts/build-templates.mjs"
88
90
  },
89
91
  "peerDependencies": {
90
92
  "cmdk": "^1.1.0",