import { useQueryClient } from "@tanstack/react-query"; import type { CaStatus } from "@yaakapp-internal/proxy-lib"; import { Button, Dropdown, type DropdownItem, type DropdownRef, Icon } from "@yaakapp-internal/ui"; import classNames from "classnames"; import type { HTMLAttributes, ReactNode, Ref } from "react"; import { useRef } from "react"; import { useRpcMutation } from "../hooks/useRpcMutation"; import { useRpcQuery } from "../hooks/useRpcQuery"; import { useRpcQueryWithEvent } from "../hooks/useRpcQueryWithEvent"; /** The header's state pill and its menu, with the certificate stepping out when untrusted */ export function ProxyStatus() { const queryClient = useQueryClient(); const dropdownRef = useRef(null); const { data: proxyState } = useRpcQueryWithEvent("get_proxy_state", {}, "proxy_state_changed"); const caStatus = useRpcQuery("ca_status", {}); const trust = useRpcMutation("ca_trust", { onSuccess: (status) => queryClient.setQueryData(["ca_status", {}], status), }); const execute = useRpcMutation("execute_action"); const running = proxyState?.state === "running"; const port = proxyState?.port; const untrusted = caStatus.data != null && !caStatus.data.trusted; const items: DropdownItem[] = [ { type: "content", label: ( } title={running && port != null ? `Running on port ${port}` : "Stopped"} description={ running ? "Listening on 127.0.0.1." : "Start the proxy to capture traffic from apps pointed at it." } /> ), }, { type: "separator" }, { type: "content", label: caStatus.data?.trusted ? ( } title="Certificate trusted" description="Yaak Proxy CA, in your login keychain." /> ) : ( } title="Certificate not trusted" description={ trust.error != null ? ( {trust.error.message} ) : trust.isPending ? ( "Waiting for your password in the macOS prompt." ) : ( "So HTTPS reads as plain text. macOS asks for your login password once." ) } action={ } /> ), }, { type: "separator" }, { label: running ? "Stop Proxy" : "Start Proxy", icon: running ? "circle_stop" : "circle_play", hotKeyAction: running ? "proxy_stop" : "proxy_start", hotKeyLabelOnly: true, onSelect: () => { execute.mutate({ scope: "global", action: running ? "proxy_stop" : "proxy_start" }); }, }, ]; return ( <> {untrusted && ( dropdownRef.current?.open()}> Certificate not trusted )} {running ? ( <> Running {port != null && :{port}} ) : ( <> Stopped )} ); } interface PillProps extends HTMLAttributes { color?: "warning"; ref?: Ref; } function Pill({ children, color, ref, ...props }: PillProps) { return ( ); } function StateDot({ running }: { running: boolean }) { return ( ); } function MenuRow({ icon, title, description, action, }: { icon: ReactNode; title: string; description: ReactNode; action?: ReactNode; }) { return (
{icon}
{title}
{description}
{action}
); }