import { Dropdown, type DropdownItem, type DropdownRef } from "@yaakapp-internal/ui"; import { useAtomValue, useSetAtom } from "jotai"; import type { Ref } from "react"; import { useState } from "react"; import { clearFlowsAtom, flowTotalAtom, lockedAtom } from "../lib/store"; /** Whether there are flows to clear: some, and readable, as they aren't while traffic is locked */ export function useCanClearFlows(): boolean { const total = useAtomValue(flowTotalAtom); const locked = useAtomValue(lockedAtom); return locked == null && total > 0; } /** * Clears every flow, once confirmed in its menu, which its hotkey or the status menu's Clear * Traffic opens. It has no button of its own: it covers the element it is placed over, without * taking the pointer, so the menu opens from there. The list empties at once; the flows are * deleted in the background. Neither menu nor hotkey with nothing to clear, or while captured * traffic is locked. */ export function ClearFlowsMenu({ ref }: { ref?: Ref }) { const total = useAtomValue(flowTotalAtom); const canClear = useCanClearFlows(); 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)); }); }, }, ]; return ( ); }