import { Icon, IconButton, Tree } from "@yaakapp-internal/ui"; import classNames from "classnames"; import { atom, useAtom, useAtomValue } from "jotai"; import { atomWithStorage } from "jotai/utils"; import { useCallback } from "react"; import { SIDEBAR_TREE_ID } from "../lib/atoms"; import { applyCollapsed, type CollapsedMap, collapsedMap, folderIds } from "../lib/sidebarCollapse"; import { type SidebarItem, SYSTEM_ID } from "../lib/sidebarTree"; import { hideSystemAtom, noteSidebarInput, sidebarTreeAtom } from "../lib/store"; import { AppIcon } from "./AppIcon"; /** 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.countLabel ?? (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.appPath != null && } {item.label} {item.sublabel != null && ( {item.sublabel} )}
); } /** Beside the row's button rather than in it, since it is a button of its own */ function ItemRightSlot({ item }: { item: SidebarItem }) { return item.id === SYSTEM_ID ? : ; } /** The eye on System's row, which leaves its daemons out of All traffic */ function HideSystemToggle() { const [hidden, setHidden] = useAtom(hideSystemAtom); return ( { e.stopPropagation(); setHidden(!hidden); }} /> ); } export function Sidebar() { const tree = useAtomValue(sidebarTreeAtom); const getItemKey = useCallback( (item: SidebarItem) => `${item.id}:${item.count}:${item.countLabel ?? ""}`, [], ); 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. // oxlint-disable-next-line jsx-a11y/no-static-element-interactions -- only notes input the tree handles ); }