@peers-app/peers-ui 0.26.0 → 0.26.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 (36) hide show
  1. package/dist/app-updates/app-update-controls.d.ts +23 -0
  2. package/dist/app-updates/app-update-controls.js +40 -0
  3. package/dist/app-updates/app-updates-consumer.d.ts +41 -0
  4. package/dist/app-updates/app-updates-consumer.js +96 -0
  5. package/dist/app-updates/app-updates-consumer.test.d.ts +1 -0
  6. package/dist/app-updates/app-updates-consumer.test.js +56 -0
  7. package/dist/app-updates/app-updates-settings.d.ts +5 -0
  8. package/dist/app-updates/app-updates-settings.js +44 -0
  9. package/dist/app-updates/app-updates-settings.test.d.ts +1 -0
  10. package/dist/app-updates/app-updates-settings.test.js +93 -0
  11. package/dist/app-updates/remote-app-update-panel.d.ts +18 -0
  12. package/dist/app-updates/remote-app-update-panel.js +125 -0
  13. package/dist/app-updates/remote-app-update-panel.test.d.ts +1 -0
  14. package/dist/app-updates/remote-app-update-panel.test.js +165 -0
  15. package/dist/app-updates/use-app-update-status.d.ts +8 -0
  16. package/dist/app-updates/use-app-update-status.js +17 -0
  17. package/dist/screens/devices/devices-list.js +2 -1
  18. package/dist/screens/network-viewer/device-details-modal.js +2 -1
  19. package/dist/screens/settings/settings-page.js +5 -0
  20. package/dist/tabs-layout/network-status-indicator.d.ts +12 -0
  21. package/dist/tabs-layout/network-status-indicator.js +33 -5
  22. package/dist/tabs-layout/network-status-indicator.test.js +90 -0
  23. package/package.json +3 -3
  24. package/src/app-updates/app-update-controls.tsx +73 -0
  25. package/src/app-updates/app-updates-consumer.test.ts +83 -0
  26. package/src/app-updates/app-updates-consumer.ts +121 -0
  27. package/src/app-updates/app-updates-settings.test.tsx +109 -0
  28. package/src/app-updates/app-updates-settings.tsx +136 -0
  29. package/src/app-updates/remote-app-update-panel.test.tsx +205 -0
  30. package/src/app-updates/remote-app-update-panel.tsx +210 -0
  31. package/src/app-updates/use-app-update-status.ts +16 -0
  32. package/src/screens/devices/devices-list.tsx +4 -0
  33. package/src/screens/network-viewer/device-details-modal.tsx +6 -0
  34. package/src/screens/settings/settings-page.tsx +5 -0
  35. package/src/tabs-layout/network-status-indicator.test.tsx +119 -1
  36. package/src/tabs-layout/network-status-indicator.tsx +56 -5
@@ -0,0 +1,23 @@
1
+ import type { IAppUpdateStatus } from "@peers-app/peers-sdk";
2
+ /** Bootstrap Icons class used for "update available" everywhere in the shell. */
3
+ export declare const APP_UPDATE_ICON_CLASS = "bi-arrow-up-circle-fill";
4
+ /**
5
+ * Button that installs this device's available update and relaunches, or restarts it when
6
+ * there is nothing to install.
7
+ *
8
+ * @param props.status - This device's update status.
9
+ * @param props.className - Bootstrap button classes.
10
+ */
11
+ export declare function UpdateAndRestartButton({ status, className, }: {
12
+ status: IAppUpdateStatus;
13
+ className?: string;
14
+ }): import("react/jsx-runtime").JSX.Element;
15
+ /**
16
+ * Update section of the network status popover. Renders only while an update is available,
17
+ * downloading, ready, or installing.
18
+ *
19
+ * @param props.status - This device's update status.
20
+ */
21
+ export declare function AppUpdatePopoverSection({ status }: {
22
+ status: IAppUpdateStatus | undefined;
23
+ }): import("react/jsx-runtime").JSX.Element | null;
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.APP_UPDATE_ICON_CLASS = void 0;
4
+ exports.UpdateAndRestartButton = UpdateAndRestartButton;
5
+ exports.AppUpdatePopoverSection = AppUpdatePopoverSection;
6
+ const jsx_runtime_1 = require("react/jsx-runtime");
7
+ const react_1 = require("react");
8
+ const app_updates_consumer_1 = require("./app-updates-consumer");
9
+ /** Bootstrap Icons class used for "update available" everywhere in the shell. */
10
+ exports.APP_UPDATE_ICON_CLASS = "bi-arrow-up-circle-fill";
11
+ /**
12
+ * Button that installs this device's available update and relaunches, or restarts it when
13
+ * there is nothing to install.
14
+ *
15
+ * @param props.status - This device's update status.
16
+ * @param props.className - Bootstrap button classes.
17
+ */
18
+ function UpdateAndRestartButton({ status, className = "btn btn-sm btn-warning", }) {
19
+ const [error, setError] = (0, react_1.useState)();
20
+ const busy = (0, app_updates_consumer_1.isAppRestarting)(status);
21
+ const label = (0, app_updates_consumer_1.hasAppUpdate)(status) ? "Update & restart" : "Restart";
22
+ return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsxs)("button", { type: "button", className: className, disabled: busy || !status.canRestart, onClick: () => {
23
+ setError(undefined);
24
+ (0, app_updates_consumer_1.getAppUpdatesConsumer)()
25
+ .then((appUpdates) => appUpdates.tools.installUpdateAndRestart())
26
+ .catch((err) => setError(err instanceof Error ? err.message : String(err)));
27
+ }, children: [(0, jsx_runtime_1.jsx)("i", { className: `bi ${exports.APP_UPDATE_ICON_CLASS} me-1`, "aria-hidden": "true" }), busy ? (0, app_updates_consumer_1.describeAppUpdateStatus)(status) : label] }), error && (0, jsx_runtime_1.jsx)("div", { className: "small text-danger mt-1", children: error })] }));
28
+ }
29
+ /**
30
+ * Update section of the network status popover. Renders only while an update is available,
31
+ * downloading, ready, or installing.
32
+ *
33
+ * @param props.status - This device's update status.
34
+ */
35
+ function AppUpdatePopoverSection({ status }) {
36
+ if (!status?.supported || !((0, app_updates_consumer_1.hasAppUpdate)(status) || (0, app_updates_consumer_1.isAppRestarting)(status))) {
37
+ return null;
38
+ }
39
+ return ((0, jsx_runtime_1.jsxs)("div", { className: "alert alert-warning py-2 px-2 small mb-3", "data-testid": "app-update-section", children: [(0, jsx_runtime_1.jsxs)("div", { className: "mb-1", children: [(0, jsx_runtime_1.jsx)("i", { className: `bi ${exports.APP_UPDATE_ICON_CLASS} me-1`, "aria-hidden": "true" }), (0, app_updates_consumer_1.describeAppUpdateStatus)(status)] }), status.installOnQuitArmed && ((0, jsx_runtime_1.jsx)("div", { className: "text-muted mb-2", children: "Installs automatically the next time Peers restarts." })), (0, jsx_runtime_1.jsx)(UpdateAndRestartButton, { status: status, className: "btn btn-sm btn-warning w-100" })] }));
40
+ }
@@ -0,0 +1,41 @@
1
+ import { type IAppUpdateStatus, type IAppUpdatesConsumer } from "@peers-app/peers-sdk";
2
+ /** Shared handle for System App Updates on the local host; stable for `useHostContract`. */
3
+ export declare const appUpdatesContract: import("@peers-app/peers-sdk").IHostContractConsumer<IAppUpdatesConsumer>;
4
+ /** Return the System App Updates consumer bound to the current local host transport. */
5
+ export declare function getAppUpdatesConsumer(): Promise<IAppUpdatesConsumer>;
6
+ /**
7
+ * Why a call to another device's App Updates failed:
8
+ *
9
+ * - `unavailable` — the device cannot update or restart itself, or runs a build without App Updates.
10
+ * - `denied` — the device does not trust this user as `Self`, or does not allow remote restarts.
11
+ * - `unreachable` — the device is not connected or did not answer.
12
+ * - `failed` — anything else.
13
+ */
14
+ export type AppUpdatesErrorKind = "unavailable" | "denied" | "unreachable" | "failed";
15
+ /**
16
+ * Classify an App Updates error so the UI can hide, disable, or explain the action.
17
+ *
18
+ * @param error - Error thrown by a relayed App Updates call.
19
+ */
20
+ export declare function classifyAppUpdatesError(error: unknown): AppUpdatesErrorKind;
21
+ /**
22
+ * Whether a newer version is available, downloading, or ready to install. A failed download
23
+ * or check still counts while a newer version is known: Update & restart retries it.
24
+ *
25
+ * @param status - Update status, or `undefined` while loading.
26
+ */
27
+ export declare function hasAppUpdate(status: IAppUpdateStatus | undefined): boolean;
28
+ /**
29
+ * Whether the device is already installing an update or restarting.
30
+ *
31
+ * @param status - Update status, or `undefined` while loading.
32
+ */
33
+ export declare function isAppRestarting(status: IAppUpdateStatus | undefined): boolean;
34
+ /** Version being downloaded, or the cached version being prepared for installation. */
35
+ export declare function getAppUpdateVersion(status: IAppUpdateStatus): string | undefined;
36
+ /**
37
+ * One-line, human-readable summary of an update status.
38
+ *
39
+ * @param status - Update status to describe.
40
+ */
41
+ export declare function describeAppUpdateStatus(status: IAppUpdateStatus): string;
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.appUpdatesContract = void 0;
4
+ exports.getAppUpdatesConsumer = getAppUpdatesConsumer;
5
+ exports.classifyAppUpdatesError = classifyAppUpdatesError;
6
+ exports.hasAppUpdate = hasAppUpdate;
7
+ exports.isAppRestarting = isAppRestarting;
8
+ exports.getAppUpdateVersion = getAppUpdateVersion;
9
+ exports.describeAppUpdateStatus = describeAppUpdateStatus;
10
+ const peers_sdk_1 = require("@peers-app/peers-sdk");
11
+ /** Shared handle for System App Updates on the local host; stable for `useHostContract`. */
12
+ exports.appUpdatesContract = (0, peers_sdk_1.hostContractConsumer)((0, peers_sdk_1.getAppUpdatesContractDefinition)());
13
+ /** Return the System App Updates consumer bound to the current local host transport. */
14
+ function getAppUpdatesConsumer() {
15
+ return exports.appUpdatesContract.get();
16
+ }
17
+ /**
18
+ * Classify an App Updates error so the UI can hide, disable, or explain the action.
19
+ *
20
+ * @param error - Error thrown by a relayed App Updates call.
21
+ */
22
+ function classifyAppUpdatesError(error) {
23
+ const message = error instanceof Error ? error.message : String(error);
24
+ if (/No contract provider registered|has no live resolver|cannot install updates or restart/i.test(message)) {
25
+ return "unavailable";
26
+ }
27
+ if (/Permission denied/i.test(message)) {
28
+ return "denied";
29
+ }
30
+ if (/not connected|not reachable|timed out|did not answer/i.test(message)) {
31
+ return "unreachable";
32
+ }
33
+ return "failed";
34
+ }
35
+ function knowsNewerVersion(status) {
36
+ return [status.latestVersion, status.downloadedVersion].some((version) => !!version && (0, peers_sdk_1.isNewerVersion)(status.currentVersion, version));
37
+ }
38
+ /**
39
+ * Whether a newer version is available, downloading, or ready to install. A failed download
40
+ * or check still counts while a newer version is known: Update & restart retries it.
41
+ *
42
+ * @param status - Update status, or `undefined` while loading.
43
+ */
44
+ function hasAppUpdate(status) {
45
+ if (!status) {
46
+ return false;
47
+ }
48
+ return (status.state === "available" ||
49
+ status.state === "downloading" ||
50
+ status.state === "downloaded" ||
51
+ (status.state === "error" && knowsNewerVersion(status)));
52
+ }
53
+ /**
54
+ * Whether the device is already installing an update or restarting.
55
+ *
56
+ * @param status - Update status, or `undefined` while loading.
57
+ */
58
+ function isAppRestarting(status) {
59
+ return status?.state === "installing" || status?.state === "restarting";
60
+ }
61
+ /** Version being downloaded, or the cached version being prepared for installation. */
62
+ function getAppUpdateVersion(status) {
63
+ return status.state === "downloaded" || status.state === "installing"
64
+ ? (status.downloadedVersion ?? status.latestVersion)
65
+ : (status.latestVersion ?? status.downloadedVersion);
66
+ }
67
+ /**
68
+ * One-line, human-readable summary of an update status.
69
+ *
70
+ * @param status - Update status to describe.
71
+ */
72
+ function describeAppUpdateStatus(status) {
73
+ const version = getAppUpdateVersion(status);
74
+ switch (status.state) {
75
+ case "installing":
76
+ return version ? `Installing ${version}…` : "Installing update…";
77
+ case "restarting":
78
+ return "Restarting…";
79
+ case "downloaded":
80
+ return `Version ${version} is ready to install${status.error ? `. Update problem: ${status.error}` : ""}`;
81
+ case "downloading":
82
+ return `Downloading ${version}${status.downloadPercent != null ? ` (${status.downloadPercent}%)` : ""}`;
83
+ case "available":
84
+ return `Version ${version} is available`;
85
+ case "checking":
86
+ return "Checking for updates…";
87
+ case "up-to-date":
88
+ return "Up to date";
89
+ case "error":
90
+ return knowsNewerVersion(status)
91
+ ? `Version ${version} is available. Update problem: ${status.error ?? "unknown error"}`
92
+ : `Update problem: ${status.error ?? "unknown error"}`;
93
+ default:
94
+ return status.supported ? "Not checked yet" : "Updates are not available for this build";
95
+ }
96
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const app_updates_consumer_1 = require("./app-updates-consumer");
4
+ function status(overrides = {}) {
5
+ return {
6
+ supported: true,
7
+ canRestart: true,
8
+ platform: "darwin",
9
+ currentVersion: "1.0.0",
10
+ state: "idle",
11
+ installOnRestart: true,
12
+ installOnQuitArmed: false,
13
+ remoteRestartAllowed: true,
14
+ ...overrides,
15
+ };
16
+ }
17
+ describe("classifyAppUpdatesError", () => {
18
+ it.each([
19
+ ["No contract provider registered for abc v1", "unavailable"],
20
+ ["Provider p has no live resolver for abc v1", "unavailable"],
21
+ ["This device cannot install updates or restart itself", "unavailable"],
22
+ ["Permission denied: tool 'getStatus'.run on contract abc", "denied"],
23
+ ["Device d is not connected", "unreachable"],
24
+ ["Device d is not reachable through the peers mesh", "unreachable"],
25
+ ["Remote contract call to d timed out after 30000ms", "unreachable"],
26
+ ["Device d did not answer within 30000ms", "unreachable"],
27
+ ["Something else", "failed"],
28
+ ])("classifies %s as %s", (message, kind) => {
29
+ expect((0, app_updates_consumer_1.classifyAppUpdatesError)(new Error(message))).toBe(kind);
30
+ });
31
+ });
32
+ describe("status helpers", () => {
33
+ it("treats available, downloading, and downloaded as an available update", () => {
34
+ expect((0, app_updates_consumer_1.hasAppUpdate)(undefined)).toBe(false);
35
+ expect((0, app_updates_consumer_1.hasAppUpdate)(status({ state: "up-to-date" }))).toBe(false);
36
+ for (const state of ["available", "downloading", "downloaded"]) {
37
+ expect((0, app_updates_consumer_1.hasAppUpdate)(status({ state }))).toBe(true);
38
+ }
39
+ expect((0, app_updates_consumer_1.isAppRestarting)(status({ state: "installing" }))).toBe(true);
40
+ expect((0, app_updates_consumer_1.isAppRestarting)(status({ state: "downloaded" }))).toBe(false);
41
+ });
42
+ it("keeps a known newer version as an update after a failed download", () => {
43
+ expect((0, app_updates_consumer_1.hasAppUpdate)(status({ state: "error", latestVersion: "1.1.0" }))).toBe(true);
44
+ expect((0, app_updates_consumer_1.hasAppUpdate)(status({ state: "error", downloadedVersion: "1.1.0" }))).toBe(true);
45
+ expect((0, app_updates_consumer_1.hasAppUpdate)(status({ state: "error", latestVersion: "1.0.0" }))).toBe(false);
46
+ expect((0, app_updates_consumer_1.hasAppUpdate)(status({ state: "error" }))).toBe(false);
47
+ });
48
+ it("describes each state", () => {
49
+ expect((0, app_updates_consumer_1.describeAppUpdateStatus)(status({ state: "downloading", latestVersion: "1.2.0", downloadedVersion: "1.1.0" }))).toBe("Downloading 1.2.0");
50
+ expect((0, app_updates_consumer_1.describeAppUpdateStatus)(status({ state: "downloading", latestVersion: "1.1.0", downloadPercent: 42 }))).toBe("Downloading 1.1.0 (42%)");
51
+ expect((0, app_updates_consumer_1.describeAppUpdateStatus)(status({ state: "downloaded", downloadedVersion: "1.1.0" }))).toBe("Version 1.1.0 is ready to install");
52
+ expect((0, app_updates_consumer_1.describeAppUpdateStatus)(status({ state: "error", error: "offline" }))).toBe("Update problem: offline");
53
+ expect((0, app_updates_consumer_1.describeAppUpdateStatus)(status({ state: "error", latestVersion: "1.1.0", error: "download failed" }))).toBe("Version 1.1.0 is available. Update problem: download failed");
54
+ expect((0, app_updates_consumer_1.describeAppUpdateStatus)(status({ supported: false }))).toBe("Updates are not available for this build");
55
+ });
56
+ });
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Settings tab for this device's application updates: version, check and install controls,
3
+ * and the two per-device update settings.
4
+ */
5
+ export declare function AppUpdatesSettings(): import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AppUpdatesSettings = AppUpdatesSettings;
4
+ const jsx_runtime_1 = require("react/jsx-runtime");
5
+ const peers_sdk_1 = require("@peers-app/peers-sdk");
6
+ const react_1 = require("react");
7
+ const checkbox_1 = require("../components/checkbox");
8
+ const tooltip_1 = require("../components/tooltip");
9
+ const app_update_controls_1 = require("./app-update-controls");
10
+ const app_updates_consumer_1 = require("./app-updates-consumer");
11
+ const use_app_update_status_1 = require("./use-app-update-status");
12
+ /**
13
+ * macOS accepted the update for an Update & restart that timed out, and installs it when
14
+ * Peers quits; that cannot be withdrawn.
15
+ */
16
+ function isPreparedDespiteOptOut(status) {
17
+ return status.platform === "darwin" && !status.installOnRestart && status.installOnQuitArmed;
18
+ }
19
+ function SettingCheckbox({ id, label, checked, help, }) {
20
+ return ((0, jsx_runtime_1.jsxs)("div", { className: "form-check mb-2", children: [(0, jsx_runtime_1.jsx)(checkbox_1.Checkbox, { id: id, className: "form-check-input", checked: checked }), (0, jsx_runtime_1.jsx)("label", { htmlFor: id, className: "form-check-label small me-1", children: label }), (0, jsx_runtime_1.jsx)(tooltip_1.Tooltip, { markdownContent: help })] }));
21
+ }
22
+ /**
23
+ * Settings tab for this device's application updates: version, check and install controls,
24
+ * and the two per-device update settings.
25
+ */
26
+ function AppUpdatesSettings() {
27
+ const status = (0, use_app_update_status_1.useAppUpdateStatus)();
28
+ const [checking, setChecking] = (0, react_1.useState)(false);
29
+ const [checkError, setCheckError] = (0, react_1.useState)();
30
+ if (!status) {
31
+ return (0, jsx_runtime_1.jsx)("p", { className: "small text-muted", children: "Loading update status\u2026" });
32
+ }
33
+ if (!status.canRestart) {
34
+ return ((0, jsx_runtime_1.jsx)("p", { className: "small text-muted", children: "This device does not update itself. Browser installs update when the page reloads." }));
35
+ }
36
+ return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("h5", { className: "mb-3", children: "Updates" }), (0, jsx_runtime_1.jsxs)("dl", { className: "row small mb-2", children: [(0, jsx_runtime_1.jsx)("dt", { className: "col-4", children: "Installed version" }), (0, jsx_runtime_1.jsx)("dd", { className: "col-8", children: status.currentVersion }), status.latestVersion && ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("dt", { className: "col-4", children: "Latest version" }), (0, jsx_runtime_1.jsx)("dd", { className: "col-8", children: status.latestVersion })] })), (0, jsx_runtime_1.jsx)("dt", { className: "col-4", children: "Status" }), (0, jsx_runtime_1.jsx)("dd", { className: "col-8", children: (0, app_updates_consumer_1.describeAppUpdateStatus)(status) }), status.lastCheckedAt != null && ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("dt", { className: "col-4", children: "Last checked" }), (0, jsx_runtime_1.jsx)("dd", { className: "col-8", children: new Date(status.lastCheckedAt).toLocaleString() })] }))] }), (0, jsx_runtime_1.jsxs)("div", { className: "d-flex gap-2 mb-3", children: [(0, jsx_runtime_1.jsx)("button", { type: "button", className: "btn btn-sm btn-outline-secondary", disabled: !status.supported || checking || (0, app_updates_consumer_1.isAppRestarting)(status), onClick: () => {
37
+ setChecking(true);
38
+ setCheckError(undefined);
39
+ (0, app_updates_consumer_1.getAppUpdatesConsumer)()
40
+ .then((appUpdates) => appUpdates.tools.checkForUpdates())
41
+ .catch((err) => setCheckError(err instanceof Error ? err.message : String(err)))
42
+ .finally(() => setChecking(false));
43
+ }, children: "Check now" }), (0, jsx_runtime_1.jsx)(app_update_controls_1.UpdateAndRestartButton, { status: status, className: `btn btn-sm ${(0, app_updates_consumer_1.hasAppUpdate)(status) ? "btn-warning" : "btn-outline-secondary"}` })] }), checkError && ((0, jsx_runtime_1.jsx)("p", { className: "small text-danger", role: "alert", children: checkError })), (0, jsx_runtime_1.jsx)(SettingCheckbox, { id: "installAppUpdatesOnRestart", label: "Install updates automatically when Peers restarts", checked: peers_sdk_1.installAppUpdatesOnRestart, help: "Updates always download in the background. When this is off, they install only when you choose **Update & restart** here or in the network status menu." }), isPreparedDespiteOptOut(status) && ((0, jsx_runtime_1.jsxs)("p", { className: "small text-muted ms-4", children: ["macOS prepared version ", status.downloadedVersion, " for an earlier Update & restart, so it will still install when Peers quits. Later updates will wait for you."] })), (0, jsx_runtime_1.jsx)(SettingCheckbox, { id: "allowRemoteAppUpdateRestart", label: "Allow my other devices to update and restart this device", checked: peers_sdk_1.allowRemoteAppUpdateRestart, help: "Lets your other devices, and people you trust as yourself, run **Update & restart** on this device from Identity \u2192 Devices or the Network Viewer." })] }));
44
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,93 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const jsx_runtime_1 = require("react/jsx-runtime");
4
+ const react_1 = require("@testing-library/react");
5
+ const app_updates_settings_1 = require("./app-updates-settings");
6
+ let mockStatus;
7
+ const checkForUpdates = jest.fn();
8
+ jest.mock("../components/tooltip", () => ({ Tooltip: () => null }));
9
+ jest.mock("./use-app-update-status", () => ({ useAppUpdateStatus: () => mockStatus }));
10
+ jest.mock("./app-updates-consumer", () => ({
11
+ ...jest.requireActual("./app-updates-consumer"),
12
+ getAppUpdatesConsumer: () => Promise.resolve({ tools: { checkForUpdates: () => checkForUpdates() } }),
13
+ }));
14
+ function status(overrides = {}) {
15
+ return {
16
+ supported: true,
17
+ canRestart: true,
18
+ platform: "darwin",
19
+ currentVersion: "1.0.0",
20
+ latestVersion: "1.0.0",
21
+ state: "up-to-date",
22
+ installOnRestart: true,
23
+ installOnQuitArmed: false,
24
+ remoteRestartAllowed: true,
25
+ ...overrides,
26
+ };
27
+ }
28
+ describe("AppUpdatesSettings", () => {
29
+ beforeEach(() => {
30
+ checkForUpdates.mockReset();
31
+ checkForUpdates.mockResolvedValue(status());
32
+ });
33
+ it("explains hosts that do not update themselves", () => {
34
+ mockStatus = status({ supported: false, canRestart: false });
35
+ const view = (0, react_1.render)((0, jsx_runtime_1.jsx)(app_updates_settings_1.AppUpdatesSettings, {}));
36
+ expect(view.container).toHaveTextContent("This device does not update itself");
37
+ });
38
+ it("shows versions, both settings, and checks on demand", async () => {
39
+ mockStatus = status();
40
+ const view = (0, react_1.render)((0, jsx_runtime_1.jsx)(app_updates_settings_1.AppUpdatesSettings, {}));
41
+ expect(view.container).toHaveTextContent("Installed version1.0.0");
42
+ expect(view.getByLabelText("Install updates automatically when Peers restarts")).toBeTruthy();
43
+ expect(view.getByLabelText("Allow my other devices to update and restart this device")).toBeTruthy();
44
+ expect(view.getByRole("button", { name: /^Restart$/ })).toBeTruthy();
45
+ react_1.fireEvent.click(view.getByRole("button", { name: "Check now" }));
46
+ await (0, react_1.waitFor)(() => expect(checkForUpdates).toHaveBeenCalledTimes(1));
47
+ });
48
+ it("adds no note when the user opts out of a downloaded update", () => {
49
+ mockStatus = status({
50
+ state: "downloaded",
51
+ latestVersion: "1.1.0",
52
+ downloadedVersion: "1.1.0",
53
+ installOnRestart: false,
54
+ installOnQuitArmed: false,
55
+ });
56
+ const view = (0, react_1.render)((0, jsx_runtime_1.jsx)(app_updates_settings_1.AppUpdatesSettings, {}));
57
+ expect(view.container).not.toHaveTextContent("will still install");
58
+ expect(view.getByRole("button", { name: /Update & restart/ })).toBeTruthy();
59
+ });
60
+ it("notes when macOS accepted an update for an earlier Update & restart", () => {
61
+ mockStatus = status({
62
+ state: "downloaded",
63
+ latestVersion: "1.1.0",
64
+ downloadedVersion: "1.1.0",
65
+ installOnRestart: false,
66
+ installOnQuitArmed: true,
67
+ });
68
+ const view = (0, react_1.render)((0, jsx_runtime_1.jsx)(app_updates_settings_1.AppUpdatesSettings, {}));
69
+ expect(view.container).toHaveTextContent("macOS prepared version 1.1.0 for an earlier Update & restart");
70
+ expect(view.getByRole("button", { name: /Update & restart/ })).toBeTruthy();
71
+ });
72
+ it("shows a failed check and lets the user retry", async () => {
73
+ mockStatus = status();
74
+ checkForUpdates.mockRejectedValueOnce(new Error("Host disconnected"));
75
+ const view = (0, react_1.render)((0, jsx_runtime_1.jsx)(app_updates_settings_1.AppUpdatesSettings, {}));
76
+ react_1.fireEvent.click(view.getByRole("button", { name: "Check now" }));
77
+ expect(await view.findByRole("alert")).toHaveTextContent("Host disconnected");
78
+ react_1.fireEvent.click(view.getByRole("button", { name: "Check now" }));
79
+ await (0, react_1.waitFor)(() => expect(checkForUpdates).toHaveBeenCalledTimes(2));
80
+ expect(view.queryByRole("alert")).toBeNull();
81
+ });
82
+ it("shows feed errors while a downloaded update remains ready to install", () => {
83
+ mockStatus = status({
84
+ state: "downloaded",
85
+ latestVersion: "1.1.0",
86
+ downloadedVersion: "1.1.0",
87
+ error: "Feed unreachable",
88
+ });
89
+ const view = (0, react_1.render)((0, jsx_runtime_1.jsx)(app_updates_settings_1.AppUpdatesSettings, {}));
90
+ expect(view.container).toHaveTextContent("Version 1.1.0 is ready to install. Update problem: Feed unreachable");
91
+ expect(view.getByRole("button", { name: /Update & restart/ })).toBeEnabled();
92
+ });
93
+ });
@@ -0,0 +1,18 @@
1
+ /** How often the panel refreshes the target's status after an update or restart request. */
2
+ export declare const REMOTE_UPDATE_POLL_MS = 5000;
3
+ /** How long the panel keeps refreshing after a request. */
4
+ export declare const REMOTE_UPDATE_POLL_DURATION_MS: number;
5
+ /**
6
+ * Card showing another device's app version with an **Update & restart** action.
7
+ *
8
+ * The local host relays both calls to the target, which requires that it trusts this user
9
+ * as `Self` and allows remote restarts. Renders nothing when the target cannot update or
10
+ * restart itself, runs a build without App Updates, or does not trust this user.
11
+ *
12
+ * @param props.deviceId - Target device.
13
+ * @param props.deviceName - Name used in the confirmation prompt.
14
+ */
15
+ export declare function RemoteAppUpdatePanel({ deviceId, deviceName, }: {
16
+ deviceId: string;
17
+ deviceName?: string;
18
+ }): import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,125 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.REMOTE_UPDATE_POLL_DURATION_MS = exports.REMOTE_UPDATE_POLL_MS = void 0;
4
+ exports.RemoteAppUpdatePanel = RemoteAppUpdatePanel;
5
+ const jsx_runtime_1 = require("react/jsx-runtime");
6
+ const react_1 = require("react");
7
+ const app_update_controls_1 = require("./app-update-controls");
8
+ const app_updates_consumer_1 = require("./app-updates-consumer");
9
+ /** How often the panel refreshes the target's status after an update or restart request. */
10
+ exports.REMOTE_UPDATE_POLL_MS = 5_000;
11
+ /** How long the panel keeps refreshing after a request. */
12
+ exports.REMOTE_UPDATE_POLL_DURATION_MS = 3 * 60_000;
13
+ function errorMessage(error) {
14
+ return error instanceof Error ? error.message : String(error);
15
+ }
16
+ function disabledReason(status) {
17
+ if (!status.remoteRestartAllowed) {
18
+ return "This device does not allow remote restarts. Turn on “Allow my other devices to update and restart this device” in its Settings → Updates.";
19
+ }
20
+ if (status.installNeedsElevation) {
21
+ return "Installing updates on this device needs an administrator password, so update it at the device.";
22
+ }
23
+ return undefined;
24
+ }
25
+ /**
26
+ * Card showing another device's app version with an **Update & restart** action.
27
+ *
28
+ * The local host relays both calls to the target, which requires that it trusts this user
29
+ * as `Self` and allows remote restarts. Renders nothing when the target cannot update or
30
+ * restart itself, runs a build without App Updates, or does not trust this user.
31
+ *
32
+ * @param props.deviceId - Target device.
33
+ * @param props.deviceName - Name used in the confirmation prompt.
34
+ */
35
+ function RemoteAppUpdatePanel({ deviceId, deviceName, }) {
36
+ // Route changes must not keep another device's status, consent switch, or pending request.
37
+ return (0, jsx_runtime_1.jsx)(DeviceAppUpdatePanel, { deviceId: deviceId, deviceName: deviceName }, deviceId);
38
+ }
39
+ function DeviceAppUpdatePanel({ deviceId, deviceName }) {
40
+ const [state, setState] = (0, react_1.useState)({ kind: "loading" });
41
+ const [refreshKey, setRefreshKey] = (0, react_1.useState)(0);
42
+ const [requestedAt, setRequestedAt] = (0, react_1.useState)();
43
+ const [requestError, setRequestError] = (0, react_1.useState)();
44
+ const [sending, setSending] = (0, react_1.useState)(false);
45
+ (0, react_1.useEffect)(() => {
46
+ let active = true;
47
+ let poll;
48
+ const load = () => {
49
+ void (0, app_updates_consumer_1.getAppUpdatesConsumer)()
50
+ .then((appUpdates) => appUpdates.tools.getDeviceUpdateStatus({ deviceId }))
51
+ .then((status) => {
52
+ if (active) {
53
+ setState(status.canRestart ? { kind: "ready", status } : { kind: "hidden" });
54
+ }
55
+ })
56
+ .catch((error) => {
57
+ if (!active)
58
+ return;
59
+ const kind = (0, app_updates_consumer_1.classifyAppUpdatesError)(error);
60
+ if (kind === "unavailable" || kind === "denied") {
61
+ setState({ kind: "hidden" });
62
+ }
63
+ else {
64
+ setState({
65
+ kind: kind === "unreachable" ? "unreachable" : "error",
66
+ message: errorMessage(error),
67
+ });
68
+ }
69
+ })
70
+ .finally(() => {
71
+ // Wait for the preceding RPC: a slow target can take up to 30s to answer.
72
+ // An interval would overlap calls and discard every response as stale.
73
+ if (active &&
74
+ requestedAt !== undefined &&
75
+ Date.now() - requestedAt < exports.REMOTE_UPDATE_POLL_DURATION_MS) {
76
+ poll = setTimeout(load, exports.REMOTE_UPDATE_POLL_MS);
77
+ }
78
+ });
79
+ };
80
+ load();
81
+ return () => {
82
+ active = false;
83
+ if (poll)
84
+ clearTimeout(poll);
85
+ };
86
+ }, [deviceId, refreshKey, requestedAt]);
87
+ (0, react_1.useEffect)(() => {
88
+ if (requestedAt === undefined)
89
+ return;
90
+ const stop = setTimeout(() => setRequestedAt(undefined), exports.REMOTE_UPDATE_POLL_DURATION_MS);
91
+ return () => {
92
+ clearTimeout(stop);
93
+ };
94
+ }, [requestedAt]);
95
+ if (state.kind === "hidden") {
96
+ return null;
97
+ }
98
+ const requestUpdate = (status) => {
99
+ const name = deviceName || deviceId;
100
+ const prompt = (0, app_updates_consumer_1.hasAppUpdate)(status)
101
+ ? `Update ${name} to ${(0, app_updates_consumer_1.getAppUpdateVersion)(status)} and restart it?`
102
+ : `Restart ${name}? It will check for an update first.`;
103
+ if (!confirm(prompt))
104
+ return;
105
+ setSending(true);
106
+ setRequestError(undefined);
107
+ (0, app_updates_consumer_1.getAppUpdatesConsumer)()
108
+ .then((appUpdates) => appUpdates.tools.installUpdateAndRestartOnDevice({ deviceId }))
109
+ .then(() => {
110
+ setRequestedAt(Date.now());
111
+ setRefreshKey((key) => key + 1);
112
+ })
113
+ .catch((error) => setRequestError(errorMessage(error)))
114
+ .finally(() => setSending(false));
115
+ };
116
+ return ((0, jsx_runtime_1.jsx)("div", { className: "card mt-3", "data-testid": "remote-app-update", children: (0, jsx_runtime_1.jsxs)("div", { className: "card-body py-2 px-3 small", children: [(0, jsx_runtime_1.jsx)("div", { className: "fw-semibold mb-1", children: "App version" }), state.kind === "loading" && (0, jsx_runtime_1.jsx)("div", { className: "text-muted", children: "Checking this device\u2026" }), state.kind === "unreachable" && ((0, jsx_runtime_1.jsx)("div", { className: "text-muted", children: requestedAt !== undefined ? "Restarting…" : `Not reachable: ${state.message}` })), state.kind === "error" && (0, jsx_runtime_1.jsx)("div", { className: "text-danger", children: state.message }), (state.kind === "unreachable" || state.kind === "error") && ((0, jsx_runtime_1.jsx)("button", { type: "button", className: "btn btn-sm btn-outline-secondary mt-2", onClick: () => {
117
+ setState({ kind: "loading" });
118
+ setRefreshKey((key) => key + 1);
119
+ }, children: "Retry" })), state.kind === "ready" && ((0, jsx_runtime_1.jsx)(RemoteStatus, { status: state.status, sending: sending, requested: requestedAt !== undefined, onRequest: () => requestUpdate(state.status) })), requestError && (0, jsx_runtime_1.jsx)("div", { className: "text-danger mt-1", children: requestError })] }) }));
120
+ }
121
+ function RemoteStatus({ status, sending, requested, onRequest, }) {
122
+ const reason = disabledReason(status);
123
+ const update = (0, app_updates_consumer_1.hasAppUpdate)(status);
124
+ return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsxs)("div", { className: "mb-2", children: [status.currentVersion ? `Version ${status.currentVersion} · ` : "", (0, app_updates_consumer_1.describeAppUpdateStatus)(status)] }), (0, jsx_runtime_1.jsxs)("button", { type: "button", className: `btn btn-sm ${update ? "btn-warning" : "btn-outline-secondary"}`, disabled: Boolean(reason) || sending || (0, app_updates_consumer_1.isAppRestarting)(status), onClick: onRequest, children: [(0, jsx_runtime_1.jsx)("i", { className: `bi ${app_update_controls_1.APP_UPDATE_ICON_CLASS} me-1`, "aria-hidden": "true" }), update ? "Update & restart" : "Restart"] }), requested && !(0, app_updates_consumer_1.isAppRestarting)(status) && ((0, jsx_runtime_1.jsx)("span", { className: "text-muted ms-2", children: "Request sent" })), reason && (0, jsx_runtime_1.jsx)("div", { className: "text-muted mt-1", children: reason })] }));
125
+ }
@@ -0,0 +1 @@
1
+ export {};