@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.
- package/dist/app-updates/app-update-controls.d.ts +23 -0
- package/dist/app-updates/app-update-controls.js +40 -0
- package/dist/app-updates/app-updates-consumer.d.ts +41 -0
- package/dist/app-updates/app-updates-consumer.js +96 -0
- package/dist/app-updates/app-updates-consumer.test.d.ts +1 -0
- package/dist/app-updates/app-updates-consumer.test.js +56 -0
- package/dist/app-updates/app-updates-settings.d.ts +5 -0
- package/dist/app-updates/app-updates-settings.js +44 -0
- package/dist/app-updates/app-updates-settings.test.d.ts +1 -0
- package/dist/app-updates/app-updates-settings.test.js +93 -0
- package/dist/app-updates/remote-app-update-panel.d.ts +18 -0
- package/dist/app-updates/remote-app-update-panel.js +125 -0
- package/dist/app-updates/remote-app-update-panel.test.d.ts +1 -0
- package/dist/app-updates/remote-app-update-panel.test.js +165 -0
- package/dist/app-updates/use-app-update-status.d.ts +8 -0
- package/dist/app-updates/use-app-update-status.js +17 -0
- package/dist/screens/devices/devices-list.js +2 -1
- package/dist/screens/network-viewer/device-details-modal.js +2 -1
- package/dist/screens/settings/settings-page.js +5 -0
- package/dist/tabs-layout/network-status-indicator.d.ts +12 -0
- package/dist/tabs-layout/network-status-indicator.js +33 -5
- package/dist/tabs-layout/network-status-indicator.test.js +90 -0
- package/package.json +3 -3
- package/src/app-updates/app-update-controls.tsx +73 -0
- package/src/app-updates/app-updates-consumer.test.ts +83 -0
- package/src/app-updates/app-updates-consumer.ts +121 -0
- package/src/app-updates/app-updates-settings.test.tsx +109 -0
- package/src/app-updates/app-updates-settings.tsx +136 -0
- package/src/app-updates/remote-app-update-panel.test.tsx +205 -0
- package/src/app-updates/remote-app-update-panel.tsx +210 -0
- package/src/app-updates/use-app-update-status.ts +16 -0
- package/src/screens/devices/devices-list.tsx +4 -0
- package/src/screens/network-viewer/device-details-modal.tsx +6 -0
- package/src/screens/settings/settings-page.tsx +5 -0
- package/src/tabs-layout/network-status-indicator.test.tsx +119 -1
- package/src/tabs-layout/network-status-indicator.tsx +56 -5
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import type { IAppUpdateStatus } from "@peers-app/peers-sdk";
|
|
2
|
+
import { act, fireEvent, render, waitFor } from "@testing-library/react";
|
|
3
|
+
import { REMOTE_UPDATE_POLL_MS, RemoteAppUpdatePanel } from "./remote-app-update-panel";
|
|
4
|
+
|
|
5
|
+
const getDeviceUpdateStatus = jest.fn();
|
|
6
|
+
const installUpdateAndRestartOnDevice = jest.fn();
|
|
7
|
+
|
|
8
|
+
jest.mock("./app-updates-consumer", () => ({
|
|
9
|
+
...jest.requireActual("./app-updates-consumer"),
|
|
10
|
+
getAppUpdatesConsumer: () =>
|
|
11
|
+
Promise.resolve({
|
|
12
|
+
tools: {
|
|
13
|
+
getDeviceUpdateStatus: (...args: unknown[]) => getDeviceUpdateStatus(...args),
|
|
14
|
+
installUpdateAndRestartOnDevice: (...args: unknown[]) =>
|
|
15
|
+
installUpdateAndRestartOnDevice(...args),
|
|
16
|
+
},
|
|
17
|
+
}),
|
|
18
|
+
}));
|
|
19
|
+
|
|
20
|
+
function remoteStatus(overrides: Partial<IAppUpdateStatus> = {}): IAppUpdateStatus {
|
|
21
|
+
return {
|
|
22
|
+
supported: true,
|
|
23
|
+
canRestart: true,
|
|
24
|
+
platform: "win32",
|
|
25
|
+
currentVersion: "1.0.0",
|
|
26
|
+
latestVersion: "1.1.0",
|
|
27
|
+
downloadedVersion: "1.1.0",
|
|
28
|
+
state: "downloaded",
|
|
29
|
+
installOnRestart: true,
|
|
30
|
+
installOnQuitArmed: true,
|
|
31
|
+
remoteRestartAllowed: true,
|
|
32
|
+
...overrides,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
describe("RemoteAppUpdatePanel", () => {
|
|
37
|
+
beforeEach(() => {
|
|
38
|
+
getDeviceUpdateStatus.mockReset();
|
|
39
|
+
installUpdateAndRestartOnDevice.mockReset();
|
|
40
|
+
installUpdateAndRestartOnDevice.mockResolvedValue({ action: "installing" });
|
|
41
|
+
jest.spyOn(window, "confirm").mockReturnValue(true);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
afterEach(() => {
|
|
45
|
+
jest.restoreAllMocks();
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("updates the other device after confirmation", async () => {
|
|
49
|
+
getDeviceUpdateStatus.mockResolvedValue(remoteStatus());
|
|
50
|
+
const view = render(<RemoteAppUpdatePanel deviceId="desktop" deviceName="Desktop" />);
|
|
51
|
+
|
|
52
|
+
const button = await view.findByRole("button", { name: /Update & restart/ });
|
|
53
|
+
expect(view.getByTestId("remote-app-update")).toHaveTextContent(
|
|
54
|
+
"Version 1.0.0 · Version 1.1.0 is ready to install",
|
|
55
|
+
);
|
|
56
|
+
expect(getDeviceUpdateStatus).toHaveBeenCalledWith({ deviceId: "desktop" });
|
|
57
|
+
|
|
58
|
+
fireEvent.click(button);
|
|
59
|
+
expect(window.confirm).toHaveBeenCalledWith("Update Desktop to 1.1.0 and restart it?");
|
|
60
|
+
await waitFor(() =>
|
|
61
|
+
expect(installUpdateAndRestartOnDevice).toHaveBeenCalledWith({ deviceId: "desktop" }),
|
|
62
|
+
);
|
|
63
|
+
await view.findByText("Request sent");
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("offers a plain restart when the device is up to date", async () => {
|
|
67
|
+
getDeviceUpdateStatus.mockResolvedValue(remoteStatus({ state: "up-to-date" }));
|
|
68
|
+
const view = render(<RemoteAppUpdatePanel deviceId="desktop" />);
|
|
69
|
+
fireEvent.click(await view.findByRole("button", { name: /^Restart$/ }));
|
|
70
|
+
expect(window.confirm).toHaveBeenCalledWith(
|
|
71
|
+
"Restart desktop? It will check for an update first.",
|
|
72
|
+
);
|
|
73
|
+
await waitFor(() => expect(installUpdateAndRestartOnDevice).toHaveBeenCalled());
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("does nothing when the confirmation is declined", async () => {
|
|
77
|
+
(window.confirm as jest.Mock).mockReturnValue(false);
|
|
78
|
+
getDeviceUpdateStatus.mockResolvedValue(remoteStatus());
|
|
79
|
+
const view = render(<RemoteAppUpdatePanel deviceId="desktop" />);
|
|
80
|
+
fireEvent.click(await view.findByRole("button", { name: /Update & restart/ }));
|
|
81
|
+
expect(installUpdateAndRestartOnDevice).not.toHaveBeenCalled();
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("explains why the action is disabled", async () => {
|
|
85
|
+
getDeviceUpdateStatus.mockResolvedValue(remoteStatus({ remoteRestartAllowed: false }));
|
|
86
|
+
const view = render(<RemoteAppUpdatePanel deviceId="desktop" />);
|
|
87
|
+
expect(await view.findByRole("button", { name: /Update & restart/ })).toBeDisabled();
|
|
88
|
+
expect(view.getByTestId("remote-app-update")).toHaveTextContent(
|
|
89
|
+
"does not allow remote restarts",
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
getDeviceUpdateStatus.mockResolvedValue(remoteStatus({ installNeedsElevation: true }));
|
|
93
|
+
const elevated = render(<RemoteAppUpdatePanel deviceId="linux-box" />);
|
|
94
|
+
await waitFor(() =>
|
|
95
|
+
expect(elevated.getAllByRole("button", { name: /Update & restart/ })[1]).toBeDisabled(),
|
|
96
|
+
);
|
|
97
|
+
expect(elevated.getAllByTestId("remote-app-update")[1]).toHaveTextContent(
|
|
98
|
+
"administrator password",
|
|
99
|
+
);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it.each([
|
|
103
|
+
[
|
|
104
|
+
"a device that cannot restart itself",
|
|
105
|
+
() => Promise.resolve(remoteStatus({ canRestart: false })),
|
|
106
|
+
],
|
|
107
|
+
[
|
|
108
|
+
"a device that does not trust this user",
|
|
109
|
+
() =>
|
|
110
|
+
Promise.reject(new Error("Permission denied: tool 'getUpdateStatus'.run on contract x")),
|
|
111
|
+
],
|
|
112
|
+
[
|
|
113
|
+
"a build without App Updates",
|
|
114
|
+
() => Promise.reject(new Error("No contract provider registered for x v1")),
|
|
115
|
+
],
|
|
116
|
+
])("renders nothing for %s", async (_label, result) => {
|
|
117
|
+
getDeviceUpdateStatus.mockImplementation(result);
|
|
118
|
+
const view = render(<RemoteAppUpdatePanel deviceId="desktop" />);
|
|
119
|
+
await waitFor(() => expect(view.queryByTestId("remote-app-update")).toBeNull());
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("reports an unreachable device", async () => {
|
|
123
|
+
getDeviceUpdateStatus.mockRejectedValue(new Error("Device desktop is not connected"));
|
|
124
|
+
const view = render(<RemoteAppUpdatePanel deviceId="desktop" />);
|
|
125
|
+
await view.findByText("Not reachable: Device desktop is not connected");
|
|
126
|
+
getDeviceUpdateStatus.mockResolvedValue(remoteStatus());
|
|
127
|
+
fireEvent.click(view.getByRole("button", { name: "Retry" }));
|
|
128
|
+
await view.findByRole("button", { name: /Update & restart/ });
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it("clears the previous device's action while a new target loads", async () => {
|
|
132
|
+
getDeviceUpdateStatus.mockResolvedValueOnce(remoteStatus());
|
|
133
|
+
const view = render(<RemoteAppUpdatePanel deviceId="desktop" />);
|
|
134
|
+
await view.findByRole("button", { name: /Update & restart/ });
|
|
135
|
+
let finish!: (status: IAppUpdateStatus) => void;
|
|
136
|
+
getDeviceUpdateStatus.mockImplementationOnce(
|
|
137
|
+
() =>
|
|
138
|
+
new Promise<IAppUpdateStatus>((resolve) => {
|
|
139
|
+
finish = resolve;
|
|
140
|
+
}),
|
|
141
|
+
);
|
|
142
|
+
view.rerender(<RemoteAppUpdatePanel deviceId="another-desktop" />);
|
|
143
|
+
expect(view.queryByRole("button", { name: /Update & restart/ })).toBeNull();
|
|
144
|
+
expect(view.getByText("Checking this device…")).toBeTruthy();
|
|
145
|
+
await act(async () => {
|
|
146
|
+
await Promise.resolve();
|
|
147
|
+
finish(remoteStatus({ remoteRestartAllowed: false }));
|
|
148
|
+
});
|
|
149
|
+
expect(view.getByRole("button", { name: /Update & restart/ })).toBeDisabled();
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("accepts slow poll responses without overlapping requests and cleans up timers", async () => {
|
|
153
|
+
jest.useFakeTimers();
|
|
154
|
+
try {
|
|
155
|
+
getDeviceUpdateStatus.mockResolvedValue(remoteStatus());
|
|
156
|
+
const view = render(<RemoteAppUpdatePanel deviceId="desktop" />);
|
|
157
|
+
await act(async () => {});
|
|
158
|
+
let finish!: (status: IAppUpdateStatus) => void;
|
|
159
|
+
getDeviceUpdateStatus.mockImplementationOnce(
|
|
160
|
+
() =>
|
|
161
|
+
new Promise<IAppUpdateStatus>((resolve) => {
|
|
162
|
+
finish = resolve;
|
|
163
|
+
}),
|
|
164
|
+
);
|
|
165
|
+
fireEvent.click(view.getByRole("button", { name: /Update & restart/ }));
|
|
166
|
+
await act(async () => {});
|
|
167
|
+
await act(async () => {
|
|
168
|
+
jest.advanceTimersByTime(REMOTE_UPDATE_POLL_MS * 3);
|
|
169
|
+
});
|
|
170
|
+
expect(getDeviceUpdateStatus).toHaveBeenCalledTimes(2);
|
|
171
|
+
await act(async () => {
|
|
172
|
+
finish(remoteStatus({ currentVersion: "1.1.0", state: "up-to-date" }));
|
|
173
|
+
});
|
|
174
|
+
expect(view.getByText("Version 1.1.0 · Up to date")).toBeTruthy();
|
|
175
|
+
await act(async () => {
|
|
176
|
+
jest.advanceTimersByTime(REMOTE_UPDATE_POLL_MS);
|
|
177
|
+
});
|
|
178
|
+
expect(getDeviceUpdateStatus).toHaveBeenCalledTimes(3);
|
|
179
|
+
view.unmount();
|
|
180
|
+
expect(jest.getTimerCount()).toBe(0);
|
|
181
|
+
} finally {
|
|
182
|
+
jest.useRealTimers();
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it("confirms the newer downloading version instead of the older cached version", async () => {
|
|
187
|
+
getDeviceUpdateStatus.mockResolvedValue(
|
|
188
|
+
remoteStatus({ state: "downloading", latestVersion: "1.2.0", downloadedVersion: "1.1.0" }),
|
|
189
|
+
);
|
|
190
|
+
const view = render(<RemoteAppUpdatePanel deviceId="desktop" deviceName="Desktop" />);
|
|
191
|
+
fireEvent.click(await view.findByRole("button", { name: /Update & restart/ }));
|
|
192
|
+
expect(window.confirm).toHaveBeenCalledWith("Update Desktop to 1.2.0 and restart it?");
|
|
193
|
+
await view.findByText("Request sent");
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it("shows a failed request", async () => {
|
|
197
|
+
getDeviceUpdateStatus.mockResolvedValue(remoteStatus());
|
|
198
|
+
installUpdateAndRestartOnDevice.mockRejectedValue(
|
|
199
|
+
new Error("Permission denied: this device does not allow other devices to update"),
|
|
200
|
+
);
|
|
201
|
+
const view = render(<RemoteAppUpdatePanel deviceId="desktop" />);
|
|
202
|
+
fireEvent.click(await view.findByRole("button", { name: /Update & restart/ }));
|
|
203
|
+
await view.findByText(/does not allow other devices to update/);
|
|
204
|
+
});
|
|
205
|
+
});
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import type { IAppUpdateStatus } from "@peers-app/peers-sdk";
|
|
2
|
+
import { useEffect, useState } from "react";
|
|
3
|
+
import { APP_UPDATE_ICON_CLASS } from "./app-update-controls";
|
|
4
|
+
import {
|
|
5
|
+
classifyAppUpdatesError,
|
|
6
|
+
describeAppUpdateStatus,
|
|
7
|
+
getAppUpdatesConsumer,
|
|
8
|
+
getAppUpdateVersion,
|
|
9
|
+
hasAppUpdate,
|
|
10
|
+
isAppRestarting,
|
|
11
|
+
} from "./app-updates-consumer";
|
|
12
|
+
|
|
13
|
+
/** How often the panel refreshes the target's status after an update or restart request. */
|
|
14
|
+
export const REMOTE_UPDATE_POLL_MS = 5_000;
|
|
15
|
+
/** How long the panel keeps refreshing after a request. */
|
|
16
|
+
export const REMOTE_UPDATE_POLL_DURATION_MS = 3 * 60_000;
|
|
17
|
+
|
|
18
|
+
type RemoteUpdateState =
|
|
19
|
+
| { kind: "loading" }
|
|
20
|
+
| { kind: "hidden" }
|
|
21
|
+
| { kind: "unreachable"; message: string }
|
|
22
|
+
| { kind: "error"; message: string }
|
|
23
|
+
| { kind: "ready"; status: IAppUpdateStatus };
|
|
24
|
+
|
|
25
|
+
function errorMessage(error: unknown): string {
|
|
26
|
+
return error instanceof Error ? error.message : String(error);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function disabledReason(status: IAppUpdateStatus): string | undefined {
|
|
30
|
+
if (!status.remoteRestartAllowed) {
|
|
31
|
+
return "This device does not allow remote restarts. Turn on “Allow my other devices to update and restart this device” in its Settings → Updates.";
|
|
32
|
+
}
|
|
33
|
+
if (status.installNeedsElevation) {
|
|
34
|
+
return "Installing updates on this device needs an administrator password, so update it at the device.";
|
|
35
|
+
}
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Card showing another device's app version with an **Update & restart** action.
|
|
41
|
+
*
|
|
42
|
+
* The local host relays both calls to the target, which requires that it trusts this user
|
|
43
|
+
* as `Self` and allows remote restarts. Renders nothing when the target cannot update or
|
|
44
|
+
* restart itself, runs a build without App Updates, or does not trust this user.
|
|
45
|
+
*
|
|
46
|
+
* @param props.deviceId - Target device.
|
|
47
|
+
* @param props.deviceName - Name used in the confirmation prompt.
|
|
48
|
+
*/
|
|
49
|
+
export function RemoteAppUpdatePanel({
|
|
50
|
+
deviceId,
|
|
51
|
+
deviceName,
|
|
52
|
+
}: {
|
|
53
|
+
deviceId: string;
|
|
54
|
+
deviceName?: string;
|
|
55
|
+
}) {
|
|
56
|
+
// Route changes must not keep another device's status, consent switch, or pending request.
|
|
57
|
+
return <DeviceAppUpdatePanel key={deviceId} deviceId={deviceId} deviceName={deviceName} />;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function DeviceAppUpdatePanel({ deviceId, deviceName }: { deviceId: string; deviceName?: string }) {
|
|
61
|
+
const [state, setState] = useState<RemoteUpdateState>({ kind: "loading" });
|
|
62
|
+
const [refreshKey, setRefreshKey] = useState(0);
|
|
63
|
+
const [requestedAt, setRequestedAt] = useState<number>();
|
|
64
|
+
const [requestError, setRequestError] = useState<string>();
|
|
65
|
+
const [sending, setSending] = useState(false);
|
|
66
|
+
|
|
67
|
+
useEffect(() => {
|
|
68
|
+
let active = true;
|
|
69
|
+
let poll: ReturnType<typeof setTimeout> | undefined;
|
|
70
|
+
const load = () => {
|
|
71
|
+
void getAppUpdatesConsumer()
|
|
72
|
+
.then((appUpdates) => appUpdates.tools.getDeviceUpdateStatus({ deviceId }))
|
|
73
|
+
.then((status) => {
|
|
74
|
+
if (active) {
|
|
75
|
+
setState(status.canRestart ? { kind: "ready", status } : { kind: "hidden" });
|
|
76
|
+
}
|
|
77
|
+
})
|
|
78
|
+
.catch((error: unknown) => {
|
|
79
|
+
if (!active) return;
|
|
80
|
+
const kind = classifyAppUpdatesError(error);
|
|
81
|
+
if (kind === "unavailable" || kind === "denied") {
|
|
82
|
+
setState({ kind: "hidden" });
|
|
83
|
+
} else {
|
|
84
|
+
setState({
|
|
85
|
+
kind: kind === "unreachable" ? "unreachable" : "error",
|
|
86
|
+
message: errorMessage(error),
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
})
|
|
90
|
+
.finally(() => {
|
|
91
|
+
// Wait for the preceding RPC: a slow target can take up to 30s to answer.
|
|
92
|
+
// An interval would overlap calls and discard every response as stale.
|
|
93
|
+
if (
|
|
94
|
+
active &&
|
|
95
|
+
requestedAt !== undefined &&
|
|
96
|
+
Date.now() - requestedAt < REMOTE_UPDATE_POLL_DURATION_MS
|
|
97
|
+
) {
|
|
98
|
+
poll = setTimeout(load, REMOTE_UPDATE_POLL_MS);
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
};
|
|
102
|
+
load();
|
|
103
|
+
return () => {
|
|
104
|
+
active = false;
|
|
105
|
+
if (poll) clearTimeout(poll);
|
|
106
|
+
};
|
|
107
|
+
}, [deviceId, refreshKey, requestedAt]);
|
|
108
|
+
|
|
109
|
+
useEffect(() => {
|
|
110
|
+
if (requestedAt === undefined) return;
|
|
111
|
+
const stop = setTimeout(() => setRequestedAt(undefined), REMOTE_UPDATE_POLL_DURATION_MS);
|
|
112
|
+
return () => {
|
|
113
|
+
clearTimeout(stop);
|
|
114
|
+
};
|
|
115
|
+
}, [requestedAt]);
|
|
116
|
+
|
|
117
|
+
if (state.kind === "hidden") {
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const requestUpdate = (status: IAppUpdateStatus) => {
|
|
122
|
+
const name = deviceName || deviceId;
|
|
123
|
+
const prompt = hasAppUpdate(status)
|
|
124
|
+
? `Update ${name} to ${getAppUpdateVersion(status)} and restart it?`
|
|
125
|
+
: `Restart ${name}? It will check for an update first.`;
|
|
126
|
+
if (!confirm(prompt)) return;
|
|
127
|
+
setSending(true);
|
|
128
|
+
setRequestError(undefined);
|
|
129
|
+
getAppUpdatesConsumer()
|
|
130
|
+
.then((appUpdates) => appUpdates.tools.installUpdateAndRestartOnDevice({ deviceId }))
|
|
131
|
+
.then(() => {
|
|
132
|
+
setRequestedAt(Date.now());
|
|
133
|
+
setRefreshKey((key) => key + 1);
|
|
134
|
+
})
|
|
135
|
+
.catch((error: unknown) => setRequestError(errorMessage(error)))
|
|
136
|
+
.finally(() => setSending(false));
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
return (
|
|
140
|
+
<div className="card mt-3" data-testid="remote-app-update">
|
|
141
|
+
<div className="card-body py-2 px-3 small">
|
|
142
|
+
<div className="fw-semibold mb-1">App version</div>
|
|
143
|
+
{state.kind === "loading" && <div className="text-muted">Checking this device…</div>}
|
|
144
|
+
{state.kind === "unreachable" && (
|
|
145
|
+
<div className="text-muted">
|
|
146
|
+
{requestedAt !== undefined ? "Restarting…" : `Not reachable: ${state.message}`}
|
|
147
|
+
</div>
|
|
148
|
+
)}
|
|
149
|
+
{state.kind === "error" && <div className="text-danger">{state.message}</div>}
|
|
150
|
+
{(state.kind === "unreachable" || state.kind === "error") && (
|
|
151
|
+
<button
|
|
152
|
+
type="button"
|
|
153
|
+
className="btn btn-sm btn-outline-secondary mt-2"
|
|
154
|
+
onClick={() => {
|
|
155
|
+
setState({ kind: "loading" });
|
|
156
|
+
setRefreshKey((key) => key + 1);
|
|
157
|
+
}}
|
|
158
|
+
>
|
|
159
|
+
Retry
|
|
160
|
+
</button>
|
|
161
|
+
)}
|
|
162
|
+
{state.kind === "ready" && (
|
|
163
|
+
<RemoteStatus
|
|
164
|
+
status={state.status}
|
|
165
|
+
sending={sending}
|
|
166
|
+
requested={requestedAt !== undefined}
|
|
167
|
+
onRequest={() => requestUpdate(state.status)}
|
|
168
|
+
/>
|
|
169
|
+
)}
|
|
170
|
+
{requestError && <div className="text-danger mt-1">{requestError}</div>}
|
|
171
|
+
</div>
|
|
172
|
+
</div>
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function RemoteStatus({
|
|
177
|
+
status,
|
|
178
|
+
sending,
|
|
179
|
+
requested,
|
|
180
|
+
onRequest,
|
|
181
|
+
}: {
|
|
182
|
+
status: IAppUpdateStatus;
|
|
183
|
+
sending: boolean;
|
|
184
|
+
requested: boolean;
|
|
185
|
+
onRequest: () => void;
|
|
186
|
+
}) {
|
|
187
|
+
const reason = disabledReason(status);
|
|
188
|
+
const update = hasAppUpdate(status);
|
|
189
|
+
return (
|
|
190
|
+
<>
|
|
191
|
+
<div className="mb-2">
|
|
192
|
+
{status.currentVersion ? `Version ${status.currentVersion} · ` : ""}
|
|
193
|
+
{describeAppUpdateStatus(status)}
|
|
194
|
+
</div>
|
|
195
|
+
<button
|
|
196
|
+
type="button"
|
|
197
|
+
className={`btn btn-sm ${update ? "btn-warning" : "btn-outline-secondary"}`}
|
|
198
|
+
disabled={Boolean(reason) || sending || isAppRestarting(status)}
|
|
199
|
+
onClick={onRequest}
|
|
200
|
+
>
|
|
201
|
+
<i className={`bi ${APP_UPDATE_ICON_CLASS} me-1`} aria-hidden="true" />
|
|
202
|
+
{update ? "Update & restart" : "Restart"}
|
|
203
|
+
</button>
|
|
204
|
+
{requested && !isAppRestarting(status) && (
|
|
205
|
+
<span className="text-muted ms-2">Request sent</span>
|
|
206
|
+
)}
|
|
207
|
+
{reason && <div className="text-muted mt-1">{reason}</div>}
|
|
208
|
+
</>
|
|
209
|
+
);
|
|
210
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { IAppUpdateStatus } from "@peers-app/peers-sdk";
|
|
2
|
+
import { useObservable } from "../hooks";
|
|
3
|
+
import { useHostContract } from "../hooks/use-host-contract";
|
|
4
|
+
import { appUpdatesContract } from "./app-updates-consumer";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Subscribe to the local host's application update status.
|
|
8
|
+
*
|
|
9
|
+
* @returns The live status, or `undefined` while the host transport is down. Hosts that
|
|
10
|
+
* cannot update themselves (PWA, headless) report `supported: false`.
|
|
11
|
+
*/
|
|
12
|
+
export function useAppUpdateStatus(): IAppUpdateStatus | undefined {
|
|
13
|
+
const { consumer } = useHostContract(appUpdatesContract);
|
|
14
|
+
const [status] = useObservable<IAppUpdateStatus | undefined>(consumer?.observables.status);
|
|
15
|
+
return status;
|
|
16
|
+
}
|
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
thisDeviceId,
|
|
9
9
|
} from "@peers-app/peers-sdk";
|
|
10
10
|
import { useEffect, useState } from "react";
|
|
11
|
+
import { RemoteAppUpdatePanel } from "../../app-updates/remote-app-update-panel";
|
|
11
12
|
import { Input } from "../../components/input";
|
|
12
13
|
import { LoadingIndicator } from "../../components/loading-indicator";
|
|
13
14
|
import { mainContentPath } from "../../globals";
|
|
@@ -317,6 +318,9 @@ function DeviceDetail(props: {
|
|
|
317
318
|
Forget
|
|
318
319
|
</button>
|
|
319
320
|
)}
|
|
321
|
+
{props.isThisDevice ? null : (
|
|
322
|
+
<RemoteAppUpdatePanel deviceId={props.device.deviceId} deviceName={props.device.name} />
|
|
323
|
+
)}
|
|
320
324
|
</div>
|
|
321
325
|
);
|
|
322
326
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type INetworkDeviceDetails, TrustLevel } from "@peers-app/peers-sdk";
|
|
2
2
|
import { useCallback, useEffect, useState } from "react";
|
|
3
|
+
import { RemoteAppUpdatePanel } from "../../app-updates/remote-app-update-panel";
|
|
3
4
|
import { LoadingIndicator } from "../../components/loading-indicator";
|
|
4
5
|
import { TrustLevelBadge } from "../../components/trust-level-badge";
|
|
5
6
|
import { getNetworkControlConsumer, getNetworkDiagnosticsConsumer } from "./network-viewer-ipc";
|
|
@@ -210,6 +211,11 @@ export function DeviceDetailsModal({ deviceId, onClose, onDisconnect }: IDeviceD
|
|
|
210
211
|
</div>
|
|
211
212
|
|
|
212
213
|
{device.canReadRemoteLogs && <RemoteLogsPanel deviceId={device.deviceId} />}
|
|
214
|
+
{device.canReadRemoteLogs && (
|
|
215
|
+
<div className="mb-4">
|
|
216
|
+
<RemoteAppUpdatePanel deviceId={device.deviceId} deviceName={device.deviceName} />
|
|
217
|
+
</div>
|
|
218
|
+
)}
|
|
213
219
|
|
|
214
220
|
{/* Shared Groups */}
|
|
215
221
|
{device.sharedGroups.length > 0 && (
|
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
} from "@peers-app/peers-sdk";
|
|
10
10
|
import type React from "react";
|
|
11
11
|
import { useEffect, useState } from "react";
|
|
12
|
+
import { AppUpdatesSettings } from "../../app-updates/app-updates-settings";
|
|
12
13
|
import { Input } from "../../components/input";
|
|
13
14
|
import { Tabs } from "../../components/tabs";
|
|
14
15
|
import { Tooltip } from "../../components/tooltip";
|
|
@@ -54,6 +55,10 @@ export const SettingsPage: React.FC = () => {
|
|
|
54
55
|
name: "Appearance",
|
|
55
56
|
content: <AppearanceSettingsTab />,
|
|
56
57
|
},
|
|
58
|
+
{
|
|
59
|
+
name: "Updates",
|
|
60
|
+
content: <AppUpdatesSettings />,
|
|
61
|
+
},
|
|
57
62
|
{
|
|
58
63
|
name: "Advanced",
|
|
59
64
|
content: <AdvancedSettingsTab />,
|
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
IAppUpdateStatus,
|
|
3
|
+
INetworkConnectionInfo,
|
|
4
|
+
INetworkStatusSummary,
|
|
5
|
+
} from "@peers-app/peers-sdk";
|
|
2
6
|
import { TrustLevel } from "@peers-app/peers-sdk";
|
|
3
7
|
import { act, fireEvent, render, waitFor } from "@testing-library/react";
|
|
4
8
|
import moment from "moment";
|
|
@@ -11,12 +15,15 @@ import {
|
|
|
11
15
|
NetworkStatusIndicator,
|
|
12
16
|
SYNCED_STABLE_MS,
|
|
13
17
|
SYNCING_GREEN_MS,
|
|
18
|
+
withAppUpdate,
|
|
14
19
|
} from "./network-status-indicator";
|
|
15
20
|
import type { INetworkStatusViewState } from "./use-network-status";
|
|
16
21
|
|
|
17
22
|
const goToTabPath = jest.fn();
|
|
18
23
|
const getConnections = jest.fn();
|
|
19
24
|
let mockViewState: INetworkStatusViewState = { availability: "loading" };
|
|
25
|
+
let mockAppUpdate: IAppUpdateStatus | undefined;
|
|
26
|
+
const installUpdateAndRestart = jest.fn();
|
|
20
27
|
|
|
21
28
|
jest.mock("./tabs-state", () => ({
|
|
22
29
|
goToTabPath: (...args: unknown[]) => goToTabPath(...args),
|
|
@@ -26,6 +33,36 @@ jest.mock("./use-network-status", () => ({
|
|
|
26
33
|
useNetworkStatus: () => mockViewState,
|
|
27
34
|
}));
|
|
28
35
|
|
|
36
|
+
jest.mock("../app-updates/use-app-update-status", () => ({
|
|
37
|
+
useAppUpdateStatus: () => mockAppUpdate,
|
|
38
|
+
}));
|
|
39
|
+
|
|
40
|
+
jest.mock("../app-updates/app-updates-consumer", () => ({
|
|
41
|
+
...jest.requireActual("../app-updates/app-updates-consumer"),
|
|
42
|
+
getAppUpdatesConsumer: () =>
|
|
43
|
+
Promise.resolve({
|
|
44
|
+
tools: {
|
|
45
|
+
installUpdateAndRestart: (...args: unknown[]) => installUpdateAndRestart(...args),
|
|
46
|
+
},
|
|
47
|
+
}),
|
|
48
|
+
}));
|
|
49
|
+
|
|
50
|
+
function appUpdate(overrides: Partial<IAppUpdateStatus> = {}): IAppUpdateStatus {
|
|
51
|
+
return {
|
|
52
|
+
supported: true,
|
|
53
|
+
canRestart: true,
|
|
54
|
+
platform: "darwin",
|
|
55
|
+
currentVersion: "1.0.0",
|
|
56
|
+
latestVersion: "1.1.0",
|
|
57
|
+
downloadedVersion: "1.1.0",
|
|
58
|
+
state: "downloaded",
|
|
59
|
+
installOnRestart: true,
|
|
60
|
+
installOnQuitArmed: true,
|
|
61
|
+
remoteRestartAllowed: true,
|
|
62
|
+
...overrides,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
29
66
|
jest.mock("../screens/network-viewer/network-viewer-ipc", () => ({
|
|
30
67
|
getNetworkDiagnosticsConsumer: () =>
|
|
31
68
|
Promise.resolve({
|
|
@@ -285,9 +322,64 @@ describe("getNetworkStatusPresentation", () => {
|
|
|
285
322
|
});
|
|
286
323
|
});
|
|
287
324
|
|
|
325
|
+
describe("withAppUpdate", () => {
|
|
326
|
+
const synced = getNetworkStatusPresentation({
|
|
327
|
+
availability: "ready",
|
|
328
|
+
status: status({ state: "synced" }),
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
it("leaves the presentation alone without an available update", () => {
|
|
332
|
+
expect(withAppUpdate(synced, undefined)).toBe(synced);
|
|
333
|
+
expect(withAppUpdate(synced, appUpdate({ state: "up-to-date" }))).toBe(synced);
|
|
334
|
+
expect(withAppUpdate(synced, appUpdate({ supported: false }))).toBe(synced);
|
|
335
|
+
expect(
|
|
336
|
+
withAppUpdate(
|
|
337
|
+
synced,
|
|
338
|
+
appUpdate({
|
|
339
|
+
state: "error",
|
|
340
|
+
latestVersion: "1.0.0",
|
|
341
|
+
downloadedVersion: undefined,
|
|
342
|
+
error: "feed unreachable",
|
|
343
|
+
}),
|
|
344
|
+
),
|
|
345
|
+
).toBe(synced);
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
it("keeps the update badge after a failed download while a newer version is known", () => {
|
|
349
|
+
const presentation = withAppUpdate(
|
|
350
|
+
synced,
|
|
351
|
+
appUpdate({ state: "error", downloadedVersion: undefined, error: "download failed" }),
|
|
352
|
+
);
|
|
353
|
+
expect(presentation.updateAvailableOverlay).toBe(true);
|
|
354
|
+
expect(presentation.label).toBe(
|
|
355
|
+
`${synced.label}; Version 1.1.0 is available. Update problem: download failed`,
|
|
356
|
+
);
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
it("adds the update badge and label while an update is available, downloading, or ready", () => {
|
|
360
|
+
for (const state of ["available", "downloading", "downloaded"] as const) {
|
|
361
|
+
const presentation = withAppUpdate(synced, appUpdate({ state }));
|
|
362
|
+
expect(presentation.updateAvailableOverlay).toBe(true);
|
|
363
|
+
expect(presentation.label).toMatch(/^Network synced; .*1\.1\.0/);
|
|
364
|
+
expect(presentation.colorClass).toBe(synced.colorClass);
|
|
365
|
+
}
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
it("applies offline too", () => {
|
|
369
|
+
const offline = getNetworkStatusPresentation({
|
|
370
|
+
availability: "ready",
|
|
371
|
+
status: status({ state: "offline", connectionCount: 0 }),
|
|
372
|
+
});
|
|
373
|
+
expect(withAppUpdate(offline, appUpdate()).updateAvailableOverlay).toBe(true);
|
|
374
|
+
});
|
|
375
|
+
});
|
|
376
|
+
|
|
288
377
|
describe("NetworkStatusIndicator", () => {
|
|
289
378
|
beforeEach(() => {
|
|
290
379
|
mockViewState = { availability: "loading" };
|
|
380
|
+
mockAppUpdate = undefined;
|
|
381
|
+
installUpdateAndRestart.mockReset();
|
|
382
|
+
installUpdateAndRestart.mockResolvedValue({ action: "installing" });
|
|
291
383
|
networkViewerSection("main");
|
|
292
384
|
getConnections.mockReset();
|
|
293
385
|
getConnections.mockResolvedValue({ connections: [] });
|
|
@@ -353,6 +445,32 @@ describe("NetworkStatusIndicator", () => {
|
|
|
353
445
|
expect(view.container.querySelector(".bg-danger.rounded-circle")).toBeTruthy();
|
|
354
446
|
});
|
|
355
447
|
|
|
448
|
+
it("shows a bottom-left update badge and an Update & restart action when an update is ready", async () => {
|
|
449
|
+
mockViewState = { availability: "ready", status: status({ state: "synced" }) };
|
|
450
|
+
const view = render(<NetworkStatusIndicator />);
|
|
451
|
+
expect(view.container.querySelector(".network-status-update-badge")).toBeNull();
|
|
452
|
+
|
|
453
|
+
mockAppUpdate = appUpdate();
|
|
454
|
+
view.rerender(<NetworkStatusIndicator />);
|
|
455
|
+
const badge = view.container.querySelector(".network-status-update-badge");
|
|
456
|
+
expect(badge).toHaveClass("bi-arrow-up-circle-fill", "text-warning");
|
|
457
|
+
expect(badge).toHaveStyle({ left: "-0.3rem", bottom: "-0.32rem" });
|
|
458
|
+
|
|
459
|
+
fireEvent.click(view.getByRole("button", { name: /Version 1\.1\.0 is ready to install/ }));
|
|
460
|
+
const section = view.getByTestId("app-update-section");
|
|
461
|
+
expect(section).toHaveTextContent("Installs automatically the next time Peers restarts");
|
|
462
|
+
fireEvent.click(view.getByRole("button", { name: /Update & restart/ }));
|
|
463
|
+
await waitFor(() => expect(installUpdateAndRestart).toHaveBeenCalledTimes(1));
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
it("omits the install-on-restart note when nothing is armed to install", async () => {
|
|
467
|
+
mockViewState = { availability: "ready", status: status({ state: "synced" }) };
|
|
468
|
+
mockAppUpdate = appUpdate({ installOnRestart: false, installOnQuitArmed: false });
|
|
469
|
+
const view = render(<NetworkStatusIndicator />);
|
|
470
|
+
await openNetworkDetails(view, /ready to install/);
|
|
471
|
+
expect(view.getByTestId("app-update-section")).not.toHaveTextContent("Installs automatically");
|
|
472
|
+
});
|
|
473
|
+
|
|
356
474
|
it("shows a yellow disconnected-groups count when synced with offline groups", () => {
|
|
357
475
|
mockViewState = {
|
|
358
476
|
availability: "ready",
|