The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { deadlineAfter } from "@t3tools/client-runtime/clock"; |
| 2 | import type { EnvironmentId } from "@t3tools/contracts"; |
| 3 | import { useState } from "react"; |
| 4 | |
| 5 | import { useDeadlinePassed } from "./useDeadline"; |
| 6 | |
| 7 | const DISCONNECT_DELAY_MS = 20_000; |
| 8 | |
| 9 | export function useEnvironmentDisconnectDelay(unavailableEnvironmentId: EnvironmentId | null) { |
| 10 | return useEnvironmentStateHeldFor(unavailableEnvironmentId, DISCONNECT_DELAY_MS); |
| 11 | } |
| 12 | |
| 13 | /** |
| 14 | * Whether `environmentId` has stayed the same non-null value for `delayMs`. Every change starts a |
| 15 | * new delay, so leaving and returning to an id restarts it. |
| 16 | */ |
| 17 | export function useEnvironmentStateHeldFor(environmentId: EnvironmentId | null, delayMs: number) { |
| 18 | const [held, setHeld] = useState(() => ({ |
| 19 | environmentId, |
| 20 | deadline: deadlineAfter(delayMs), |
| 21 | })); |
| 22 | if (held.environmentId !== environmentId) { |
| 23 | setHeld({ environmentId, deadline: deadlineAfter(delayMs) }); |
| 24 | } |
| 25 | const elapsed = useDeadlinePassed(held.environmentId === null ? null : held.deadline); |
| 26 | return environmentId !== null && held.environmentId === environmentId && elapsed; |
| 27 | } |