@x-tech-laboratories/x-app 0.119.0 → 0.121.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 (42) hide show
  1. package/README.md +21 -2
  2. package/dist/app/daemon.d.ts +7 -1
  3. package/dist/app/daemon.js +76 -8
  4. package/dist/app/define.d.ts +22 -1
  5. package/dist/app/define.js +34 -1
  6. package/dist/app/desktop.d.ts +13 -0
  7. package/dist/app/desktop.js +35 -3
  8. package/dist/app/host_tables.d.ts +20 -0
  9. package/dist/app/host_tables.js +91 -0
  10. package/dist/app/preload.js +5 -0
  11. package/dist/app/protocol.d.ts +29 -1
  12. package/dist/app/protocol.js +8 -1
  13. package/dist/app/remote_access.d.ts +17 -0
  14. package/dist/app/remote_access.js +85 -0
  15. package/dist/app/server.d.ts +12 -4
  16. package/dist/app/server.js +63 -14
  17. package/dist/app/services.d.ts +15 -0
  18. package/dist/app/web/android.d.ts +1 -1
  19. package/dist/app/web/android.js +9 -0
  20. package/dist/app/web/app_page.js +43 -6
  21. package/dist/app/web/hosts.d.ts +4 -2
  22. package/dist/app/web/hosts.js +33 -0
  23. package/dist/app/web/page.d.ts +3 -0
  24. package/dist/app/web/page.js +24 -4
  25. package/dist/app/web/portal.d.ts +14 -0
  26. package/dist/app/web/portal.js +115 -0
  27. package/dist/app/web/settings_page.d.ts +5 -1
  28. package/dist/app/web/settings_page.js +25 -6
  29. package/dist/change_checks.js +1 -1
  30. package/dist/github_targets.d.ts +12 -0
  31. package/dist/github_targets.js +90 -0
  32. package/dist/remote_access_protocol.d.ts +26 -0
  33. package/dist/remote_access_protocol.js +33 -0
  34. package/dist/server/github_webhook.d.ts +2 -12
  35. package/dist/server/github_webhook.js +2 -87
  36. package/dist/sql.d.ts +5 -0
  37. package/dist/sql.js +21 -0
  38. package/dist/web/react/remote_access_settings.d.ts +5 -0
  39. package/dist/web/react/remote_access_settings.js +55 -0
  40. package/dist/web/remote_daemon.d.ts +26 -0
  41. package/dist/web/remote_daemon.js +105 -0
  42. package/package.json +1 -1
@@ -4,18 +4,19 @@ import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-run
4
4
  // ── Imports ──────────────────────────────────────────────────────────────────
5
5
  import "@x-tech-laboratories/halon/halon-tokens.css";
6
6
  import "@x-tech-laboratories/halon/halon.css";
7
- import { Fragment, useEffect, useMemo, useRef } from "react";
7
+ import { Fragment, useEffect, useMemo, useRef, useState } from "react";
8
8
  import { createRoot } from "react-dom/client";
9
9
  import { create_accounts_client } from "../../web/accounts.js";
10
10
  import { AccountView, UsersView } from "../../web/react/accounts.js";
11
11
  import { SyncCertificatePrompt } from "../../web/react/certificates.js";
12
+ import { ConnectionSettings } from "../../web/react/connections.js";
12
13
  import { PageHeading } from "../../web/react/layout.js";
13
14
  import { Shell, use_route } from "../../web/react/shell.js";
14
15
  import { SignInForm, use_account } from "../../web/react/sign_in.js";
15
16
  import { SyncButton } from "../../web/react/sync_settings.js";
16
17
  import { local_theme_storage, theme_choice } from "../../web/theme.js";
17
18
  import { desktop_api, is_android, is_desktop, web_api } from "./hosts.js";
18
- import { calls, data_watch, page, set_page_api, use_settings } from "./page.js";
19
+ import { calls, data_watch, page, set_page_api, set_page_reach, use_settings } from "./page.js";
19
20
  import { SettingsView } from "./settings_page.js";
20
21
  // ── Pages ────────────────────────────────────────────────────────────────────
21
22
  // Relative, so a server behind a proxy under a folder keeps it.
@@ -82,7 +83,7 @@ function Pages({ pages, account }) {
82
83
  const children = shown?.children && shown.children.length > 1 ? shown.children : [];
83
84
  const subpage = children.find((child) => child.id === rest[0]) ?? children[0];
84
85
  const label = shown ? (subpage ? `${subpage.label} · ${shown.label}` : shown.label) : is_account ? "Your account" : "Settings";
85
- const view_key = route.parts.join("/");
86
+ const view_key = shown?.view_key ? [section, shown.view_key(rest)].join("/") : route.parts.join("/");
86
87
  const chrome = pages.use_chrome?.(route.parts) ?? {};
87
88
  const theme = use_theme(app);
88
89
  useEffect(() => {
@@ -110,15 +111,44 @@ function Pages({ pages, account }) {
110
111
  // A browser reaches the server's data, so it signs in; the desktop and phone apps keep their own and sign in only to sync.
111
112
  function BrowserApp({ pages }) {
112
113
  const { account, refresh, sign_out } = use_account(accounts);
114
+ // What the server lets this viewer reach is read again once they are signed in, as it was read before.
115
+ const [reach_read, set_reach_read] = useState(null);
116
+ const signed_in_as = account.state === "signed_in" ? account.me.name : null;
117
+ useEffect(() => {
118
+ if (signed_in_as === null || reach_read === signed_in_as)
119
+ return;
120
+ void (page().reread_abilities?.() ?? Promise.resolve({})).then((reach) => {
121
+ set_page_reach(reach);
122
+ set_reach_read(signed_in_as);
123
+ });
124
+ }, [signed_in_as, reach_read]);
113
125
  if (account.state === "checking")
114
126
  return null;
127
+ if (signed_in_as !== null && reach_read !== signed_in_as && page().reread_abilities)
128
+ return null;
115
129
  if (account.state === "signed_out")
116
130
  return _jsx(SignInForm, { client: accounts, title: pages.app.name, icon: pages.brand_icon ?? "icon.svg", on_signed_in: refresh });
117
131
  return _jsx(Pages, { pages: pages, account: { me: account.me, refresh, sign_out } });
118
132
  }
133
+ // A phone that keeps no data and has no host yet adds one before anything else.
134
+ function FirstConnection({ pages }) {
135
+ const { connections, certificates } = page();
136
+ const { app } = pages;
137
+ return (_jsxs(Shell, { brand: app.name, brand_icon: pages.brand_icon ?? "icon.svg", theme: theme_choice(local_theme_storage(`${app.id}-theme`)), children: [_jsx(PageHeading, { title: `Connect to ${app.name}`, subtitle: `Add the computer this phone works on, with its address and an admin's name and password. Turn on Let other devices connect in its Settings first.` }), _jsx(ConnectionSettings, { client: connections, certificates: certificates, placeholder: app.server_example })] }));
138
+ }
119
139
  // A device that syncs asks to trust its server's certificate whenever sync fails over it.
120
140
  function DeviceApp({ pages }) {
121
- const { certificates, device } = page();
141
+ const { certificates, connections, device, host } = page();
142
+ const portal = host === "android" && pages.app.phone === "portal" && connections !== undefined;
143
+ const [has_host, set_has_host] = useState(portal ? null : true);
144
+ useEffect(() => {
145
+ if (portal)
146
+ void connections.list_connections().then((view) => set_has_host(view.current !== null), () => set_has_host(true));
147
+ }, [portal, connections]);
148
+ if (has_host === null)
149
+ return null;
150
+ if (!has_host)
151
+ return _jsx(FirstConnection, { pages: pages });
122
152
  return _jsxs(_Fragment, { children: [device && certificates && _jsx(SyncCertificatePrompt, { device: device, certificates: certificates, subscribe: (listener) => data_watch.subscribe(listener) }), _jsx(Pages, { pages: pages, account: null })] });
123
153
  }
124
154
  export function AppView({ pages }) {
@@ -126,9 +156,14 @@ export function AppView({ pages }) {
126
156
  }
127
157
  // ── Start ────────────────────────────────────────────────────────────────────
128
158
  // The phone's code loads only on the phone, so a browser never fetches SQLite.
129
- async function host_api(app, phone_services) {
159
+ async function host_api(app, pages) {
130
160
  if (is_desktop(app))
131
161
  return desktop_api(app);
162
+ if (is_android() && app.phone === "portal") {
163
+ const [{ portal_api }, { App: CapacitorApp }] = await Promise.all([import("./portal.js"), import("@capacitor/app")]);
164
+ return portal_api(app, { lifecycle: CapacitorApp, ...(pages.phone_notifications ? { notifications: await pages.phone_notifications() } : {}) });
165
+ }
166
+ const phone_services = pages.phone_services;
132
167
  if (is_android()) {
133
168
  const { android_api } = await import("./android.js");
134
169
  return android_api(app, phone_services ? { services: await phone_services() } : {});
@@ -137,7 +172,9 @@ async function host_api(app, phone_services) {
137
172
  }
138
173
  // Starts the page on whichever host it runs: the desktop app, the phone app or a browser.
139
174
  export async function render_app(pages, root = document.getElementById("app")) {
140
- const api = await host_api(pages.app, pages.phone_services);
175
+ const api = await host_api(pages.app, pages);
176
+ if (api.reread_abilities)
177
+ Object.assign(api, await api.reread_abilities());
141
178
  set_page_api(api);
142
179
  // A link the desktop app was opened with, or a tray row, opens its page.
143
180
  api.on_event?.("open", (route) => {
@@ -1,6 +1,8 @@
1
1
  import type { App } from "../define.ts";
2
- import { type PageApi } from "../protocol.ts";
2
+ import { type HostAbilities, type PageApi } from "../protocol.ts";
3
+ export declare function read_host_abilities(read: () => Promise<unknown>): Promise<HostAbilities>;
4
+ export declare function host_tables_of(app: Pick<App, "bridges">, abilities: HostAbilities, client_of: (name: string) => unknown): NonNullable<PageApi["tables"]>;
3
5
  export declare function web_api(app: Pick<App, "bridges">): PageApi;
4
6
  export declare function is_desktop(app: Pick<App, "bridges">): boolean;
5
- export declare function desktop_api(app: Pick<App, "bridges"> & Partial<Pick<App, "local_mcp">>): Promise<PageApi>;
7
+ export declare function desktop_api(app: Pick<App, "bridges"> & Partial<Pick<App, "local_mcp" | "remote_access">>): Promise<PageApi>;
6
8
  export declare function is_android(): boolean;
@@ -4,6 +4,22 @@
4
4
  import { LOCAL_CONNECTION_ID } from "../../connection_protocol.js";
5
5
  import { LoginNeeded } from "../../web/states.js";
6
6
  import { data_change_of } from "../protocol.js";
7
+ // ── Host tables ──────────────────────────────────────────────────────────────
8
+ // What the host lets the page reach, or nothing while it cannot be asked, such as before signing in.
9
+ export async function read_host_abilities(read) {
10
+ try {
11
+ const found = (await read());
12
+ return { admin: found?.admin === true, tables: Array.isArray(found?.tables) ? found.tables.filter((name) => typeof name === "string") : [] };
13
+ }
14
+ catch {
15
+ return { admin: false, tables: [] };
16
+ }
17
+ }
18
+ // The host tables the abilities name, each a client built by the host's own way of calling.
19
+ export function host_tables_of(app, abilities, client_of) {
20
+ const known = abilities.tables.filter((name) => Object.hasOwn(app.bridges.host_tables, name));
21
+ return Object.fromEntries(known.map((name) => [name, client_of(name)]));
22
+ }
7
23
  // ── Web ──────────────────────────────────────────────────────────────────────
8
24
  // Relative, so a server behind a proxy under a folder keeps it.
9
25
  const call_path = "api/call/";
@@ -11,6 +27,7 @@ const backups_path = "api/backups/";
11
27
  // The server's own sync calls sit beside the route paired devices sync through.
12
28
  const device_path = "api/sync/";
13
29
  const events_path = "api/events";
30
+ const host_path = "api/host/";
14
31
  const client_id_header = "x-client-id";
15
32
  const reconnect_delay_ms = 5000;
16
33
  // A greeting after the first means the stream reconnected and may have missed changes, so the page reloads too.
@@ -72,8 +89,18 @@ export function web_api(app) {
72
89
  listener(change);
73
90
  }, (given) => (client_id = given), named);
74
91
  const { bridges } = app;
92
+ // What this viewer reaches on the server, read again once they sign in.
93
+ const read_abilities = () => read_host_abilities(() => post_call(host_path, "get_host_abilities", []));
94
+ const reach = (abilities) => ({
95
+ admin: abilities.admin,
96
+ tables: host_tables_of(app, abilities, (name) => bridges.host_tables[name].web_rpc((method, args) => post_call(`api/${name}/`, method, args))),
97
+ });
75
98
  return {
76
99
  host: "web",
100
+ // None until read, which render_app does before the page shows, and again once the viewer signs in.
101
+ tables: {},
102
+ admin: false,
103
+ reread_abilities: async () => reach(await read_abilities()),
77
104
  calls: bridges.calls.web_rpc((method, args) => post_call(call_path, method, args)),
78
105
  // The server's own sync peer and backups, which only an admin reaches.
79
106
  device: bridges.device.web_rpc((method, args) => post_call(device_path, method, args)),
@@ -104,8 +131,14 @@ export async function desktop_api(app) {
104
131
  const events = exposed[bridges.events.api_name];
105
132
  const connections = exposed[bridges.connections.api_name];
106
133
  const is_here = (await connections.list_connections()).current === LOCAL_CONNECTION_ID;
134
+ const host = exposed[bridges.host.api_name];
135
+ const abilities = await read_host_abilities(async () => host?.get_host_abilities());
107
136
  return {
108
137
  host: "desktop",
138
+ admin: abilities.admin,
139
+ tables: host_tables_of(app, abilities, (name) => exposed[bridges.host_tables[name].api_name]),
140
+ daemon_is_here: is_here,
141
+ ...(is_here && app.remote_access && exposed[bridges.remote_access.api_name] ? { remote_access: exposed[bridges.remote_access.api_name] } : {}),
109
142
  calls: exposed[bridges.calls.api_name],
110
143
  // Sync belongs to this computer's own daemon, so it shows only while that daemon is in use.
111
144
  device: is_here ? exposed[bridges.device.api_name] : null,
@@ -4,8 +4,10 @@ import type { PageApi } from "../protocol.ts";
4
4
  type AnyMethods = Record<string, (...args: never[]) => unknown>;
5
5
  export declare const data_watch: import("../../web/watch.ts").Watch;
6
6
  export declare function set_page_api<Methods extends Record<keyof Methods, (...args: never[]) => unknown>>(api: PageApi<Methods>): void;
7
+ export declare function set_page_reach(reach: Pick<PageApi, "admin" | "tables">): void;
7
8
  export declare function page(): PageApi;
8
9
  export declare function calls(): Client<AnyMethods>;
10
+ export declare function host_table<Methods extends Record<keyof Methods, (...args: never[]) => unknown> = AnyMethods>(name: string): Client<Methods> | null;
9
11
  export declare function use_data<Data>(load: (api: Client<AnyMethods>) => Promise<Data>, dependencies?: unknown[]): Resource<Data>;
10
12
  export declare function use_settings(): Resource<Record<string, unknown>>;
11
13
  export declare function page_hooks<Methods extends Record<keyof Methods, (...args: never[]) => unknown>>(): {
@@ -13,5 +15,6 @@ export declare function page_hooks<Methods extends Record<keyof Methods, (...arg
13
15
  calls: () => Client<Methods>;
14
16
  use_data: <Data>(load: (api: Client<Methods>) => Promise<Data>, dependencies?: unknown[]) => Resource<Data>;
15
17
  use_settings: typeof use_settings;
18
+ host_table: typeof host_table;
16
19
  };
17
20
  export {};
@@ -6,9 +6,9 @@ import { is_read_only } from "../define.js";
6
6
  // Bumped after each write this page makes and each change from elsewhere.
7
7
  export const data_watch = create_watch();
8
8
  let page_api = null;
9
- // A write tells this page's views too, since the host only tells the other pages.
10
- export function set_page_api(api) {
11
- const calls = Object.fromEntries(Object.entries(api.calls).map(([name, call]) => [
9
+ // Each call of a table, a write telling this page's views too, since the host only tells the other pages.
10
+ function watched(table) {
11
+ return Object.fromEntries(Object.entries(table).map(([name, call]) => [
12
12
  name,
13
13
  async (...args) => {
14
14
  const result = await call(...args);
@@ -17,9 +17,23 @@ export function set_page_api(api) {
17
17
  return result;
18
18
  },
19
19
  ]));
20
- page_api = { ...api, calls };
20
+ }
21
+ let following_changes = false;
22
+ export function set_page_api(api) {
23
+ const tables = api.tables ? Object.fromEntries(Object.entries(api.tables).map(([name, table]) => [name, watched(table)])) : undefined;
24
+ page_api = { ...api, calls: watched(api.calls), ...(tables ? { tables } : {}) };
25
+ // Set again when what the host lets the page reach changed, such as after signing in, but followed once.
26
+ if (following_changes)
27
+ return;
28
+ following_changes = true;
21
29
  api.on_change(() => data_watch.bump());
22
30
  }
31
+ // What the host lets the page reach, read again, such as once a browser signed in.
32
+ export function set_page_reach(reach) {
33
+ const api = page();
34
+ const tables = reach.tables ? Object.fromEntries(Object.entries(reach.tables).map(([name, table]) => [name, watched(table)])) : api.tables;
35
+ page_api = { ...api, ...(reach.admin === undefined ? {} : { admin: reach.admin }), ...(tables ? { tables } : {}) };
36
+ }
23
37
  export function page() {
24
38
  if (!page_api)
25
39
  throw new Error("the page's api has not been set up");
@@ -29,6 +43,11 @@ export function page() {
29
43
  export function calls() {
30
44
  return page().calls;
31
45
  }
46
+ // One of the app's host tables, such as host_table("stages"), or null where the host does not answer it or does not
47
+ // let this viewer reach it.
48
+ export function host_table(name) {
49
+ return page().tables?.[name] ?? null;
50
+ }
32
51
  // ── Hooks ────────────────────────────────────────────────────────────────────
33
52
  // Loads with the page's calls and loads again quietly whenever the data changed.
34
53
  export function use_data(load, dependencies = []) {
@@ -45,5 +64,6 @@ export function page_hooks() {
45
64
  calls: calls,
46
65
  use_data: use_data,
47
66
  use_settings,
67
+ host_table,
48
68
  };
49
69
  }
@@ -0,0 +1,14 @@
1
+ import { type LocalNotificationsLike } from "../../phone_notices.ts";
2
+ import type { App } from "../define.ts";
3
+ import { type AppNotice, type PageApi } from "../protocol.ts";
4
+ import type { PhoneLifecycle } from "./android.ts";
5
+ export declare function read_notice_switches(app: Pick<App, "desktop_settings">, saved: string | null): Record<string, boolean>;
6
+ export declare function phone_notice_shower(plugin: LocalNotificationsLike, open_link: (url: string) => void): (notice: AppNotice) => void;
7
+ export interface PortalOptions {
8
+ lifecycle: PhoneLifecycle;
9
+ notifications?: LocalNotificationsLike;
10
+ storage?: Pick<Storage, "getItem" | "setItem">;
11
+ fetch?: typeof fetch;
12
+ reload?: () => void;
13
+ }
14
+ export declare function portal_api(app: App, options: PortalOptions): Promise<PageApi>;
@@ -0,0 +1,115 @@
1
+ // The phone's page for an app whose phone keeps no data: a window onto a daemon or server on another computer, chosen
2
+ // under Settings → Connections and signed in once as this phone. Every call goes there with the phone's token, its
3
+ // events keep the page current, and the notices that host tells of show as phone notifications where this phone's
4
+ // switch for their kind is on. Only the phone build loads it.
5
+ // ── Imports ──────────────────────────────────────────────────────────────────
6
+ import { create_connections } from "../../connections.js";
7
+ import { notice_id } from "../../phone_notices.js";
8
+ import { phone_certificates } from "../../phone_certificates.js";
9
+ import { local_connection_storage } from "../../web/connection_storage.js";
10
+ import { call_remote, follow_remote } from "../../web/remote_daemon.js";
11
+ import { route_of_link } from "../links.js";
12
+ import { data_change_of, notice_event, notice_is_on } from "../protocol.js";
13
+ import { host_tables_of, read_host_abilities } from "./hosts.js";
14
+ // ── Switches ─────────────────────────────────────────────────────────────────
15
+ // The notice switches this phone keeps, each of the app's `notify_` desktop switches at its default where unset.
16
+ export function read_notice_switches(app, saved) {
17
+ let parsed = {};
18
+ try {
19
+ parsed = JSON.parse(saved ?? "{}");
20
+ }
21
+ catch {
22
+ parsed = {};
23
+ }
24
+ const notice_keys = Object.entries(app.desktop_settings ?? {}).filter(([key]) => key.startsWith("notify_"));
25
+ // Saved by kind, as notify_<kind> or by the kind alone, as an older phone kept them.
26
+ return Object.fromEntries(notice_keys.map(([key, spec]) => {
27
+ const value = parsed[key] ?? parsed[key.slice("notify_".length)];
28
+ return [key, typeof value === "boolean" ? value : spec.default];
29
+ }));
30
+ }
31
+ // ── Notices ──────────────────────────────────────────────────────────────────
32
+ // Shows notices at once without asking for exact alarms, which would open a settings screen; a tap opens the page its
33
+ // link names.
34
+ export function phone_notice_shower(plugin, open_link) {
35
+ void plugin.addListener("localNotificationActionPerformed", (action) => open_link(String(action.notification.extra?.url ?? "")));
36
+ void plugin.checkPermissions().then(({ display }) => (display === "granted" || display === "denied" ? undefined : plugin.requestPermissions()));
37
+ return (notice) => void plugin.schedule({ notifications: [{ id: notice_id(notice.key), title: notice.title, body: notice.body, extra: { url: notice.url }, isExactNotification: false }] }).catch((error) => console.error("could not show a notice", error));
38
+ }
39
+ export async function portal_api(app, options) {
40
+ const storage = options.storage ?? localStorage;
41
+ const switches_key = `${app.id}-notice-kinds`;
42
+ const connections = create_connections({ storage: local_connection_storage(`${app.id}-connections`), offers_local: false, device_name: `${app.name} on Android` });
43
+ let client_id = "";
44
+ const link = {
45
+ daemon: () => {
46
+ const current = connections.current();
47
+ return current?.kind === "remote" ? { address: current.address, token: current.token } : null;
48
+ },
49
+ client_id: () => client_id,
50
+ ...(options.fetch ? { fetch: options.fetch } : {}),
51
+ };
52
+ // Another host chosen, or one added first: the page starts again on it.
53
+ connections.subscribe(() => (options.reload ?? (() => location.reload()))());
54
+ const listeners = new Set();
55
+ const named = new Map();
56
+ const announce = (change = null) => {
57
+ for (const listener of listeners)
58
+ listener(change);
59
+ };
60
+ const rpc = (path) => (method, args) => call_remote(link, `/api/${path}/${method}`, args);
61
+ const { bridges } = app;
62
+ const abilities = link.daemon() ? await read_host_abilities(() => call_remote(link, "/api/host/get_host_abilities", [])) : { admin: false, tables: [] };
63
+ const switches = () => read_notice_switches(app, storage.getItem(switches_key));
64
+ const open_link = (url) => {
65
+ const route = app.link_scheme ? route_of_link(app.link_scheme, url) : null;
66
+ if (route)
67
+ location.hash = route;
68
+ };
69
+ const show = options.notifications ? phone_notice_shower(options.notifications, open_link) : null;
70
+ if (link.daemon()) {
71
+ follow_remote(link, (event, data) => {
72
+ if (event === "hello")
73
+ client_id = data;
74
+ else if (event === "changed")
75
+ announce(data_change_of(data));
76
+ else {
77
+ const parsed = data === "" ? null : JSON.parse(data);
78
+ if (event === notice_event && show && notice_is_on(parsed.kind, switches()))
79
+ show(parsed);
80
+ for (const listener of named.get(event) ?? [])
81
+ listener(parsed);
82
+ }
83
+ },
84
+ // A reconnect may have missed changes, so the page reads again.
85
+ { on_connect: () => announce() });
86
+ // Back from the background, the page catches up on what it missed.
87
+ options.lifecycle.addListener("resume", () => announce());
88
+ }
89
+ return {
90
+ host: "android",
91
+ calls: bridges.calls.web_rpc(rpc("call")),
92
+ device: null,
93
+ // The host's backups, which only an admin there reaches.
94
+ backups: abilities.admin ? bridges.backups.web_rpc(rpc("backups")) : null,
95
+ connections: connections.methods,
96
+ certificates: phone_certificates(),
97
+ admin: abilities.admin,
98
+ tables: host_tables_of(app, abilities, (name) => bridges.host_tables[name].web_rpc(rpc(name))),
99
+ notice_switches: {
100
+ get: async () => switches(),
101
+ set: async (key, on) => storage.setItem(switches_key, JSON.stringify({ ...switches(), [key]: on })),
102
+ },
103
+ on_change: (listener) => {
104
+ listeners.add(listener);
105
+ return () => listeners.delete(listener);
106
+ },
107
+ on_event: (event, listener) => {
108
+ let kept = named.get(event);
109
+ if (!kept)
110
+ named.set(event, (kept = new Set()));
111
+ kept.add(listener);
112
+ return () => void kept.delete(listener);
113
+ },
114
+ };
115
+ }
@@ -1,6 +1,7 @@
1
1
  import type { IconGlyph } from "../../web/react/icons.tsx";
2
2
  import { type SettingsCategory, type SettingsSection } from "../../web/react/layout.tsx";
3
3
  import type { ShellSidePane, SidebarWidth } from "../../web/react/shell.tsx";
4
+ import type { LocalNotificationsLike } from "../../phone_notices.ts";
4
5
  import type { App } from "../define.ts";
5
6
  import type { StartPhoneServices } from "./android.ts";
6
7
  export interface PageSettingsProps {
@@ -16,6 +17,7 @@ export interface AppPage {
16
17
  use_badge?: () => number | string | null;
17
18
  in_sidebar?: boolean;
18
19
  active_as?: string;
20
+ view_key?: (parts: string[]) => string;
19
21
  children?: readonly AppSubpage[];
20
22
  }
21
23
  export interface AppSubpage {
@@ -33,7 +35,7 @@ export interface AppChrome {
33
35
  sidebar_width?: SidebarWidth;
34
36
  }
35
37
  export interface AppPages {
36
- app: Pick<App, "id" | "name" | "data_name" | "server_example" | "bridges"> & Partial<Pick<App, "desktop_settings" | "synced_theme">>;
38
+ app: Pick<App, "id" | "name" | "data_name" | "server_example" | "bridges"> & Partial<Pick<App, "desktop_settings" | "synced_theme" | "phone">>;
37
39
  pages: readonly AppPage[];
38
40
  general_settings?: (props: PageSettingsProps) => SettingsSection[];
39
41
  settings_categories?: (props: PageSettingsProps) => SettingsCategory[];
@@ -41,6 +43,8 @@ export interface AppPages {
41
43
  default_page?: string;
42
44
  use_chrome?: (parts: readonly string[]) => AppChrome;
43
45
  phone_services?: () => Promise<StartPhoneServices>;
46
+ phone_notifications?: () => Promise<LocalNotificationsLike>;
47
+ remote_access_reach?: string;
44
48
  }
45
49
  export declare function SettingsView({ pages, is_admin }: {
46
50
  pages: AppPages;
@@ -10,6 +10,7 @@ import { LocalMcpSettings } from "../../web/react/local_mcp_settings.js";
10
10
  import { Switch } from "../../web/react/controls.js";
11
11
  import { PageHeading, SettingRow, SettingsPage } from "../../web/react/layout.js";
12
12
  import { Loaded } from "../../web/react/states.js";
13
+ import { RemoteAccessSettings } from "../../web/react/remote_access_settings.js";
13
14
  import { PeerListeningSettings, SyncSettings } from "../../web/react/sync_settings.js";
14
15
  import { attempt } from "../../web/react/toasts.js";
15
16
  import { calls, data_watch, page, use_settings } from "./page.js";
@@ -43,17 +44,35 @@ function DataFolder({ desktop }) {
43
44
  return null;
44
45
  return (_jsx(SettingRow, { label: "Database", hint: _jsx("span", { className: "path", children: paths.database }), children: _jsx("button", { type: "button", onClick: () => attempt(desktop.show_data_path("database")), children: "Show in folder" }) }));
45
46
  }
46
- // A desktop window on a server's daemon works on the server's data, as a browser does.
47
- function storage_category(app, is_admin) {
48
- const { backups, backup_folder, certificates, connections, desktop, device, host, local_mcp, peer_listening } = page();
47
+ // Which kinds of notice this phone shows, of those the host it works on tells of.
48
+ function NoticeSwitches({ app, switches }) {
49
+ const [values, set_values] = useState(null);
50
+ useEffect(() => void switches.get().then(set_values, () => undefined), [switches]);
51
+ if (!values)
52
+ return null;
53
+ const notice_specs = Object.entries(app.desktop_settings ?? {}).filter(([key]) => key.startsWith("notify_"));
54
+ return (_jsx(_Fragment, { children: notice_specs.map(([key, spec]) => (_jsx(SettingRow, { label: spec.label, ...(spec.hint ? { hint: spec.hint } : {}), as_label: true, children: _jsx(Switch, { label: spec.label, checked: values[key] ?? spec.default, on_change: (on) => {
55
+ set_values({ ...values, [key]: on });
56
+ attempt(switches.set(key, on));
57
+ } }) }, key))) }));
58
+ }
59
+ // A desktop window on a server's daemon, or a phone that keeps no data, works on the host's data, as a browser does.
60
+ function storage_category(pages, is_admin) {
61
+ const { app } = pages;
62
+ const { backups, backup_folder, certificates, connections, desktop, device, host, local_mcp, notice_switches, peer_listening, remote_access } = page();
49
63
  const { data_name, name } = app;
50
- const on_server = host === "web" || (host === "desktop" && !device);
64
+ const on_server = host === "web" || !device;
51
65
  const sections = [];
52
66
  if (connections) {
53
- sections.push({ id: "connections", heading: "Connections", hint: `The ${data_name} this window works on: This computer's own, or an ${name} server's.`, content: _jsxs(_Fragment, { children: [_jsx(ConnectionSettings, { client: connections, certificates: certificates, placeholder: app.server_example }), !device && certificates && _jsx(TrustedCertificates, { certificates: certificates })] }) });
67
+ const hint = host === "android" ? `The ${name} daemon or server this phone works on, signed in once as this phone.` : `The ${data_name} this window works on: This computer's own, or an ${name} server's.`;
68
+ sections.push({ id: "connections", heading: "Connections", hint, content: _jsxs(_Fragment, { children: [_jsx(ConnectionSettings, { client: connections, certificates: certificates, placeholder: app.server_example }), !device && certificates && _jsx(TrustedCertificates, { certificates: certificates })] }) });
54
69
  }
55
70
  if (desktop)
56
71
  sections.push({ id: "desktop", heading: "Desktop app", content: _jsx(Desktop, { app_name: name, own: app.desktop_settings ?? {} }) });
72
+ if (notice_switches)
73
+ sections.push({ id: "notices", heading: "Notifications", hint: `What this phone tells you of from the ${name} it works on.`, content: _jsx(NoticeSwitches, { app: app, switches: notice_switches }) });
74
+ if (remote_access)
75
+ sections.push({ id: "remote", heading: "Other devices", hint: `Let phones, browsers and other computers work on this computer's ${data_name}.`, content: _jsx(RemoteAccessSettings, { remote: remote_access, ...(pages.remote_access_reach ? { reach: pages.remote_access_reach } : {}) }) });
57
76
  if (local_mcp)
58
77
  sections.push({ id: "mcp", heading: "MCP server", hint: `Let AI assistants such as Claude Code reach this computer's ${data_name} without the server.`, content: _jsx(LocalMcpSettings, { device: local_mcp, name: name }) });
59
78
  if (device && (!on_server || is_admin)) {
@@ -84,7 +103,7 @@ export function SettingsView({ pages, is_admin = true }) {
84
103
  ...pages.pages
85
104
  .map((each) => ({ heading: each.label, sections: each.settings?.(props) ?? [] }))
86
105
  .filter((category) => category.sections.length > 0),
87
- ...storage_category(pages.app, is_admin),
106
+ ...storage_category(pages, is_admin && page().admin !== false),
88
107
  ] }));
89
108
  } })] }));
90
109
  }
@@ -10,7 +10,7 @@ export const x_tech_options = {
10
10
  commit_trailer: "Assisted-by",
11
11
  trailer_organisations: ["x-tech-laboratories"],
12
12
  ai_mention: /\b(claude|anthropic|co-authored-by|generated with|ai[- ]generated|chatgpt|copilot)\b/i,
13
- wrapped_libraries: ["drizzle-orm", "@tanstack/react-table", "lucide-react", "marked", "dompurify", "@tiptap/", "fflate", "rrule", "frimousse", "squarify", "react-error-boundary", "es-module-lexer", "hookable", "parse-duration", "pretty-bytes", "emojibase", "emojibase-data", "@sqlite.org/sqlite-wasm", "@modelcontextprotocol/server", "@fastify/session", "nodemailer", "sshpk"],
13
+ wrapped_libraries: ["drizzle-orm", "@tanstack/react-table", "lucide-react", "marked", "dompurify", "@tiptap/", "fflate", "rrule", "frimousse", "squarify", "react-error-boundary", "es-module-lexer", "hookable", "parse-duration", "pretty-bytes", "emojibase", "emojibase-data", "@sqlite.org/sqlite-wasm", "@modelcontextprotocol/server", "@fastify/session", "nodemailer", "sshpk", "diff", "parse-diff"],
14
14
  wrapping_repositories: ["x-tech-laboratories/X-App"],
15
15
  allowed_licences: ["0BSD", "MIT", "MIT-0", "ISC", "BSD-2-Clause", "BSD-3-Clause", "Apache-2.0", "Zlib", "Unlicense", "CC0-1.0", "CC-BY-3.0", "CC-BY-4.0", "BlueOak-1.0.0", "Python-2.0", "Artistic-2.0", "MPL-2.0", "LGPL-2.1-or-later", "LGPL-3.0-or-later", "GPL-3.0-only", "GPL-3.0-or-later"],
16
16
  };
@@ -0,0 +1,12 @@
1
+ export declare const github_target_events: readonly ["pull_request", "check_suite", "check_run", "status", "workflow_run", "issues", "issue_comment"];
2
+ export interface WebhookTargets {
3
+ repository: string;
4
+ pull_requests: number[];
5
+ branches: string[];
6
+ issues: number[];
7
+ }
8
+ export declare function is_repository_name(name: string): boolean;
9
+ export declare function repository_of(payload: unknown): string | null;
10
+ export declare function delivery_id_of(header: unknown): string;
11
+ export declare function targets_of(event: string, payload: unknown): WebhookTargets | null;
12
+ export declare function read_targets(repository: string, stored: string): WebhookTargets;
@@ -0,0 +1,90 @@
1
+ // GitHub's webhook deliveries as any part of an app reads them, the same in a page, a phone and Node: the events that
2
+ // name what changed, the repository and the pull requests, branches and issues a payload names, and a delivery's id.
3
+ // server/github_webhook receives them.
4
+ // ── Shapes ───────────────────────────────────────────────────────────────────
5
+ // The events that name pull requests, branches or issues, for the app to tick on GitHub.
6
+ export const github_target_events = ["pull_request", "check_suite", "check_run", "status", "workflow_run", "issues", "issue_comment"];
7
+ const repository_pattern = /^[A-Za-z0-9_.-]{1,100}\/[A-Za-z0-9_.-]{1,100}$/;
8
+ const branch_pattern = /^[A-Za-z0-9._/-]{1,255}$/;
9
+ const delivery_pattern = /^[A-Za-z0-9-]{1,100}$/;
10
+ const most_targets = 50;
11
+ function fields_of(value) {
12
+ return typeof value === "object" && value !== null && !Array.isArray(value) ? value : {};
13
+ }
14
+ function number_of(value) {
15
+ return typeof value === "number" && Number.isInteger(value) && value > 0 && value < 2 ** 31 ? value : null;
16
+ }
17
+ function branch_of(value) {
18
+ return typeof value === "string" && branch_pattern.test(value) && !value.includes("..") && !value.startsWith("-") ? value : null;
19
+ }
20
+ // The pull request numbers in a list such as check_suite.pull_requests, left out where they are not plain numbers.
21
+ function numbers_in(list) {
22
+ return (Array.isArray(list) ? list : []).map((entry) => number_of(fields_of(entry).number)).filter((number) => number !== null);
23
+ }
24
+ // Whether a name is a repository as owner/name.
25
+ export function is_repository_name(name) {
26
+ return repository_pattern.test(name);
27
+ }
28
+ // The repository a payload names as owner/name, or null when it names none that looks like one.
29
+ export function repository_of(payload) {
30
+ const name = fields_of(fields_of(payload).repository).full_name;
31
+ return typeof name === "string" && repository_pattern.test(name) ? name : null;
32
+ }
33
+ // The delivery id as GitHub sends it, or "" when it is missing or not an id.
34
+ export function delivery_id_of(header) {
35
+ return typeof header === "string" && delivery_pattern.test(header) ? header : "";
36
+ }
37
+ // The repository and the pull requests, branches and issues a delivery names, or null; nothing else in it is kept.
38
+ export function targets_of(event, payload) {
39
+ const repository = repository_of(payload);
40
+ if (repository === null)
41
+ return null;
42
+ const values = fields_of(payload);
43
+ const pull_requests = [];
44
+ const branches = [];
45
+ const issues = [];
46
+ if (event === "ping") {
47
+ // Only says the hook works.
48
+ }
49
+ else if (event === "pull_request") {
50
+ const pull = fields_of(values.pull_request);
51
+ const head = fields_of(pull.head);
52
+ pull_requests.push(number_of(pull.number) ?? number_of(values.number));
53
+ // A fork's branch is not one of the repository's own, so only a branch of the same repository counts.
54
+ if (fields_of(head.repo).full_name === repository)
55
+ branches.push(branch_of(head.ref));
56
+ }
57
+ else if (event === "check_suite" || event === "workflow_run") {
58
+ const suite = fields_of(values[event]);
59
+ branches.push(branch_of(suite.head_branch));
60
+ pull_requests.push(...numbers_in(suite.pull_requests));
61
+ }
62
+ else if (event === "check_run") {
63
+ const run = fields_of(values.check_run);
64
+ branches.push(branch_of(fields_of(run.check_suite).head_branch));
65
+ pull_requests.push(...numbers_in(run.pull_requests));
66
+ }
67
+ else if (event === "status") {
68
+ branches.push(...(Array.isArray(values.branches) ? values.branches : []).map((branch) => branch_of(fields_of(branch).name)));
69
+ }
70
+ else if (event === "issues" || event === "issue_comment") {
71
+ issues.push(number_of(fields_of(values.issue).number));
72
+ }
73
+ else
74
+ return null;
75
+ const kept = (list) => [...new Set(list.filter((value) => value !== null))].slice(0, most_targets);
76
+ return { repository, pull_requests: kept(pull_requests), branches: kept(branches), issues: kept(issues) };
77
+ }
78
+ // A delivery's targets as stored, each list left empty where the stored text is damaged.
79
+ export function read_targets(repository, stored) {
80
+ let fields = {};
81
+ try {
82
+ fields = fields_of(JSON.parse(stored));
83
+ }
84
+ catch {
85
+ fields = {};
86
+ }
87
+ const numbers = (value) => (Array.isArray(value) ? value.map(number_of).filter((number) => number !== null) : []);
88
+ const names = Array.isArray(fields.branches) ? fields.branches.map(branch_of).filter((branch) => branch !== null) : [];
89
+ return { repository, pull_requests: numbers(fields.pull_requests), branches: names, issues: numbers(fields.issues) };
90
+ }
@@ -0,0 +1,26 @@
1
+ export interface RemoteAccessSettings {
2
+ enabled: boolean;
3
+ host: string;
4
+ port: number;
5
+ certificate_file: string;
6
+ key_file: string;
7
+ }
8
+ export declare function remote_access_defaults(port: number): RemoteAccessSettings;
9
+ export declare function read_remote_access(saved: unknown, default_port: number): RemoteAccessSettings;
10
+ export declare function remote_access_problem(settings: RemoteAccessSettings, admins: string[]): string;
11
+ export interface RemoteAccessStatus {
12
+ settings: RemoteAccessSettings;
13
+ admins: string[];
14
+ listening: string;
15
+ problem: string;
16
+ }
17
+ export interface RemoteAccessMethods {
18
+ get_remote_access: () => RemoteAccessStatus;
19
+ set_remote_access: (settings: RemoteAccessSettings) => Promise<RemoteAccessStatus>;
20
+ set_remote_admin: (name: string, password: string) => Promise<RemoteAccessStatus>;
21
+ remove_remote_admin: (name: string) => Promise<RemoteAccessStatus>;
22
+ }
23
+ export declare const remote_access_method_names: readonly ["get_remote_access", "set_remote_access", "set_remote_admin", "remove_remote_admin"];
24
+ export type RemoteAccessClient = {
25
+ [Name in keyof RemoteAccessMethods]: (...args: Parameters<RemoteAccessMethods[Name]>) => Promise<Awaited<ReturnType<RemoteAccessMethods[Name]>>>;
26
+ };