|
|
|
@@ -49,6 +49,18 @@ window.__ModuleLoader__.load({
|
|
|
|
|
/** In-app popup lifetime, and how many may stack. */
|
|
|
|
|
const TOAST_DURATION_MS = 6000
|
|
|
|
|
const TOAST_LIMIT = 3
|
|
|
|
|
/** Notifications kept referenced, so a collection can never cancel a pending display. */
|
|
|
|
|
const RAISED_LIMIT = 8
|
|
|
|
|
/** Alerts carried by the system channel and still worth replaying in-app on return. */
|
|
|
|
|
const REPLAY_LIMIT = 8
|
|
|
|
|
/** How long a system notification outlives the moment the user comes back. */
|
|
|
|
|
const REPLAY_TTL_MS = 30 * 60 * 1000
|
|
|
|
|
/**
|
|
|
|
|
* A confirmed banner that the user came back to within this window is treated as
|
|
|
|
|
* seen: the desktop notification is still on screen at that moment, so replaying
|
|
|
|
|
* it in-app would be a duplicate rather than a reminder.
|
|
|
|
|
*/
|
|
|
|
|
const REPLAY_QUIET_MS = 20 * 1000
|
|
|
|
|
|
|
|
|
|
const zh = {
|
|
|
|
|
'notification.completion': '会话已完成',
|
|
|
|
@@ -73,7 +85,8 @@ window.__ModuleLoader__.load({
|
|
|
|
|
'settings.permission.hint.mac': '系统设置 → 通知 → DeepSeek Harness',
|
|
|
|
|
'settings.permission.hint.linux': '在桌面环境的通知设置(GNOME / KDE)中允许 DeepSeek Harness',
|
|
|
|
|
'settings.action.allow': '允许通知',
|
|
|
|
|
'settings.action.test': '测试通知',
|
|
|
|
|
'settings.action.testSystem': '测试系统通知',
|
|
|
|
|
'settings.action.testAny': '测试提醒',
|
|
|
|
|
'settings.action.unsupported': '不可用',
|
|
|
|
|
'settings.permission.promptFailed': '无法请求通知权限:{message}',
|
|
|
|
|
'settings.health.watching': '正在监听会话状态',
|
|
|
|
@@ -84,12 +97,27 @@ window.__ModuleLoader__.load({
|
|
|
|
|
'settings.kind.completion': '完成',
|
|
|
|
|
'settings.kind.approval': '授权',
|
|
|
|
|
'settings.kind.question': '提问',
|
|
|
|
|
'config.title': '会话通知',
|
|
|
|
|
'config.note': '只在会话需要你注意时提醒,而且只在 DSH 运行时。',
|
|
|
|
|
'config.section.kinds': '提醒内容',
|
|
|
|
|
'config.section.permission': '系统通知权限',
|
|
|
|
|
'config.section.status': '运行状态',
|
|
|
|
|
'config.intro': '提醒都从当前页面发出:窗口不在前台时是系统通知,窗口在前台时是这里的轻弹窗。',
|
|
|
|
|
'config.saveNote': '开关保存在浏览器本地,重装插件不会丢失。',
|
|
|
|
|
'config.testHint': '这里的按钮直接走系统通知通道,用来验证系统通知本身是否可用(窗口在前台也照发)。',
|
|
|
|
|
'config.testAny': '测试提醒',
|
|
|
|
|
'config.testSystem': '测试系统通知',
|
|
|
|
|
'config.diag.window': '窗口状态',
|
|
|
|
|
'config.diag.inFront': '在前台(会走轻弹窗)',
|
|
|
|
|
'config.diag.away': '不在前台(会走系统通知)',
|
|
|
|
|
'config.diag.last': '最近一次投递',
|
|
|
|
|
'config.diag.none': '还没有投递过',
|
|
|
|
|
'config.diag.systemShown': '系统通知(系统已确认弹出)',
|
|
|
|
|
'config.diag.systemUnconfirmed': '系统通知(系统没有回报,回到窗口时补发轻弹窗)',
|
|
|
|
|
'config.diag.systemFailed': '系统通知没弹出来,回到窗口时补发轻弹窗',
|
|
|
|
|
'config.diag.replayDelivery': '应用内轻弹窗(回到窗口时补发)',
|
|
|
|
|
'config.diag.popup': '应用内轻弹窗',
|
|
|
|
|
'config.diag.refused': '系统通知被拒绝,改用了轻弹窗',
|
|
|
|
|
'config.diag.replay': '等你回到窗口时补发:{count} 条',
|
|
|
|
|
'config.diag.replayNone': '没有待补发的提醒',
|
|
|
|
|
}
|
|
|
|
|
const en = {
|
|
|
|
|
'notification.completion': 'Conversation finished',
|
|
|
|
@@ -114,7 +142,8 @@ window.__ModuleLoader__.load({
|
|
|
|
|
'settings.permission.hint.mac': 'System Settings → Notifications → DeepSeek Harness',
|
|
|
|
|
'settings.permission.hint.linux': 'Allow DeepSeek Harness in your desktop notification settings (GNOME / KDE)',
|
|
|
|
|
'settings.action.allow': 'Allow notifications',
|
|
|
|
|
'settings.action.test': 'Test notification',
|
|
|
|
|
'settings.action.testSystem': 'Test system notification',
|
|
|
|
|
'settings.action.testAny': 'Test alert',
|
|
|
|
|
'settings.action.unsupported': 'Unavailable',
|
|
|
|
|
'settings.permission.promptFailed': 'Could not request notification permission: {message}',
|
|
|
|
|
'settings.health.watching': 'Watching session status',
|
|
|
|
@@ -125,12 +154,27 @@ window.__ModuleLoader__.load({
|
|
|
|
|
'settings.kind.completion': 'Finished',
|
|
|
|
|
'settings.kind.approval': 'Approval',
|
|
|
|
|
'settings.kind.question': 'Questions',
|
|
|
|
|
'config.title': 'Session notifications',
|
|
|
|
|
'config.note': 'Alerts you when a conversation needs attention, while DSH is running.',
|
|
|
|
|
'config.section.kinds': 'What to notify about',
|
|
|
|
|
'config.section.permission': 'System notification permission',
|
|
|
|
|
'config.section.status': 'Status',
|
|
|
|
|
'config.intro': 'Every alert comes from this page: a system notification while the window is in the background, the light popup here while it is in the foreground.',
|
|
|
|
|
'config.saveNote': 'The switches are stored in this browser and survive reinstalling the plugin.',
|
|
|
|
|
'config.testHint': 'The buttons here go straight to the system channel, so you can verify it on its own (they fire even while the window is in front).',
|
|
|
|
|
'config.testAny': 'Test alert',
|
|
|
|
|
'config.testSystem': 'Test system notification',
|
|
|
|
|
'config.diag.window': 'Window',
|
|
|
|
|
'config.diag.inFront': 'in front (popup is used)',
|
|
|
|
|
'config.diag.away': 'not in front (system notification is used)',
|
|
|
|
|
'config.diag.last': 'Last delivery',
|
|
|
|
|
'config.diag.none': 'nothing delivered yet',
|
|
|
|
|
'config.diag.systemShown': 'system notification (confirmed on screen)',
|
|
|
|
|
'config.diag.systemUnconfirmed': 'system notification (no confirmation, replayed in-app on return)',
|
|
|
|
|
'config.diag.systemFailed': 'system notification never appeared, replayed in-app on return',
|
|
|
|
|
'config.diag.replayDelivery': 'in-app popup (replayed when you came back)',
|
|
|
|
|
'config.diag.popup': 'in-app popup',
|
|
|
|
|
'config.diag.refused': 'system channel refused, popup was used instead',
|
|
|
|
|
'config.diag.replay': 'waiting for you to come back: {count}',
|
|
|
|
|
'config.diag.replayNone': 'nothing waiting to be replayed',
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@@ -311,13 +355,33 @@ window.__ModuleLoader__.load({
|
|
|
|
|
return 'default'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Whether the Harness window is somewhere the user cannot see the app.
|
|
|
|
|
*
|
|
|
|
|
* Two independent browser facts are consulted, and ANY of them counts as "not
|
|
|
|
|
* in the foreground": the page's visibility and the document's focus. They are
|
|
|
|
|
* read at delivery time rather than remembered from an event, because a missed
|
|
|
|
|
* blur in the desktop shell made this plugin believe the window was in front
|
|
|
|
|
* and swallow the alert.
|
|
|
|
|
* @returns whether the alert must go to the system notification channel.
|
|
|
|
|
*/
|
|
|
|
|
function windowIsAway() {
|
|
|
|
|
try {
|
|
|
|
|
if (document.visibilityState === 'hidden') return true
|
|
|
|
|
} catch {
|
|
|
|
|
/* an unreadable visibility state leaves the focus fact */
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
if (typeof document.hasFocus === 'function') return !document.hasFocus()
|
|
|
|
|
} catch {
|
|
|
|
|
/* an unreadable focus state leaves the default below */
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Read the window's live focus state rather than a cached value. */
|
|
|
|
|
function currentFocus() {
|
|
|
|
|
try {
|
|
|
|
|
return typeof document.hasFocus === 'function' ? document.hasFocus() : true
|
|
|
|
|
} catch {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return !windowIsAway()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Focus the window without letting a refusal stop the navigation. */
|
|
|
|
@@ -506,9 +570,10 @@ window.__ModuleLoader__.load({
|
|
|
|
|
toasts: [],
|
|
|
|
|
promptError: '',
|
|
|
|
|
health: { state: 'starting', message: '' },
|
|
|
|
|
lastDelivery: null,
|
|
|
|
|
})
|
|
|
|
|
const notify = (message) => {
|
|
|
|
|
report(`session-notify: ${message}`)
|
|
|
|
|
report(message)
|
|
|
|
|
}
|
|
|
|
|
/** Every component translates through this: the slot's own seat when the owner supplies one, else the locale service. */
|
|
|
|
|
const seatFor = (props) => (typeof props.t === 'function' ? props.t : t)
|
|
|
|
@@ -520,10 +585,14 @@ window.__ModuleLoader__.load({
|
|
|
|
|
settleTimer: 0,
|
|
|
|
|
lastDelivery: 0,
|
|
|
|
|
toastSeq: 0,
|
|
|
|
|
deliverySeq: 0,
|
|
|
|
|
/** System notifications this page still holds open, so nothing collects them mid-display. */
|
|
|
|
|
raised: new Set(),
|
|
|
|
|
/** Alerts the user has not been able to see yet, replayed in-app when the window returns. */
|
|
|
|
|
replay: [],
|
|
|
|
|
live: {
|
|
|
|
|
list: undefined,
|
|
|
|
|
status: undefined,
|
|
|
|
|
focus: true,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
const timers = new Map()
|
|
|
|
@@ -634,12 +703,48 @@ window.__ModuleLoader__.load({
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Raise one system notification, tolerating every refusal a browser may give. */
|
|
|
|
|
/**
|
|
|
|
|
* The system-notification channel's own answer about one notification.
|
|
|
|
|
*
|
|
|
|
|
* A browser reports the platform's verdict asynchronously: `show` means the
|
|
|
|
|
* notification reached the desktop, `error` means the platform refused it, and
|
|
|
|
|
* silence means neither answer arrived. That third case is the one that used to
|
|
|
|
|
* lose alerts silently, so it is recorded rather than assumed to be a success.
|
|
|
|
|
* @param monitor - the record returned by {@link systemNotify} for this notification.
|
|
|
|
|
* @param shown - `true` when the platform displayed it, `false` when it refused.
|
|
|
|
|
*/
|
|
|
|
|
const confirmDelivery = (monitor, shown) => {
|
|
|
|
|
if (monitor === null || monitor === undefined || typeof monitor !== 'object') return
|
|
|
|
|
monitor.shown = shown
|
|
|
|
|
const snapshot = store.getSnapshot()
|
|
|
|
|
const last = snapshot.lastDelivery
|
|
|
|
|
if (last === null || last === undefined || last.monitor !== monitor || last.shown === shown) return
|
|
|
|
|
store.set({ ...snapshot, lastDelivery: { ...last, shown } })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Hold one raised notification open, bounded, so it cannot be collected while pending. */
|
|
|
|
|
const keepRaised = (notification) => {
|
|
|
|
|
state.raised.add(notification)
|
|
|
|
|
while (state.raised.size > RAISED_LIMIT) {
|
|
|
|
|
const oldest = state.raised.values().next().value
|
|
|
|
|
if (oldest === undefined) break
|
|
|
|
|
state.raised.delete(oldest)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Raise one system notification, tolerating every refusal a browser may give,
|
|
|
|
|
* and report what the platform said about it.
|
|
|
|
|
* @returns a record whose `outcome` is the channel's verdict (`raised`,
|
|
|
|
|
* `permission`, `unsupported`, or `threw`) and whose `shown` is filled in
|
|
|
|
|
* later by {@link confirmDelivery} — `true` displayed, `false` refused,
|
|
|
|
|
* `undefined` while the platform has said nothing.
|
|
|
|
|
*/
|
|
|
|
|
const systemNotify = (candidate, force) => {
|
|
|
|
|
const Ctor = globalThis.Notification
|
|
|
|
|
const permission = notificationPermission()
|
|
|
|
|
if (permission === 'unsupported') return false
|
|
|
|
|
if (!force && permission !== 'granted') return false
|
|
|
|
|
if (permission === 'unsupported') return { outcome: 'unsupported' }
|
|
|
|
|
if (!force && permission !== 'granted') return { outcome: 'permission' }
|
|
|
|
|
try {
|
|
|
|
|
const notice = copyFor(candidate)
|
|
|
|
|
const options = {
|
|
|
|
@@ -649,7 +754,20 @@ window.__ModuleLoader__.load({
|
|
|
|
|
}
|
|
|
|
|
if (systemIcon !== undefined) options.icon = systemIcon
|
|
|
|
|
const notification = new Ctor(notice.title, options)
|
|
|
|
|
const record = { outcome: 'raised', notice, shown: undefined }
|
|
|
|
|
keepRaised(notification)
|
|
|
|
|
notification.onshow = () => {
|
|
|
|
|
confirmDelivery(record, true)
|
|
|
|
|
}
|
|
|
|
|
notification.onerror = (event) => {
|
|
|
|
|
confirmDelivery(record, false)
|
|
|
|
|
notify(`system notification was refused: ${String(event?.message ?? 'unknown')}`)
|
|
|
|
|
}
|
|
|
|
|
notification.onclose = () => {
|
|
|
|
|
state.raised.delete(notification)
|
|
|
|
|
}
|
|
|
|
|
notification.onclick = () => {
|
|
|
|
|
dropReplay(candidate)
|
|
|
|
|
focusWindow()
|
|
|
|
|
openSession(candidate.sessionId)
|
|
|
|
|
try {
|
|
|
|
@@ -658,13 +776,70 @@ window.__ModuleLoader__.load({
|
|
|
|
|
/* close is best effort on every platform */
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
return record
|
|
|
|
|
} catch (error) {
|
|
|
|
|
notify(`system notification failed: ${text(error)}`)
|
|
|
|
|
return false
|
|
|
|
|
return { outcome: 'threw' }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------ replay
|
|
|
|
|
|
|
|
|
|
/** Forget one queued replay: the user has just answered that alert in the system channel. */
|
|
|
|
|
const dropReplay = (candidate) => {
|
|
|
|
|
const next = state.replay.filter((entry) => (
|
|
|
|
|
entry.candidate.sessionId !== candidate.sessionId || entry.candidate.kind !== candidate.kind
|
|
|
|
|
))
|
|
|
|
|
if (next.length === state.replay.length) return
|
|
|
|
|
state.replay = next
|
|
|
|
|
publish()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Keep one away-channel alert for an in-app replay.
|
|
|
|
|
*
|
|
|
|
|
* A desktop notification raised while nobody is looking at the machine is
|
|
|
|
|
* easy to miss: its banner lives a few seconds, its history lives in the
|
|
|
|
|
* system's own notification centre, which the user may never open, and a
|
|
|
|
|
* refused display leaves no trace at all on the page. The alert is therefore
|
|
|
|
|
* replayed as a light in-app popup as soon as the window is back in the
|
|
|
|
|
* foreground, except when the platform confirmed the banner and the user was
|
|
|
|
|
* back within {@link REPLAY_QUIET_MS} — then the banner is still on screen and
|
|
|
|
|
* a second surface would only be noise. Entries older than
|
|
|
|
|
* {@link REPLAY_TTL_MS} are dropped instead of waiting for a user who has
|
|
|
|
|
* moved on.
|
|
|
|
|
* @param candidate - the alert to keep for a replay.
|
|
|
|
|
* @param monitor - the system channel's own record, so its confirmation can be read later.
|
|
|
|
|
*/
|
|
|
|
|
const holdForReplay = (candidate, monitor) => {
|
|
|
|
|
const now = Date.now()
|
|
|
|
|
state.replay = [
|
|
|
|
|
...state.replay.filter((entry) => (
|
|
|
|
|
entry.candidate.sessionId !== candidate.sessionId || entry.candidate.kind !== candidate.kind
|
|
|
|
|
)),
|
|
|
|
|
{ candidate, at: now, monitor },
|
|
|
|
|
].slice(-REPLAY_LIMIT)
|
|
|
|
|
publish()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Show every away-channel alert the user has not had a chance to see yet. */
|
|
|
|
|
const flushReplay = () => {
|
|
|
|
|
if (state.replay.length === 0) return
|
|
|
|
|
const now = Date.now()
|
|
|
|
|
const waiting = state.replay
|
|
|
|
|
state.replay = []
|
|
|
|
|
const due = waiting.filter((entry) => {
|
|
|
|
|
if (now - entry.at > REPLAY_TTL_MS) return false
|
|
|
|
|
// A banner that the platform confirmed and that is still inside its own
|
|
|
|
|
// visible window has already told the user; do not say it twice.
|
|
|
|
|
return !(entry.monitor?.shown === true && now - entry.at <= REPLAY_QUIET_MS)
|
|
|
|
|
})
|
|
|
|
|
const shown = due.slice(-TOAST_LIMIT)
|
|
|
|
|
for (const entry of shown) showToast(entry.candidate)
|
|
|
|
|
if (shown.length > 0) recordDelivery('replay', shown[shown.length - 1].candidate)
|
|
|
|
|
else publish()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -------------------------------------------------------------- delivery
|
|
|
|
|
|
|
|
|
|
/** Whether the user is looking at this exact conversation right now. */
|
|
|
|
@@ -677,6 +852,37 @@ window.__ModuleLoader__.load({
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Record what actually happened to the newest alert.
|
|
|
|
|
*
|
|
|
|
|
* Everything on the delivery path used to fail invisibly: the alert simply
|
|
|
|
|
* never appeared, with no way to tell a wrong channel decision from a
|
|
|
|
|
* refused constructor. The config page shows this record, so the next
|
|
|
|
|
* question ("did it even try?") has an answer.
|
|
|
|
|
*
|
|
|
|
|
* The time is the user's own clock, not UTC: a log line the reader has to
|
|
|
|
|
* translate by eight hours is worse than no log line.
|
|
|
|
|
* @param outcome - which channel carried the alert, or why it fell back.
|
|
|
|
|
* @param candidate - the alert being recorded.
|
|
|
|
|
* @param monitor - the system channel's own record, when that is the channel.
|
|
|
|
|
* @returns the stored record, so a later platform answer can update it.
|
|
|
|
|
*/
|
|
|
|
|
const recordDelivery = (outcome, candidate, monitor) => {
|
|
|
|
|
state.deliverySeq += 1
|
|
|
|
|
const now = new Date()
|
|
|
|
|
const pad = (value) => String(value).padStart(2, '0')
|
|
|
|
|
const record = {
|
|
|
|
|
seq: state.deliverySeq,
|
|
|
|
|
outcome,
|
|
|
|
|
at: `${pad(now.getHours())}:${pad(now.getMinutes())}:${pad(now.getSeconds())}`,
|
|
|
|
|
title: copyFor(candidate).title,
|
|
|
|
|
shown: monitor?.shown,
|
|
|
|
|
monitor,
|
|
|
|
|
}
|
|
|
|
|
store.set({ ...store.getSnapshot(), lastDelivery: record })
|
|
|
|
|
return record
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Whether a queued candidate is still worth delivering. Read from the LIVE
|
|
|
|
|
* snapshot rather than the one captured when the transition was seen: a
|
|
|
|
@@ -696,9 +902,15 @@ window.__ModuleLoader__.load({
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Deliver one candidate through exactly one channel: the system
|
|
|
|
|
* notification while the window is in the background, the light popup while
|
|
|
|
|
* it is in the foreground, and nothing at all for the conversation on
|
|
|
|
|
* screen.
|
|
|
|
|
* notification while the window is away, the light popup while it is in
|
|
|
|
|
* front, and nothing at all for the conversation on screen.
|
|
|
|
|
*
|
|
|
|
|
* The channel decision reads the window state right here, at delivery time,
|
|
|
|
|
* instead of trusting the state the observer last remembered — that is what
|
|
|
|
|
* an unreported blur used to defeat. An alert that leaves through the system
|
|
|
|
|
* channel is additionally held for an in-app replay, because a desktop banner
|
|
|
|
|
* raised while nobody is looking at the machine is a notification the user
|
|
|
|
|
* never actually receives.
|
|
|
|
|
*/
|
|
|
|
|
const deliver = (candidate, ignoreThrottle) => {
|
|
|
|
|
if (ignoreThrottle !== true) {
|
|
|
|
@@ -706,23 +918,37 @@ window.__ModuleLoader__.load({
|
|
|
|
|
if (now - state.lastDelivery < THROTTLE_MS) return
|
|
|
|
|
state.lastDelivery = now
|
|
|
|
|
}
|
|
|
|
|
if (state.live.focus) {
|
|
|
|
|
if (!isOnScreen(candidate.sessionId, state.live.list)) showToast(candidate)
|
|
|
|
|
if (!windowIsAway()) {
|
|
|
|
|
if (isOnScreen(candidate.sessionId, state.live.list)) return
|
|
|
|
|
showToast(candidate)
|
|
|
|
|
recordDelivery('popup', candidate)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if (systemNotify(candidate, candidate.test === true)) return
|
|
|
|
|
if (candidate.test === true) showToast(candidate)
|
|
|
|
|
const result = systemNotify(candidate, candidate.test === true)
|
|
|
|
|
if (result.outcome === 'raised') {
|
|
|
|
|
recordDelivery('system', candidate, result)
|
|
|
|
|
if (candidate.test !== true) holdForReplay(candidate, result)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// A test alert still has to reach the user, and so does a real one when
|
|
|
|
|
// the system channel refuses: the popup is the channel that remains.
|
|
|
|
|
showToast(candidate)
|
|
|
|
|
recordDelivery(`system-refused-${result.outcome}`, candidate)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Take the delivery decision on a later tick, from the freshest state.
|
|
|
|
|
*
|
|
|
|
|
* One candidate per tick keeps the throttle meaningful, and anything still
|
|
|
|
|
* queued arms its own follow-up tick — a burst used to leave every candidate
|
|
|
|
|
* after the first stranded in the queue with no timer to flush it.
|
|
|
|
|
*/
|
|
|
|
|
const flushSettle = () => {
|
|
|
|
|
state.settleTimer = 0
|
|
|
|
|
if (disposed) return
|
|
|
|
|
const candidate = state.pending.shift()
|
|
|
|
|
if (disposed || candidate === undefined) return
|
|
|
|
|
if (!stillWorth(candidate)) return
|
|
|
|
|
deliver(candidate, false)
|
|
|
|
|
if (candidate !== undefined && stillWorth(candidate)) deliver(candidate, false)
|
|
|
|
|
if (state.pending.length > 0) state.settleTimer = setTimeout(() => flushSettle(), SETTLE_MS)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Hold one candidate for the settle tick, collapsing a burst onto one timer. */
|
|
|
|
@@ -738,11 +964,13 @@ window.__ModuleLoader__.load({
|
|
|
|
|
* One derivation pass over the client's own session state: remember the
|
|
|
|
|
* freshest snapshot for the settle tick, compare it against what this page
|
|
|
|
|
* already observed, emit at most one candidate per transition, and let the
|
|
|
|
|
* settle tick decide delivery.
|
|
|
|
|
* settle tick decide delivery. The window state is deliberately NOT captured
|
|
|
|
|
* here — the delivery reads it fresh, because it can change inside the
|
|
|
|
|
* settle window.
|
|
|
|
|
*/
|
|
|
|
|
const observe = (focus, list, status) => {
|
|
|
|
|
const observe = (list, status) => {
|
|
|
|
|
if (list === undefined || status === undefined) return
|
|
|
|
|
state.live = { list, status, focus }
|
|
|
|
|
state.live = { list, status }
|
|
|
|
|
const kinds = store.getSnapshot().kinds
|
|
|
|
|
for (const sessionId of Object.keys(list.byId ?? {})) {
|
|
|
|
|
const summary = list.byId[sessionId]
|
|
|
|
@@ -802,18 +1030,36 @@ window.__ModuleLoader__.load({
|
|
|
|
|
|
|
|
|
|
// --------------------------------------------------------------- actions
|
|
|
|
|
|
|
|
|
|
/** Raise one test notification through the channel the current state selects. */
|
|
|
|
|
/** Build one test alert that skips the throttle and the "on screen" rule. */
|
|
|
|
|
const testCandidate = (title) => ({
|
|
|
|
|
kind: 'completion',
|
|
|
|
|
sessionId: 'test',
|
|
|
|
|
title,
|
|
|
|
|
detail: '',
|
|
|
|
|
pendingKind: '',
|
|
|
|
|
test: true,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Raise one test alert through whichever channel the window state selects.
|
|
|
|
|
* Verified from the config page, where the user is looking at the app, so it
|
|
|
|
|
* normally lands in the popup — the way to prove the system channel is to
|
|
|
|
|
* press the button in a channel-specific test instead.
|
|
|
|
|
*/
|
|
|
|
|
const sendTest = () => {
|
|
|
|
|
const candidate = {
|
|
|
|
|
kind: 'completion',
|
|
|
|
|
sessionId: 'test',
|
|
|
|
|
title: t('settings.title'),
|
|
|
|
|
detail: '',
|
|
|
|
|
pendingKind: '',
|
|
|
|
|
test: true,
|
|
|
|
|
deliver(testCandidate(t('config.testAny')), true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Raise the test alert on the system channel specifically, whatever the focus is. */
|
|
|
|
|
const sendTestSystem = () => {
|
|
|
|
|
const candidate = testCandidate(t('config.testSystem'))
|
|
|
|
|
const result = systemNotify(candidate, true)
|
|
|
|
|
if (result.outcome === 'raised') {
|
|
|
|
|
recordDelivery('system', candidate, result)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if (!currentFocus() && notificationPermission() === 'granted' && systemNotify(candidate, true)) return
|
|
|
|
|
showToast(candidate)
|
|
|
|
|
recordDelivery(`system-refused-${result.outcome}`, candidate)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Request notification permission inside a user gesture, then report the outcome. */
|
|
|
|
@@ -856,21 +1102,27 @@ window.__ModuleLoader__.load({
|
|
|
|
|
return React.useSyncExternalStore(source.subscribe, source.getSnapshot, source.getSnapshot)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** The window's focus state as a React value, kept current by the DOM's own events. */
|
|
|
|
|
function useFocus() {
|
|
|
|
|
/**
|
|
|
|
|
* Subscribe to every browser signal that says whether the app is visible:
|
|
|
|
|
* the window's focus events and the document's visibility change. The value
|
|
|
|
|
* itself is a boolean, so the observer re-renders once per transition.
|
|
|
|
|
*/
|
|
|
|
|
function useWindowState() {
|
|
|
|
|
const subscribe = React.useCallback((listener) => {
|
|
|
|
|
try {
|
|
|
|
|
window.addEventListener('focus', listener)
|
|
|
|
|
window.addEventListener('blur', listener)
|
|
|
|
|
document.addEventListener('visibilitychange', listener)
|
|
|
|
|
} catch {
|
|
|
|
|
return () => {}
|
|
|
|
|
}
|
|
|
|
|
return () => {
|
|
|
|
|
window.removeEventListener('focus', listener)
|
|
|
|
|
window.removeEventListener('blur', listener)
|
|
|
|
|
document.removeEventListener('visibilitychange', listener)
|
|
|
|
|
}
|
|
|
|
|
}, [])
|
|
|
|
|
return React.useSyncExternalStore(subscribe, currentFocus, () => true)
|
|
|
|
|
return React.useSyncExternalStore(subscribe, () => !windowIsAway(), () => true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@@ -878,9 +1130,13 @@ window.__ModuleLoader__.load({
|
|
|
|
|
* how a plugin reaches the session hooks without occupying visible UI. It
|
|
|
|
|
* renders `null`, and any failure in its own derivation is contained here so
|
|
|
|
|
* a slot entry can never crash.
|
|
|
|
|
*
|
|
|
|
|
* It subscribes to the window state only so a foreground/background change
|
|
|
|
|
* re-derives promptly; the delivery itself re-reads that state, so a missed
|
|
|
|
|
* event can no longer pick the wrong channel.
|
|
|
|
|
*/
|
|
|
|
|
function NotifyObserver(props) {
|
|
|
|
|
const focus = useFocus()
|
|
|
|
|
useWindowState()
|
|
|
|
|
const useSessions = props.useSessions
|
|
|
|
|
const useSessionStatus = props.useSessionStatus
|
|
|
|
|
const select = React.useCallback((snapshot) => snapshot, [])
|
|
|
|
@@ -896,7 +1152,7 @@ window.__ModuleLoader__.load({
|
|
|
|
|
applyHealth(store, { state: 'starting', message: '' })
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
observe(focus, list, status)
|
|
|
|
|
observe(list, status)
|
|
|
|
|
applyHealth(store, { state: 'watching', message: '' })
|
|
|
|
|
} catch (error) {
|
|
|
|
|
applyHealth(store, { state: 'error', message: text(error) })
|
|
|
|
@@ -1012,9 +1268,11 @@ window.__ModuleLoader__.load({
|
|
|
|
|
return h(React.Fragment, null,
|
|
|
|
|
h('style', null, CSS),
|
|
|
|
|
h('div', { className: 'dsn-sections' },
|
|
|
|
|
// The page header already shows this bundle's title and description, so
|
|
|
|
|
// this section opens with what the header cannot say: how alerts are
|
|
|
|
|
// carried, and what this form changes.
|
|
|
|
|
h('section', { className: 'dsn-section' },
|
|
|
|
|
h('h3', { className: 'dsn-section-title' }, tr('config.title')),
|
|
|
|
|
h('p', { className: 'dsn-section-desc' }, tr('config.note'))),
|
|
|
|
|
h('p', { className: 'dsn-section-desc' }, tr('config.intro'))),
|
|
|
|
|
h('section', { className: 'dsn-section' },
|
|
|
|
|
h('h3', { className: 'dsn-section-title' }, tr('config.section.kinds')),
|
|
|
|
|
KINDS.map((kind) => h(KindRow, {
|
|
|
|
@@ -1037,37 +1295,82 @@ window.__ModuleLoader__.load({
|
|
|
|
|
type: 'button',
|
|
|
|
|
className: 'dsn-button',
|
|
|
|
|
disabled: !supported,
|
|
|
|
|
onClick: permission === 'granted' ? props.test : props.ask,
|
|
|
|
|
onClick: permission === 'granted' ? props.testSystem : props.ask,
|
|
|
|
|
}, tr(!supported
|
|
|
|
|
? 'settings.action.unsupported'
|
|
|
|
|
: permission === 'granted' ? 'settings.action.test' : 'settings.action.allow')))),
|
|
|
|
|
: permission === 'granted' ? 'settings.action.testSystem' : 'settings.action.allow'))),
|
|
|
|
|
h('p', { className: 'dsn-section-desc' }, tr('config.testHint'))),
|
|
|
|
|
h('section', { className: 'dsn-section' },
|
|
|
|
|
h('h3', { className: 'dsn-section-title' }, tr('config.section.status')),
|
|
|
|
|
h('div', { className: 'dsn-row' },
|
|
|
|
|
h('div', { className: 'dsn-row-text' },
|
|
|
|
|
h('p', { className: 'dsn-status' }, `${tr('config.diag.window')}: ${tr(windowIsAway() ? 'config.diag.away' : 'config.diag.inFront')}`),
|
|
|
|
|
h('p', {
|
|
|
|
|
className: `dsn-status${healthFailed ? ' dsn-status-error' : ''}`,
|
|
|
|
|
role: healthFailed ? 'alert' : 'status',
|
|
|
|
|
}, healthText))))))
|
|
|
|
|
}, healthText),
|
|
|
|
|
h('p', { className: 'dsn-status' }, deliveryText(tr)),
|
|
|
|
|
h('p', { className: 'dsn-status' }, replayText(tr))),
|
|
|
|
|
h('button', {
|
|
|
|
|
type: 'button',
|
|
|
|
|
className: 'dsn-button',
|
|
|
|
|
onClick: props.testAny,
|
|
|
|
|
}, tr('settings.action.testAny'))))))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Describe the newest alert's fate in one line. */
|
|
|
|
|
function deliveryText(tr) {
|
|
|
|
|
const last = store.getSnapshot().lastDelivery
|
|
|
|
|
if (last === null || last === undefined) return tr('config.diag.none')
|
|
|
|
|
const outcome = last.outcome.startsWith('system-refused')
|
|
|
|
|
? tr('config.diag.refused')
|
|
|
|
|
: last.outcome === 'system'
|
|
|
|
|
? last.shown === true
|
|
|
|
|
? tr('config.diag.systemShown')
|
|
|
|
|
: last.shown === false
|
|
|
|
|
? tr('config.diag.systemFailed')
|
|
|
|
|
: tr('config.diag.systemUnconfirmed')
|
|
|
|
|
: last.outcome === 'replay'
|
|
|
|
|
? tr('config.diag.replayDelivery')
|
|
|
|
|
: tr('config.diag.popup')
|
|
|
|
|
return `${tr('config.diag.last')}: ${last.at} · ${outcome} · ${last.title}`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Say how many away-channel alerts are still waiting to be replayed in-app. */
|
|
|
|
|
function replayText(tr) {
|
|
|
|
|
const pending = state.replay.length
|
|
|
|
|
return pending === 0 ? tr('config.diag.replayNone') : tr('config.diag.replay', { count: pending })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------- registration
|
|
|
|
|
|
|
|
|
|
/** Keep the page's own focus events owned by this plugin's effect. */
|
|
|
|
|
const installBridges = () => {
|
|
|
|
|
const onFocusChange = () => {
|
|
|
|
|
const onBlur = () => {
|
|
|
|
|
state.lastDelivery = 0
|
|
|
|
|
}
|
|
|
|
|
// Coming back is the moment the user can finally be told about whatever the
|
|
|
|
|
// system channel carried while the window was away, and it also clears the
|
|
|
|
|
// throttle so the first alert after returning is never swallowed.
|
|
|
|
|
const onReturn = () => {
|
|
|
|
|
state.lastDelivery = 0
|
|
|
|
|
flushReplay()
|
|
|
|
|
}
|
|
|
|
|
const onVisibilityChange = () => {
|
|
|
|
|
if (!windowIsAway()) onReturn()
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
window.addEventListener('focus', onFocusChange)
|
|
|
|
|
window.addEventListener('blur', onFocusChange)
|
|
|
|
|
window.addEventListener('focus', onReturn)
|
|
|
|
|
window.addEventListener('blur', onBlur)
|
|
|
|
|
document.addEventListener('visibilitychange', onVisibilityChange)
|
|
|
|
|
} catch {
|
|
|
|
|
/* an unreadable window still delivers through live focus reads */
|
|
|
|
|
}
|
|
|
|
|
return () => {
|
|
|
|
|
try {
|
|
|
|
|
window.removeEventListener('focus', onFocusChange)
|
|
|
|
|
window.removeEventListener('blur', onFocusChange)
|
|
|
|
|
window.removeEventListener('focus', onReturn)
|
|
|
|
|
window.removeEventListener('blur', onBlur)
|
|
|
|
|
document.removeEventListener('visibilitychange', onVisibilityChange)
|
|
|
|
|
} catch {
|
|
|
|
|
/* nothing to detach */
|
|
|
|
|
}
|
|
|
|
@@ -1077,6 +1380,8 @@ window.__ModuleLoader__.load({
|
|
|
|
|
}
|
|
|
|
|
for (const timer of timers.values()) clearTimeout(timer)
|
|
|
|
|
timers.clear()
|
|
|
|
|
state.raised.clear()
|
|
|
|
|
state.replay = []
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -1116,7 +1421,8 @@ window.__ModuleLoader__.load({
|
|
|
|
|
store,
|
|
|
|
|
platform: platformKey(),
|
|
|
|
|
ask: requestPermission,
|
|
|
|
|
test: sendTest,
|
|
|
|
|
testAny: sendTest,
|
|
|
|
|
testSystem: sendTestSystem,
|
|
|
|
|
setKind: setKindEnabled,
|
|
|
|
|
}),
|
|
|
|
|
}, ConfigSection))
|
|
|
|
|