import { Button, CONTROL_TOOLTIP_DELAY, Dropdown, type DropdownItem, Icon, IconButton, Tooltip, } from "@yaakapp-internal/ui"; import { useAtomValue } from "jotai"; import { useMemo, useState } from "react"; import { useLocalStorage } from "react-use"; import { canTrustHere, useDecrypt, useTrustCertificate, useTrusting } from "../hooks/useDecrypt"; import { useDecryptList } from "../hooks/useDecryptList"; import { useRpcQuery } from "../hooks/useRpcQuery"; import { everythingEdit, listEdit, setDecrypted } from "../lib/decrypt"; import { chooseOptions, connectionCount, stripState } from "../lib/httpsStrip"; import { sidebarFlowsAtom } from "../lib/store"; import { AppIcon } from "./AppIcon"; import { SETTINGS_TAB_PROXY, useOpenSettings } from "./Settings"; const STRIP = "flex items-center gap-2.5 h-[2.2rem] mx-2 mb-1 pl-3 pr-1.5 rounded-md bg-surface-highlight/45 border border-border-subtle text-sm text-text-subtle min-w-0"; /** * Above the list while nothing is decrypted: says once that HTTPS is encrypted, and offers * Decrypt Everything, or Choose for apps and sites one at a time. Goes once anything is on * the list, or when dismissed; comes back, with Trust, while the list decrypts something the * certificate isn't trusted for. */ export function HttpsStrip() { const list = useDecryptList(); const flows = useAtomValue(sidebarFlowsAtom); const caStatus = useRpcQuery("ca_status", {}); const [dismissed, setDismissed] = useLocalStorage("https_strip_dismissed", false); // Kept while Choose is open, so checking a row there leaves the menu where it is. const [choosing, setChoosing] = useState(false); const { apply } = useDecrypt(); const trusting = useTrusting(); const trust = useTrustCertificate(); const openSettings = useOpenSettings(); const encrypted = useMemo(() => flows.filter((f) => f.tunnelled).length, [flows]); const state = stripState({ list, encrypted, dismissed: dismissed ?? false, trusted: caStatus.data?.trusted ?? null, canTrust: canTrustHere(), }); const options = useMemo(() => chooseOptions(flows, list), [flows, list]); if (state === "hidden" && !choosing) return null; if (state === "broken") { return (
Certificate not trusted The certificate must be trusted to decrypt traffic.
); } const items: DropdownItem[] = [ { type: "separator", label: "Apps", hidden: options.apps.length === 0 }, ...options.apps.map((app): DropdownItem => ({ label: app.path == null ? ( app.name ) : ( {app.name} ), keepOpenOnSelect: true, leftSlot: , rightSlot: , onSelect: async () => { const subject = { kind: "app", name: app.name, bundleId: app.bundleId } as const; await apply({ edit: listEdit(list, setDecrypted(list, [subject], !app.checked)), message: `${app.checked ? "Stopped decrypting" : "Decrypting"} ${app.name}`, }); }, })), { type: "separator", label: "Domains", hidden: options.sites.length === 0 }, ...options.sites.map((site): DropdownItem => ({ label: *.{site.site}, keepOpenOnSelect: true, leftSlot: , rightSlot: , onSelect: async () => { const subject = { kind: "site", site: site.site } as const; await apply({ edit: listEdit(list, setDecrypted(list, [subject], !site.checked)), message: `${site.checked ? "Stopped decrypting" : "Decrypting"} *.${site.site}`, }); }, })), { type: "separator" }, { label: "Decryption Settings", leftSlot: , onSelect: () => openSettings(SETTINGS_TAB_PROXY), }, ]; return (
HTTPS is encrypted {connectionCount(encrypted)}
setChoosing(true)} onClose={() => setChoosing(false)}> setDismissed(true)} />
); } function Check({ on }: { on: boolean }) { return on ? ( ) : ( ); } function Count({ n }: { n: number }) { return {n.toLocaleString()}; }