import { Dropdown, type DropdownItem, IconButton } from "@yaakapp-internal/ui"; import { useAtomValue, useSetAtom } from "jotai"; import { useState } from "react"; import { clearFlowsAtom, flowTotalAtom, lockedAtom } from "../lib/store"; /** * Clears every flow, once confirmed in its menu, which its hotkey opens too. The list empties at * once; the flows are deleted in the background. Disabled with none to clear, or while captured * traffic is locked. */ export function ClearFlowsButton() { const total = useAtomValue(flowTotalAtom); const locked = useAtomValue(lockedAtom); const clear = useSetAtom(clearFlowsAtom); const [error, setError] = useState(null); const items: DropdownItem[] = [ { type: "content", label: (
{total === 1 ? "Clear 1 flow?" : `Clear all ${total.toLocaleString()} flows?`}
{error != null ? ( {error} ) : ( "They are deleted from disk with their bodies, for good." )}
), }, { type: "separator" }, { label: "Clear", icon: "trash", color: "danger", onSelect: () => { setError(null); clear().catch((err: unknown) => { const message = (err as { message?: unknown } | null)?.message; setError(typeof message === "string" ? message : String(err)); }); }, }, ]; // Nothing to clear, or nothing that can be read while traffic is locked: no menu, nor hotkey. if (locked != null || total === 0) { return ( ); } return ( ); }