fix(webui): make pairing recovery explicit and retryable

This commit is contained in:
Xubin Ren committed 2026-10-12 17:38:43 +08:00
1 parent 2aa98677c6
commit b89ef23df5
13 files changed
+103 -16

No files matched your search

+42 -16
View File
@@ -617,10 +617,11 @@ function PairingCodePopup({
busyCode: string | null;
error: string | null;
onApprove: (code: string) => void;
onDismiss: (code: string) => void;
onDismiss: (request: PairingRequestInfo) => void;
}) {
const { t } = useTranslation();
const [value, setValue] = useState("");
const attemptedCode = useRef<string | null>(null);
const normalizedCode = normalizePairingCode(value);
const matchedRequest = useMemo(
() => requests.find((request) => request.code === normalizedCode) ?? null,
@@ -638,12 +639,16 @@ function PairingCodePopup({
const showNoMatch = isCompleteCode && !matchedRequest && !busyCode;
useEffect(() => {
if (!matchedRequest || busyCode) return;
if (!matchedRequest || busyCode || attemptedCode.current === matchedRequest.code) return;
attemptedCode.current = matchedRequest.code;
onApprove(matchedRequest.code);
}, [busyCode, matchedRequest, onApprove]);
useEffect(() => {
if (!requests.length) setValue("");
if (!requests.length) {
setValue("");
attemptedCode.current = null;
}
}, [requests.length]);
if (!firstRequest) return null;
@@ -655,7 +660,10 @@ function PairingCodePopup({
matched={Boolean(matchedRequest)}
invalid={showNoMatch}
ariaLabel={t("app.pairing.code", { defaultValue: "Pairing code" })}
onChange={(next) => setValue(formatPairingCodeInput(next))}
onChange={(next) => {
attemptedCode.current = null;
setValue(formatPairingCodeInput(next));
}}
/>
);
@@ -696,7 +704,7 @@ function PairingCodePopup({
<button
type="button"
aria-label={t("common.close", { defaultValue: "Close" })}
onClick={() => onDismiss(displayRequest.code)}
onClick={() => onDismiss(displayRequest)}
className="rounded-full p-1 text-muted-foreground transition hover:bg-muted hover:text-foreground"
>
<X className="h-4 w-4" aria-hidden />
@@ -722,7 +730,7 @@ function PairingCodePopup({
{!deliveryBlocked || total > 1 ? (
<div className="mt-3 flex items-center justify-between gap-3 text-[12.5px] text-muted-foreground">
<span>
{matchedRequest
{matchedRequest && busyCode === matchedRequest.code
? t("app.pairing.matched", {
defaultValue: "Matched {{channel}}. Connecting...",
channel: channelLabel(matchedRequest.channel),
@@ -752,7 +760,19 @@ function PairingCodePopup({
) : null}
{error ? (
<p className="mt-2 text-[12px] leading-5 text-destructive">{error}</p>
<div className="mt-2 space-y-2">
<p role="alert" className="text-[12px] leading-5 text-destructive">{error}</p>
{matchedRequest ? (
<Button
variant="outline"
size="sm"
disabled={Boolean(busyCode)}
onClick={() => onApprove(matchedRequest.code)}
>
{t("common.retry", { defaultValue: "Retry" })}
</Button>
) : null}
</div>
) : null}
</div>
</div>
@@ -1256,7 +1276,10 @@ function Shell({
const [pairingBusyCode, setPairingBusyCode] = useState<string | null>(null);
const [pairingError, setPairingError] = useState<string | null>(null);
const pairingRefreshRef = useRef<Promise<number> | null>(null);
const [snoozedPairingCodes, setSnoozedPairingCodes] = useState<Map<string, number>>(
const [snoozedPairingCodes, setSnoozedPairingCodes] = useState<Map<string, {
until: number;
deliveryStatus: PairingRequestInfo["delivery_status"];
}>>(
() => new Map(),
);
const [runningChatIds, setRunningChatIds] = useState<Set<string>>(() => new Set());
@@ -1451,11 +1474,13 @@ function Shell({
setPairingRequests(requests);
setSnoozedPairingCodes((current) => {
if (current.size === 0) return current;
const activeCodes = new Set(requests.map((request) => request.code));
const activeRequests = new Map(requests.map((request) => [request.code, request]));
const now = Date.now();
const next = new Map(
Array.from(current).filter(
([code, snoozedUntil]) => activeCodes.has(code) && snoozedUntil > now,
([code, snooze]) => activeRequests.has(code)
&& snooze.until > now
&& activeRequests.get(code)?.delivery_status === snooze.deliveryStatus,
),
);
return next.size === current.size ? current : next;
@@ -2456,8 +2481,8 @@ function Shell({
() => {
const now = Date.now();
return pairingRequests.filter((request) => {
const snoozedUntil = snoozedPairingCodes.get(request.code);
return !snoozedUntil || snoozedUntil <= now;
const snooze = snoozedPairingCodes.get(request.code);
return !snooze || snooze.until <= now;
});
},
[pairingRequests, snoozedPairingCodes],
@@ -2486,12 +2511,13 @@ function Shell({
[client, refreshPairingRequests],
);
const onDismissPairingRequest = useCallback((code: string) => {
const onDismissPairingRequest = useCallback((request: PairingRequestInfo) => {
setSnoozedPairingCodes((current) => {
const snoozedUntil = Date.now() + PAIRING_DISMISS_SNOOZE_MS;
if (current.get(code) === snoozedUntil) return current;
const next = new Map(current);
next.set(code, snoozedUntil);
next.set(request.code, {
until: Date.now() + PAIRING_DISMISS_SNOOZE_MS,
deliveryStatus: request.delivery_status,
});
return next;
});
}, []);
+1
View File
@@ -2478,6 +2478,7 @@
}
},
"common": {
"retry": "Retry",
"dismiss": "Dismiss",
"close": "Close",
"current": "Current",
+1
View File
@@ -2279,6 +2279,7 @@
}
},
"common": {
"retry": "Reintentar",
"dismiss": "Cerrar",
"close": "Cerrar",
"current": "Actual",
+1
View File
@@ -2278,6 +2278,7 @@
}
},
"common": {
"retry": "Réessayer",
"dismiss": "Fermer",
"close": "Fermer",
"current": "Actuel",
+1
View File
@@ -2278,6 +2278,7 @@
}
},
"common": {
"retry": "Coba lagi",
"dismiss": "Tutup",
"close": "Tutup",
"current": "Saat ini",
+1
View File
@@ -2278,6 +2278,7 @@
}
},
"common": {
"retry": "再試行",
"dismiss": "閉じる",
"close": "閉じる",
"current": "現在",
+1
View File
@@ -2278,6 +2278,7 @@
}
},
"common": {
"retry": "다시 시도",
"dismiss": "닫기",
"close": "닫기",
"current": "현재",
+1
View File
@@ -2292,6 +2292,7 @@
}
},
"common": {
"retry": "Tentar novamente",
"dismiss": "Descartar",
"close": "Fechar",
"current": "Atual",
+1
View File
@@ -2278,6 +2278,7 @@
}
},
"common": {
"retry": "Thử lại",
"dismiss": "Đóng",
"close": "Đóng",
"current": "Hiện tại",
+1
View File
@@ -2478,6 +2478,7 @@
}
},
"common": {
"retry": "重试",
"dismiss": "关闭",
"close": "关闭",
"current": "当前",
+1
View File
@@ -2278,6 +2278,7 @@
}
},
"common": {
"retry": "重試",
"dismiss": "關閉",
"close": "關閉",
"current": "目前",
+50
View File
@@ -4089,6 +4089,56 @@ describe("App layout", () => {
expect(within(popup).getByText("2 pending")).toBeVisible();
});
it("shows a recovered pairing reply even after its failure notice was dismissed", async () => {
vi.useFakeTimers();
let delivery_status = "failed";
mockFetchRoutes({ "/api/settings/pairing": () => ({ requests: [{
code: "ABCD-EFGH", channel: "feishu", sender_id: "alice", delivery_status,
}] }) });
render(<App />);
await act(async () => {});
fireEvent.click(within(screen.getByRole("dialog", { name: "Pair account" }))
.getByRole("button", { name: "Close" }));
expect(screen.queryByRole("dialog", { name: "Pair account" })).not.toBeInTheDocument();
await act(async () => { await vi.advanceTimersByTimeAsync(3_000); });
expect(screen.queryByRole("dialog", { name: "Pair account" })).not.toBeInTheDocument();
delivery_status = "sent";
await act(async () => { await vi.advanceTimersByTimeAsync(3_000); });
const popup = screen.getByRole("dialog", { name: "Pair account" });
expect(within(popup).getByRole("textbox", { name: "Pairing code" })).toBeEnabled();
expect(within(popup).queryByRole("alert")).not.toBeInTheDocument();
expect(requestMutationSpy).not.toHaveBeenCalled();
});
it("keeps a failed pairing approval idle until the user retries", async () => {
vi.useFakeTimers();
let requests = [{ code: "ABCD-EFGH", channel: "feishu", sender_id: "alice", delivery_status: "sent" }];
mockFetchRoutes({ "/api/settings/pairing": () => ({ requests }) });
requestMutationSpy
.mockRejectedValueOnce(new Error("Approval unavailable"))
// Bound the old automatic retry loop while reproducing it.
.mockImplementation(() => new Promise(() => {}));
render(<App />);
await act(async () => {});
const popup = screen.getByRole("dialog", { name: "Pair account" });
fireEvent.change(within(popup).getByRole("textbox", { name: "Pairing code" }), {
target: { value: "ABCD-EFGH" },
});
await act(async () => {});
await act(async () => { await vi.advanceTimersByTimeAsync(3_000); });
expect(requestMutationSpy).toHaveBeenCalledTimes(1);
expect(within(popup).getByText("Approval unavailable")).toBeVisible();
expect(within(popup).getByRole("textbox", { name: "Pairing code" })).toBeEnabled();
requests = [];
requestMutationSpy.mockResolvedValueOnce({ requests });
fireEvent.click(within(popup).getByRole("button", { name: "Retry" }));
await act(async () => {});
expect(requestMutationSpy).toHaveBeenCalledTimes(2);
expect(screen.queryByRole("dialog", { name: "Pair account" })).not.toBeInTheDocument();
});
it("reuses an in-flight pairing poll when the page becomes visible again", async () => {
let resolvePairing!: (response: Response) => void;
const pendingPairing = new Promise<Response>((resolve) => {