godcrm/components/AIChatPanel/hooks/useProcessingTimer.ts
GOD CRM Release f89e074dd1
Some checks failed
CI / Lint / Typecheck / Test / Build (push) Has been cancelled
CI / PostgreSQL Integration Tests (push) Has been cancelled
GOD CRM — public scrubbed snapshot
Governed substrate for autonomous agents: scoped identity (passports),
audited actions, MCP workspace. Infra IPs and secrets redacted for public release.
2026-08-10 04:01:45 +03:00

59 lines
2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* useProcessingTimer Hook
* Extracted from AIChatPanel.tsx — lines 370383
*
* Tracks elapsed seconds while an AI agent is processing a request.
* Shows warnings in the UI when processing takes too long ("stuck" detection).
*
* Ticket #36708: Track elapsed processing time for stuck state detection.
*
* Lifecycle:
* - When `isAgentProcessing` is true and `processingStartedAt` is set,
* a 1-second interval updates `processingElapsed`.
* - When processing ends (either flag clears), resets elapsed to 0.
*
* @param isAgentProcessing Whether the agent is currently processing
* @param processingStartedAt Timestamp (ms) when processing began, or null
* @param setProcessingElapsed State setter from useChatState
*/
import { useEffect } from 'react';
import type { Dispatch, SetStateAction } from 'react';
interface UseProcessingTimerParams {
/** Whether the agent is currently processing a request */
isAgentProcessing: boolean;
/** Epoch timestamp (ms) when processing started, or null/undefined */
processingStartedAt: number | null | undefined;
/** Setter to update the elapsed seconds counter in ChatState */
setProcessingElapsed: Dispatch<SetStateAction<number>>;
}
/**
* Stuck-agent detection timer.
*
* Updates `processingElapsed` every second so the UI can show
* "Processing for Xs..." warnings and offer a cancel/retry button.
*/
export function useProcessingTimer({
isAgentProcessing,
processingStartedAt,
setProcessingElapsed,
}: UseProcessingTimerParams): void {
useEffect(() => {
if (!isAgentProcessing || !processingStartedAt) {
setProcessingElapsed(0);
return;
}
// Immediately set the current elapsed value
setProcessingElapsed(Math.floor((Date.now() - processingStartedAt) / 1000));
// Then update every second
const timer = setInterval(() => {
setProcessingElapsed(Math.floor((Date.now() - processingStartedAt) / 1000));
}, 1000);
return () => clearInterval(timer);
}, [isAgentProcessing, processingStartedAt, setProcessingElapsed]);
}