@peers-app/peers-ui 0.33.11 → 0.33.13

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.
@@ -0,0 +1,173 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PackageWaitingScreen = PackageWaitingScreen;
4
+ exports.PackageWaitingNotice = PackageWaitingNotice;
5
+ exports.providersToAdd = providersToAdd;
6
+ const jsx_runtime_1 = require("react/jsx-runtime");
7
+ const peers_sdk_1 = require("@peers-app/peers-sdk");
8
+ const react_1 = require("react");
9
+ const current_group_name_1 = require("../../apps-home/current-group-name");
10
+ const get_apps_sheet_1 = require("../../apps-home/get-apps-sheet");
11
+ const use_observable_1 = require("../../hooks/use-observable");
12
+ const use_promise_1 = require("../../hooks/use-promise");
13
+ const tabs_state_1 = require("../../tabs-layout/tabs-state");
14
+ const app_catalog_1 = require("../../ui-router/app-catalog");
15
+ const routes_loader_1 = require("../../ui-router/routes-loader");
16
+ const package_helpers_1 = require("./package-helpers");
17
+ const packages_ipc_1 = require("./packages-ipc");
18
+ /**
19
+ * The system screen a package shows in place of its own while it waits for a contract it
20
+ * requires: what it needs, why it can't start, and the packages that provide it, to add from
21
+ * here. Once one runs here the package starts by itself, and its own screen replaces this one.
22
+ */
23
+ function PackageWaitingScreen({ packageId, missing }) {
24
+ const groupName = (0, current_group_name_1.useCurrentGroupName)();
25
+ // Undefined until the role is read, so neither the admin note nor Add shows early.
26
+ const admin = (0, use_promise_1.usePromise)(package_helpers_1.isGroupAdmin, undefined, []);
27
+ const canAdd = admin === true;
28
+ const { report, error, refresh } = usePackageRequirements(packageId);
29
+ const app = useAppLook(packageId);
30
+ const needs = report?.missing ?? [];
31
+ // The report names contracts other contexts know; the runtime state may not have yet.
32
+ const names = (0, peers_sdk_1.missingContractsLabel)(report?.missing.length ? report.missing : missing);
33
+ const here = groupName || "this group";
34
+ return ((0, jsx_runtime_1.jsxs)("div", { className: "peers-waiting", "data-testid": "package-waiting", children: [(0, jsx_runtime_1.jsxs)("div", { className: "peers-waiting-head", children: [(0, jsx_runtime_1.jsxs)("span", { className: `peers-app-icon peers-app-color-${app.color}`, "aria-hidden": "true", children: [(0, jsx_runtime_1.jsx)("i", { className: app.iconClassName }), (0, jsx_runtime_1.jsx)("span", { className: "peers-waiting-badge", children: (0, jsx_runtime_1.jsx)("i", { className: "bi-hourglass-split" }) })] }), (0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsxs)("h2", { children: [app.name, " is waiting to start"] }), (0, jsx_runtime_1.jsxs)("p", { className: "peers-waiting-lead", children: ["It needs ", names, ", and no package in ", here, " provides ", missing.length > 1 ? "them" : "it", " ", "yet."] })] })] }), (0, jsx_runtime_1.jsxs)("p", { className: "peers-waiting-explain", children: [app.name, " is installed, and its data keeps syncing. It starts by itself as soon as a package that provides ", missing.length > 1 ? "them" : "it", " is running here."] }), admin === false && ((0, jsx_runtime_1.jsxs)("p", { className: "peers-waiting-explain", "data-testid": "package-waiting-admin-only", children: ["Only an admin of ", here, " can add packages here."] })), error ? ((0, jsx_runtime_1.jsx)("div", { className: "alert alert-danger py-2 px-3 small", role: "alert", children: error })) : !report ? ((0, jsx_runtime_1.jsx)("div", { className: "text-center py-3", children: (0, jsx_runtime_1.jsx)("output", { className: "spinner-border spinner-border-sm text-secondary", children: (0, jsx_runtime_1.jsx)("span", { className: "visually-hidden", children: "Loading" }) }) })) : (needs.map((need) => ((0, jsx_runtime_1.jsx)(RequiredContract, { need: need, appName: app.name, groupName: here, canAdd: canAdd, onAdded: refresh }, `${need.contractId}@${need.version}`)))), (0, jsx_runtime_1.jsx)("div", { className: "peers-waiting-foot", children: (0, jsx_runtime_1.jsx)("button", { type: "button", className: "btn btn-link p-0", onClick: () => (0, tabs_state_1.navigateTo)(`packages/${packageId}`), "data-testid": "package-waiting-details", children: "Details and versions" }) })] }));
35
+ }
36
+ /**
37
+ * The line a waiting package shows where another screen embeds one of its parts: what it waits
38
+ * for, and a way to open it and see why.
39
+ */
40
+ function PackageWaitingNotice({ packageId, missing }) {
41
+ const app = useAppLook(packageId);
42
+ return ((0, jsx_runtime_1.jsxs)("div", { className: "peers-waiting-notice", role: "status", "data-testid": "package-waiting-notice", children: [(0, jsx_runtime_1.jsx)("i", { className: "bi-hourglass-split", "aria-hidden": "true" }), (0, jsx_runtime_1.jsxs)("span", { children: [app.name, " is waiting for ", (0, peers_sdk_1.missingContractsLabel)(missing), "."] }), (0, jsx_runtime_1.jsx)("button", { type: "button", className: "btn btn-link btn-sm p-0", onClick: () => (app.path ? (0, tabs_state_1.goToTabPath)(app.path) : (0, tabs_state_1.navigateTo)(`packages/${packageId}`)), children: "See why" })] }));
43
+ }
44
+ /** One contract the package needs, with what could provide it. */
45
+ function RequiredContract({ need, appName, groupName, canAdd, onAdded, }) {
46
+ const nothing = need.installed.length === 0 && need.toAdd.length === 0;
47
+ return ((0, jsx_runtime_1.jsxs)("section", { className: "peers-waiting-need", "aria-label": `Needs ${(0, peers_sdk_1.missingContractLabel)(need)}`, children: [(0, jsx_runtime_1.jsxs)("h3", { children: [(0, jsx_runtime_1.jsx)("i", { className: "bi-plug", "aria-hidden": "true" }), (0, peers_sdk_1.missingContractLabel)(need)] }), need.description && (0, jsx_runtime_1.jsx)("p", { className: "peers-waiting-desc", children: need.description }), need.providerPackageIds?.length ? ((0, jsx_runtime_1.jsxs)("p", { className: "peers-waiting-desc", children: [appName, " names which packages may provide it."] })) : null, need.installed.map((provider) => ((0, jsx_runtime_1.jsx)(InstalledProvider, { provider: provider, appName: appName, groupName: groupName, canAdd: canAdd, onAdded: onAdded }, provider.packageId))), need.toAdd.map((entry) => ((0, jsx_runtime_1.jsx)(ProviderToAdd, { entry: entry, groupName: groupName, canAdd: canAdd, onAdded: onAdded }, entry.packageId))), nothing && ((0, jsx_runtime_1.jsx)("p", { className: "peers-waiting-none", "data-testid": "package-waiting-none", children: "None of your groups has a package that provides it." }))] }));
48
+ }
49
+ /** A package installed here that provides the contract but doesn't serve it, and why. */
50
+ function InstalledProvider({ provider, appName, groupName, canAdd, onAdded, }) {
51
+ const look = useAppLook(provider.packageId, provider.name);
52
+ const [state, add] = useAddAction(onAdded);
53
+ const turnOn = provider.status === "disabled" && canAdd;
54
+ return ((0, jsx_runtime_1.jsxs)("div", { className: "peers-waiting-provider", children: [(0, jsx_runtime_1.jsxs)("div", { className: "peers-get-apps-row", "data-testid": `package-waiting-installed-${provider.packageId}`, children: [(0, jsx_runtime_1.jsx)("span", { className: `peers-app-icon peers-app-color-${look.color}`, "aria-hidden": "true", children: (0, jsx_runtime_1.jsx)("i", { className: look.iconClassName }) }), (0, jsx_runtime_1.jsxs)("div", { className: "peers-get-apps-text", children: [(0, jsx_runtime_1.jsx)("div", { className: "peers-get-apps-name", children: provider.name }), (0, jsx_runtime_1.jsx)("div", { className: "peers-get-apps-desc", children: provider.cycle
55
+ ? `It needs ${appName} too, so neither can start. One of them has to stop requiring the other: tell their authors.`
56
+ : `Installed in ${groupName}, but ${installedPhrase(provider.status)}.` }), provider.reason && provider.status !== "waiting" && !provider.cycle && ((0, jsx_runtime_1.jsx)("div", { className: "peers-get-apps-desc", children: provider.reason })), state.error && ((0, jsx_runtime_1.jsx)("div", { className: "small text-danger mt-1", role: "alert", children: state.error }))] }), (0, jsx_runtime_1.jsx)("div", { className: "peers-get-apps-action", "aria-live": "polite", children: turnOn ? ((0, jsx_runtime_1.jsx)("button", { type: "button", className: "btn peers-get-apps-add", disabled: state.adding, onClick: () => add([{ packageId: provider.packageId }]), children: state.adding ? "Turning on…" : "Turn on" })) : provider.status !== "waiting" ? ((0, jsx_runtime_1.jsx)("button", { type: "button", className: "btn btn-link btn-sm p-0", onClick: () => (0, tabs_state_1.navigateTo)(`packages/${provider.packageId}`), children: "Details" })) : null })] }), provider.missing?.length ? ((0, jsx_runtime_1.jsx)("div", { className: "peers-waiting-chain", children: provider.missing.map((need) => ((0, jsx_runtime_1.jsx)(RequiredContract, { need: need, appName: provider.name, groupName: groupName, canAdd: canAdd, onAdded: onAdded }, `${need.contractId}@${need.version}`))) })) : null] }));
57
+ }
58
+ /** A package in another of the user's groups that provides the contract, to add here. */
59
+ function ProviderToAdd({ entry, groupName, canAdd, onAdded, }) {
60
+ const display = (0, get_apps_sheet_1.packageToAddDisplay)(entry);
61
+ const [state, add] = useAddAction(onAdded);
62
+ const plan = providersToAdd(entry);
63
+ const also = plan.add.slice(0, -1).map((each) => (0, get_apps_sheet_1.packageToAddDisplay)(each).title);
64
+ return ((0, jsx_runtime_1.jsxs)("div", { className: "peers-get-apps-row", "data-testid": `package-waiting-add-row-${entry.packageId}`, children: [(0, jsx_runtime_1.jsx)("span", { className: `peers-app-icon peers-app-color-${display.color}`, "aria-hidden": "true", children: (0, jsx_runtime_1.jsx)("i", { className: display.iconClassName }) }), (0, jsx_runtime_1.jsxs)("div", { className: "peers-get-apps-text", children: [(0, jsx_runtime_1.jsx)("div", { className: "peers-get-apps-name", children: display.title }), (0, jsx_runtime_1.jsxs)("div", { className: "peers-get-apps-desc", children: ["In ", entry.sourceName, entry.version ? ` · v${entry.version}` : "", entry.description ? ` · ${entry.description}` : ""] }), also.length > 0 && ((0, jsx_runtime_1.jsxs)("div", { className: "peers-get-apps-desc", children: ["Adding it adds ", (0, peers_sdk_1.joinNames)(also), " too, which it needs."] })), plan.unmet.length > 0 && ((0, jsx_runtime_1.jsxs)("div", { className: "peers-get-apps-desc", children: ["It needs ", (0, peers_sdk_1.joinNames)(plan.unmet), " as well, which none of your groups has."] })), state.error && ((0, jsx_runtime_1.jsx)("div", { className: "small text-danger mt-1", role: "alert", children: state.error }))] }), (0, jsx_runtime_1.jsx)("div", { className: "peers-get-apps-action", "aria-live": "polite", children: state.added ? ((0, jsx_runtime_1.jsxs)("span", { className: "peers-get-apps-done", children: [(0, jsx_runtime_1.jsx)("i", { className: "bi-check-lg", "aria-hidden": "true" }), "Added"] })) : entry.unsupportedHere ? ((0, jsx_runtime_1.jsx)("span", { className: "peers-get-apps-note", children: "Not on this device" })) : canAdd ? ((0, jsx_runtime_1.jsx)("button", { type: "button", className: "btn peers-get-apps-add", disabled: state.adding, onClick: () => add(plan.add.map((each) => ({
65
+ packageId: each.packageId,
66
+ fromDataContextId: each.sourceDataContextId,
67
+ }))), "aria-label": `Add ${display.title} to ${groupName}`, "data-testid": `package-waiting-add-${entry.packageId}`, children: state.adding ? "Adding…" : "Add" })) : null })] }));
68
+ }
69
+ /**
70
+ * The packages to add, in order, so a provider starts as soon as it's added: what it needs first
71
+ * (the first provider listed for each of its needs, recursively), the provider last. `unmet` names
72
+ * the needs no group has a provider for, so adding it would leave it waiting.
73
+ *
74
+ * @param entry - The provider to add.
75
+ */
76
+ function providersToAdd(entry) {
77
+ const add = [];
78
+ const unmet = [];
79
+ const visit = (provider) => {
80
+ if (add.some((each) => each.packageId === provider.packageId))
81
+ return;
82
+ for (const need of provider.missing ?? []) {
83
+ const next = need.toAdd.find((candidate) => !candidate.unsupportedHere && !candidate.cycle);
84
+ if (next)
85
+ visit(next);
86
+ else if (!need.installed.length)
87
+ unmet.push((0, peers_sdk_1.missingContractLabel)(need));
88
+ }
89
+ add.push(provider);
90
+ };
91
+ visit(entry);
92
+ return { add, unmet };
93
+ }
94
+ /** Add or turn on packages in order, then refresh; the button shows progress and failure. */
95
+ function useAddAction(onAdded) {
96
+ const [state, setState] = (0, react_1.useState)({
97
+ adding: false,
98
+ added: false,
99
+ });
100
+ const add = (packages) => {
101
+ setState({ adding: true, added: false });
102
+ void (async () => {
103
+ try {
104
+ const tools = await (0, packages_ipc_1.packagesTools)();
105
+ for (const each of packages)
106
+ await tools.installPackage(each);
107
+ setState({ adding: false, added: true });
108
+ }
109
+ catch (error) {
110
+ setState({
111
+ adding: false,
112
+ added: false,
113
+ error: error instanceof Error ? error.message : String(error),
114
+ });
115
+ }
116
+ onAdded();
117
+ })();
118
+ };
119
+ return [state, add];
120
+ }
121
+ /**
122
+ * The package's requirements report, read again whenever a package here changes state (a
123
+ * provider added, a link of the chain starting) and on `refresh`.
124
+ */
125
+ function usePackageRequirements(packageId) {
126
+ const states = (0, packages_ipc_1.usePackageRuntimeStates)();
127
+ const [revision, setRevision] = (0, react_1.useState)(0);
128
+ const [result, setResult] = (0, react_1.useState)({});
129
+ (0, react_1.useEffect)(() => {
130
+ let current = true;
131
+ (0, packages_ipc_1.packagesTools)()
132
+ .then((tools) => tools.getPackageRequirements({ packageId }))
133
+ .then(({ requirements }) => current && setResult({ report: requirements }), (error) => current &&
134
+ setResult((previous) => ({
135
+ ...previous,
136
+ error: error instanceof Error ? error.message : String(error),
137
+ })));
138
+ return () => {
139
+ current = false;
140
+ };
141
+ }, [packageId, states, revision]);
142
+ return { ...result, refresh: () => setRevision((value) => value + 1) };
143
+ }
144
+ /** How a package's app looks on the Apps screen: name, icon, color, and the path that opens it. */
145
+ function useAppLook(packageId, fallbackName) {
146
+ const [packages] = (0, use_observable_1.useObservable)(routes_loader_1.allPackages);
147
+ const pkg = packages.find((each) => each.packageId === packageId);
148
+ const nav = pkg?.appNavs?.[0];
149
+ const name = (pkg?.appNavs?.length === 1 && (nav?.displayName || nav?.name)) || pkg?.name;
150
+ return {
151
+ name: name || fallbackName || "This package",
152
+ iconClassName: nav?.iconClassName || "bi-box-seam",
153
+ color: (0, peers_sdk_1.resolveAppColor)(packageId, nav ?? { name: pkg?.name ?? packageId }),
154
+ ...(nav ? { path: (0, app_catalog_1.appPath)(packageId, nav) } : {}),
155
+ };
156
+ }
157
+ /** Why an installed provider doesn't serve the contract, after "Installed in Home, but". */
158
+ function installedPhrase(status) {
159
+ switch (status) {
160
+ case "waiting":
161
+ return "it's waiting too";
162
+ case "disabled":
163
+ return "it's turned off";
164
+ case "failed":
165
+ return "it couldn't start on this device";
166
+ case "unsupported":
167
+ return "this device can't run it";
168
+ case "unavailable":
169
+ return "it's still downloading to this device";
170
+ default:
171
+ return "it isn't running on this device";
172
+ }
173
+ }
@@ -1,4 +1,4 @@
1
- import { type IHostContractConsumer, type IPackageObservedRuntime, type IPackagesConsumer, type PackageRuntimeStates } from "@peers-app/peers-sdk";
1
+ import { type IHostContractConsumer, type IPackageMissingContract, type IPackageObservedRuntime, type IPackagesConsumer, type IPackageWaiting, type PackageRuntimeStates } from "@peers-app/peers-sdk";
2
2
  /**
3
3
  * Shared System Packages handle for one data context; stable for `useHostContract`.
4
4
  *
@@ -25,6 +25,67 @@ export declare function packagesTools(dataContextId?: string): Promise<IPackages
25
25
  * @returns States keyed by package id, or `undefined` until the host answers.
26
26
  */
27
27
  export declare function usePackageRuntimeStates(dataContextId?: string): PackageRuntimeStates | undefined;
28
+ /** A context's live package runtime states, and why they couldn't be read. */
29
+ export interface IPackageRuntimeStatesResult {
30
+ /** States keyed by package id; `undefined` until the host answers. */
31
+ states: PackageRuntimeStates | undefined;
32
+ /** Set when System Packages can't be reached, so a caller needn't wait for the states. */
33
+ error: Error | undefined;
34
+ }
35
+ /**
36
+ * Live runtime state of a context's packages on this device, with the failure to reach System
37
+ * Packages, for a caller that must not wait forever for an answer.
38
+ *
39
+ * @param dataContextId - Context to watch; defaults to the active group.
40
+ * @param enabled - Whether to watch at all; `false` reads nothing (a built-in screen).
41
+ */
42
+ export declare function usePackageRuntimeStatesResult(dataContextId?: string, enabled?: boolean): IPackageRuntimeStatesResult;
43
+ /**
44
+ * What a package waits for when it can't start on this device because a contract it requires has
45
+ * no provider here, or `undefined` when it isn't held back. A package whose older version keeps
46
+ * serving while the selected one waits isn't counted: its screens still work.
47
+ *
48
+ * @param runtime - The package's observed runtime state on this device.
49
+ */
50
+ export declare function packageWaiting(runtime: Pick<IPackageObservedRuntime, "status" | "running" | "waiting"> | undefined): IPackageWaiting | undefined;
51
+ /**
52
+ * The version that still serves while a newer one waits for a contract it requires ("Update
53
+ * waiting"), or `undefined` when the package runs the version selected for it. Its UI is the one
54
+ * to show, so screens match the worker that answers them.
55
+ *
56
+ * @param runtime - The package's observed runtime state on this device.
57
+ */
58
+ export declare function servingVersionWhileUpdateWaits(runtime: Pick<IPackageObservedRuntime, "status" | "running"> | undefined): string | undefined;
59
+ /** Whether a package's UI code may run, and which version's, from {@link usePackageScreenGate}. */
60
+ export interface IPackageScreenGate {
61
+ /**
62
+ * The host has said whether the package waits, or can't say. Until then a screen shows UI it
63
+ * already loaded, but runs no bundle it hasn't run yet.
64
+ */
65
+ ready: boolean;
66
+ /** Set while it waits with nothing running: show why instead of its screens. */
67
+ missing?: IPackageMissingContract[];
68
+ /**
69
+ * Set while a newer version waits and this older one still serves: load this version's UI
70
+ * rather than the selected one's ({@link servingVersionWhileUpdateWaits}).
71
+ */
72
+ servingPackageVersionId?: string;
73
+ }
74
+ /**
75
+ * Whether a package's UI code may run on this device, and which version's: none of it runs while
76
+ * the package waits for a contract it requires, and while an update waits, the version that still
77
+ * serves is the one whose UI runs. Nothing waits on this hook: a screen reads its bundle at once
78
+ * and runs it once `ready` says the package isn't waiting, and UI it already loaded renders
79
+ * straight away. `ready` is set at once when System Packages can't be reached (a host that
80
+ * doesn't serve it), so no screen hangs on it. A built-in screen passes no package and reads
81
+ * nothing.
82
+ *
83
+ * A screen renders again only when its own package's gate changes, not on every change to the
84
+ * context's package states.
85
+ *
86
+ * @param packageId - The package that owns the screen, if a package does.
87
+ */
88
+ export declare function usePackageScreenGate(packageId: string | undefined): IPackageScreenGate;
28
89
  /** Display label and Bootstrap badge color for an observed package status. */
29
90
  export interface IPackageStatusBadge {
30
91
  /** Short status label. */
@@ -4,6 +4,10 @@ exports.packagesContractFor = packagesContractFor;
4
4
  exports.getPackagesConsumer = getPackagesConsumer;
5
5
  exports.packagesTools = packagesTools;
6
6
  exports.usePackageRuntimeStates = usePackageRuntimeStates;
7
+ exports.usePackageRuntimeStatesResult = usePackageRuntimeStatesResult;
8
+ exports.packageWaiting = packageWaiting;
9
+ exports.servingVersionWhileUpdateWaits = servingVersionWhileUpdateWaits;
10
+ exports.usePackageScreenGate = usePackageScreenGate;
7
11
  exports.packageStatusBadge = packageStatusBadge;
8
12
  const peers_sdk_1 = require("@peers-app/peers-sdk");
9
13
  const react_1 = require("react");
@@ -55,8 +59,33 @@ const pendingHandle = {
55
59
  * @returns States keyed by package id, or `undefined` until the host answers.
56
60
  */
57
61
  function usePackageRuntimeStates(dataContextId) {
62
+ return usePackageRuntimeStatesResult(dataContextId).states;
63
+ }
64
+ /**
65
+ * Live runtime state of a context's packages on this device, with the failure to reach System
66
+ * Packages, for a caller that must not wait forever for an answer.
67
+ *
68
+ * @param dataContextId - Context to watch; defaults to the active group.
69
+ * @param enabled - Whether to watch at all; `false` reads nothing (a built-in screen).
70
+ */
71
+ function usePackageRuntimeStatesResult(dataContextId, enabled = true) {
72
+ const { consumer, error } = usePackagesConsumer(dataContextId, enabled);
73
+ const [states] = (0, hooks_1.useObservable)(consumer?.observables.runtimeStates);
74
+ return { states, error };
75
+ }
76
+ /**
77
+ * The System Packages consumer for a context, kept current across reconnects.
78
+ *
79
+ * @param dataContextId - Context to bind; defaults to the active group.
80
+ * @param enabled - Whether to connect at all.
81
+ */
82
+ function usePackagesConsumer(dataContextId, enabled) {
58
83
  const [handle, setHandle] = (0, react_1.useState)();
59
84
  (0, react_1.useEffect)(() => {
85
+ if (!enabled) {
86
+ setHandle(undefined);
87
+ return;
88
+ }
60
89
  let cancelled = false;
61
90
  (dataContextId
62
91
  ? Promise.resolve(dataContextId)
@@ -67,10 +96,77 @@ function usePackageRuntimeStates(dataContextId) {
67
96
  return () => {
68
97
  cancelled = true;
69
98
  };
70
- }, [dataContextId]);
71
- const { consumer } = (0, use_host_contract_1.useHostContract)(handle ?? pendingHandle);
72
- const [states] = (0, hooks_1.useObservable)(consumer?.observables.runtimeStates);
73
- return states;
99
+ }, [dataContextId, enabled]);
100
+ return (0, use_host_contract_1.useHostContract)(handle ?? pendingHandle);
101
+ }
102
+ /**
103
+ * What a package waits for when it can't start on this device because a contract it requires has
104
+ * no provider here, or `undefined` when it isn't held back. A package whose older version keeps
105
+ * serving while the selected one waits isn't counted: its screens still work.
106
+ *
107
+ * @param runtime - The package's observed runtime state on this device.
108
+ */
109
+ function packageWaiting(runtime) {
110
+ return runtime?.status === "waiting" && !runtime.running ? runtime.waiting : undefined;
111
+ }
112
+ /**
113
+ * The version that still serves while a newer one waits for a contract it requires ("Update
114
+ * waiting"), or `undefined` when the package runs the version selected for it. Its UI is the one
115
+ * to show, so screens match the worker that answers them.
116
+ *
117
+ * @param runtime - The package's observed runtime state on this device.
118
+ */
119
+ function servingVersionWhileUpdateWaits(runtime) {
120
+ return runtime?.status === "waiting" ? runtime.running?.packageVersionId : undefined;
121
+ }
122
+ /**
123
+ * Whether a package's UI code may run on this device, and which version's: none of it runs while
124
+ * the package waits for a contract it requires, and while an update waits, the version that still
125
+ * serves is the one whose UI runs. Nothing waits on this hook: a screen reads its bundle at once
126
+ * and runs it once `ready` says the package isn't waiting, and UI it already loaded renders
127
+ * straight away. `ready` is set at once when System Packages can't be reached (a host that
128
+ * doesn't serve it), so no screen hangs on it. A built-in screen passes no package and reads
129
+ * nothing.
130
+ *
131
+ * A screen renders again only when its own package's gate changes, not on every change to the
132
+ * context's package states.
133
+ *
134
+ * @param packageId - The package that owns the screen, if a package does.
135
+ */
136
+ function usePackageScreenGate(packageId) {
137
+ const { consumer, error } = usePackagesConsumer(undefined, !!packageId);
138
+ const runtimeStates = consumer?.observables.runtimeStates;
139
+ const subscribe = (0, react_1.useCallback)((onChange) => {
140
+ if (!runtimeStates)
141
+ return () => undefined;
142
+ const subscription = runtimeStates.subscribe(onChange);
143
+ return () => subscription.dispose();
144
+ }, [runtimeStates]);
145
+ // A string, so an unrelated state change gives the same snapshot and renders nothing.
146
+ const gate = (0, react_1.useSyncExternalStore)(subscribe, () => JSON.stringify(screenGate(packageId, runtimeStates?.(), !!error)));
147
+ return (0, react_1.useMemo)(() => JSON.parse(gate), [gate]);
148
+ }
149
+ /** One package's gate, from its context's states; only the fields a screen reads, in one order. */
150
+ function screenGate(packageId, states, unreachable) {
151
+ if (!packageId)
152
+ return { ready: true };
153
+ if (!states)
154
+ return { ready: unreachable };
155
+ const state = states[packageId];
156
+ const waiting = packageWaiting(state);
157
+ if (waiting) {
158
+ return {
159
+ ready: true,
160
+ missing: waiting.missing.map(({ contractId, version, name, providerPackageIds }) => ({
161
+ contractId,
162
+ version,
163
+ ...(name ? { name } : {}),
164
+ ...(providerPackageIds?.length ? { providerPackageIds } : {}),
165
+ })),
166
+ };
167
+ }
168
+ const serving = servingVersionWhileUpdateWaits(state);
169
+ return serving ? { ready: true, servingPackageVersionId: serving } : { ready: true };
74
170
  }
75
171
  /**
76
172
  * Describe a package's observed runtime status for a badge.
@@ -86,6 +182,11 @@ function packageStatusBadge(runtime) {
86
182
  switch (runtime.status) {
87
183
  case "running":
88
184
  return { label: "Running", color: "success" };
185
+ case "waiting":
186
+ // An older version may keep serving while the selected one waits.
187
+ return runtime.running
188
+ ? { label: "Update waiting", color: "warning" }
189
+ : { label: "Waiting", color: "warning" };
89
190
  case "failed":
90
191
  return runtime.running
91
192
  ? { label: "Update failed", color: "warning" }
@@ -27,8 +27,8 @@ const SetupUser = () => {
27
27
  // Generate new user credentials
28
28
  const keys = (0, peers_sdk_1.newKeys)();
29
29
  const _userId = (0, peers_sdk_1.newid)();
30
- // signIn triggers device init, which installs peers-core into the personal
31
- // context (S3-first, bundle fallback).
30
+ // signIn triggers device init, which installs peers-core (Tasks) into the personal
31
+ // context from S3. Without S3, a later start installs it.
32
32
  await (await (0, identity_ipc_1.identityTools)()).signIn({ userId: _userId, secretKey: keys.secretKey });
33
33
  // Success - reload the app (peers-services registration runs after Welcome modal)
34
34
  window.location.reload();
package/dist/theme.css CHANGED
@@ -704,6 +704,22 @@
704
704
  opacity: 0.6;
705
705
  }
706
706
 
707
+ /* A package waiting for a contract it requires: an hourglass, and a quieter icon. */
708
+ .peers-app-status-waiting {
709
+ background-color: #b86e00;
710
+ border: 2px solid var(--bs-body-bg);
711
+ color: #ffffff;
712
+ font-size: 11px;
713
+ height: 22px;
714
+ padding: 0;
715
+ width: 22px;
716
+ }
717
+
718
+ .peers-app-tile-waiting .peers-app-icon {
719
+ filter: grayscale(0.5);
720
+ opacity: 0.7;
721
+ }
722
+
707
723
  .peers-app-tile-dev .peers-app-icon {
708
724
  border: 1px dashed var(--bs-secondary-color);
709
725
  }
@@ -795,6 +811,10 @@
795
811
  color: inherit;
796
812
  }
797
813
 
814
+ .peers-app-menu-waiting {
815
+ color: var(--bs-warning-text-emphasis);
816
+ }
817
+
798
818
  /* The confirmation before removing an app from the group. */
799
819
  .peers-apps-dialog-backdrop {
800
820
  align-items: flex-start;
@@ -1153,3 +1173,148 @@
1153
1173
  display: -webkit-box;
1154
1174
  overflow: hidden;
1155
1175
  }
1176
+
1177
+ /*
1178
+ * The screen a package shows in place of its own while it waits for a contract it requires:
1179
+ * what it needs, and the packages that provide it. Its rows are the Get apps sheet's.
1180
+ */
1181
+ .peers-waiting {
1182
+ margin: 0 auto;
1183
+ max-width: 640px;
1184
+ padding: 28px 8px 48px;
1185
+ }
1186
+
1187
+ .peers-waiting-head {
1188
+ align-items: center;
1189
+ display: flex;
1190
+ gap: 18px;
1191
+ margin-bottom: 14px;
1192
+ }
1193
+
1194
+ .peers-waiting-head .peers-app-icon {
1195
+ --peers-tile-size: 64px;
1196
+ position: relative;
1197
+ }
1198
+
1199
+ .peers-waiting-badge {
1200
+ align-items: center;
1201
+ background-color: #b86e00;
1202
+ border: 2px solid var(--bs-body-bg);
1203
+ border-radius: 50%;
1204
+ bottom: -6px;
1205
+ color: #ffffff;
1206
+ display: flex;
1207
+ font-size: 12px;
1208
+ height: 26px;
1209
+ justify-content: center;
1210
+ position: absolute;
1211
+ right: -6px;
1212
+ width: 26px;
1213
+ }
1214
+
1215
+ .peers-waiting-head h2 {
1216
+ color: var(--bs-emphasis-color);
1217
+ font-size: 22px;
1218
+ font-weight: 650;
1219
+ margin: 0 0 4px;
1220
+ }
1221
+
1222
+ .peers-waiting-lead {
1223
+ font-size: 15px;
1224
+ margin: 0;
1225
+ }
1226
+
1227
+ .peers-waiting-explain {
1228
+ color: var(--bs-secondary-color);
1229
+ font-size: 13.5px;
1230
+ line-height: 1.5;
1231
+ margin: 0 0 16px;
1232
+ }
1233
+
1234
+ .peers-waiting-need {
1235
+ border: 1px solid var(--bs-border-color);
1236
+ border-radius: 14px;
1237
+ margin: 0 0 14px;
1238
+ padding: 14px 16px 4px;
1239
+ }
1240
+
1241
+ .peers-waiting-need h3 {
1242
+ align-items: center;
1243
+ color: var(--bs-emphasis-color);
1244
+ display: flex;
1245
+ font-size: 15px;
1246
+ font-weight: 650;
1247
+ gap: 8px;
1248
+ margin: 0 0 4px;
1249
+ }
1250
+
1251
+ .peers-waiting-desc {
1252
+ color: var(--bs-secondary-color);
1253
+ font-size: 13px;
1254
+ line-height: 1.45;
1255
+ margin: 0 0 4px;
1256
+ }
1257
+
1258
+ .peers-waiting-none {
1259
+ color: var(--bs-secondary-color);
1260
+ font-size: 13.5px;
1261
+ margin: 8px 0 12px;
1262
+ }
1263
+
1264
+ .peers-waiting-need .peers-get-apps-row .peers-app-icon {
1265
+ --peers-tile-size: 44px;
1266
+ }
1267
+
1268
+ .peers-waiting-need .peers-get-apps-row:last-child,
1269
+ .peers-waiting-provider:last-child > .peers-get-apps-row {
1270
+ border-bottom: none;
1271
+ }
1272
+
1273
+ /* The next link of a chain: what a waiting provider waits for, indented under it. */
1274
+ .peers-waiting-chain {
1275
+ border-left: 2px solid var(--bs-border-color);
1276
+ margin: 0 0 10px 22px;
1277
+ padding-left: 14px;
1278
+ }
1279
+
1280
+ .peers-waiting-chain .peers-waiting-need {
1281
+ border: none;
1282
+ margin: 0;
1283
+ padding: 6px 0 0;
1284
+ }
1285
+
1286
+ .peers-waiting-foot {
1287
+ font-size: 14px;
1288
+ margin-top: 18px;
1289
+ }
1290
+
1291
+ /* Where another screen embeds a part of a waiting package. */
1292
+ .peers-waiting-notice {
1293
+ align-items: center;
1294
+ border: 1px dashed var(--bs-border-color);
1295
+ border-radius: 10px;
1296
+ color: var(--bs-secondary-color);
1297
+ display: flex;
1298
+ flex-wrap: wrap;
1299
+ font-size: 13.5px;
1300
+ gap: 8px;
1301
+ padding: 10px 12px;
1302
+ }
1303
+
1304
+ @media (max-width: 575.98px) {
1305
+ .peers-waiting {
1306
+ padding: 14px 0 32px;
1307
+ }
1308
+ .peers-waiting-head {
1309
+ gap: 14px;
1310
+ }
1311
+ .peers-waiting-head .peers-app-icon {
1312
+ --peers-tile-size: 52px;
1313
+ }
1314
+ .peers-waiting-head h2 {
1315
+ font-size: 19px;
1316
+ }
1317
+ .peers-waiting-need {
1318
+ padding: 12px 12px 2px;
1319
+ }
1320
+ }
@@ -13,8 +13,11 @@ import { type DataContext, type IPackage, type IPackageVersion } from "@peers-ap
13
13
  * with fields added (`appNavs`), and the row's signature covers only the row as it was signed.
14
14
  *
15
15
  * @param pkg - The package.
16
+ * @param servingPackageVersionId - A version that runs here while the selected one waits for a
17
+ * contract it requires: it's the version to show, so screens match the worker that answers
18
+ * them. It runs here, so the dev rule above doesn't apply to it; the group's checks still do.
16
19
  */
17
- export declare function resolveRunnableVersion(pkg: IPackage): Promise<{
20
+ export declare function resolveRunnableVersion(pkg: IPackage, servingPackageVersionId?: string): Promise<{
18
21
  pv: IPackageVersion;
19
22
  dataContext: DataContext;
20
23
  } | undefined>;
@@ -17,8 +17,11 @@ const peers_sdk_1 = require("@peers-app/peers-sdk");
17
17
  * with fields added (`appNavs`), and the row's signature covers only the row as it was signed.
18
18
  *
19
19
  * @param pkg - The package.
20
+ * @param servingPackageVersionId - A version that runs here while the selected one waits for a
21
+ * contract it requires: it's the version to show, so screens match the worker that answers
22
+ * them. It runs here, so the dev rule above doesn't apply to it; the group's checks still do.
20
23
  */
21
- async function resolveRunnableVersion(pkg) {
24
+ async function resolveRunnableVersion(pkg, servingPackageVersionId) {
22
25
  try {
23
26
  const dataContext = await (0, peers_sdk_1.getDefaultDataContext)();
24
27
  const stored = await (0, peers_sdk_1.Packages)(dataContext).get(pkg.packageId);
@@ -34,13 +37,13 @@ async function resolveRunnableVersion(pkg) {
34
37
  catch {
35
38
  /* use group default */
36
39
  }
37
- const pvId = devicePvId ?? stored.activePackageVersionId;
40
+ const pvId = servingPackageVersionId ?? devicePvId ?? stored.activePackageVersionId;
38
41
  if (!pvId)
39
42
  return undefined;
40
43
  const pv = await (0, peers_sdk_1.PackageVersions)(dataContext).get(pvId);
41
44
  if (!pv)
42
45
  return undefined;
43
- if (pv.versionTag === "dev" && !devicePvId) {
46
+ if (pv.versionTag === "dev" && !devicePvId && !servingPackageVersionId) {
44
47
  // Same reason resolveDevicePackageVersion unloads it with.
45
48
  throw new Error(`Only a dev version (v${pv.version}) is selected; activate it on this device to run it`);
46
49
  }