@celljs/ta-web 4.18.0 → 4.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/esm/browser/account/account-side.d.ts +13 -0
  2. package/esm/browser/account/account-side.js +74 -0
  3. package/esm/browser/account/device-account.d.ts +69 -0
  4. package/esm/browser/account/device-account.js +215 -0
  5. package/esm/browser/components/account-menu.d.ts +1 -1
  6. package/esm/browser/components/account-menu.js +5 -2
  7. package/esm/browser/components/account-view.d.ts +17 -0
  8. package/esm/browser/components/account-view.js +216 -0
  9. package/esm/browser/components/app-view.js +2 -2
  10. package/esm/browser/components/cloud-sign-in-view.d.ts +5 -1
  11. package/esm/browser/components/cloud-sign-in-view.js +1 -1
  12. package/esm/browser/components/host-view.d.ts +3 -1
  13. package/esm/browser/components/host-view.js +7 -0
  14. package/esm/browser/components/workbench-view.js +5 -1
  15. package/esm/browser/hosts.d.ts +5 -0
  16. package/esm/browser/hosts.js +11 -0
  17. package/esm/browser/i18n/locales.d.ts +28 -3
  18. package/esm/browser/i18n/messages-ja.js +25 -0
  19. package/esm/browser/i18n/messages-zh-tw.js +25 -0
  20. package/esm/browser/i18n/messages.d.ts +25 -0
  21. package/esm/browser/i18n/messages.js +50 -0
  22. package/esm/browser/style/dialogs.d.ts +1 -1
  23. package/esm/browser/style/dialogs.js +31 -0
  24. package/esm/browser/style/phone.d.ts +1 -1
  25. package/esm/browser/style/phone.js +2 -1
  26. package/esm/browser/workbench-hosts.d.ts +7 -0
  27. package/esm/browser/workbench-hosts.js +20 -0
  28. package/esm/browser/workbench-protocol.d.ts +8 -1
  29. package/esm/browser/workbench-services.d.ts +2 -1
  30. package/esm/browser/workbench-services.js +3 -0
  31. package/esm/browser/workbench-util.d.ts +1 -1
  32. package/lib/browser/account/account-side.d.ts +14 -0
  33. package/lib/browser/account/account-side.js +111 -0
  34. package/lib/browser/account/device-account.d.ts +70 -0
  35. package/lib/browser/account/device-account.js +219 -0
  36. package/lib/browser/components/account-menu.d.ts +1 -1
  37. package/lib/browser/components/account-menu.js +4 -1
  38. package/lib/browser/components/account-view.d.ts +18 -0
  39. package/lib/browser/components/account-view.js +255 -0
  40. package/lib/browser/components/app-view.js +1 -1
  41. package/lib/browser/components/cloud-sign-in-view.d.ts +5 -1
  42. package/lib/browser/components/cloud-sign-in-view.js +1 -0
  43. package/lib/browser/components/host-view.d.ts +3 -1
  44. package/lib/browser/components/host-view.js +8 -0
  45. package/lib/browser/components/workbench-view.js +5 -1
  46. package/lib/browser/hosts.d.ts +5 -0
  47. package/lib/browser/hosts.js +11 -0
  48. package/lib/browser/i18n/locales.d.ts +28 -3
  49. package/lib/browser/i18n/messages-ja.js +25 -0
  50. package/lib/browser/i18n/messages-zh-tw.js +25 -0
  51. package/lib/browser/i18n/messages.d.ts +25 -0
  52. package/lib/browser/i18n/messages.js +50 -0
  53. package/lib/browser/style/dialogs.d.ts +1 -1
  54. package/lib/browser/style/dialogs.js +31 -0
  55. package/lib/browser/style/phone.d.ts +1 -1
  56. package/lib/browser/style/phone.js +2 -1
  57. package/lib/browser/workbench-hosts.d.ts +7 -0
  58. package/lib/browser/workbench-hosts.js +20 -0
  59. package/lib/browser/workbench-protocol.d.ts +8 -1
  60. package/lib/browser/workbench-services.d.ts +2 -1
  61. package/lib/browser/workbench-services.js +3 -0
  62. package/lib/browser/workbench-util.d.ts +1 -1
  63. package/package.json +7 -7
@@ -0,0 +1,70 @@
1
+ import type { TaAccountInfo } from '@celljs/ta-protocol';
2
+ import { AccountAgent, type AccountKept, type AccountView, type AccountWay } from '@celljs/ta-relay';
3
+ import type { SavedHost } from '../workbench-protocol';
4
+ /** What a page does about the account: of its ta serve (a computer), or of the device itself (the app). */
5
+ export interface AccountSide {
6
+ read(): Promise<TaAccountInfo>;
7
+ signIn(provider: string, service?: string): Promise<TaAccountInfo>;
8
+ cancelSignIn(): Promise<TaAccountInfo>;
9
+ approve(id: string): Promise<TaAccountInfo>;
10
+ remove(id: string): Promise<TaAccountInfo>;
11
+ signOut(): Promise<TaAccountInfo>;
12
+ }
13
+ /** The computers a device's account brings, kept with the device's computers (TaWorkbench). */
14
+ export interface AccountHosts {
15
+ readonly state: {
16
+ hosts: SavedHost[];
17
+ };
18
+ addAccountHost(link: string, account: string): Promise<void>;
19
+ forgetAccountHosts(account?: string): void;
20
+ }
21
+ /**
22
+ * The app as a device of the user's account (docs/design/ta-account.md): its own keys in localStorage; the computers
23
+ * of the account come into its list of computers as they let it in, and go as they are removed. The same agent as a
24
+ * computer's (AccountAgent), with the hooks of a device that pairs with computers rather than letting devices in.
25
+ */
26
+ export declare class DeviceAccount implements AccountSide {
27
+ protected readonly hosts: AccountHosts;
28
+ protected readonly service: () => Promise<string>;
29
+ protected readonly gateways: () => Promise<string[]>;
30
+ protected static instance?: DeviceAccount;
31
+ protected agent?: AccountAgent;
32
+ protected view?: AccountView;
33
+ protected signingIn?: {
34
+ way: AccountWay;
35
+ handle: string;
36
+ code: string;
37
+ url: string;
38
+ interval: number;
39
+ expiresAt: number;
40
+ };
41
+ protected error?: string;
42
+ protected timer?: ReturnType<typeof setTimeout>;
43
+ protected running?: Promise<void>;
44
+ protected constructor(hosts: AccountHosts, service: () => Promise<string>, gateways: () => Promise<string[]>);
45
+ /** The one of the page. */
46
+ static of(hosts: AccountHosts, service: () => Promise<string>, gateways: () => Promise<string[]>): DeviceAccount;
47
+ /** What the device calls itself in the account. */
48
+ static deviceName(agent?: string): string;
49
+ protected static read(): AccountKept | undefined;
50
+ protected agentOf(kept: AccountKept): AccountAgent;
51
+ /** A Ta in the cloud signed in to: its token is the account of its gateway, which this device joins by itself. */
52
+ protected cloudToken(): {
53
+ service: string;
54
+ token: string;
55
+ } | undefined;
56
+ /** Joins the account of the gateway of a Ta in the cloud of this device, unless the user left the account. */
57
+ protected adopt(): Promise<boolean>;
58
+ protected forget(): void;
59
+ protected schedule(after: number): void;
60
+ protected round(): Promise<void>;
61
+ protected pollSignIn(): Promise<number>;
62
+ read(): Promise<TaAccountInfo>;
63
+ signIn(provider: string, service?: string): Promise<TaAccountInfo>;
64
+ cancelSignIn(): Promise<TaAccountInfo>;
65
+ protected act(run: (agent: AccountAgent) => Promise<AccountView>): Promise<TaAccountInfo>;
66
+ approve(id: string): Promise<TaAccountInfo>;
67
+ remove(id: string): Promise<TaAccountInfo>;
68
+ signOut(): Promise<TaAccountInfo>;
69
+ }
70
+ //# sourceMappingURL=device-account.d.ts.map
@@ -0,0 +1,219 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DeviceAccount = void 0;
4
+ const ta_relay_1 = require("@celljs/ta-relay");
5
+ const workbench_util_1 = require("../workbench-util");
6
+ const KEY = 'ta.account';
7
+ /** Signed out of the account by the user: not signed in again by itself with the token of a Ta in the cloud. */
8
+ const OFF_KEY = 'ta.account.off';
9
+ const SYNC_MS = 15000;
10
+ const WAITING_SYNC_MS = 3000;
11
+ /**
12
+ * The app as a device of the user's account (docs/design/ta-account.md): its own keys in localStorage; the computers
13
+ * of the account come into its list of computers as they let it in, and go as they are removed. The same agent as a
14
+ * computer's (AccountAgent), with the hooks of a device that pairs with computers rather than letting devices in.
15
+ */
16
+ class DeviceAccount {
17
+ constructor(hosts, service, gateways) {
18
+ this.hosts = hosts;
19
+ this.service = service;
20
+ this.gateways = gateways;
21
+ const kept = DeviceAccount.read();
22
+ if (kept) {
23
+ this.agent = this.agentOf(kept);
24
+ }
25
+ this.schedule(0);
26
+ document.addEventListener('visibilitychange', () => document.visibilityState === 'visible' && this.schedule(0));
27
+ }
28
+ /** The one of the page. */
29
+ static of(hosts, service, gateways) {
30
+ var _a;
31
+ (_a = DeviceAccount.instance) !== null && _a !== void 0 ? _a : (DeviceAccount.instance = new DeviceAccount(hosts, service, gateways));
32
+ return DeviceAccount.instance;
33
+ }
34
+ /** What the device calls itself in the account. */
35
+ static deviceName(agent = navigator.userAgent) {
36
+ return /iPad/.test(agent) ? 'iPad' : /iPhone/.test(agent) ? 'iPhone' : /Android/.test(agent) ? 'Android' : 'Phone';
37
+ }
38
+ static read() {
39
+ var _a, _b;
40
+ var _c;
41
+ try {
42
+ const kept = JSON.parse((_c = (_a = (0, workbench_util_1.storage)()) === null || _a === void 0 ? void 0 : _a.getItem(KEY)) !== null && _c !== void 0 ? _c : 'null');
43
+ return (kept === null || kept === void 0 ? void 0 : kept.token) && ((_b = kept.self) === null || _b === void 0 ? void 0 : _b.signKey) ? kept : undefined;
44
+ }
45
+ catch (_d) {
46
+ return undefined;
47
+ }
48
+ }
49
+ agentOf(kept) {
50
+ return new ta_relay_1.AccountAgent(kept, {
51
+ save: next => { var _a; return (_a = (0, workbench_util_1.storage)()) === null || _a === void 0 ? void 0 : _a.setItem(KEY, JSON.stringify(next)); },
52
+ name: () => DeviceAccount.deviceName(),
53
+ pairing: (from, link) => this.hosts.addAccountHost(link, from.id),
54
+ unpaired: from => this.hosts.forgetAccountHosts(from),
55
+ signedOut: () => {
56
+ var _a;
57
+ this.forget();
58
+ // A token no longer good, or this device removed: not joined again by itself.
59
+ (_a = (0, workbench_util_1.storage)()) === null || _a === void 0 ? void 0 : _a.setItem(OFF_KEY, '1');
60
+ },
61
+ });
62
+ }
63
+ /** A Ta in the cloud signed in to: its token is the account of its gateway, which this device joins by itself. */
64
+ cloudToken() {
65
+ for (const host of this.hosts.state.hosts) {
66
+ if ('token' in host.credential && host.credential.url) {
67
+ return { service: host.credential.url.replace(/\/+$/, ''), token: host.credential.token };
68
+ }
69
+ }
70
+ return undefined;
71
+ }
72
+ /** Joins the account of the gateway of a Ta in the cloud of this device, unless the user left the account. */
73
+ async adopt() {
74
+ var _a, _b;
75
+ const cloud = ((_a = (0, workbench_util_1.storage)()) === null || _a === void 0 ? void 0 : _a.getItem(OFF_KEY)) ? undefined : this.cloudToken();
76
+ if (!cloud || !(await ta_relay_1.AccountService.ways(cloud.service, [cloud.service])).some(one => one.gateway)) {
77
+ return false;
78
+ }
79
+ const kept = await ta_relay_1.AccountAgent.create(cloud.service, cloud.token, { id: '', name: '' }, 'phone', DeviceAccount.deviceName());
80
+ (_b = (0, workbench_util_1.storage)()) === null || _b === void 0 ? void 0 : _b.setItem(KEY, JSON.stringify(kept));
81
+ this.agent = this.agentOf(kept);
82
+ return true;
83
+ }
84
+ forget() {
85
+ var _a;
86
+ (_a = (0, workbench_util_1.storage)()) === null || _a === void 0 ? void 0 : _a.removeItem(KEY);
87
+ this.agent = undefined;
88
+ this.view = undefined;
89
+ this.hosts.forgetAccountHosts();
90
+ }
91
+ schedule(after) {
92
+ clearTimeout(this.timer);
93
+ if (!this.agent && !this.signingIn && !this.cloudToken()) {
94
+ return;
95
+ }
96
+ this.timer = setTimeout(() => void this.round(), after);
97
+ }
98
+ round() {
99
+ var _a;
100
+ (_a = this.running) !== null && _a !== void 0 ? _a : (this.running = (async () => {
101
+ let next = SYNC_MS;
102
+ try {
103
+ if (this.signingIn) {
104
+ next = await this.pollSignIn();
105
+ }
106
+ else if (this.agent || (await this.adopt())) {
107
+ const agent = this.agent;
108
+ this.view = await agent.sync();
109
+ this.error = undefined;
110
+ if (this.view.waiting || this.view.devices.some(one => one.waiting)) {
111
+ next = WAITING_SYNC_MS;
112
+ }
113
+ }
114
+ }
115
+ catch (error) {
116
+ this.error = error instanceof Error ? error.message : String(error);
117
+ }
118
+ finally {
119
+ this.running = undefined;
120
+ }
121
+ if (document.visibilityState !== 'hidden') {
122
+ this.schedule(next);
123
+ }
124
+ })());
125
+ return this.running;
126
+ }
127
+ async pollSignIn() {
128
+ var _a, _b;
129
+ const signing = this.signingIn;
130
+ if (Date.now() > signing.expiresAt) {
131
+ this.signingIn = undefined;
132
+ return SYNC_MS;
133
+ }
134
+ const answer = await ta_relay_1.AccountService.check(signing.way, signing.handle);
135
+ if (answer.status === 'pending') {
136
+ return signing.interval * 1000;
137
+ }
138
+ this.signingIn = undefined;
139
+ if (answer.status === 'failed') {
140
+ this.error = answer.error;
141
+ return SYNC_MS;
142
+ }
143
+ const kept = await ta_relay_1.AccountAgent.create(signing.way.service, answer.token, answer.account, 'phone', DeviceAccount.deviceName());
144
+ (_a = (0, workbench_util_1.storage)()) === null || _a === void 0 ? void 0 : _a.setItem(KEY, JSON.stringify(kept));
145
+ (_b = (0, workbench_util_1.storage)()) === null || _b === void 0 ? void 0 : _b.removeItem(OFF_KEY);
146
+ this.agent = this.agentOf(kept);
147
+ this.view = await this.agent.sync();
148
+ return WAITING_SYNC_MS;
149
+ }
150
+ async read() {
151
+ var _a, _b;
152
+ var _c, _d;
153
+ const service = (_d = (_c = (_a = this.agent) === null || _a === void 0 ? void 0 : _a.state.service) !== null && _c !== void 0 ? _c : (_b = this.signingIn) === null || _b === void 0 ? void 0 : _b.way.service) !== null && _d !== void 0 ? _d : (await this.service());
154
+ const error = this.error ? { error: this.error } : {};
155
+ if (!this.agent) {
156
+ const signing = this.signingIn;
157
+ return {
158
+ service,
159
+ providers: await ta_relay_1.AccountService.ways(service, await this.gateways().catch(() => [])).catch(() => []),
160
+ devices: [],
161
+ ...(signing ? { signingIn: { provider: signing.way.id, code: signing.code, url: signing.url, expiresAt: signing.expiresAt } } : {}),
162
+ ...error,
163
+ };
164
+ }
165
+ if (!this.view) {
166
+ await this.round();
167
+ }
168
+ const view = this.view;
169
+ if (!view) {
170
+ return { service, providers: [], account: this.agent.state.account, devices: [], ...error };
171
+ }
172
+ const devices = view.devices.map(({ trusted: _trusted, ...device }) => device);
173
+ return { service, providers: [], account: view.account, waiting: view.waiting, code: view.code, devices, ...error };
174
+ }
175
+ async signIn(provider, service) {
176
+ if (!this.agent) {
177
+ const ways = await ta_relay_1.AccountService.ways(await this.service(), await this.gateways().catch(() => []));
178
+ const way = ways.find(one => one.id === provider && (service === undefined || one.service === service));
179
+ if (!way) {
180
+ throw new ta_relay_1.AccountCallError(404, `No way to sign in with ${provider} here.`);
181
+ }
182
+ const start = await ta_relay_1.AccountService.begin(way, DeviceAccount.deviceName());
183
+ this.signingIn = { way, ...start, expiresAt: Date.now() + start.expiresIn * 1000 };
184
+ this.error = undefined;
185
+ this.schedule(start.interval * 1000);
186
+ }
187
+ return this.read();
188
+ }
189
+ async cancelSignIn() {
190
+ this.signingIn = undefined;
191
+ return this.read();
192
+ }
193
+ async act(run) {
194
+ if (!this.agent) {
195
+ throw new ta_relay_1.AccountCallError(401, 'Sign in to an account first.');
196
+ }
197
+ this.view = await run(this.agent);
198
+ this.schedule(WAITING_SYNC_MS);
199
+ return this.read();
200
+ }
201
+ approve(id) {
202
+ return this.act(agent => agent.approve(id));
203
+ }
204
+ remove(id) {
205
+ return this.act(agent => agent.remove(id));
206
+ }
207
+ async signOut() {
208
+ var _a;
209
+ if (this.agent) {
210
+ await this.agent.signOut();
211
+ this.forget();
212
+ (_a = (0, workbench_util_1.storage)()) === null || _a === void 0 ? void 0 : _a.setItem(OFF_KEY, '1');
213
+ }
214
+ this.error = undefined;
215
+ return this.read();
216
+ }
217
+ }
218
+ exports.DeviceAccount = DeviceAccount;
219
+ //# sourceMappingURL=device-account.js.map
@@ -2,7 +2,7 @@ import { type LucideIcon } from 'lucide-react';
2
2
  import * as React from 'react';
3
3
  import type { SettingsSection } from './settings-view';
4
4
  /** What an entry of the account opens over the page. */
5
- export type AccountDialog = 'accounts' | 'directory' | 'relay' | 'hosts';
5
+ export type AccountDialog = 'accounts' | 'directory' | 'relay' | 'hosts' | 'devices';
6
6
  export interface AccountChoice {
7
7
  key: string;
8
8
  label: string;
@@ -46,6 +46,7 @@ const hooks_1 = require("../hooks");
46
46
  const hosts_1 = require("../hosts");
47
47
  const use_t_1 = require("../i18n/use-t");
48
48
  const workbench_util_1 = require("../workbench-util");
49
+ const account_view_1 = require("./account-view");
49
50
  const auth_view_1 = require("./auth-view");
50
51
  const dino_look_picker_1 = require("./dino-look-picker");
51
52
  const dismiss_1 = require("./dismiss");
@@ -93,6 +94,7 @@ function useAccountEntries(onSettings, onDialog) {
93
94
  ...(touch ? [] : [entry('shortcuts', t('ta.manage.shortcuts'), lucide_react_1.Keyboard, () => onSettings('shortcuts'))]),
94
95
  ],
95
96
  [
97
+ entry('devices', t('ta.myDevices.title'), lucide_react_1.MonitorSmartphone, () => onDialog('devices')),
96
98
  entry('hosts', t('ta.hosts.title'), lucide_react_1.Monitor, () => onDialog('hosts'), { detail: inUse && (hosts_1.HostUtil.label(inUse) || t('ta.hosts.thisServer')) }),
97
99
  entry('accounts', t('ta.manage.accounts'), lucide_react_1.KeyRound, () => onDialog('accounts')),
98
100
  entry('directory', t('ta.manage.workingDirectory'), lucide_react_1.FolderOpen, () => onDialog('directory')),
@@ -136,7 +138,8 @@ function AccountDialogs({ dialog, onClose }) {
136
138
  dialog === 'accounts' && React.createElement(auth_view_1.AuthDialog, { onClose: onClose }),
137
139
  dialog === 'directory' && React.createElement(working_directory_1.WorkingDirectoryPicker, { onClose: onClose }),
138
140
  dialog === 'relay' && React.createElement(relay_view_1.RelayDialog, { onClose: onClose }),
139
- dialog === 'hosts' && React.createElement(host_view_1.HostsDialog, { onClose: onClose })));
141
+ dialog === 'hosts' && React.createElement(host_view_1.HostsDialog, { onClose: onClose }),
142
+ dialog === 'devices' && React.createElement(account_view_1.DevicesDialog, { onClose: onClose })));
140
143
  }
141
144
  /** The round figure of the account: a close-up of the double, the dinosaur the user picked for it (a T. rex until then). */
142
145
  function AccountFigure({ size, className }) {
@@ -0,0 +1,18 @@
1
+ import * as React from 'react';
2
+ import type { AccountSide } from '../account/device-account';
3
+ /** What the account panel shows: signing in, waiting, or the devices. */
4
+ export declare function AccountPanel({ side }: {
5
+ side: AccountSide;
6
+ }): React.JSX.Element;
7
+ /** The account and its devices, over the page: from Me, the menu of the account, the settings. */
8
+ export declare function DevicesDialog({ onClose }: {
9
+ onClose(): void;
10
+ }): React.ReactPortal;
11
+ /**
12
+ * A device asks to join the account: whichever device of it the user has at hand says so, with the code to compare,
13
+ * until it is let in, refused, or put off for now.
14
+ */
15
+ export declare function AccountAsk(): React.ReactPortal | null;
16
+ /** On the sign-in of the app: sign in to the account, and the computers of it come by themselves. */
17
+ export declare function AccountSignInEntry(): React.JSX.Element | null;
18
+ //# sourceMappingURL=account-view.d.ts.map
@@ -0,0 +1,255 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.AccountPanel = AccountPanel;
37
+ exports.DevicesDialog = DevicesDialog;
38
+ exports.AccountAsk = AccountAsk;
39
+ exports.AccountSignInEntry = AccountSignInEntry;
40
+ const lucide_react_1 = require("lucide-react");
41
+ const React = __importStar(require("react"));
42
+ const react_dom_1 = require("react-dom");
43
+ const account_side_1 = require("../account/account-side");
44
+ const back_stack_1 = require("../device/back-stack");
45
+ const console_util_1 = require("../gateway/console-util");
46
+ const hooks_1 = require("../hooks");
47
+ const use_t_1 = require("../i18n/use-t");
48
+ const workbench_util_1 = require("../workbench-util");
49
+ const busy_1 = require("./busy");
50
+ const callout_1 = require("./callout");
51
+ const cloud_sign_in_view_1 = require("./cloud-sign-in-view");
52
+ const icon_1 = require("./icon");
53
+ const loading_1 = require("./loading");
54
+ const sign_in_marks_1 = require("./sign-in-marks");
55
+ /*
56
+ * The user's account and its devices (docs/design/ta-account.md): signing in, waiting to be let in, letting a new
57
+ * device in after comparing its code, and the devices with whether they are online. The same on a computer (its ta
58
+ * serve's account) and in the app (the phone's own).
59
+ */
60
+ const KIND_ICONS = { computer: lucide_react_1.Laptop, phone: lucide_react_1.Smartphone, browser: lucide_react_1.Globe };
61
+ /** The code of six digits, split in two for the eye: 482 913. */
62
+ function codeText(code) {
63
+ return `${code.slice(0, 3)} ${code.slice(3)}`;
64
+ }
65
+ function providerName(info, id) {
66
+ var _a;
67
+ var _b;
68
+ return (_b = (_a = info.providers.find(one => one.id === id)) === null || _a === void 0 ? void 0 : _a.name) !== null && _b !== void 0 ? _b : (id === 'github' ? 'GitHub' : id);
69
+ }
70
+ /** Signing in: the ways of the service, then the code to enter on the provider's page. */
71
+ function SignInPart({ info, act }) {
72
+ const t = (0, use_t_1.useT)();
73
+ const [busy, run] = (0, busy_1.useBusy)();
74
+ const [, notices] = (0, hooks_1.useNotices)();
75
+ const go = (action) => void run(() => act(action).catch((error) => notices.error(error)));
76
+ const signing = info.signingIn;
77
+ if (signing) {
78
+ const name = providerName(info, signing.provider);
79
+ return (React.createElement("div", { className: "ta-account-signing" },
80
+ React.createElement("p", null, t('ta.myDevices.enterCode', { name })),
81
+ React.createElement(cloud_sign_in_view_1.CodeBox, { code: signing.code }),
82
+ React.createElement("button", { type: "button", className: "ta-button ta-primary ta-wide", onClick: () => window.open(signing.url, '_blank', 'noopener,noreferrer') },
83
+ React.createElement(sign_in_marks_1.SignInMark, { provider: signing.provider, size: 16 }),
84
+ " ",
85
+ t('ta.myDevices.openProvider', { name })),
86
+ React.createElement("button", { type: "button", className: "ta-link-button", onClick: () => go(side => side.cancelSignIn()) }, t('ta.cancel'))));
87
+ }
88
+ if (info.providers.length === 0) {
89
+ return React.createElement(loading_1.Loading, { compact: true });
90
+ }
91
+ return (React.createElement("div", { className: "ta-account-ways" }, info.providers.map(provider => (React.createElement("button", { key: `${provider.service} ${provider.id}`, type: "button", className: busy ? 'ta-button ta-primary ta-wide ta-busy' : 'ta-button ta-primary ta-wide', "aria-busy": busy || undefined, onClick: () => go(async (side) => {
92
+ const next = await side.signIn(provider.id, provider.service);
93
+ if (next.signingIn) {
94
+ window.open(next.signingIn.url, '_blank', 'noopener,noreferrer');
95
+ }
96
+ return next;
97
+ }) },
98
+ React.createElement(sign_in_marks_1.SignInMark, { provider: provider.id, size: 16 }),
99
+ " ",
100
+ t('ta.myDevices.signInWith', { name: provider.name }),
101
+ React.createElement(busy_1.BusyMark, { busy: busy }))))));
102
+ }
103
+ /** This device waits to be let in by another of the account: the code both show. */
104
+ function WaitingPart({ code }) {
105
+ const t = (0, use_t_1.useT)();
106
+ return (React.createElement("div", { className: "ta-account-waiting" },
107
+ React.createElement("strong", null, t('ta.myDevices.waitingTitle')),
108
+ React.createElement("div", { className: "ta-account-code", role: "img", "aria-label": code }, codeText(code)),
109
+ React.createElement("p", null, t('ta.myDevices.waitingHint'))));
110
+ }
111
+ function DeviceRow({ device, onApprove, onRemove, onOpen }) {
112
+ var _a;
113
+ const t = (0, use_t_1.useT)();
114
+ const language = (0, use_t_1.useLanguage)();
115
+ const detail = device.waiting
116
+ ? `${t('ta.myDevices.waiting')} · ${t('ta.myDevices.code', { code: codeText((_a = device.code) !== null && _a !== void 0 ? _a : '') })}`
117
+ : device.self
118
+ ? t('ta.myDevices.thisDevice')
119
+ : device.online
120
+ ? t('ta.myDevices.online')
121
+ : t('ta.myDevices.seen', { time: console_util_1.ConsoleUtil.ago(device.seenAt, language) });
122
+ return (React.createElement("li", { className: `ta-account-device${device.waiting ? ' ta-waiting' : ''}` },
123
+ React.createElement("span", { className: "ta-account-device-icon", "aria-hidden": "true" },
124
+ React.createElement(icon_1.Icon, { icon: KIND_ICONS[device.kind], size: 18 }),
125
+ !device.waiting && React.createElement("span", { className: `ta-account-dot${device.online ? ' ta-on' : ''}` })),
126
+ React.createElement("span", { className: "ta-account-device-text" },
127
+ React.createElement("span", { className: "ta-ellipsis" }, device.name),
128
+ React.createElement("span", { className: "ta-account-device-detail ta-ellipsis" }, detail)),
129
+ device.waiting ? (React.createElement("span", { className: "ta-account-device-actions" },
130
+ React.createElement("button", { type: "button", className: "ta-button", onClick: onRemove }, t('ta.myDevices.notMe')),
131
+ React.createElement("button", { type: "button", className: "ta-button ta-primary", onClick: onApprove }, t('ta.myDevices.allow')))) : (React.createElement("span", { className: "ta-account-device-actions" },
132
+ onOpen && (React.createElement("button", { type: "button", className: "ta-button", onClick: onOpen }, t('ta.myDevices.open'))),
133
+ !device.self && React.createElement(icon_1.IconButton, { icon: lucide_react_1.Trash2, label: t('ta.myDevices.remove'), onClick: onRemove })))));
134
+ }
135
+ /** What the account panel shows: signing in, waiting, or the devices. */
136
+ function AccountPanel({ side }) {
137
+ var _a;
138
+ const t = (0, use_t_1.useT)();
139
+ const [{ hosts, host }, bench] = (0, hooks_1.useWorkbench)();
140
+ const [, notices] = (0, hooks_1.useNotices)();
141
+ const { info, failed, act } = (0, account_side_1.useAccountInfo)(side);
142
+ const app = workbench_util_1.WorkbenchUtil.isApp();
143
+ const go = (action) => void act(action).catch((error) => notices.error(error));
144
+ if (!info) {
145
+ return failed ? React.createElement(callout_1.Callout, { level: "error" }, String((_a = failed.message) !== null && _a !== void 0 ? _a : failed)) : React.createElement(loading_1.Loading, { compact: true });
146
+ }
147
+ if (!info.account) {
148
+ return (React.createElement(React.Fragment, null,
149
+ React.createElement("p", { className: "ta-account-hint" }, t('ta.myDevices.hint')),
150
+ info.error && React.createElement(callout_1.Callout, { level: "error" }, info.error),
151
+ React.createElement(SignInPart, { info: info, act: act })));
152
+ }
153
+ const remove = async (device) => {
154
+ if (device.waiting ||
155
+ (await notices.confirm({ title: t('ta.myDevices.removeConfirm', { name: device.name }), confirmLabel: t('ta.myDevices.remove'), danger: true }))) {
156
+ go(one => one.remove(device.id));
157
+ }
158
+ };
159
+ const signOut = async () => {
160
+ if (await notices.confirm({
161
+ title: t(app ? 'ta.myDevices.signOutConfirm' : 'ta.myDevices.signOutConfirmComputer'),
162
+ confirmLabel: t('ta.myDevices.signOut'),
163
+ danger: true,
164
+ })) {
165
+ go(one => one.signOut());
166
+ }
167
+ };
168
+ /** In the app, a computer of the account is one of the list: a tap switches to it. */
169
+ const openOf = (device) => {
170
+ if (device.self || device.kind !== 'computer') {
171
+ return undefined;
172
+ }
173
+ const saved = app ? hosts.find(one => one.account === device.id) : undefined;
174
+ if (saved) {
175
+ return saved.id === host ? undefined : () => bench.switchTo(saved.id);
176
+ }
177
+ return undefined;
178
+ };
179
+ const devices = [...info.devices].sort((a, b) => Number(b.waiting) - Number(a.waiting) || Number(b.self) - Number(a.self) || a.addedAt - b.addedAt);
180
+ return (React.createElement(React.Fragment, null,
181
+ info.waiting && info.code ? React.createElement(WaitingPart, { code: info.code }) : null,
182
+ info.error && React.createElement(callout_1.Callout, { level: "error" }, info.error),
183
+ !info.waiting && (React.createElement("ul", { className: "ta-account-devices" }, devices.map(device => (React.createElement(DeviceRow, { key: device.id, device: device, onApprove: () => go(one => one.approve(device.id)), onRemove: () => void remove(device), onOpen: openOf(device) }))))),
184
+ !info.waiting && devices.length < 2 && React.createElement("p", { className: "ta-account-hint" }, t('ta.myDevices.none')),
185
+ React.createElement("div", { className: "ta-account-foot" },
186
+ React.createElement("span", { className: "ta-account-who" },
187
+ React.createElement(sign_in_marks_1.SignInMark, { provider: info.account.id.split(':')[0], size: 14 }),
188
+ React.createElement("span", { className: "ta-ellipsis" }, info.account.name)),
189
+ React.createElement("button", { type: "button", className: "ta-button", onClick: () => void signOut() },
190
+ React.createElement(icon_1.Icon, { icon: lucide_react_1.LogOut, size: 14 }),
191
+ " ",
192
+ t('ta.myDevices.signOut')))));
193
+ }
194
+ /** The account and its devices, over the page: from Me, the menu of the account, the settings. */
195
+ function DevicesDialog({ onClose }) {
196
+ const t = (0, use_t_1.useT)();
197
+ const side = (0, account_side_1.useAccountSide)();
198
+ (0, back_stack_1.useBackLayer)(true, onClose);
199
+ React.useEffect(() => {
200
+ const onKey = (event) => event.key === 'Escape' && onClose();
201
+ document.addEventListener('keydown', onKey);
202
+ return () => document.removeEventListener('keydown', onKey);
203
+ }, [onClose]);
204
+ return (0, react_dom_1.createPortal)(React.createElement("div", { className: "ta-modal-scrim" },
205
+ React.createElement("button", { type: "button", className: "ta-overlay-close", "aria-label": t('ta.close'), tabIndex: -1, onClick: onClose }),
206
+ React.createElement("div", { className: "ta-modal ta-account-dialog", role: "dialog", "aria-modal": "true", "aria-label": t('ta.myDevices.title') },
207
+ React.createElement("div", { className: "ta-modal-head" },
208
+ React.createElement("span", { className: "ta-modal-title" }, t('ta.myDevices.title')),
209
+ React.createElement(icon_1.IconButton, { icon: lucide_react_1.X, label: t('ta.close'), onClick: onClose })),
210
+ side ? React.createElement(AccountPanel, { side: side }) : React.createElement(loading_1.Loading, { compact: true }))), document.body);
211
+ }
212
+ /**
213
+ * A device asks to join the account: whichever device of it the user has at hand says so, with the code to compare,
214
+ * until it is let in, refused, or put off for now.
215
+ */
216
+ function AccountAsk() {
217
+ var _a;
218
+ const t = (0, use_t_1.useT)();
219
+ const side = (0, account_side_1.useAccountSide)();
220
+ const [, notices] = (0, hooks_1.useNotices)();
221
+ const { info, act } = (0, account_side_1.useAccountInfo)(side, 5000);
222
+ const [later, setLater] = React.useState([]);
223
+ const asking = (info === null || info === void 0 ? void 0 : info.account) && !info.waiting ? info.devices.find(one => one.waiting && !later.includes(one.id)) : undefined;
224
+ if (!asking) {
225
+ return null;
226
+ }
227
+ const go = (action) => void act(action).catch((error) => notices.error(error));
228
+ return (0, react_dom_1.createPortal)(React.createElement("div", { className: "ta-account-ask", role: "alertdialog", "aria-label": t('ta.myDevices.askTitle', { name: asking.name }) },
229
+ React.createElement("span", { className: "ta-account-device-icon", "aria-hidden": "true" },
230
+ React.createElement(icon_1.Icon, { icon: KIND_ICONS[asking.kind], size: 20 })),
231
+ React.createElement("div", { className: "ta-account-ask-text" },
232
+ React.createElement("strong", { className: "ta-ellipsis" }, t('ta.myDevices.askTitle', { name: asking.name })),
233
+ React.createElement("span", { className: "ta-account-code" }, codeText((_a = asking.code) !== null && _a !== void 0 ? _a : '')),
234
+ React.createElement("span", { className: "ta-account-hint" }, t('ta.myDevices.askHint'))),
235
+ React.createElement("div", { className: "ta-account-ask-actions" },
236
+ React.createElement("button", { type: "button", className: "ta-link-button", onClick: () => setLater(before => [...before, asking.id]) }, t('ta.myDevices.later')),
237
+ React.createElement("button", { type: "button", className: "ta-button", onClick: () => go(one => one.remove(asking.id)) }, t('ta.myDevices.notMe')),
238
+ React.createElement("button", { type: "button", className: "ta-button ta-primary", onClick: () => go(one => one.approve(asking.id)) }, t('ta.myDevices.allow')))), document.body);
239
+ }
240
+ /** On the sign-in of the app: sign in to the account, and the computers of it come by themselves. */
241
+ function AccountSignInEntry() {
242
+ const t = (0, use_t_1.useT)();
243
+ const side = (0, account_side_1.useAccountSide)();
244
+ const [open, setOpen] = React.useState(false);
245
+ const { info } = (0, account_side_1.useAccountInfo)(side);
246
+ if (!side || !info || (!info.account && info.providers.length === 0 && !info.signingIn)) {
247
+ return null;
248
+ }
249
+ if (open || info.account || info.signingIn) {
250
+ return (React.createElement("section", { className: "ta-account-sign-in" },
251
+ React.createElement(AccountPanel, { side: side })));
252
+ }
253
+ return (React.createElement("button", { type: "button", className: "ta-button ta-wide", onClick: () => setOpen(true) }, t('ta.myDevices.signInAccount')));
254
+ }
255
+ //# sourceMappingURL=account-view.js.map
@@ -184,7 +184,7 @@ function AppShell() {
184
184
  React.createElement(link_dialog_1.LinkDialog, null),
185
185
  React.createElement(remote_prompts_1.RemotePrompts, null),
186
186
  React.createElement(serve_restart_1.ServeRestartNotices, null),
187
- React.createElement(host_view_1.PageVeil, null),
187
+ React.createElement(host_view_1.DeviceOverlays, null),
188
188
  codeScan && React.createElement(scan_screen_1.ScanHost, { onCode: text => void codeScan.open(text), onAsk: askDouble }),
189
189
  React.createElement(share_dialog_1.ShareHost, null),
190
190
  React.createElement(custom_host_1.CustomHost, null),