@soffits/dsh-notify-inbox 0.0.0-stage → 0.1.1
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/CHANGELOG.md +9 -0
- package/LICENSE +21 -0
- package/README.md +96 -2
- package/client.actions.js +259 -0
- package/client.js +526 -0
- package/cordis.patch.yml +9 -0
- package/icon.svg +6 -0
- package/index.js +90 -0
- package/locale/en.json +1 -0
- package/locale/zh.json +1 -0
- package/package.json +50 -4
- package/src/access.js +92 -0
- package/src/actions.js +89 -0
- package/src/push.js +211 -0
- package/src/routes.js +205 -0
- package/src/store.js +223 -0
- package/src/sw.js +77 -0
- package/src/webpush.js +101 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.1
|
|
4
|
+
|
|
5
|
+
- Buttons come from the shared action bar (`client.actions.js`, the same one the card board uses), so both pages look and behave alike; buttons without a label get the shared words.
|
|
6
|
+
|
|
7
|
+
## 0.1.0
|
|
8
|
+
|
|
9
|
+
- First release: per-person inbox from JSON lines, read and unread kept on the server, Web Push (RFC 8291 encryption and VAPID on node:crypto) with deep links, buttons on notifications (ask about this, done, snooze, event, link) whose presses become `ui.action.<kind>` events, English and Chinese.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sakina
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,97 @@
|
|
|
1
|
-
#
|
|
1
|
+
# dsh-notify-inbox
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A notification inbox for [DeepSeek Harness](https://github.com/deepseek-ai/deepseek-harness) (DSH). Something outside DSH (your assistant's notification router, a cron script, a watcher) files notifications as JSON lines, one folder per person. The plugin shows each signed-in person their own inbox in the Web UI, keeps read and unread per person on the server, pushes new ones to the browsers and installed home-screen apps they turned notifications on for, and gives every notification buttons: ask the agent about it, mark it done, remind me later.
|
|
4
|
+
|
|
5
|
+
The contract is files, so nothing gets lost while DSH restarts: the writer keeps appending, and the plugin pushes whatever arrived once it's back.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npx @deepseek-ai/dsh plugin --profile web add @soffits/dsh-notify-inbox@latest
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Restart `dsh web`. The inbox appears in the sidebar. Then set `dataDir` in a profile override of the `notify-inbox` row:
|
|
14
|
+
|
|
15
|
+
```yaml
|
|
16
|
+
- override:
|
|
17
|
+
- id: notify-inbox
|
|
18
|
+
config:
|
|
19
|
+
dataDir: ~/notify-inbox
|
|
20
|
+
# For a DSH several people sign in to (needs a sign-in plugin that provides requestIdentity):
|
|
21
|
+
peopleFile: ~/people.json
|
|
22
|
+
peopleKey: oidc_users
|
|
23
|
+
vapidFile: ~/secrets/notify-inbox/vapid.json
|
|
24
|
+
vapidSubject: mailto:you@example.org
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
| Option | Default | Meaning |
|
|
28
|
+
|---|---|---|
|
|
29
|
+
| `dataDir` | `~/.dsh/notify-inbox` | Where the writer files notifications: `<dataDir>/<person>/inbox.jsonl`. |
|
|
30
|
+
| `people` | `{}` | Verified user id (an OIDC `sub`) → person id (the folder name, compared case-insensitively). |
|
|
31
|
+
| `peopleFile`, `peopleKey` | | A JSON file with more of the same mapping, re-read when it changes; `peopleKey` picks a top-level key. |
|
|
32
|
+
| `vapidFile` | `<dataDir>/.push/vapid.json` | The Web Push key pair, made on first start with mode 600. Keep it: a new key means every device subscribes again. |
|
|
33
|
+
| `vapidSubject` | `mailto:admin@example.invalid` | A contact push services can reach. Set your own. |
|
|
34
|
+
| `pollSeconds` | `5` | How often new lines are looked for (to push them). |
|
|
35
|
+
|
|
36
|
+
**Who sees what.** With a mapping, every request is checked on the server: a person sees, marks and presses only their own inbox, and a request without a verified identity sees nothing. The identity comes from any plugin that provides a `requestIdentity` service (`current()` → `{ provider, issuer, id, name } | null`), such as [dsh-oidc](https://github.com/soffits/dsh-oidc) 0.3.0-alpha.9 or later. Without a mapping (a DSH only you use), you see every folder.
|
|
37
|
+
|
|
38
|
+
## Writing notifications
|
|
39
|
+
|
|
40
|
+
Append one JSON object per line to `<dataDir>/<person>/inbox.jsonl` (person ids are lower-case letters, digits, `-` and `_`). Append whole lines in one write; a line still being written is picked up when it ends.
|
|
41
|
+
|
|
42
|
+
```json
|
|
43
|
+
{"v": 1, "id": "20261117T155012Z-3f9a1c", "time": "2026-11-17T15:50:12Z", "lane": "interrupt",
|
|
44
|
+
"domain": "home", "title": "Water leak", "spoken": "The kitchen sensor is wet.",
|
|
45
|
+
"text": "The sensor under the sink reported water at 15:50.", "links": ["https://example.org/cam"],
|
|
46
|
+
"files": [{"name": "photo.jpg", "size": 48213, "stored": true}],
|
|
47
|
+
"source": "leak-watch", "key": "leak-kitchen", "test": false, "push": true,
|
|
48
|
+
"actions": [{"kind": "done", "id": "done", "label": "Dealt with it", "payload": {}},
|
|
49
|
+
{"kind": "snooze", "id": "snooze", "label": "", "payload": {}}],
|
|
50
|
+
"routed": [{"destination": "chat", "type": "telegram", "ok": true},
|
|
51
|
+
{"destination": "phone", "type": "ntfy", "ok": false, "detail": "timed out"}]}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
- `id` (required, unique), `time` (required, ISO 8601). `lane: "interrupt"` marks it urgent (red dot, high push urgency).
|
|
55
|
+
- `title`, `spoken` (one short line: the push body), `text` (the full text), `links` (http/https).
|
|
56
|
+
- `files`: attachments the writer copied to `<dataDir>/<person>/files/<id>/<name>`; only those are served, and only to that person.
|
|
57
|
+
- `push: false` files it without a push. `test: true` labels it as a test.
|
|
58
|
+
- `routed`: where else the notification went and whether that worked; shown under the text.
|
|
59
|
+
- `actions`: buttons, see below.
|
|
60
|
+
|
|
61
|
+
A line `{"v": 1, "type": "handled", "record": "<id>", "press": "<press id>", "action": "done", "result": "done", "time": "..."}` tells the inbox that a press on that record was dealt with (`result: "snoozed"` with `until` shows when it comes back). Bad lines are skipped and counted.
|
|
62
|
+
|
|
63
|
+
## Buttons
|
|
64
|
+
|
|
65
|
+
Every notification gets **Ask about this**: it opens a new session with the notification quoted into the message box, ready to edit and send. Since the person sends it from the Web UI, the sign-in plugin records them as the sender.
|
|
66
|
+
|
|
67
|
+
Declared `actions` add more: `done`, `snooze` (a small picker: in an hour, this evening, tomorrow morning; or a fixed `payload.until`), `event` (anything else) and `link` (`payload.url`). A press on `done`, `snooze` or `event` is checked on the server (the record must be the caller's and declare that button), appended to `<dataDir>/<person>/actions.jsonl`, and published on the event channel `ui.action.<kind>` when [dsh-cron-jobs](https://github.com/soffits/dsh-cron-jobs) is installed, so a job listening there can act on it:
|
|
68
|
+
|
|
69
|
+
```json
|
|
70
|
+
{"action": "snooze", "id": "snooze", "payload": {"until": "2026-11-18T09:00:00-05:00"}, "person": "ana",
|
|
71
|
+
"at": "2026-11-17T21:04:11Z", "source": {"plugin": "notify-inbox", "record": "20261117T155012Z-3f9a1c", "key": "leak-kitchen"},
|
|
72
|
+
"press": "9f2c41aa07b3"}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
The page shows the press as waiting until a `handled` line for it shows up.
|
|
76
|
+
|
|
77
|
+
## Push notifications
|
|
78
|
+
|
|
79
|
+
Open the inbox on the device and press **Turn on**. The browser picks its own push service, and the plugin only ever talks to that service, with the message encrypted end to end (RFC 8291, VAPID per RFC 8292, no third-party library):
|
|
80
|
+
|
|
81
|
+
- **iPhone and iPad** (iOS 16.4+): add DSH to the Home Screen, open it from there, then turn notifications on. Pushes go through Apple's service.
|
|
82
|
+
- **Chrome, Edge, Android**: pushes go through Google's Firebase Cloud Messaging, so they don't arrive where Google is blocked.
|
|
83
|
+
- **Firefox**: Mozilla's push service.
|
|
84
|
+
|
|
85
|
+
Tapping a notification opens the inbox on it. A device whose subscription expired is dropped the first time its push service says so.
|
|
86
|
+
|
|
87
|
+
## Files the plugin writes
|
|
88
|
+
|
|
89
|
+
All under `dataDir`: `<person>/read.json` (read marks), `<person>/actions.jsonl` (presses), `.push/subscriptions.json` and `.push/cursor.json` (how far each inbox was pushed, so a restart doesn't push the backlog), and the VAPID key unless `vapidFile` says otherwise. It never rewrites `inbox.jsonl`.
|
|
90
|
+
|
|
91
|
+
## Removing it
|
|
92
|
+
|
|
93
|
+
Remove the plugin from the profile. The data folder stays until you delete it; browsers keep a dormant service worker under `/api/notify-inbox/` until their site data is cleared.
|
|
94
|
+
|
|
95
|
+
## License
|
|
96
|
+
|
|
97
|
+
MIT
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Action buttons for DSH Web UI plugins: chat, done, snooze, event and link,
|
|
3
|
+
* the same look and behaviour wherever an item offers them.
|
|
4
|
+
*
|
|
5
|
+
* A DSH client chunk: dsh-card-board's client loads it with
|
|
6
|
+
* `require.async('./client.actions.js')`. Another plugin can ship a copy as
|
|
7
|
+
* its own `client.actions.js`, changing OWNER below to its package name; the
|
|
8
|
+
* file has no imports and takes React and the client context as arguments.
|
|
9
|
+
*
|
|
10
|
+
* const { createActions } = await require.async('./client.actions.js')
|
|
11
|
+
* const ui = createActions({ React, ctx, locale: 'en', press })
|
|
12
|
+
* h(ui.ActionBar, { actions, states, chat, onPress })
|
|
13
|
+
*
|
|
14
|
+
* `press(action, extra)` is the plugin's own server call for done, snooze and
|
|
15
|
+
* event (extra carries `until` for a snooze); it should resolve once the
|
|
16
|
+
* server has accepted the press. `chat` and `link` never reach the server:
|
|
17
|
+
* chat opens a conversation with a prefilled, editable draft through the Web
|
|
18
|
+
* UI's own workspace service (so the person sends it themselves, signed in as
|
|
19
|
+
* themselves), and link opens the URL.
|
|
20
|
+
*/
|
|
21
|
+
const OWNER = '@soffits/dsh-notify-inbox'
|
|
22
|
+
|
|
23
|
+
const WORDS = {
|
|
24
|
+
en: {
|
|
25
|
+
chat: 'Ask',
|
|
26
|
+
chatAbout: 'Chat about this',
|
|
27
|
+
done: 'Done',
|
|
28
|
+
snooze: 'Snooze',
|
|
29
|
+
open: 'Open',
|
|
30
|
+
in3h: 'In 3 hours',
|
|
31
|
+
tomorrow: 'Tomorrow 9:00',
|
|
32
|
+
pick: 'Pick a time…',
|
|
33
|
+
set: 'Snooze',
|
|
34
|
+
cancel: 'Cancel',
|
|
35
|
+
sending: 'Sending…',
|
|
36
|
+
pending: 'Waiting for it to be picked up',
|
|
37
|
+
handled: 'Done',
|
|
38
|
+
unhandled: 'Nothing picked this up yet',
|
|
39
|
+
failed: 'Didn’t go through: {message}',
|
|
40
|
+
noWorkspace: 'Open a conversation once first, then try again.',
|
|
41
|
+
},
|
|
42
|
+
zh: {
|
|
43
|
+
chat: '问问',
|
|
44
|
+
chatAbout: '聊聊这个',
|
|
45
|
+
done: '完成',
|
|
46
|
+
snooze: '稍后提醒',
|
|
47
|
+
open: '打开',
|
|
48
|
+
in3h: '3 小时后',
|
|
49
|
+
tomorrow: '明天 9:00',
|
|
50
|
+
pick: '选个时间…',
|
|
51
|
+
set: '稍后提醒',
|
|
52
|
+
cancel: '取消',
|
|
53
|
+
sending: '发送中…',
|
|
54
|
+
pending: '等待处理',
|
|
55
|
+
handled: '已处理',
|
|
56
|
+
unhandled: '还没有被处理',
|
|
57
|
+
failed: '没有成功:{message}',
|
|
58
|
+
noWorkspace: '请先打开一次对话,再试一次。',
|
|
59
|
+
},
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const CSS = `
|
|
63
|
+
.ua-bar{display:flex;flex-wrap:wrap;gap:6px;align-items:center}
|
|
64
|
+
.ua-btn{box-sizing:border-box;display:inline-flex;align-items:center;gap:5px;height:28px;padding:0 10px;border:0;border-radius:8px;background:transparent;color:var(--dsw-alias-label-secondary);font:inherit;font-size:12px;line-height:18px;cursor:pointer;white-space:nowrap;text-decoration:none;box-shadow:inset 0 0 0 .5px var(--dsw-alias-border-l3)}
|
|
65
|
+
.ua-btn svg{flex:none;width:14px;height:14px}
|
|
66
|
+
.ua-btn:disabled{cursor:default;opacity:.7}
|
|
67
|
+
@media (hover:hover){.ua-btn:hover:not(:disabled){background:var(--dsw-alias-interactive-bg-hover);color:var(--dsw-alias-label-primary)}}
|
|
68
|
+
.ua-icon{width:26px;height:26px;padding:0;justify-content:center;box-shadow:none;color:var(--dsw-alias-label-tertiary)}
|
|
69
|
+
.ua-btn[data-state=pending] svg,.ua-btn[data-state=sending] svg{animation:ua-pulse 1.2s ease-in-out infinite}
|
|
70
|
+
.ua-btn[data-state=done]{color:var(--dsw-alias-state-success-primary)}
|
|
71
|
+
.ua-btn[data-state=unhandled]{color:var(--dsw-alias-state-warn-label,var(--dsw-alias-state-warn-primary))}
|
|
72
|
+
@keyframes ua-pulse{50%{opacity:.35}}
|
|
73
|
+
.ua-wrap{position:relative;display:inline-flex}
|
|
74
|
+
.ua-menu{position:absolute;z-index:20;top:calc(100% + 4px);right:0;min-width:168px;padding:4px;border-radius:12px;background:var(--dsw-alias-bg-layer-2,var(--dsw-alias-bg-base));box-shadow:0 6px 24px #0000001f,inset 0 0 0 .5px var(--dsw-alias-border-l2);display:flex;flex-direction:column}
|
|
75
|
+
.ua-menu button{display:flex;align-items:center;gap:8px;padding:7px 10px;border:0;border-radius:8px;background:none;color:var(--dsw-alias-label-primary);font:inherit;font-size:13px;text-align:left;cursor:pointer}
|
|
76
|
+
@media (hover:hover){.ua-menu button:hover{background:var(--dsw-alias-interactive-bg-hover)}}
|
|
77
|
+
.ua-menu input{margin:4px 6px;padding:6px 8px;border:.5px solid var(--dsw-alias-border-l3);border-radius:8px;background:transparent;color:inherit;font:inherit;font-size:13px}
|
|
78
|
+
.ua-menuRow{display:flex;justify-content:flex-end;gap:4px;padding:2px 4px}
|
|
79
|
+
.ua-error{color:var(--dsw-alias-state-error-primary);font-size:12px}
|
|
80
|
+
`
|
|
81
|
+
|
|
82
|
+
const ICONS = {
|
|
83
|
+
chat: 'M3 4.5A1.5 1.5 0 0 1 4.5 3h7A1.5 1.5 0 0 1 13 4.5v5A1.5 1.5 0 0 1 11.5 11H7l-3 2.5V11h.5A1.5 1.5 0 0 1 3 9.5z',
|
|
84
|
+
done: 'M3.5 8.5l3 3 6-7',
|
|
85
|
+
snooze: 'M8 4.5V8l2.2 1.4M13.5 8a5.5 5.5 0 1 1-11 0 5.5 5.5 0 0 1 11 0z',
|
|
86
|
+
event: 'M8.8 2.5 4 9h4l-.8 4.5L12 7H8z',
|
|
87
|
+
link: 'M6 3.5H3.5v9h9V10M9 3h4v4M13 3 7.5 8.5',
|
|
88
|
+
pending: 'M8 8m-2 0a2 2 0 1 0 4 0a2 2 0 1 0-4 0',
|
|
89
|
+
unhandled: 'M8 3.5v5.5M8 12v.5',
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function fill(text, params) {
|
|
93
|
+
return String(text).replace(/\{(\w+)\}/g, (match, key) => (key in params ? String(params[key]) : match))
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** The next 09:00 after now, as an ISO string. */
|
|
97
|
+
function tomorrowMorning(now = new Date()) {
|
|
98
|
+
const date = new Date(now)
|
|
99
|
+
date.setDate(date.getDate() + 1)
|
|
100
|
+
date.setHours(9, 0, 0, 0)
|
|
101
|
+
return date.toISOString()
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* The draft for a chat about an item: the person's question goes first (a
|
|
106
|
+
* prompt from the data, or nothing yet), then the item quoted, then its
|
|
107
|
+
* context as JSON for the agent.
|
|
108
|
+
*/
|
|
109
|
+
function chatText({ prompt = '', title, lines = [], context }) {
|
|
110
|
+
const quote = [title ? `**${title}**` : null, ...lines].filter(Boolean).map(line => `> ${line}`).join('\n')
|
|
111
|
+
const parts = [prompt, quote]
|
|
112
|
+
if (context !== undefined) parts.push(`\`\`\`json\n${JSON.stringify(context, null, 2)}\n\`\`\``)
|
|
113
|
+
return `${parts.filter(Boolean).join('\n\n')}\n`
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* @param {object} options
|
|
118
|
+
* @param {object} options.React - the page's React
|
|
119
|
+
* @param {object} options.ctx - the plugin's client context (ctx.get reaches the Web UI's services)
|
|
120
|
+
* @param {string} [options.locale] - 'zh…' picks Chinese words, anything else English
|
|
121
|
+
* @param {(action: object, extra: object) => Promise<unknown>} [options.press] - the server call for done, snooze and event
|
|
122
|
+
*/
|
|
123
|
+
function createActions({ React, ctx, locale = 'en', press }) {
|
|
124
|
+
const h = React.createElement
|
|
125
|
+
const { useState, useEffect, useRef } = React
|
|
126
|
+
const words = WORDS[String(locale).toLowerCase().startsWith('zh') ? 'zh' : 'en']
|
|
127
|
+
const t = key => words[key] ?? WORDS.en[key] ?? key
|
|
128
|
+
|
|
129
|
+
if (typeof document !== 'undefined' && !document.getElementById('dsh-actions-ui-style')) {
|
|
130
|
+
const style = document.createElement('style')
|
|
131
|
+
style.id = 'dsh-actions-ui-style'
|
|
132
|
+
style.dataset.plugin = OWNER
|
|
133
|
+
style.textContent = CSS
|
|
134
|
+
document.head.appendChild(style)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function Icon({ name }) {
|
|
138
|
+
return h('svg', { viewBox: '0 0 16 16', fill: 'none', stroke: 'currentColor', strokeWidth: 1.4, strokeLinecap: 'round', strokeLinejoin: 'round', 'aria-hidden': true }, h('path', { d: ICONS[name] ?? ICONS.event }))
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Open a conversation with `text` as its editable draft: a new one in the
|
|
143
|
+
* current (or most recent) workspace, or the given session.
|
|
144
|
+
*/
|
|
145
|
+
async function startChat({ text, session }) {
|
|
146
|
+
const workspace = ctx.get('uiWorkspace')
|
|
147
|
+
if (workspace === undefined) throw new Error(t('noWorkspace'))
|
|
148
|
+
if (session === undefined || session === 'new') {
|
|
149
|
+
workspace.startSession(undefined, { prompt: text, whenDraftExists: 'replace' })
|
|
150
|
+
return
|
|
151
|
+
}
|
|
152
|
+
workspace.openSession(session)
|
|
153
|
+
const sessions = ctx.get('sessions')
|
|
154
|
+
const conversation = ctx.get('conversation')
|
|
155
|
+
for (let i = 0; i < 30 && sessions && conversation; i += 1) {
|
|
156
|
+
const binding = sessions.binding(session)
|
|
157
|
+
if (binding !== undefined) {
|
|
158
|
+
try {
|
|
159
|
+
conversation.input.requestDraftInitialization(binding, { prompt: text, whenDraftExists: 'append' })
|
|
160
|
+
} catch {
|
|
161
|
+
// the session moved on before the draft landed; it is open, the person can type
|
|
162
|
+
}
|
|
163
|
+
return
|
|
164
|
+
}
|
|
165
|
+
await new Promise(resolve => setTimeout(resolve, 100))
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function SnoozeMenu({ onPick, onClose }) {
|
|
170
|
+
const [picking, setPicking] = useState(false)
|
|
171
|
+
const [value, setValue] = useState('')
|
|
172
|
+
const ref = useRef(null)
|
|
173
|
+
useEffect(() => {
|
|
174
|
+
const away = event => { if (ref.current && !ref.current.contains(event.target)) onClose() }
|
|
175
|
+
const escape = event => { if (event.key === 'Escape') onClose() }
|
|
176
|
+
document.addEventListener('pointerdown', away)
|
|
177
|
+
document.addEventListener('keydown', escape)
|
|
178
|
+
return () => { document.removeEventListener('pointerdown', away); document.removeEventListener('keydown', escape) }
|
|
179
|
+
}, [onClose])
|
|
180
|
+
return h('div', { className: 'ua-menu', ref, role: 'menu' },
|
|
181
|
+
!picking && h('button', { type: 'button', role: 'menuitem', onClick: () => onPick(new Date(Date.now() + 3 * 3600 * 1000).toISOString()) }, t('in3h')),
|
|
182
|
+
!picking && h('button', { type: 'button', role: 'menuitem', onClick: () => onPick(tomorrowMorning()) }, t('tomorrow')),
|
|
183
|
+
!picking && h('button', { type: 'button', role: 'menuitem', onClick: () => setPicking(true) }, t('pick')),
|
|
184
|
+
picking && h('input', { type: 'datetime-local', value, autoFocus: true, onChange: event => setValue(event.target.value) }),
|
|
185
|
+
picking && h('div', { className: 'ua-menuRow' },
|
|
186
|
+
h('button', { type: 'button', onClick: onClose }, t('cancel')),
|
|
187
|
+
h('button', { type: 'button', disabled: value === '', onClick: () => onPick(new Date(value).toISOString()) }, t('set'))))
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* One action as a button. `state` is the server's view of the last press
|
|
192
|
+
* (pending, done, unhandled); `compact` draws an icon only.
|
|
193
|
+
*/
|
|
194
|
+
function ActionButton({ action, state, compact, chat, onPress }) {
|
|
195
|
+
const [local, setLocal] = useState(null)
|
|
196
|
+
const [error, setError] = useState(null)
|
|
197
|
+
const [menu, setMenu] = useState(false)
|
|
198
|
+
useEffect(() => { if (state) setLocal(null) }, [state])
|
|
199
|
+
const shown = local ?? state
|
|
200
|
+
const label = action.label ?? t(action.kind === 'chat' ? 'chat' : action.kind === 'link' ? 'open' : action.kind)
|
|
201
|
+
const title = shown === 'sending' ? t('sending') : shown === 'pending' ? t('pending') : shown === 'done' ? t('handled') : shown === 'unhandled' ? t('unhandled') : error ?? label
|
|
202
|
+
const icon = shown === 'done' ? 'done' : shown === 'unhandled' ? 'unhandled' : action.kind
|
|
203
|
+
const className = `ua-btn${compact ? ' ua-icon' : ''}`
|
|
204
|
+
const content = [h(Icon, { key: 'i', name: icon }), !compact && h('span', { key: 'l' }, label)]
|
|
205
|
+
|
|
206
|
+
if (action.kind === 'link') {
|
|
207
|
+
const url = action.payload?.url
|
|
208
|
+
const external = /^https?:/i.test(url ?? '')
|
|
209
|
+
return h('a', { className, href: url, title: label, 'aria-label': label, ...(external ? { target: '_blank', rel: 'noopener noreferrer' } : {}) }, content)
|
|
210
|
+
}
|
|
211
|
+
const send = async extra => {
|
|
212
|
+
setMenu(false)
|
|
213
|
+
setError(null)
|
|
214
|
+
setLocal('sending')
|
|
215
|
+
try {
|
|
216
|
+
if (action.kind === 'chat') {
|
|
217
|
+
await chat(action)
|
|
218
|
+
setLocal(null)
|
|
219
|
+
} else {
|
|
220
|
+
await (onPress ?? press)(action, extra ?? {})
|
|
221
|
+
setLocal('pending')
|
|
222
|
+
}
|
|
223
|
+
} catch (reason) {
|
|
224
|
+
setLocal(null)
|
|
225
|
+
setError(fill(t('failed'), { message: reason?.message ?? String(reason) }))
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
const onClick = () => {
|
|
229
|
+
if (action.kind === 'snooze' && action.payload?.until === undefined) setMenu(open => !open)
|
|
230
|
+
else send()
|
|
231
|
+
}
|
|
232
|
+
const busy = shown === 'sending' || shown === 'pending'
|
|
233
|
+
return h('span', { className: 'ua-wrap' },
|
|
234
|
+
h('button', { type: 'button', className, title, 'aria-label': `${label}${title !== label ? ` (${title})` : ''}`, 'data-state': shown ?? undefined, disabled: busy, onClick }, content),
|
|
235
|
+
menu && h(SnoozeMenu, { onPick: until => send({ until }), onClose: () => setMenu(false) }),
|
|
236
|
+
error && !compact && h('span', { className: 'ua-error', role: 'alert' }, error))
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* A row of action buttons.
|
|
241
|
+
* @param {object} props
|
|
242
|
+
* @param {object[]} props.actions - the item's actions, as the data declares them
|
|
243
|
+
* @param {Record<string, string>} [props.states] - press state by action id
|
|
244
|
+
* @param {boolean} [props.compact] - icons only (for rows)
|
|
245
|
+
* @param {(action: object) => Promise<void>} props.chat - opens the chat for a chat action
|
|
246
|
+
* @param {(action: object, extra: object) => Promise<unknown>} [props.onPress] - this bar's press call, instead of the one given to createActions
|
|
247
|
+
*/
|
|
248
|
+
function ActionBar({ actions, states = {}, compact = false, chat, onPress }) {
|
|
249
|
+
if (!actions || actions.length === 0) return null
|
|
250
|
+
return h('div', { className: 'ua-bar' }, actions.map(action =>
|
|
251
|
+
h(ActionButton, { key: action.id, action, state: states[action.id], compact, chat, onPress })))
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
return { ActionBar, ActionButton, Icon, startChat, chatText, t }
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
if (typeof window !== 'undefined' && window.__ModuleLoader__) {
|
|
258
|
+
window.__ModuleLoader__.load({ id: `${OWNER}/client.actions.js`, factory: () => ({ createActions, chatText }) })
|
|
259
|
+
}
|