@ququ-jiaxk/dsh-groupchat 0.1.8 → 0.1.9
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/client.js +73 -3
- package/package.json +1 -1
package/client.js
CHANGED
|
@@ -438,6 +438,26 @@ window.__ModuleLoader__.load({
|
|
|
438
438
|
}
|
|
439
439
|
})()
|
|
440
440
|
|
|
441
|
+
// ---- 插件是否仍然启用(宿主接口还活着) ----
|
|
442
|
+
// 生产包里停用插件时客户端 bundle 不一定立刻被 dispose;
|
|
443
|
+
// 靠轮询 /state 的 404 判定「宿主半边已停用」,主动拆掉注入的 UI 和样式。
|
|
444
|
+
const pluginAliveStore = (() => {
|
|
445
|
+
let value = true
|
|
446
|
+
const listeners = new Set()
|
|
447
|
+
return {
|
|
448
|
+
get: () => value,
|
|
449
|
+
set: (next) => {
|
|
450
|
+
if (next === value) return
|
|
451
|
+
value = next
|
|
452
|
+
for (const l of listeners) l()
|
|
453
|
+
},
|
|
454
|
+
subscribe: (listener) => {
|
|
455
|
+
listeners.add(listener)
|
|
456
|
+
return () => listeners.delete(listener)
|
|
457
|
+
},
|
|
458
|
+
}
|
|
459
|
+
})()
|
|
460
|
+
|
|
441
461
|
// ---- 面板记忆(跨卸载保留 UI 状态) ----
|
|
442
462
|
// 用户切到别的界面/会话时 GroupchatPanel 会卸载,React state 全部丢失,
|
|
443
463
|
// 再回来就"重新加载"(回到列表、草稿没了、滚动回顶部)。
|
|
@@ -557,12 +577,18 @@ window.__ModuleLoader__.load({
|
|
|
557
577
|
async function api(path, options) {
|
|
558
578
|
const res = await fetch(API + path, options)
|
|
559
579
|
const json = await res.json().catch(() => ({}))
|
|
560
|
-
if (!res.ok)
|
|
580
|
+
if (!res.ok) {
|
|
581
|
+
const error = new Error(json.error || ('HTTP ' + res.status))
|
|
582
|
+
error.status = res.status
|
|
583
|
+
throw error
|
|
584
|
+
}
|
|
561
585
|
return json
|
|
562
586
|
}
|
|
563
587
|
|
|
564
588
|
// ---- 全局轮询:无论是否打开群聊面板,都拉增量并弹 toast / 更新角标 ----
|
|
565
589
|
const globalLastTsRef = { current: 0 }
|
|
590
|
+
let groupchatStyleEl = null
|
|
591
|
+
let hostGoneCount = 0
|
|
566
592
|
|
|
567
593
|
// 按群聚合新消息并弹全局 toast(供全局轮询复用)
|
|
568
594
|
function fireToastForNewMessages(newMsgs, groups) {
|
|
@@ -624,7 +650,15 @@ window.__ModuleLoader__.load({
|
|
|
624
650
|
unreadStore.set(totalUnread)
|
|
625
651
|
// 把整个 state 推给面板,让 agent 状态实时更新(不依赖面板自己的轮询)
|
|
626
652
|
if (st.groups) stateStore.set(st, r.delta === true)
|
|
627
|
-
|
|
653
|
+
hostGoneCount = 0
|
|
654
|
+
if (!pluginAliveStore.get()) recoverPluginUi()
|
|
655
|
+
} catch (e) {
|
|
656
|
+
// 宿主接口 404 = 插件在宿主侧已被停用/卸载;连续 3 次才动手,避开重启抖动
|
|
657
|
+
if (e && e.status === 404) {
|
|
658
|
+
hostGoneCount += 1
|
|
659
|
+
if (hostGoneCount >= 3) handleHostDisabled()
|
|
660
|
+
}
|
|
661
|
+
}
|
|
628
662
|
}
|
|
629
663
|
|
|
630
664
|
// ---- 小工具 ----
|
|
@@ -1257,6 +1291,10 @@ window.__ModuleLoader__.load({
|
|
|
1257
1291
|
try { await fn() } catch (e) { setError(e.message || '操作失败') } finally { setBusy(false) }
|
|
1258
1292
|
}, [busy])
|
|
1259
1293
|
|
|
1294
|
+
// 宿主侧停用后不渲染(handleHostDisabled 通常已经切回会话,这里是兜底)
|
|
1295
|
+
const alive = useStoreValue(pluginAliveStore)
|
|
1296
|
+
if (!alive) return null
|
|
1297
|
+
|
|
1260
1298
|
// ---- 空态 ----
|
|
1261
1299
|
if (error && !state) {
|
|
1262
1300
|
return wrap({ alignItems: 'center', justifyContent: 'center', padding: 40 },
|
|
@@ -3802,6 +3840,33 @@ window.__ModuleLoader__.load({
|
|
|
3802
3840
|
document.documentElement.removeAttribute('data-groupchat-active')
|
|
3803
3841
|
}
|
|
3804
3842
|
|
|
3843
|
+
// 宿主侧插件被停用/卸载:客户端这半边可能没被 dispose(生产包里常见),
|
|
3844
|
+
// 由轮询到 /state 连续 404 触发,把注入的 DOM、样式、属性全部撤掉,恢复原生侧栏。
|
|
3845
|
+
function handleHostDisabled() {
|
|
3846
|
+
if (!pluginAliveStore.get()) return
|
|
3847
|
+
pluginAliveStore.set(false)
|
|
3848
|
+
try {
|
|
3849
|
+
if (panelActiveStore.get() && layoutRef.current && typeof layoutRef.current.selectPanel === 'function') {
|
|
3850
|
+
layoutRef.current.selectPanel(null)
|
|
3851
|
+
}
|
|
3852
|
+
} catch (_e) { /* ignore */ }
|
|
3853
|
+
try { unmountGroupchatSidebar() } catch (_e) { /* ignore */ }
|
|
3854
|
+
if (groupchatStyleEl) {
|
|
3855
|
+
try { groupchatStyleEl.remove() } catch (_e) { /* ignore */ }
|
|
3856
|
+
}
|
|
3857
|
+
document.documentElement.removeAttribute('data-groupchat-active')
|
|
3858
|
+
}
|
|
3859
|
+
|
|
3860
|
+
// 宿主侧重新启用:接口恢复 200 时把 UI 挂回去(客户端没重新 import 也能恢复)
|
|
3861
|
+
function recoverPluginUi() {
|
|
3862
|
+
if (pluginAliveStore.get()) return
|
|
3863
|
+
pluginAliveStore.set(true)
|
|
3864
|
+
try {
|
|
3865
|
+
if (groupchatStyleEl && !groupchatStyleEl.isConnected) document.head.appendChild(groupchatStyleEl)
|
|
3866
|
+
} catch (_e) { /* ignore */ }
|
|
3867
|
+
try { mountGroupchatSidebar() } catch (_e) { /* ignore */ }
|
|
3868
|
+
}
|
|
3869
|
+
|
|
3805
3870
|
/**
|
|
3806
3871
|
* 侧栏顶部(`sidebar.session.action` 槽位):「会话 / 群聊」两个 tab,
|
|
3807
3872
|
* 以及会话模式下的「+ 新会话」入口(就在插件列表上方)。
|
|
@@ -3817,6 +3882,7 @@ window.__ModuleLoader__.load({
|
|
|
3817
3882
|
const children = props ? props.children : null
|
|
3818
3883
|
const [unread, setUnread] = useState(0)
|
|
3819
3884
|
const [active, setActive] = useState(() => panelActiveStore.get())
|
|
3885
|
+
const alive = useStoreValue(pluginAliveStore)
|
|
3820
3886
|
const nativeRef = useRef(null)
|
|
3821
3887
|
useEffect(() => {
|
|
3822
3888
|
const unsubscribe = unreadStore.subscribe(() => setUnread(unreadStore.get()))
|
|
@@ -3853,6 +3919,9 @@ window.__ModuleLoader__.load({
|
|
|
3853
3919
|
try { layout.selectPanel(PANEL_ID) } catch (_e) { /* 主面板未注册时忽略 */ }
|
|
3854
3920
|
}
|
|
3855
3921
|
|
|
3922
|
+
// 插件已在宿主侧停用:把槽位还给原生「新会话」按钮
|
|
3923
|
+
if (!alive) return children
|
|
3924
|
+
|
|
3856
3925
|
const groupchatIcon = h('svg', {
|
|
3857
3926
|
width: 16, height: 16, viewBox: '0 0 18 18', fill: 'none', stroke: 'currentColor',
|
|
3858
3927
|
strokeWidth: 1.7, strokeLinecap: 'round', strokeLinejoin: 'round',
|
|
@@ -4020,7 +4089,8 @@ window.__ModuleLoader__.load({
|
|
|
4020
4089
|
}
|
|
4021
4090
|
`
|
|
4022
4091
|
document.head.appendChild(style)
|
|
4023
|
-
|
|
4092
|
+
groupchatStyleEl = style
|
|
4093
|
+
return () => { try { style.remove() } catch (_e) { /* ignore */ }; if (groupchatStyleEl === style) groupchatStyleEl = null }
|
|
4024
4094
|
}, 'groupchat: animations')
|
|
4025
4095
|
|
|
4026
4096
|
// 全局轮询:页面加载即开始,不依赖群聊面板是否打开
|