import { ContextMenu, CONTROL_TOOLTIP_DELAY, type DropdownItem, Icon, Tooltip, Tree, type TreeProps, } from "@yaakapp-internal/ui"; import { atom, useAtom, useAtomValue, useSetAtom, useStore } from "jotai"; import { atomWithStorage } from "jotai/utils"; import { type PointerEvent, type SyntheticEvent, useCallback, useEffect } from "react"; import { useDecrypt } from "../hooks/useDecrypt"; import { useDecryptList } from "../hooks/useDecryptList"; import { useRpcMutation } from "../hooks/useRpcMutation"; import { appliedFilterAtom, SIDEBAR_TREE_ID } from "../lib/atoms"; import { blockerOf, decryptMenuAction, decryptsEverything, everythingEdit, listEdit, setDecrypted, } from "../lib/decrypt"; import { revealLabel, revealPath } from "../lib/opener"; import { applyCollapsed, type CollapsedMap, collapsedMap, folderIds } from "../lib/sidebarCollapse"; import { copyRowsText, ENCRYPTED_ID, filterWithRows, type SidebarItem, SYSTEM_ID, valueRows, } from "../lib/sidebarTree"; import { applyFilter, noteSidebarInput, noteSidebarMenu } from "../lib/navigation"; import { hideEncryptedAtom, hideSystemAtom, sidebarTreeAtom } from "../lib/store"; import { getOsType } from "../lib/tauri"; import { useShowToast } from "../lib/toast"; import { AppIcon } from "./AppIcon"; import { savedSearchDialogAtom } from "./SavedSearchDialog"; /** The apps, sites, hosts and paths the user expanded, by node id, kept across launches */ const expandedAtom = atomWithStorage>("sidebar_expanded", {}, undefined, { getOnInit: true, }); /** The other nodes' collapsed flags, for this launch, as before */ const otherCollapsedAtom = atom({}); /** Every app's tree, site, host and path, and System, collapsed until the user expands them */ const collapsedAtom = atom( (get) => collapsedMap(folderIds(get(sidebarTreeAtom)), get(expandedAtom), get(otherCollapsedAtom)), (get, set, action: CollapsedMap | ((prev: CollapsedMap) => CollapsedMap)) => { const prev = get(collapsedAtom); const next = typeof action === "function" ? action(prev) : action; const split = applyCollapsed(prev, next, get(expandedAtom), get(otherCollapsedAtom)); set(expandedAtom, split.expanded); set(otherCollapsedAtom, split.others); }, ); function Count({ item }: { item: SidebarItem }) { const text = item.count > 0 ? item.count.toLocaleString() : null; if (text == null && item.kind !== "all") return null; return ( {text ?? "0"} ); } function ItemInner({ item }: { item: SidebarItem }) { if (item.kind === "group") { return (
{item.label}
); } return (
{item.kind === "all" && } {item.kind === "encrypted" && } {item.kind === "saved" && } {item.kind === "folder" && } {item.appPath != null && ( )} {item.kind === "folder" && item.path != null ? ( // Named by its last part; the whole path on hover {item.label} ) : ( {item.label} )} {item.sublabel != null && ( {item.sublabel} )} {item.encryptedOnly && ( )}
); } /** Input in the sidebar itself, not in a row's menu, whose events reach it through its portal */ function inSidebar(e: SyntheticEvent): boolean { return e.target instanceof Node && e.currentTarget.contains(e.target); } /** A press that opens a context menu: the secondary button, or Control and a click on macOS */ function opensMenu(e: PointerEvent): boolean { return e.button !== 0 || (e.ctrlKey && getOsType() === "macos"); } /** The menu's sections that have items, a separator between each */ function withSeparators(sections: DropdownItem[][]): DropdownItem[] { return sections .filter((s) => s.some((i) => !i.hidden)) .flatMap((s, n) => (n === 0 ? s : [{ type: "separator" } as const, ...s])); } /** A row with nothing in its menu: the menu closes as it opens, so the row is not left marked */ function CloseAtOnce({ onClose }: { onClose: () => void }) { useEffect(() => onClose(), [onClose]); return null; } /** A checkable menu item's mark: a check when on, the same room when off */ function checkMark(on: boolean) { return on ? ( ) : ( ); } export function Sidebar() { const store = useStore(); const tree = useAtomValue(sidebarTreeAtom); const getItemKey = useCallback( // The label too, as a saved search's changes when it is renamed (item: SidebarItem) => `${item.id}:${item.count}:${item.label}`, [], ); const [hideSystem, setHideSystem] = useAtom(hideSystemAtom); const [hideEncrypted, setHideEncrypted] = useAtom(hideEncryptedAtom); const decryptList = useDecryptList(); const { apply: applyDecrypt } = useDecrypt(); const showToast = useShowToast(); const setSavedDialog = useSetAtom(savedSearchDialogAtom); const { mutate: deleteSaved } = useRpcMutation("delete_saved_query", { onError: (err) => showToast({ id: "saved-search", message: err.message, color: "danger" }), }); // A program's folder, or the program, is shown in Finder, and a folder's path copied. const fail = useCallback( (message: string) => showToast({ id: "sidebar-path", message, color: "danger" }), [showToast], ); const pathItems = useCallback( (item: SidebarItem | undefined): DropdownItem[] => { const path = item?.path; if (item == null || path == null) return []; return [ { label: revealLabel(getOsType()), leftSlot: , onSelect: () => void revealPath(path, fail), }, { label: "Copy Path", leftSlot: , hidden: item.kind !== "folder", onSelect: () => { navigator.clipboard.writeText(path).catch((err) => fail(String(err))); }, }, ]; }, [fail], ); // The selected apps, folders, sites, hosts and paths added to the filter, either way, at once, // or their values copied, as a cell's menu in the list does. const filterItems = useCallback( (items: SidebarItem[]): DropdownItem[] => { if (valueRows(items).length === 0) return []; const add = (negated: boolean) => applyFilter(store, filterWithRows(store.get(appliedFilterAtom), items, negated)); return [ { label: "Filter by This Value", icon: "filter", onSelect: () => add(false) }, { label: "Exclude This Value", icon: "minus_circle", onSelect: () => add(true) }, { label: "Copy Value", icon: "copy", onSelect: async () => { try { await navigator.clipboard.writeText(copyRowsText(items)); showToast({ message: "Copied to clipboard" }); } catch (err) { showToast({ message: `Failed to copy: ${String(err)}`, color: "danger" }); } }, }, ]; }, [store, showToast], ); // Filter by the selected rows, Decrypt or Stop Decrypting the selected hosts, sites and apps, // from new connections on, then a program's folder. A saved search is renamed or deleted, and // System is shown in All traffic or left out of it. const getContextMenu = useCallback( (items: SidebarItem[]): DropdownItem[] => { if (items.length === 1 && items[0]?.id === SYSTEM_ID) { return [ { label: "Show in All Traffic", leftSlot: checkMark(!hideSystem), onSelect: () => setHideSystem(!hideSystem), }, ]; } if (items.length === 1 && items[0]?.id === ENCRYPTED_ID) { return withSeparators([ [ { label: "Decrypt Everything", leftSlot: , hidden: decryptsEverything(decryptList), onSelect: () => void applyDecrypt({ edit: everythingEdit(true), message: "Decrypting everything", sub: "Excluded hosts and apps stay encrypted.", }), }, ], [ { label: "Show in All Traffic", leftSlot: checkMark(!hideEncrypted), onSelect: () => setHideEncrypted(!hideEncrypted), }, ], ]); } const paths = items.length === 1 ? pathItems(items[0]) : []; const saved = items.flatMap((i) => (i.savedId == null ? [] : [{ ...i, id: i.savedId }])); if (saved.length > 0) { const [first] = saved; return [ { label: "Rename", leftSlot: , hidden: saved.length > 1 || first == null, onSelect: () => { if (first != null) setSavedDialog({ mode: "rename", id: first.id, name: first.label }); }, }, { label: "Delete", color: "danger", leftSlot: , onSelect: () => { for (const s of saved) deleteSaved({ id: s.id }); }, }, ]; } const subjects = items.flatMap((i) => (i.decrypt == null ? [] : [i.decrypt])); const action = decryptMenuAction(decryptList, subjects); const decrypt: DropdownItem[] = action == null ? [] : [ { label: action.label, icon: action.on ? "lock_open" : "lock", onSelect: () => { const blocked = action.on ? blockerOf(decryptList, subjects) : null; if (blocked != null) { showToast({ id: "decrypt-blocked", color: "notice", message: `Excluded by ${blocked}`, }); return; } // Only what this changes, applied to the list as it is then, so a second change // made before this one lands keeps it. const next = setDecrypted(decryptList, subjects, action.on); void applyDecrypt({ edit: listEdit(decryptList, next), message: `${action.on ? "Decrypting" : "Stopped decrypting"} ${action.subject}`, }); }, }, ]; return withSeparators([filterItems(items), decrypt, paths]); }, [ decryptList, applyDecrypt, showToast, setSavedDialog, deleteSaved, pathItems, filterItems, hideSystem, setHideSystem, hideEncrypted, setHideEncrypted, ], ); const renderContextMenu = useCallback["renderContextMenu"]>>( ({ items, position, onClose }) => items.length === 0 ? ( ) : ( ), [], ); return ( // The tree scrolls itself, in a row of bounded height, so its virtualizer mounts only the rows // in view rather than every site, host and path. // A click or key here marks the selection change it makes as the person's, so it writes the // search; the tree's own upkeep does not. Opening a row's menu selects it too, but leaves the // search as it is, for the menu's Filter by This Value to add to, and the rows as it says. // oxlint-disable-next-line jsx-a11y/no-static-element-interactions -- only notes input the tree handles ); }