bom-pi 0.0.0-stage → 0.0.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/README.md +33 -2
- package/apps/README.md +7 -0
- package/apps/web/README.md +21 -0
- package/apps/web/pocket-adapter.mjs +323 -0
- package/apps/web/public/LICENSE.pocket +21 -0
- package/apps/web/public/activity-fold.js +28 -0
- package/apps/web/public/agent-now.js +37 -0
- package/apps/web/public/agent-tile.js +79 -0
- package/apps/web/public/app.js +975 -0
- package/apps/web/public/apple-touch-icon.png +0 -0
- package/apps/web/public/artifact-bridge.js +687 -0
- package/apps/web/public/avatar.js +87 -0
- package/apps/web/public/back.js +554 -0
- package/apps/web/public/box-keys.js +216 -0
- package/apps/web/public/browser-panel.js +1097 -0
- package/apps/web/public/browser.js +68 -0
- package/apps/web/public/calls.js +139 -0
- package/apps/web/public/chat.js +919 -0
- package/apps/web/public/clock.js +34 -0
- package/apps/web/public/commands.js +578 -0
- package/apps/web/public/composer.js +2068 -0
- package/apps/web/public/diff-parse.js +672 -0
- package/apps/web/public/diff.js +1646 -0
- package/apps/web/public/dock.js +299 -0
- package/apps/web/public/extension-ui.js +2734 -0
- package/apps/web/public/files-panel.js +775 -0
- package/apps/web/public/files.js +368 -0
- package/apps/web/public/fonts/FiraCode-VF.woff2 +0 -0
- package/apps/web/public/fonts/Geist.woff2 +0 -0
- package/apps/web/public/fonts/IBMPlexMono-Variable-Italic.woff2 +0 -0
- package/apps/web/public/fonts/IBMPlexMono-Variable.woff2 +0 -0
- package/apps/web/public/fonts/InterVariable-Italic.woff2 +0 -0
- package/apps/web/public/fonts/InterVariable.woff2 +0 -0
- package/apps/web/public/fonts/Iosevka-Bold.woff2 +0 -0
- package/apps/web/public/fonts/Iosevka-BoldItalic.woff2 +0 -0
- package/apps/web/public/fonts/Iosevka-Italic.woff2 +0 -0
- package/apps/web/public/fonts/Iosevka-Regular.woff2 +0 -0
- package/apps/web/public/fonts/Iosevka-SemiBold.woff2 +0 -0
- package/apps/web/public/fonts/Iosevka-SemiBoldItalic.woff2 +0 -0
- package/apps/web/public/fonts/JetBrainsMono-Italic.woff2 +0 -0
- package/apps/web/public/fonts/JetBrainsMono.woff2 +0 -0
- package/apps/web/public/fonts/NerdFontsSymbolsMono.woff2 +0 -0
- package/apps/web/public/fonts/SOURCES.md +69 -0
- package/apps/web/public/gestures.js +610 -0
- package/apps/web/public/highlight.js +480 -0
- package/apps/web/public/history.js +70 -0
- package/apps/web/public/home.js +169 -0
- package/apps/web/public/icon-192.png +0 -0
- package/apps/web/public/icon-512.png +0 -0
- package/apps/web/public/icon-maskable-512.png +0 -0
- package/apps/web/public/icon.svg +16 -0
- package/apps/web/public/index.html +71 -0
- package/apps/web/public/launcher.js +1293 -0
- package/apps/web/public/linked-sessions.js +55 -0
- package/apps/web/public/live.js +39 -0
- package/apps/web/public/manifest.webmanifest +32 -0
- package/apps/web/public/model-refresh.js +73 -0
- package/apps/web/public/notify.js +251 -0
- package/apps/web/public/peeks.js +920 -0
- package/apps/web/public/pip-attachments.js +182 -0
- package/apps/web/public/pip-window.js +141 -0
- package/apps/web/public/pip.js +1217 -0
- package/apps/web/public/quote-reply.js +109 -0
- package/apps/web/public/quote-text.js +31 -0
- package/apps/web/public/rich.js +403 -0
- package/apps/web/public/run-frame.js +106 -0
- package/apps/web/public/safe-mode.js +71 -0
- package/apps/web/public/seen.js +169 -0
- package/apps/web/public/session-tree.js +117 -0
- package/apps/web/public/sessions.js +2695 -0
- package/apps/web/public/share.js +126 -0
- package/apps/web/public/sheets/appearance.js +820 -0
- package/apps/web/public/sheets/branch.js +353 -0
- package/apps/web/public/sheets/extensions.js +818 -0
- package/apps/web/public/sheets/file.js +318 -0
- package/apps/web/public/sheets/find.js +250 -0
- package/apps/web/public/sheets/folder.js +680 -0
- package/apps/web/public/sheets/gallery.js +2340 -0
- package/apps/web/public/sheets/hooks.js +350 -0
- package/apps/web/public/sheets/memory.js +531 -0
- package/apps/web/public/sheets/menu.js +580 -0
- package/apps/web/public/sheets/message.js +220 -0
- package/apps/web/public/sheets/model.js +412 -0
- package/apps/web/public/sheets/people.js +414 -0
- package/apps/web/public/sheets/pi-sessions.js +264 -0
- package/apps/web/public/sheets/providers.js +525 -0
- package/apps/web/public/sheets/running.js +130 -0
- package/apps/web/public/sheets/schedules.js +71 -0
- package/apps/web/public/sheets/session.js +249 -0
- package/apps/web/public/sheets/shortcuts.js +101 -0
- package/apps/web/public/sheets/spend.js +150 -0
- package/apps/web/public/sheets/text.js +53 -0
- package/apps/web/public/sheets/viewers.js +363 -0
- package/apps/web/public/sheets/worktree.js +50 -0
- package/apps/web/public/sheets.js +410 -0
- package/apps/web/public/signin.js +57 -0
- package/apps/web/public/slots.js +208 -0
- package/apps/web/public/store.js +1760 -0
- package/apps/web/public/style.css +18625 -0
- package/apps/web/public/subagents-board.js +879 -0
- package/apps/web/public/subagents.js +273 -0
- package/apps/web/public/sw.js +169 -0
- package/apps/web/public/taps.js +26 -0
- package/apps/web/public/theme-surface.d.ts +4 -0
- package/apps/web/public/theme-surface.js +266 -0
- package/apps/web/public/theme.js +845 -0
- package/apps/web/public/themes/lantern/colors.toml +25 -0
- package/apps/web/public/themes/lantern/direction.css +60 -0
- package/apps/web/public/themes/lantern/frame.css +254 -0
- package/apps/web/public/themes/lantern/theme.css +10 -0
- package/apps/web/public/themes/lantern/theme.json +7 -0
- package/apps/web/public/themes/pocket/theme.css +189 -0
- package/apps/web/public/themes/pocket/theme.json +6 -0
- package/apps/web/public/themes.js +556 -0
- package/apps/web/public/transcript.js +3348 -0
- package/apps/web/public/trust.js +295 -0
- package/apps/web/public/turns.js +507 -0
- package/apps/web/public/ui.js +1910 -0
- package/apps/web/public/voice.js +345 -0
- package/apps/web/server.mjs +774 -0
- package/package.json +43 -4
- package/packages/cli/src/bom-pi.mjs +452 -0
- package/packages/cli/templates/blank.factory.json +7 -0
- package/packages/cli/templates/dev.factory.json +11 -0
- package/packages/cli/templates/docs.factory.json +12 -0
- package/packages/cli/templates/ops.factory.json +14 -0
- package/packages/cli/templates/triage.factory.json +10 -0
- package/packages/core/src/daemon.mjs +2047 -0
- package/packages/core/src/limit-gate.mjs +192 -0
- package/packages/core/src/rpc-client.mjs +66 -0
- package/systemd/README.md +11 -0
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// Links between linked sessions, above the transcript. Only the server's list for this person may name one.
|
|
2
|
+
import { useState } from "preact/hooks";
|
|
3
|
+
import { navigate, store } from "./store.js";
|
|
4
|
+
import { html, Icon } from "./ui.js";
|
|
5
|
+
|
|
6
|
+
/** Links shown before the rest fold into "+N more": a coordinator's transcript stays near the top on a phone. */
|
|
7
|
+
const SHOWN = 3;
|
|
8
|
+
|
|
9
|
+
export function LinkedSessions() {
|
|
10
|
+
const { conversationId, sessions, sessionsLoaded } = store.state;
|
|
11
|
+
// Unfolded for one session: another one opens folded.
|
|
12
|
+
const [openFor, setOpenFor] = useState(null);
|
|
13
|
+
const open = openFor === conversationId;
|
|
14
|
+
|
|
15
|
+
if (!sessionsLoaded) {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const current = sessions.find((session) => session.id === conversationId);
|
|
20
|
+
const origin =
|
|
21
|
+
current?.linkedFrom && sessions.find((session) => session.id === current.linkedFrom.id);
|
|
22
|
+
const started = sessions.filter((session) => session.linkedFrom?.id === conversationId);
|
|
23
|
+
|
|
24
|
+
if (!origin && !current?.linkedOrigin && !current?.linkedFrom && started.length === 0) {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const link = (session, label) => html`<button
|
|
29
|
+
class="breadcrumb"
|
|
30
|
+
key=${session.id}
|
|
31
|
+
onClick=${() => navigate(session.id)}
|
|
32
|
+
>
|
|
33
|
+
<${Icon} name="fork" size=${14} />
|
|
34
|
+
<span>${label}: ${session.title ?? "New session"}</span>
|
|
35
|
+
</button>`;
|
|
36
|
+
const shown = open || started.length <= SHOWN ? started : started.slice(0, SHOWN);
|
|
37
|
+
|
|
38
|
+
return html`<nav class="linked-sessions" aria-label="Linked sessions">
|
|
39
|
+
${
|
|
40
|
+
origin
|
|
41
|
+
? link(origin, "Started from")
|
|
42
|
+
: (current?.linkedOrigin || current?.linkedFrom) &&
|
|
43
|
+
html`<p class="muted small">
|
|
44
|
+
Started from another session
|
|
45
|
+
</p>`
|
|
46
|
+
}
|
|
47
|
+
${shown.map((session) => link(session, "Linked session"))}
|
|
48
|
+
${
|
|
49
|
+
shown.length < started.length &&
|
|
50
|
+
html`<button class="breadcrumb" onClick=${() => setOpenFor(conversationId)}>
|
|
51
|
+
+${started.length - shown.length} more
|
|
52
|
+
</button>`
|
|
53
|
+
}
|
|
54
|
+
</nav>`;
|
|
55
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// The message Pi is writing, put together from view updates. It imports nothing, so the server's tests can run it.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The live message after `update`, given the `current` one. An update of protocol 3 is what changed (`LiveMessageDelta`
|
|
5
|
+
* in `projection.ts`); an older one is the whole message. Blocks that did not change stay the same objects, and
|
|
6
|
+
* `current` itself comes back when nothing changed. Null: the update does not fit `current`.
|
|
7
|
+
*/
|
|
8
|
+
export function liveMessage(current, update) {
|
|
9
|
+
if (update?.keep === undefined) {
|
|
10
|
+
return update;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const had = current?.blocks ?? [];
|
|
14
|
+
const { keep, blocks: changed } = update;
|
|
15
|
+
const [first, ...rest] = changed;
|
|
16
|
+
|
|
17
|
+
if (
|
|
18
|
+
keep > had.length ||
|
|
19
|
+
(first?.from !== undefined &&
|
|
20
|
+
(had[keep]?.type !== first.type || had[keep].text.length !== first.from))
|
|
21
|
+
) {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (changed.length === 0 && keep === had.length) {
|
|
26
|
+
return current;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return {
|
|
30
|
+
blocks:
|
|
31
|
+
first?.from === undefined
|
|
32
|
+
? [...had.slice(0, keep), ...changed]
|
|
33
|
+
: [
|
|
34
|
+
...had.slice(0, keep),
|
|
35
|
+
{ ...had[keep], text: had[keep].text + first.text },
|
|
36
|
+
...rest,
|
|
37
|
+
],
|
|
38
|
+
};
|
|
39
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "Pi Pocket",
|
|
3
|
+
"short_name": "Pi Pocket",
|
|
4
|
+
"description": "Durable, multiplayer Pi agents in your browser",
|
|
5
|
+
"start_url": "/",
|
|
6
|
+
"scope": "/",
|
|
7
|
+
"display": "standalone",
|
|
8
|
+
"background_color": "#13141c",
|
|
9
|
+
"theme_color": "#13141c",
|
|
10
|
+
"share_target": {
|
|
11
|
+
"action": "/share",
|
|
12
|
+
"method": "POST",
|
|
13
|
+
"enctype": "multipart/form-data",
|
|
14
|
+
"params": {
|
|
15
|
+
"title": "title",
|
|
16
|
+
"text": "text",
|
|
17
|
+
"url": "url",
|
|
18
|
+
"files": [{ "name": "files", "accept": ["*/*"] }]
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"icons": [
|
|
22
|
+
{ "src": "/icon.svg", "sizes": "any", "type": "image/svg+xml" },
|
|
23
|
+
{ "src": "/icon-192.png", "sizes": "192x192", "type": "image/png" },
|
|
24
|
+
{ "src": "/icon-512.png", "sizes": "512x512", "type": "image/png" },
|
|
25
|
+
{
|
|
26
|
+
"src": "/icon-maskable-512.png",
|
|
27
|
+
"sizes": "512x512",
|
|
28
|
+
"type": "image/png",
|
|
29
|
+
"purpose": "maskable"
|
|
30
|
+
}
|
|
31
|
+
]
|
|
32
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// One refresh action and its persistent feedback, shared by the menu, Providers, and the launcher.
|
|
2
|
+
import { api, notify, store } from "./store.js";
|
|
3
|
+
import { html, Icon } from "./ui.js";
|
|
4
|
+
|
|
5
|
+
/** Reloading shared model configuration and contacting providers is the owner's choice. */
|
|
6
|
+
export const modelRefreshAvailable = () =>
|
|
7
|
+
store.state.me?.role === "owner" && store.state.server?.refreshModels === true;
|
|
8
|
+
|
|
9
|
+
/** A refresh does not choose a model: the server's existing models event updates the lists. */
|
|
10
|
+
export async function refreshModels() {
|
|
11
|
+
if (!modelRefreshAvailable() || store.state.modelRefresh.loading) {
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
store.set({ modelRefresh: { loading: true, result: null, error: null } });
|
|
16
|
+
notify("info", "Refreshing models…");
|
|
17
|
+
|
|
18
|
+
try {
|
|
19
|
+
const result = await api("models/refresh", {});
|
|
20
|
+
|
|
21
|
+
store.set({ modelRefresh: { loading: false, result, error: null } });
|
|
22
|
+
notify(
|
|
23
|
+
result.errors.length > 0 ? "error" : "info",
|
|
24
|
+
`${completion(result)}${result.errors.length > 0 ? " Some providers could not refresh. See Providers for details." : ""}`,
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
return result;
|
|
28
|
+
} catch (error) {
|
|
29
|
+
const message = error.message ?? String(error);
|
|
30
|
+
|
|
31
|
+
store.set({ modelRefresh: { loading: false, result: null, error: message } });
|
|
32
|
+
notify("error", `Could not refresh models: ${message}`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function completion({ added, removed, total, offline }) {
|
|
37
|
+
const counts =
|
|
38
|
+
added === 0 && removed === 0
|
|
39
|
+
? `No model changes. ${total} models available.`
|
|
40
|
+
: `Models refreshed: ${added} added, ${removed} removed. ${total} models available.`;
|
|
41
|
+
|
|
42
|
+
return `${offline ? "Offline: reloaded cached models and configuration. " : ""}${counts}`;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** The same loading state and last result survive closing or switching sheets in this tab. */
|
|
46
|
+
export function RefreshModels() {
|
|
47
|
+
if (!modelRefreshAvailable()) {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const { loading, result, error } = store.state.modelRefresh;
|
|
52
|
+
|
|
53
|
+
return html`<div class="model-refresh">
|
|
54
|
+
<button
|
|
55
|
+
class="button small model-refresh-button"
|
|
56
|
+
type="button"
|
|
57
|
+
disabled=${loading}
|
|
58
|
+
aria-busy=${loading}
|
|
59
|
+
onClick=${refreshModels}
|
|
60
|
+
>
|
|
61
|
+
<${Icon} name="reload" size=${15} />
|
|
62
|
+
${loading ? "Refreshing models…" : "Refresh models"}
|
|
63
|
+
</button>
|
|
64
|
+
<div class="model-refresh-status small" role="status" aria-live="polite">
|
|
65
|
+
${loading && html`<p class="muted">Checking configured providers…</p>`}
|
|
66
|
+
${error && html`<p class="warn">Could not refresh models: ${error}</p>`}
|
|
67
|
+
${result && html`<p class="muted">${completion(result)}</p>`}
|
|
68
|
+
${result?.errors.map(
|
|
69
|
+
({ provider, message }) => html`<p class="warn">${provider}: ${message}</p>`,
|
|
70
|
+
)}
|
|
71
|
+
</div>
|
|
72
|
+
</div>`;
|
|
73
|
+
}
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
// Phone notifications: this device subscribes to the server's pushes, and each person picks what they hear about.
|
|
2
|
+
import { useEffect, useState } from "preact/hooks";
|
|
3
|
+
import { api, attempt, closeSheet, notify, sessionUnread } from "./store.js";
|
|
4
|
+
import { html, Loader, Sheet, Switch } from "./ui.js";
|
|
5
|
+
|
|
6
|
+
const supported =
|
|
7
|
+
"serviceWorker" in navigator && "PushManager" in globalThis && "Notification" in globalThis;
|
|
8
|
+
|
|
9
|
+
let registering = null;
|
|
10
|
+
|
|
11
|
+
/** The service worker that shows notifications. Push needs a secure page: https, or localhost. */
|
|
12
|
+
export function registerWorker() {
|
|
13
|
+
if (!supported || !isSecureContext) {
|
|
14
|
+
return Promise.resolve(null);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
registering ??= navigator.serviceWorker.register("/sw.js").catch(() => null);
|
|
18
|
+
|
|
19
|
+
return registering;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The count on the icon, null until this page sets it: the service worker may have set one meanwhile. */
|
|
23
|
+
let badged = null;
|
|
24
|
+
/** What the count was worked out from: it changes only with these, while streaming updates the store many times a second. */
|
|
25
|
+
let countedFrom = null;
|
|
26
|
+
|
|
27
|
+
/** The home-screen icon counts the sessions that need you: a call waits for approval, or the chat has news. */
|
|
28
|
+
export function updateBadge(state) {
|
|
29
|
+
if (!("setAppBadge" in navigator) || !state.me) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (
|
|
34
|
+
countedFrom?.sessions === state.sessions &&
|
|
35
|
+
countedFrom.chatRead === state.chatRead &&
|
|
36
|
+
countedFrom.chatReads === state.chatReads &&
|
|
37
|
+
countedFrom.me === state.me &&
|
|
38
|
+
countedFrom.conversationId === state.conversationId
|
|
39
|
+
) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
countedFrom = {
|
|
44
|
+
sessions: state.sessions,
|
|
45
|
+
chatRead: state.chatRead,
|
|
46
|
+
chatReads: state.chatReads,
|
|
47
|
+
me: state.me,
|
|
48
|
+
conversationId: state.conversationId,
|
|
49
|
+
};
|
|
50
|
+
const count = state.sessions.filter(
|
|
51
|
+
(session) => session.waiting || sessionUnread(session, state),
|
|
52
|
+
).length;
|
|
53
|
+
|
|
54
|
+
if (count === badged) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
badged = count;
|
|
59
|
+
(count === 0 ? navigator.clearAppBadge() : navigator.setAppBadge(count)).catch(() => {});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function keyBytes(base64url) {
|
|
63
|
+
const base64 = base64url
|
|
64
|
+
.replace(/-/g, "+")
|
|
65
|
+
.replace(/_/g, "/")
|
|
66
|
+
.padEnd(Math.ceil(base64url.length / 4) * 4, "=");
|
|
67
|
+
|
|
68
|
+
return Uint8Array.from(atob(base64), (char) => char.charCodeAt(0));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function sameKey(subscription, publicKey) {
|
|
72
|
+
const key = subscription.options?.applicationServerKey;
|
|
73
|
+
|
|
74
|
+
if (!key) {
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const a = new Uint8Array(key);
|
|
79
|
+
const b = keyBytes(publicKey);
|
|
80
|
+
|
|
81
|
+
return a.length === b.length && a.every((byte, index) => byte === b[index]);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Why this device cannot get notifications, or null when it can. */
|
|
85
|
+
function blocker() {
|
|
86
|
+
if (!isSecureContext) {
|
|
87
|
+
return "Notifications need a secure (https) address. Open Pi Pocket through the Cloudflare Tunnel, or through tailscale serve, on this device.";
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (!supported) {
|
|
91
|
+
return "This browser cannot receive notifications. On iPhone and iPad, add Pi Pocket to the home screen first, then open it from there.";
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (Notification.permission === "denied") {
|
|
95
|
+
return "Notifications are blocked for this site. Allow them in the browser's site settings, then come back.";
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const KINDS = [
|
|
102
|
+
["done", "Pi finished", "when a run you took part in ends"],
|
|
103
|
+
[
|
|
104
|
+
"approval",
|
|
105
|
+
"Approvals",
|
|
106
|
+
"when Lancet Guard asks before a command; allow or deny it from the notification",
|
|
107
|
+
],
|
|
108
|
+
[
|
|
109
|
+
"limit",
|
|
110
|
+
"Waiting for a limit",
|
|
111
|
+
"when a run waits more than ten minutes for a provider's limit to clear",
|
|
112
|
+
],
|
|
113
|
+
["mention", "Mentions", "when someone writes @your name in a chat"],
|
|
114
|
+
["chat", "Chat messages", "in sessions you take part in"],
|
|
115
|
+
];
|
|
116
|
+
|
|
117
|
+
export function NotificationsSheet() {
|
|
118
|
+
const [info, setInfo] = useState(null);
|
|
119
|
+
const [subscription, setSubscription] = useState(undefined);
|
|
120
|
+
const [busy, setBusy] = useState(false);
|
|
121
|
+
const problem = blocker();
|
|
122
|
+
|
|
123
|
+
useEffect(() => {
|
|
124
|
+
attempt(async () => setInfo(await api("push")));
|
|
125
|
+
registerWorker()
|
|
126
|
+
.then((registration) => registration?.pushManager.getSubscription() ?? null)
|
|
127
|
+
.then(setSubscription, () => setSubscription(null));
|
|
128
|
+
}, []);
|
|
129
|
+
|
|
130
|
+
const enable = () =>
|
|
131
|
+
attempt(async () => {
|
|
132
|
+
setBusy(true);
|
|
133
|
+
|
|
134
|
+
try {
|
|
135
|
+
if ((await Notification.requestPermission()) !== "granted") {
|
|
136
|
+
throw new Error("Notifications were not allowed.");
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const registration = await registerWorker();
|
|
140
|
+
|
|
141
|
+
if (!registration) {
|
|
142
|
+
throw new Error("The service worker did not start.");
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
await navigator.serviceWorker.ready;
|
|
146
|
+
let current = await registration.pushManager.getSubscription();
|
|
147
|
+
|
|
148
|
+
// A subscription made for another server key cannot receive this server's pushes.
|
|
149
|
+
if (current && !sameKey(current, info.publicKey)) {
|
|
150
|
+
await current.unsubscribe();
|
|
151
|
+
current = null;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
current ??= await registration.pushManager.subscribe({
|
|
155
|
+
userVisibleOnly: true,
|
|
156
|
+
applicationServerKey: keyBytes(info.publicKey),
|
|
157
|
+
});
|
|
158
|
+
const result = await api("push/subscribe", { subscription: current.toJSON() });
|
|
159
|
+
|
|
160
|
+
setSubscription(current);
|
|
161
|
+
setInfo({ ...info, devices: result.devices });
|
|
162
|
+
notify("info", "Notifications are on for this device.");
|
|
163
|
+
} finally {
|
|
164
|
+
setBusy(false);
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
const disable = () =>
|
|
169
|
+
attempt(async () => {
|
|
170
|
+
setBusy(true);
|
|
171
|
+
|
|
172
|
+
try {
|
|
173
|
+
const endpoint = subscription?.endpoint;
|
|
174
|
+
|
|
175
|
+
await subscription?.unsubscribe();
|
|
176
|
+
const result = await api("push/unsubscribe", { endpoint });
|
|
177
|
+
|
|
178
|
+
setSubscription(null);
|
|
179
|
+
setInfo({ ...info, devices: result.devices });
|
|
180
|
+
} finally {
|
|
181
|
+
setBusy(false);
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
const setPref = (key, value) =>
|
|
186
|
+
attempt(async () => setInfo({ ...info, ...(await api("push/prefs", { [key]: value })) }));
|
|
187
|
+
const test = () =>
|
|
188
|
+
attempt(async () => {
|
|
189
|
+
await api("push/test", {});
|
|
190
|
+
notify("info", "Test sent. It shows as a system notification.");
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
const on = Boolean(subscription);
|
|
194
|
+
|
|
195
|
+
return html`<${Sheet}
|
|
196
|
+
title="Notifications"
|
|
197
|
+
onClose=${closeSheet}
|
|
198
|
+
className="notifications-sheet"
|
|
199
|
+
>
|
|
200
|
+
<p class="muted small">
|
|
201
|
+
Get a notification on this device when you are not looking at the session: Pi finished, Pi needs approval, or someone wrote to you.
|
|
202
|
+
</p>
|
|
203
|
+
${problem && html`<div class="error-box small">${problem}</div>`}
|
|
204
|
+
${
|
|
205
|
+
!info || subscription === undefined
|
|
206
|
+
? html`<${Loader} label="Checking this device" />`
|
|
207
|
+
: html`<div class="sheet-list">
|
|
208
|
+
<div class="extension">
|
|
209
|
+
<div class="extension-main">
|
|
210
|
+
<div class="extension-title">This device</div>
|
|
211
|
+
<div class="muted small">
|
|
212
|
+
${on ? "Notifications are on." : "Notifications are off."} ${info.devices > 0 ? `${info.devices} device${info.devices === 1 ? "" : "s"} of yours ${info.devices === 1 ? "gets" : "get"} them.` : ""}
|
|
213
|
+
</div>
|
|
214
|
+
</div>
|
|
215
|
+
<div class="extension-actions">
|
|
216
|
+
<${Switch}
|
|
217
|
+
on=${on}
|
|
218
|
+
disabled=${busy || (!on && problem !== null)}
|
|
219
|
+
label="Notifications on this device"
|
|
220
|
+
onChange=${on ? disable : enable}
|
|
221
|
+
/>
|
|
222
|
+
</div>
|
|
223
|
+
</div>
|
|
224
|
+
</div>
|
|
225
|
+
<div class="group-title">Notify me about</div>
|
|
226
|
+
<div class="sheet-list">
|
|
227
|
+
${KINDS.map(
|
|
228
|
+
([key, title, hint]) => html`<div class="extension">
|
|
229
|
+
<div class="extension-main">
|
|
230
|
+
<div>${title}</div>
|
|
231
|
+
<div class="muted small">${hint}</div>
|
|
232
|
+
</div>
|
|
233
|
+
<div class="extension-actions">
|
|
234
|
+
<${Switch}
|
|
235
|
+
on=${info.prefs[key]}
|
|
236
|
+
label=${title}
|
|
237
|
+
onChange=${() => setPref(key, !info.prefs[key])}
|
|
238
|
+
/>
|
|
239
|
+
</div>
|
|
240
|
+
</div>`,
|
|
241
|
+
)}
|
|
242
|
+
</div>
|
|
243
|
+
${
|
|
244
|
+
on &&
|
|
245
|
+
html`<button class="button wide" onClick=${test}>
|
|
246
|
+
Send a test notification
|
|
247
|
+
</button>`
|
|
248
|
+
}`
|
|
249
|
+
}
|
|
250
|
+
<//>`;
|
|
251
|
+
}
|