Files
Netcatty/application/state/useMonitorHostKeyVerification.ts
T
陈大猫 bace30578c feat: add synced custom host monitoring panels (#3703)
* feat: add synced custom host monitoring panels

* fix: preserve shared monitoring demand and complete terminal stats

* fix: transfer queued monitoring prompts when a window closes

* fix: keep reused hosts sampling while authentication is queued

* docs: record monitoring acceptance evidence

* fix: transfer pending monitoring auth after panel stop

* fix: release monitor owners in direct SSH runtime

* fix: keep worker monitoring sender compatibility

* fix: include monitoring configuration in automatic sync

* fix: disambiguate independent synced monitoring panel names

* test: assert existing panel order after cloud merge

* test: model browser locks and await monitoring state on Node 22
2026-10-17 15:05:30 +08:00

62 lines
1.8 KiB
TypeScript

import { useEffect, useRef, useState } from "react";
import { useAppVaultRuntime } from "./appRuntimeBridge";
import { useMonitorSnapshot } from "./monitorRuntimeStore";
import { netcattyBridge } from "../../infrastructure/services/netcattyBridge";
import {
createKnownHostFromHostKeyInfo,
upsertKnownHost,
} from "../../domain/knownHosts";
import { toHostKeyInfo } from "../../domain/hostKey";
type Request = Parameters<
Parameters<NonNullable<NetcattyBridge["onHostKeyVerification"]>>[0]
>[0];
export function useMonitorHostKeyVerification() {
const { knownHosts, updateKnownHosts } = useAppVaultRuntime();
const [request, setRequest] = useState<Request | null>(null);
const snapshot = useMonitorSnapshot();
const receivedAt = useRef(0);
useEffect(
() =>
netcattyBridge.get()?.onHostKeyVerification?.((r) => {
if (r.sessionId?.startsWith("monitor-")) {
receivedAt.current = Date.now();
setRequest(r);
}
}),
[],
);
const respond = (accept: boolean, save = false) => {
if (!request) return;
if (accept && save)
updateKnownHosts(
upsertKnownHost(
knownHosts,
createKnownHostFromHostKeyInfo(toHostKeyInfo(request)),
),
);
void netcattyBridge
.get()
?.respondHostKeyVerification?.(request.requestId, accept, save);
setRequest(null);
};
useEffect(() => {
if (
request &&
snapshot.takenAt >= receivedAt.current &&
(!snapshot.running.length ||
!snapshot.activeSessions.includes(request.sessionId ?? ""))
) {
void netcattyBridge
.get()
?.respondHostKeyVerification?.(request.requestId, false, false);
setRequest(null);
}
}, [
request,
snapshot.running.length,
snapshot.takenAt,
snapshot.activeSessions,
]);
return { request, respond };
}