Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

3.9 KB
Raw Blame
// The Files-sidebar category picker: a "tune" button opening a modal that lists the fixed, shared
// categories and lets the user show or hide each one in their own sidebar. Device-local and
// presentational only — it never changes the shared categories or the label vocabulary; files in a
// hidden (or no) category fall back to "Other".

import { useMemo, useState, useSyncExternalStore } from "react";
import { useTranslation } from "react-i18next";
import { Icon } from "@editor/ui/Icon";
import { Modal } from "@editor/ui/Modal";
import { Button } from "@editor/ui/Button";
import { ActionIcon } from "@editor/ui/ActionIcon";
import { ClassificationCategoryManager } from "@editor/components/policies/ClassificationCategoryManager";
import { useClassificationEnabled } from "@editor/hooks/useClassificationEnabled";
import { bucketStubsByLabel } from "@editor/components/shared/fileSidebarGroupingLogic";
import {
  getSidebarCategories,
  resetHiddenCategories,
  setCategoryHidden,
  subscribeSidebarCategories,
} from "@editor/services/fileSidebarCategories";
import type { StirlingFileStub } from "@editor/types/fileContext";
import "@editor/components/shared/FileSidebarGroupControls.css";

interface FileSidebarGroupControlsProps {
  /** The files currently listed, for live per-category counts. */
  stubs: StirlingFileStub[];
}

export function FileSidebarGroupControls({
  stubs,
}: FileSidebarGroupControlsProps) {
  const { t } = useTranslation();
  const enabled = useClassificationEnabled();
  const [open, setOpen] = useState(false);
  const categories = useSyncExternalStore(
    subscribeSidebarCategories,
    getSidebarCategories,
  );

  // Files per category (deduped across its labels), from the same bucketing the sidebar groups use.
  const categoryCounts = useMemo(() => {
    const byLabel = bucketStubsByLabel(stubs);
    const counts = new Map<string, number>();
    for (const category of categories) {
      const ids = new Set<string>();
      for (const key of category.labelKeys) {
        for (const stub of byLabel.get(key)?.stubs ?? []) {
          ids.add(stub.id);
        }
      }
      counts.set(category.id, ids.size);
    }
    return counts;
  }, [stubs, categories]);

  // Classification off (non-AI SaaS tenant) → no "customize groups" affordance,
  // matching the flat, ungrouped list. (All hooks above run unconditionally.)
  if (!enabled) return null;

  return (
    <>
      {/* -external: revealed on section-header hover, like the Browse button. */}
      <ActionIcon
        variant="quiet"
        className="file-sidebar-section-btn file-sidebar-section-btn-external"
        onClick={() => setOpen(true)}
        aria-label={t("fileSidebar.customizeGroups", "Customize groups")}
        data-testid="customize-groups"
      >
        <Icon name="sliders-horizontal" size={"1rem"} />
      </ActionIcon>

      <Modal
        open={open}
        onClose={() => setOpen(false)}
        width="md"
        title={t("fileSidebar.groupsModal.title", "Sidebar categories")}
        subtitle={t(
          "fileSidebar.groupsModal.subtitle",
          "Show or hide categories in the files sidebar.",
        )}
        footer={
          <div className="fsg-footer">
            <Button
              variant="tertiary"
              size="sm"
              leftSection={<Icon name="rotate-ccw" size={"1rem"} />}
              onClick={resetHiddenCategories}
            >
              {t("fileSidebar.groupsModal.reset", "Show all")}
            </Button>
            <Button variant="primary" size="sm" onClick={() => setOpen(false)}>
              {t("fileSidebar.groupsModal.done", "Done")}
            </Button>
          </div>
        }
      >
        <div className="fsg-body">
          <ClassificationCategoryManager
            categories={categories}
            onToggleHidden={setCategoryHidden}
            counts={categoryCounts}
          />
        </div>
      </Modal>
    </>
  );
}