@laraveladmin/dsh-stay-awake 0.0.0-stage → 0.1.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.
- package/CHANGELOG.md +26 -0
- package/LICENSE +21 -0
- package/README.en.md +224 -0
- package/README.md +226 -2
- package/client.js +650 -0
- package/cordis.patch.yml +3 -0
- package/docs/REQUIREMENTS.md +516 -0
- package/docs/VERIFICATION.md +122 -0
- package/docs/images/indicator.svg +19 -0
- package/docs/images/panel.svg +65 -0
- package/icon.svg +7 -0
- package/index.js +270 -0
- package/lib/activity.js +74 -0
- package/lib/config.js +81 -0
- package/lib/controller.js +305 -0
- package/lib/log.js +21 -0
- package/lib/platform.js +105 -0
- package/lib/power.js +121 -0
- package/lib/server.js +91 -0
- package/lib/state.js +70 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +60 -4
package/client.js
ADDED
|
@@ -0,0 +1,650 @@
|
|
|
1
|
+
window.__ModuleLoader__.load({
|
|
2
|
+
id: '@laraveladmin/dsh-stay-awake',
|
|
3
|
+
factory(require) {
|
|
4
|
+
const React = require('react')
|
|
5
|
+
const h = React.createElement
|
|
6
|
+
|
|
7
|
+
// Package-owned stylesheet insertion; falls back to an inline <style> node
|
|
8
|
+
// when this client version does not expose the builtin.
|
|
9
|
+
const stylesheet = (() => {
|
|
10
|
+
try {
|
|
11
|
+
const candidate = require('styles')
|
|
12
|
+
return candidate && typeof candidate.insert === 'function' ? candidate : null
|
|
13
|
+
} catch {
|
|
14
|
+
return null
|
|
15
|
+
}
|
|
16
|
+
})()
|
|
17
|
+
|
|
18
|
+
const NS = 'stay-awake'
|
|
19
|
+
const ROUTE = '/api/stay-awake'
|
|
20
|
+
const PACKAGE_NAME = '@laraveladmin/dsh-stay-awake'
|
|
21
|
+
const REPO_URL = 'https://gitee.com/laravel-admin/dsh-stay-awake'
|
|
22
|
+
const NPM_URL = `https://www.npmjs.com/package/${PACKAGE_NAME}/v/`
|
|
23
|
+
const POLL_MIN_MS = 3000
|
|
24
|
+
const POLL_MAX_MS = 60000
|
|
25
|
+
// A protection indicator must never claim "holding" while it cannot prove it.
|
|
26
|
+
const UNKNOWN_AFTER_FAILURES = 2
|
|
27
|
+
|
|
28
|
+
const ZH = {
|
|
29
|
+
'brand': '守夜',
|
|
30
|
+
'state.connecting': '连接中',
|
|
31
|
+
'state.idle': '未保持',
|
|
32
|
+
'state.releasing': '释放中',
|
|
33
|
+
'state.holding': '保持中',
|
|
34
|
+
'state.failed': '异常',
|
|
35
|
+
'state.disabled': '已关闭',
|
|
36
|
+
'state.unknown': '状态未知',
|
|
37
|
+
'detail.state': '状态',
|
|
38
|
+
'detail.backend': '后端',
|
|
39
|
+
'detail.scope': '范围',
|
|
40
|
+
'detail.sources': '活跃源',
|
|
41
|
+
'detail.since': '已持续',
|
|
42
|
+
'detail.error': '原因',
|
|
43
|
+
'detail.version': '版本',
|
|
44
|
+
'link.repo': '查看源码',
|
|
45
|
+
'sources.agents': '会话',
|
|
46
|
+
'sources.maintenance': '维护',
|
|
47
|
+
'sources.jobs': '后台任务',
|
|
48
|
+
'scope.system': '仅系统',
|
|
49
|
+
'scope.display': '含屏幕',
|
|
50
|
+
'unit.none': '无',
|
|
51
|
+
'hint.unreachable': '无法连接宿主状态服务,DSH 可能正在重启',
|
|
52
|
+
'error.exited': '电源断言被意外终止,正在重试',
|
|
53
|
+
'error.backend': '无法启动电源断言后端',
|
|
54
|
+
'error.unsupported': '当前平台没有可用的电源后端',
|
|
55
|
+
'panel.enabled': '总开关',
|
|
56
|
+
'panel.allowDisplaySleep': '允许屏幕休眠',
|
|
57
|
+
'panel.allowDisplaySleepHint': '开启后只防系统休眠,屏幕按系统设置熄灭',
|
|
58
|
+
'panel.hold': '临时保持',
|
|
59
|
+
'panel.hold30': '30 分钟',
|
|
60
|
+
'panel.hold120': '2 小时',
|
|
61
|
+
'panel.holdCancel': '取消',
|
|
62
|
+
'panel.holdDisabled': '总开关关闭时无法临时保持',
|
|
63
|
+
'panel.holdLeft': '剩余',
|
|
64
|
+
'panel.saved': '已保存',
|
|
65
|
+
'panel.saveFailed': '保存失败,请重试',
|
|
66
|
+
'panel.staleHost': '宿主端未加载最新版本:请在插件页禁用再启用「守夜」,或重启 DSH',
|
|
67
|
+
'panel.close': '关闭',
|
|
68
|
+
'panel.advanced': '防抖、兜底时长等高级参数在 profile 的 cordis.patch.yml 中配置',
|
|
69
|
+
'panel.repo': '打开代码仓库',
|
|
70
|
+
'panel.npm': '在 npm 查看该版本',
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const EN = {
|
|
74
|
+
'brand': 'Stay awake',
|
|
75
|
+
'state.connecting': 'Connecting',
|
|
76
|
+
'state.idle': 'Not holding',
|
|
77
|
+
'state.releasing': 'Releasing',
|
|
78
|
+
'state.holding': 'Holding',
|
|
79
|
+
'state.failed': 'Error',
|
|
80
|
+
'state.disabled': 'Off',
|
|
81
|
+
'state.unknown': 'Unknown',
|
|
82
|
+
'detail.state': 'State',
|
|
83
|
+
'detail.backend': 'Backend',
|
|
84
|
+
'detail.scope': 'Scope',
|
|
85
|
+
'detail.sources': 'Sources',
|
|
86
|
+
'detail.since': 'Held for',
|
|
87
|
+
'detail.error': 'Reason',
|
|
88
|
+
'detail.version': 'Version',
|
|
89
|
+
'link.repo': 'View source',
|
|
90
|
+
'sources.agents': 'Agents',
|
|
91
|
+
'sources.maintenance': 'Maintenance',
|
|
92
|
+
'sources.jobs': 'Jobs',
|
|
93
|
+
'scope.system': 'system only',
|
|
94
|
+
'scope.display': 'incl. display',
|
|
95
|
+
'unit.none': 'none',
|
|
96
|
+
'hint.unreachable': 'Cannot reach the host status service; DSH may be restarting',
|
|
97
|
+
'error.exited': 'The power assertion exited unexpectedly; retrying',
|
|
98
|
+
'error.backend': 'Could not start the power assertion backend',
|
|
99
|
+
'error.unsupported': 'No power backend is available on this platform',
|
|
100
|
+
'panel.enabled': 'Enabled',
|
|
101
|
+
'panel.allowDisplaySleep': 'Allow display sleep',
|
|
102
|
+
'panel.allowDisplaySleepHint': 'Only system sleep is blocked; the screen follows the OS setting',
|
|
103
|
+
'panel.hold': 'Hold for',
|
|
104
|
+
'panel.hold30': '30 min',
|
|
105
|
+
'panel.hold120': '2 h',
|
|
106
|
+
'panel.holdCancel': 'Cancel',
|
|
107
|
+
'panel.holdDisabled': 'Enable the plugin first to hold',
|
|
108
|
+
'panel.holdLeft': 'left',
|
|
109
|
+
'panel.saved': 'Saved',
|
|
110
|
+
'panel.saveFailed': 'Could not save, please retry',
|
|
111
|
+
'panel.staleHost': 'The host half is running an older build: disable and re-enable Stay awake in Plugins, or restart DSH',
|
|
112
|
+
'panel.close': 'Close',
|
|
113
|
+
'panel.advanced': 'Debounce, hold cap and other advanced options live in the profile cordis.patch.yml',
|
|
114
|
+
'panel.repo': 'Open the source repository',
|
|
115
|
+
'panel.npm': 'View this version on npm',
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const CSS = `
|
|
119
|
+
.dsa-panel{position:fixed;left:50%;bottom:150px;transform:translateX(-50%);width:min(360px,calc(100vw - 24px));box-sizing:border-box;background:var(--dsw-alias-bg-overlay);color:var(--dsw-alias-label-primary);border-radius:12px;padding:10px 12px;z-index:80;pointer-events:auto;font-size:calc(var(--dsh-content-font-size-secondary,13px) - 1px);line-height:calc(19px + var(--dsh-content-font-delta-secondary,0px))}
|
|
120
|
+
.dsa-head{display:flex;align-items:center;gap:8px}
|
|
121
|
+
.dsa-sub{color:var(--dsw-alias-label-secondary);font-size:calc(var(--dsh-content-font-size-secondary,13px) - 2px)}
|
|
122
|
+
.dsa-close{margin-left:auto;border:0;background:transparent;color:var(--dsw-alias-label-secondary);cursor:pointer;font-size:16px;line-height:1;padding:2px 4px;border-radius:6px}
|
|
123
|
+
.dsa-close:hover{background:var(--dsw-alias-bg-layer-2);color:var(--dsw-alias-label-primary)}
|
|
124
|
+
.dsa-box{margin-top:8px}
|
|
125
|
+
.dsa-grid{display:grid;grid-template-columns:auto 1fr;gap:2px 12px}
|
|
126
|
+
.dsa-key{color:var(--dsw-alias-label-secondary)}
|
|
127
|
+
.dsa-val{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
|
|
128
|
+
.dsa-grow{flex:1 1 auto;min-width:0}
|
|
129
|
+
.dsa-row{display:flex;align-items:center;gap:10px;padding:4px 0}
|
|
130
|
+
.dsa-switch{flex:0 0 auto;width:34px;height:18px;border-radius:9px;border:1px solid var(--dsw-alias-border-l2);background:var(--dsw-alias-state-idle-primary);position:relative;cursor:pointer;padding:0}
|
|
131
|
+
.dsa-switch::after{content:'';position:absolute;top:2px;left:2px;width:12px;height:12px;border-radius:50%;background:var(--dsw-alias-bg-base);transition:transform .15s ease}
|
|
132
|
+
.dsa-switch[aria-checked="true"]{background:var(--dsw-alias-state-success-primary);border-color:var(--dsw-alias-state-success-primary)}
|
|
133
|
+
.dsa-switch[aria-checked="true"]::after{transform:translateX(16px)}
|
|
134
|
+
.dsa-switch:disabled{opacity:.5;cursor:default}
|
|
135
|
+
.dsa-actions{display:flex;gap:6px;flex-wrap:wrap;padding-top:2px}
|
|
136
|
+
.dsa-button{border:1px solid var(--dsw-alias-border-l1);background:var(--dsw-alias-bg-layer-1);color:var(--dsw-alias-label-primary);border-radius:8px;padding:3px 10px;cursor:pointer;font:inherit}
|
|
137
|
+
.dsa-button:hover{background:var(--dsw-alias-bg-layer-2)}
|
|
138
|
+
.dsa-button:disabled{opacity:.5;cursor:default}
|
|
139
|
+
.dsa-button:focus-visible,.dsa-switch:focus-visible,.dsa-close:focus-visible,.dsa-link:focus-visible{outline:2px solid var(--dsw-alias-brand-primary);outline-offset:2px}
|
|
140
|
+
.dsa-note{color:var(--dsw-alias-label-secondary);font-size:calc(var(--dsh-content-font-size-secondary,13px) - 2px);margin-top:6px}
|
|
141
|
+
.dsa-error{color:var(--dsw-alias-state-error-primary)}
|
|
142
|
+
.dsa-foot{display:flex;gap:10px;align-items:center;margin-top:10px;color:var(--dsw-alias-label-secondary);font-size:calc(var(--dsh-content-font-size-secondary,13px) - 2px)}
|
|
143
|
+
.dsa-foot .dsa-spacer{flex:1 1 auto}
|
|
144
|
+
.dsa-icon-link{display:inline-flex;align-items:center;justify-content:center;width:20px;height:20px;border-radius:6px;color:var(--dsw-alias-label-secondary);text-decoration:none}
|
|
145
|
+
.dsa-icon-link:hover{background:var(--dsw-alias-bg-layer-2);color:var(--dsw-alias-label-primary)}
|
|
146
|
+
.dsa-version{color:var(--dsw-alias-label-secondary);text-decoration:none}
|
|
147
|
+
.dsa-version:hover{color:var(--dsw-alias-label-primary);text-decoration:underline}
|
|
148
|
+
.dsa-link{color:var(--dsw-alias-brand-primary);text-decoration:none}
|
|
149
|
+
.dsa-link:hover{text-decoration:underline}
|
|
150
|
+
.dsa-line{display:inline-flex;align-items:center;gap:6px;min-width:0;white-space:nowrap;font-size:calc(var(--dsh-content-font-size-secondary,13px) - 1px);line-height:calc(20px + var(--dsh-content-font-delta-secondary,0px))}
|
|
151
|
+
.dsa-indicator{display:inline-flex;align-items:center;min-width:0;flex:0 1 auto;background:transparent;border:0;padding:0;font:inherit;cursor:pointer;border-radius:6px}
|
|
152
|
+
.dsa-indicator:focus-visible{outline:2px solid var(--dsw-alias-brand-primary);outline-offset:2px}
|
|
153
|
+
`
|
|
154
|
+
|
|
155
|
+
function createStore() {
|
|
156
|
+
let state = { open: false, status: null, failures: 0, busy: false, notice: null, localeRevision: 0 }
|
|
157
|
+
const listeners = new Set()
|
|
158
|
+
const store = {
|
|
159
|
+
get: () => state,
|
|
160
|
+
set(patch) {
|
|
161
|
+
state = { ...state, ...patch }
|
|
162
|
+
for (const listener of [...listeners]) listener()
|
|
163
|
+
},
|
|
164
|
+
subscribe(listener) {
|
|
165
|
+
listeners.add(listener)
|
|
166
|
+
return () => listeners.delete(listener)
|
|
167
|
+
},
|
|
168
|
+
toggle() {
|
|
169
|
+
store.set({ open: !state.open, notice: null })
|
|
170
|
+
},
|
|
171
|
+
close() {
|
|
172
|
+
if (state.open) store.set({ open: false })
|
|
173
|
+
},
|
|
174
|
+
}
|
|
175
|
+
return store
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** Re-render once per second while the panel is open (live countdown). */
|
|
179
|
+
function useTick(active) {
|
|
180
|
+
const [, bump] = React.useState(0)
|
|
181
|
+
React.useEffect(() => {
|
|
182
|
+
if (!active) return undefined
|
|
183
|
+
const timer = globalThis.setInterval(() => bump((value) => value + 1), 1000)
|
|
184
|
+
return () => globalThis.clearInterval(timer)
|
|
185
|
+
}, [active])
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function useStore(store) {
|
|
189
|
+
const [snapshot, setSnapshot] = React.useState(store.get())
|
|
190
|
+
React.useEffect(() => store.subscribe(() => setSnapshot(store.get())), [store])
|
|
191
|
+
return snapshot
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function formatDuration(ms) {
|
|
195
|
+
const total = Math.max(0, Math.floor(ms / 1000))
|
|
196
|
+
const hours = String(Math.floor(total / 3600)).padStart(2, '0')
|
|
197
|
+
const minutes = String(Math.floor((total % 3600) / 60)).padStart(2, '0')
|
|
198
|
+
const seconds = String(total % 60).padStart(2, '0')
|
|
199
|
+
return `${hours}:${minutes}:${seconds}`
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function classify(status, failures) {
|
|
203
|
+
if (status === null) {
|
|
204
|
+
return failures >= 1 ? { key: 'unknown', tone: 'error' } : { key: 'connecting', tone: 'idle' }
|
|
205
|
+
}
|
|
206
|
+
if (failures >= UNKNOWN_AFTER_FAILURES) return { key: 'unknown', tone: 'error' }
|
|
207
|
+
if (status.enabled === false) return { key: 'disabled', tone: 'idle' }
|
|
208
|
+
if (status.state === 'failed') return { key: 'failed', tone: 'error' }
|
|
209
|
+
if (status.holding === true) return { key: 'holding', tone: 'success' }
|
|
210
|
+
if (status.state === 'releasing') return { key: 'releasing', tone: 'idle' }
|
|
211
|
+
return { key: 'idle', tone: 'idle' }
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function toneColor(tone) {
|
|
215
|
+
if (tone === 'success') return 'var(--dsw-alias-state-success-primary)'
|
|
216
|
+
if (tone === 'error') return 'var(--dsw-alias-state-error-primary)'
|
|
217
|
+
return 'var(--dsw-alias-state-idle-primary)'
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function errorText(t, status) {
|
|
221
|
+
if (status.lastErrorCode === 'exited') return t('error.exited')
|
|
222
|
+
if (status.lastErrorCode === 'backend-failed') return t('error.backend')
|
|
223
|
+
if (status.lastErrorCode === 'unsupported') return t('error.unsupported')
|
|
224
|
+
return typeof status.lastError === 'string' && status.lastError !== '' ? status.lastError : null
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function Moon({ color }) {
|
|
228
|
+
return h(
|
|
229
|
+
'svg',
|
|
230
|
+
{
|
|
231
|
+
viewBox: '0 0 16 16',
|
|
232
|
+
width: 14,
|
|
233
|
+
height: 14,
|
|
234
|
+
'aria-hidden': true,
|
|
235
|
+
style: { display: 'block', flex: '0 0 auto' },
|
|
236
|
+
},
|
|
237
|
+
h('path', {
|
|
238
|
+
d: 'M10.2 1.6a6.6 6.6 0 1 0 4.2 9.6A5.4 5.4 0 0 1 10.2 1.6z',
|
|
239
|
+
style: { fill: color },
|
|
240
|
+
}),
|
|
241
|
+
)
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function RepoIcon() {
|
|
245
|
+
return h(
|
|
246
|
+
'svg',
|
|
247
|
+
{
|
|
248
|
+
viewBox: '0 0 16 16',
|
|
249
|
+
width: 14,
|
|
250
|
+
height: 14,
|
|
251
|
+
'aria-hidden': true,
|
|
252
|
+
style: { display: 'block' },
|
|
253
|
+
},
|
|
254
|
+
h('path', {
|
|
255
|
+
d: 'M5.6 4.4 2.2 8l3.4 3.6M10.4 4.4 13.8 8l-3.4 3.6',
|
|
256
|
+
fill: 'none',
|
|
257
|
+
stroke: 'currentColor',
|
|
258
|
+
strokeWidth: 1.5,
|
|
259
|
+
strokeLinecap: 'round',
|
|
260
|
+
strokeLinejoin: 'round',
|
|
261
|
+
}),
|
|
262
|
+
)
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function StatusLine({ t, view, color }) {
|
|
266
|
+
return h(
|
|
267
|
+
'span',
|
|
268
|
+
{
|
|
269
|
+
className: 'dsa-line',
|
|
270
|
+
style: {
|
|
271
|
+
color: view.tone === 'error'
|
|
272
|
+
? 'var(--dsw-alias-state-error-primary)'
|
|
273
|
+
: 'var(--dsw-alias-label-secondary)',
|
|
274
|
+
},
|
|
275
|
+
},
|
|
276
|
+
h(Moon, { key: 'icon', color }),
|
|
277
|
+
h(
|
|
278
|
+
'span',
|
|
279
|
+
{ key: 'label', style: { overflow: 'hidden', textOverflow: 'ellipsis' } },
|
|
280
|
+
`${t('brand')} · ${t(`state.${view.key}`)}`,
|
|
281
|
+
),
|
|
282
|
+
view.tone === 'idle'
|
|
283
|
+
? null
|
|
284
|
+
: h('span', {
|
|
285
|
+
key: 'dot',
|
|
286
|
+
'aria-hidden': true,
|
|
287
|
+
style: { width: '5px', height: '5px', borderRadius: '50%', background: color, flex: '0 0 auto' },
|
|
288
|
+
}),
|
|
289
|
+
)
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function Indicator({ store, t }) {
|
|
293
|
+
const snapshot = useStore(store)
|
|
294
|
+
const view = classify(snapshot.status, snapshot.failures)
|
|
295
|
+
const color = toneColor(view.tone)
|
|
296
|
+
const textColor = view.tone === 'error'
|
|
297
|
+
? 'var(--dsw-alias-state-error-primary)'
|
|
298
|
+
: 'var(--dsw-alias-label-secondary)'
|
|
299
|
+
return h(
|
|
300
|
+
React.Fragment,
|
|
301
|
+
null,
|
|
302
|
+
stylesheet === null ? h('style', { key: 'css' }, CSS) : null,
|
|
303
|
+
h(
|
|
304
|
+
'button',
|
|
305
|
+
{
|
|
306
|
+
type: 'button',
|
|
307
|
+
className: 'dsa-indicator dsa-root',
|
|
308
|
+
'aria-expanded': snapshot.open === true,
|
|
309
|
+
'aria-haspopup': 'dialog',
|
|
310
|
+
style: { color: textColor },
|
|
311
|
+
onClick: () => store.toggle(),
|
|
312
|
+
},
|
|
313
|
+
h(StatusLine, { t, view, color }),
|
|
314
|
+
),
|
|
315
|
+
)
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function Panel({ store, t, act }) {
|
|
319
|
+
const snapshot = useStore(store)
|
|
320
|
+
const open = snapshot.open === true
|
|
321
|
+
useTick(open)
|
|
322
|
+
const status = snapshot.status
|
|
323
|
+
const failures = snapshot.failures
|
|
324
|
+
const view = classify(status, failures)
|
|
325
|
+
const unreachable = status === null || failures >= UNKNOWN_AFTER_FAILURES
|
|
326
|
+
const enabled = status === null ? true : status.enabled !== false
|
|
327
|
+
const allowDisplaySleep = (status === null ? 'system' : status.scope) === 'system'
|
|
328
|
+
const busy = snapshot.busy === true
|
|
329
|
+
const staleHost = status !== null && status.protocol !== 2
|
|
330
|
+
const counts = status && status.counts ? status.counts : { agents: 0, maintenance: 0, jobs: 0 }
|
|
331
|
+
const holdLeft = status && typeof status.manualUntil === 'number' && status.manualUntil > Date.now()
|
|
332
|
+
? status.manualUntil - Date.now()
|
|
333
|
+
: null
|
|
334
|
+
|
|
335
|
+
React.useEffect(() => {
|
|
336
|
+
if (!open) return undefined
|
|
337
|
+
const onKey = (event) => {
|
|
338
|
+
if (event.key === 'Escape') store.close()
|
|
339
|
+
}
|
|
340
|
+
const onPointerDown = (event) => {
|
|
341
|
+
const target = event.target
|
|
342
|
+
if (target && typeof target.closest === 'function' && (target.closest('.dsa-panel') || target.closest('.dsa-root'))) return
|
|
343
|
+
store.close()
|
|
344
|
+
}
|
|
345
|
+
globalThis.document?.addEventListener('keydown', onKey)
|
|
346
|
+
globalThis.document?.addEventListener('mousedown', onPointerDown, true)
|
|
347
|
+
return () => {
|
|
348
|
+
globalThis.document?.removeEventListener('keydown', onKey)
|
|
349
|
+
globalThis.document?.removeEventListener('mousedown', onPointerDown, true)
|
|
350
|
+
}
|
|
351
|
+
}, [open, store])
|
|
352
|
+
|
|
353
|
+
if (!open) return null
|
|
354
|
+
|
|
355
|
+
const rows = [
|
|
356
|
+
[t('detail.state'), t(`state.${view.key}`)],
|
|
357
|
+
[t('detail.sources'), `${t('sources.agents')} ${counts.agents ?? 0} · ${t('sources.maintenance')} ${counts.maintenance ?? 0} · ${t('sources.jobs')} ${counts.jobs ?? 0}`],
|
|
358
|
+
[t('detail.scope'), allowDisplaySleep ? t('scope.system') : t('scope.display')],
|
|
359
|
+
[t('detail.backend'), status && status.backend ? status.backend : t('unit.none')],
|
|
360
|
+
]
|
|
361
|
+
if (status && typeof status.since === 'number' && status.since > 0) {
|
|
362
|
+
rows.push([t('detail.since'), formatDuration(Date.now() - status.since)])
|
|
363
|
+
}
|
|
364
|
+
if (holdLeft !== null) rows.push([t('panel.hold'), `${t('panel.holdLeft')} ${formatDuration(holdLeft)}`])
|
|
365
|
+
const reason = status !== null && !unreachable ? errorText(t, status) : null
|
|
366
|
+
if (reason !== null) rows.push([t('detail.error'), reason])
|
|
367
|
+
|
|
368
|
+
return h(
|
|
369
|
+
'div',
|
|
370
|
+
{ className: 'dsa-panel', role: 'dialog', 'aria-label': t('brand') },
|
|
371
|
+
stylesheet === null ? h('style', { key: 'css' }, CSS) : null,
|
|
372
|
+
h(
|
|
373
|
+
'div',
|
|
374
|
+
{ className: 'dsa-head', key: 'head' },
|
|
375
|
+
h(StatusLine, { key: 'line', t, view, color: toneColor(view.tone) }),
|
|
376
|
+
h('button', {
|
|
377
|
+
type: 'button',
|
|
378
|
+
className: 'dsa-close',
|
|
379
|
+
title: t('panel.close'),
|
|
380
|
+
'aria-label': t('panel.close'),
|
|
381
|
+
onClick: () => store.close(),
|
|
382
|
+
}, '×'),
|
|
383
|
+
),
|
|
384
|
+
h(
|
|
385
|
+
'div',
|
|
386
|
+
{ className: 'dsa-box', key: 'status' },
|
|
387
|
+
unreachable
|
|
388
|
+
? h('div', { className: 'dsa-note dsa-error' }, t('hint.unreachable'))
|
|
389
|
+
: h('div', { className: 'dsa-grid' }, rows.flatMap(([key, value], index) => [
|
|
390
|
+
h('div', { key: `k${index}`, className: 'dsa-key' }, key),
|
|
391
|
+
h('div', { key: `v${index}`, className: 'dsa-val', title: value }, value),
|
|
392
|
+
])),
|
|
393
|
+
),
|
|
394
|
+
h(
|
|
395
|
+
'div',
|
|
396
|
+
{ className: 'dsa-box', key: 'toggles' },
|
|
397
|
+
h(
|
|
398
|
+
'div',
|
|
399
|
+
{ className: 'dsa-row' },
|
|
400
|
+
h('span', { className: 'dsa-grow' }, t('panel.enabled')),
|
|
401
|
+
h('button', {
|
|
402
|
+
type: 'button',
|
|
403
|
+
className: 'dsa-switch',
|
|
404
|
+
role: 'switch',
|
|
405
|
+
'aria-checked': enabled,
|
|
406
|
+
'aria-label': t('panel.enabled'),
|
|
407
|
+
disabled: busy || unreachable,
|
|
408
|
+
onClick: () => act({ action: 'set-enabled', enabled: !enabled }),
|
|
409
|
+
}),
|
|
410
|
+
),
|
|
411
|
+
h(
|
|
412
|
+
'div',
|
|
413
|
+
{ className: 'dsa-row' },
|
|
414
|
+
h('span', { className: 'dsa-grow' },
|
|
415
|
+
h('div', null, t('panel.allowDisplaySleep')),
|
|
416
|
+
h('div', { className: 'dsa-sub' }, t('panel.allowDisplaySleepHint')),
|
|
417
|
+
),
|
|
418
|
+
h('button', {
|
|
419
|
+
type: 'button',
|
|
420
|
+
className: 'dsa-switch',
|
|
421
|
+
role: 'switch',
|
|
422
|
+
'aria-checked': allowDisplaySleep,
|
|
423
|
+
'aria-label': t('panel.allowDisplaySleep'),
|
|
424
|
+
disabled: busy || unreachable,
|
|
425
|
+
onClick: () => act({ action: 'set-scope', scope: allowDisplaySleep ? 'display' : 'system' }),
|
|
426
|
+
}),
|
|
427
|
+
),
|
|
428
|
+
),
|
|
429
|
+
h(
|
|
430
|
+
'div',
|
|
431
|
+
{ className: 'dsa-box', key: 'hold' },
|
|
432
|
+
h('div', { className: 'dsa-sub' }, t('panel.hold')),
|
|
433
|
+
h(
|
|
434
|
+
'div',
|
|
435
|
+
{ className: 'dsa-actions' },
|
|
436
|
+
h('button', {
|
|
437
|
+
type: 'button',
|
|
438
|
+
className: 'dsa-button',
|
|
439
|
+
disabled: busy || unreachable || !enabled,
|
|
440
|
+
title: enabled ? undefined : t('panel.holdDisabled'),
|
|
441
|
+
onClick: () => act({ action: 'hold', minutes: 30 }),
|
|
442
|
+
}, t('panel.hold30')),
|
|
443
|
+
h('button', {
|
|
444
|
+
type: 'button',
|
|
445
|
+
className: 'dsa-button',
|
|
446
|
+
disabled: busy || unreachable || !enabled,
|
|
447
|
+
title: enabled ? undefined : t('panel.holdDisabled'),
|
|
448
|
+
onClick: () => act({ action: 'hold', minutes: 120 }),
|
|
449
|
+
}, t('panel.hold120')),
|
|
450
|
+
holdLeft === null
|
|
451
|
+
? null
|
|
452
|
+
: h('button', {
|
|
453
|
+
type: 'button',
|
|
454
|
+
className: 'dsa-button',
|
|
455
|
+
disabled: busy || unreachable,
|
|
456
|
+
onClick: () => act({ action: 'cancel-hold' }),
|
|
457
|
+
}, t('panel.holdCancel')),
|
|
458
|
+
),
|
|
459
|
+
snapshot.notice === 'saved'
|
|
460
|
+
? h('div', { className: 'dsa-note' }, t('panel.saved'))
|
|
461
|
+
: snapshot.notice === 'failed'
|
|
462
|
+
? h('div', { className: 'dsa-note dsa-error' }, t('panel.saveFailed'))
|
|
463
|
+
: null,
|
|
464
|
+
),
|
|
465
|
+
h(
|
|
466
|
+
'div',
|
|
467
|
+
{ className: 'dsa-foot', key: 'foot' },
|
|
468
|
+
h('span', {
|
|
469
|
+
className: 'dsa-grow',
|
|
470
|
+
style: { overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' },
|
|
471
|
+
}, t('panel.advanced')),
|
|
472
|
+
h('a', {
|
|
473
|
+
className: 'dsa-icon-link',
|
|
474
|
+
href: REPO_URL,
|
|
475
|
+
target: '_blank',
|
|
476
|
+
rel: 'noreferrer noopener',
|
|
477
|
+
title: t('panel.repo'),
|
|
478
|
+
'aria-label': t('panel.repo'),
|
|
479
|
+
onClick: (event) => event.stopPropagation(),
|
|
480
|
+
}, h(RepoIcon, null)),
|
|
481
|
+
status && typeof status.version === 'string'
|
|
482
|
+
? h('a', {
|
|
483
|
+
className: 'dsa-version',
|
|
484
|
+
href: `${NPM_URL}${status.version}`,
|
|
485
|
+
target: '_blank',
|
|
486
|
+
rel: 'noreferrer noopener',
|
|
487
|
+
title: t('panel.npm'),
|
|
488
|
+
'aria-label': t('panel.npm'),
|
|
489
|
+
onClick: (event) => event.stopPropagation(),
|
|
490
|
+
}, `v${status.version}`)
|
|
491
|
+
: null,
|
|
492
|
+
),
|
|
493
|
+
)
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
return {
|
|
497
|
+
inject: ['slots', 'locale'],
|
|
498
|
+
apply(ctx) {
|
|
499
|
+
const t = ctx.locale.bind(NS)
|
|
500
|
+
|
|
501
|
+
ctx.effect(() => {
|
|
502
|
+
const disposers = []
|
|
503
|
+
try {
|
|
504
|
+
disposers.push(ctx.locale.register(NS, { zh: ZH, en: EN }))
|
|
505
|
+
} catch {
|
|
506
|
+
for (const [locale, dict] of [['zh', ZH], ['en', EN]]) {
|
|
507
|
+
try {
|
|
508
|
+
disposers.push(ctx.locale.register(NS, locale, dict))
|
|
509
|
+
} catch {
|
|
510
|
+
// A duplicate owner is not fatal.
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
return () => {
|
|
515
|
+
for (const dispose of disposers) {
|
|
516
|
+
try {
|
|
517
|
+
dispose()
|
|
518
|
+
} catch {
|
|
519
|
+
// best effort
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
}, 'stay-awake: dictionaries')
|
|
524
|
+
|
|
525
|
+
if (stylesheet !== null) {
|
|
526
|
+
ctx.effect(() => stylesheet.insert(CSS), 'stay-awake: styles')
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
const store = createStore()
|
|
530
|
+
|
|
531
|
+
ctx.effect(() => {
|
|
532
|
+
try {
|
|
533
|
+
return ctx.locale.subscribe(() => store.set({ localeRevision: store.get().localeRevision + 1 }))
|
|
534
|
+
} catch {
|
|
535
|
+
return undefined
|
|
536
|
+
}
|
|
537
|
+
}, 'stay-awake: locale')
|
|
538
|
+
|
|
539
|
+
// One poller for the whole page: several sessions share one state.
|
|
540
|
+
ctx.effect(() => {
|
|
541
|
+
let cancelled = false
|
|
542
|
+
let timer = null
|
|
543
|
+
let delay = POLL_MIN_MS
|
|
544
|
+
|
|
545
|
+
async function poll() {
|
|
546
|
+
if (cancelled) return
|
|
547
|
+
if (globalThis.document?.visibilityState === 'hidden') {
|
|
548
|
+
timer = globalThis.setTimeout(poll, POLL_MAX_MS)
|
|
549
|
+
return
|
|
550
|
+
}
|
|
551
|
+
try {
|
|
552
|
+
const response = await globalThis.fetch(ROUTE, {
|
|
553
|
+
credentials: 'same-origin',
|
|
554
|
+
headers: { accept: 'application/json' },
|
|
555
|
+
})
|
|
556
|
+
if (!response.ok) throw new Error(`HTTP ${response.status}`)
|
|
557
|
+
const value = await response.json()
|
|
558
|
+
if (cancelled) return
|
|
559
|
+
delay = POLL_MIN_MS
|
|
560
|
+
const clearStale = store.get().notice === 'stale' && value.protocol === 2
|
|
561
|
+
store.set({ status: value, failures: 0, ...(clearStale ? { notice: null, noticeStatus: null } : {}) })
|
|
562
|
+
} catch {
|
|
563
|
+
if (cancelled) return
|
|
564
|
+
delay = Math.min(Math.max(delay * 2, POLL_MIN_MS), POLL_MAX_MS)
|
|
565
|
+
store.set({ failures: store.get().failures + 1 })
|
|
566
|
+
}
|
|
567
|
+
if (cancelled) return
|
|
568
|
+
timer = globalThis.setTimeout(poll, delay)
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
poll()
|
|
572
|
+
|
|
573
|
+
const onVisibility = () => {
|
|
574
|
+
if (globalThis.document?.visibilityState !== 'visible') return
|
|
575
|
+
if (timer !== null) globalThis.clearTimeout(timer)
|
|
576
|
+
delay = POLL_MIN_MS
|
|
577
|
+
poll()
|
|
578
|
+
}
|
|
579
|
+
globalThis.document?.addEventListener('visibilitychange', onVisibility)
|
|
580
|
+
|
|
581
|
+
return () => {
|
|
582
|
+
cancelled = true
|
|
583
|
+
if (timer !== null) globalThis.clearTimeout(timer)
|
|
584
|
+
globalThis.document?.removeEventListener('visibilitychange', onVisibility)
|
|
585
|
+
}
|
|
586
|
+
}, 'stay-awake: status polling')
|
|
587
|
+
|
|
588
|
+
let noticeTimer = null
|
|
589
|
+
function flashNotice(notice, noticeStatus) {
|
|
590
|
+
if (noticeTimer !== null) globalThis.clearTimeout(noticeTimer)
|
|
591
|
+
store.set({ notice, noticeStatus })
|
|
592
|
+
if (notice === 'saved') {
|
|
593
|
+
noticeTimer = globalThis.setTimeout(() => {
|
|
594
|
+
noticeTimer = null
|
|
595
|
+
store.set({ notice: null, noticeStatus: null })
|
|
596
|
+
}, 2500)
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
async function act(body) {
|
|
601
|
+
store.set({ busy: true, notice: null, noticeStatus: null })
|
|
602
|
+
let statusCode = 'network'
|
|
603
|
+
try {
|
|
604
|
+
const response = await globalThis.fetch(ROUTE, {
|
|
605
|
+
method: 'POST',
|
|
606
|
+
credentials: 'same-origin',
|
|
607
|
+
headers: { 'content-type': 'application/json', accept: 'application/json' },
|
|
608
|
+
body: JSON.stringify(body),
|
|
609
|
+
})
|
|
610
|
+
statusCode = String(response.status)
|
|
611
|
+
if (!response.ok) {
|
|
612
|
+
let reason = null
|
|
613
|
+
try {
|
|
614
|
+
reason = (await response.json())?.error ?? null
|
|
615
|
+
} catch {
|
|
616
|
+
reason = null
|
|
617
|
+
}
|
|
618
|
+
throw new Error(reason === 'invalid-action' ? 'stale-host' : `HTTP ${response.status}`)
|
|
619
|
+
}
|
|
620
|
+
const value = await response.json()
|
|
621
|
+
store.set({ status: value, failures: 0, busy: false })
|
|
622
|
+
flashNotice('saved', null)
|
|
623
|
+
} catch (error) {
|
|
624
|
+
const stale = error instanceof Error && error.message === 'stale-host'
|
|
625
|
+
store.set({ busy: false })
|
|
626
|
+
flashNotice(stale ? 'stale' : 'failed', statusCode)
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
ctx.slots.inject('conversation.composer.dock', () =>
|
|
631
|
+
ctx.slots.register(
|
|
632
|
+
{ name: 'conversation.composer.dock', id: 'stay-awake', order: 20 },
|
|
633
|
+
function DockIndicator() {
|
|
634
|
+
return h(Indicator, { store, t })
|
|
635
|
+
},
|
|
636
|
+
),
|
|
637
|
+
)
|
|
638
|
+
|
|
639
|
+
ctx.slots.inject('shell.overlay', () =>
|
|
640
|
+
ctx.slots.register(
|
|
641
|
+
{ name: 'shell.overlay', id: 'stay-awake-panel', order: 40 },
|
|
642
|
+
function StayAwakePanel() {
|
|
643
|
+
return h(Panel, { store, t, act })
|
|
644
|
+
},
|
|
645
|
+
),
|
|
646
|
+
)
|
|
647
|
+
},
|
|
648
|
+
}
|
|
649
|
+
},
|
|
650
|
+
})
|
package/cordis.patch.yml
ADDED