Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

12.4 KB
Raw Blame
import React from "react";
import { useTranslation } from "react-i18next";
import * as DialogPrimitive from "@radix-ui/react-dialog";
import { X } from "../icons";
import { cn } from "../lib/utils";
import { blurBehindOverlays } from "./overlayBlur";
import { InfoBox } from "./InfoBox";
import { SettingsLayoutProvider } from "./useSettingsLayout";
import { useDismissGuard } from "./useDismissGuard";

export interface SidebarItem<T extends string> {
  id: T;
  label: string;
  icon: React.ComponentType<{ className?: string }>;
  group?: string;
  description?: string;
  badge?: string;
  badgeVariant?: "default" | "new" | "update" | "dot";
  shortcut?: string;
}

interface SidebarModalProps<T extends string> {
  open: boolean;
  onOpenChange: (open: boolean) => void;
  title: string;
  sidebarItems: SidebarItem<T>[];
  activeSection: T;
  onSectionChange: (section: T) => void;
  children: React.ReactNode;
  sidebarWidth?: string;
  version?: string;
  /** Rendered above the nav (hidden in compact mode), e.g. account identity. */
  header?: React.ReactNode;
  /**
   * Account-level notice, e.g. an organization-managed policy. Sits under the
   * header in the sidebar; when the sidebar is compact it collapses to an icon
   * badge beside the close button with `description` as its tooltip.
   */
  notice?: { icon: React.ReactNode; label: string; description: string };
}

export default function SidebarModal<T extends string>({
  open,
  onOpenChange,
  title,
  sidebarItems,
  activeSection,
  onSectionChange,
  children,
  sidebarWidth = "w-52",
  version,
  header,
  notice,
}: SidebarModalProps<T>) {
  const { t } = useTranslation();
  const { registerContent, shouldBlockDismiss } = useDismissGuard<HTMLDivElement>();

  const [isCompact, setIsCompact] = React.useState(false);
  const observerRef = React.useRef<ResizeObserver | null>(null);

  const containerRef = React.useCallback((el: HTMLDivElement | null) => {
    if (observerRef.current) {
      observerRef.current.disconnect();
      observerRef.current = null;
    }
    if (!el) return;
    const observer = new ResizeObserver((entries) => {
      const width = entries[0]?.contentRect.width ?? 0;
      setIsCompact(width > 0 && width < 800);
    });
    observer.observe(el);
    observerRef.current = observer;
  }, []);

  // Group items by their group property
  const groupedItems = React.useMemo(() => {
    const groups: { label: string | null; items: SidebarItem<T>[] }[] = [];
    let currentGroup: string | null | undefined = undefined;

    for (const item of sidebarItems) {
      const group = item.group ?? null;
      if (group !== currentGroup) {
        groups.push({ label: group, items: [item] });
        currentGroup = group;
      } else {
        groups[groups.length - 1].items.push(item);
      }
    }

    return groups;
  }, [sidebarItems]);

  const renderBadge = (item: SidebarItem<T>) => {
    if (!item.badge && item.badgeVariant !== "dot") return null;

    if (item.badgeVariant === "dot") {
      return <span className="ms-auto h-1.5 w-1.5 rounded-full bg-primary shrink-0" />;
    }

    return (
      <span
        className={`ms-auto text-xs font-semibold uppercase tracking-wider px-1.5 py-px rounded-sm shrink-0 ${
          item.badgeVariant === "new"
            ? "bg-primary/10 text-primary dark:bg-primary/15"
            : item.badgeVariant === "update"
              ? "bg-warning/10 text-warning dark:bg-warning/15"
              : "bg-muted text-muted-foreground"
        }`}
      >
        {item.badge}
      </span>
    );
  };

  const actualSidebarWidth = isCompact ? "w-12" : sidebarWidth;

  return (
    <DialogPrimitive.Root open={open} onOpenChange={onOpenChange}>
      <DialogPrimitive.Portal>
        <DialogPrimitive.Overlay
          className={cn(
            "fixed inset-0 z-50 bg-black/60 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0",
            blurBehindOverlays && "backdrop-blur-sm"
          )}
        />
        <DialogPrimitive.Content
          ref={registerContent}
          // Radix focuses the first tabbable on open, which is the close button;
          // focus the dialog itself so the X doesn't open wearing a focus ring.
          onOpenAutoFocus={(e) => {
            e.preventDefault();
            (e.currentTarget as HTMLElement).focus();
          }}
          onEscapeKeyDown={(e) => {
            if (document.querySelector("[data-capturing]")) e.preventDefault();
          }}
          onInteractOutside={(e) => {
            // A dropdown open over this panel makes the panel inert, so the
            // click that closes the dropdown lands on the overlay and would
            // otherwise take the whole settings modal with it.
            if (shouldBlockDismiss(e)) e.preventDefault();
          }}
          className="fixed left-[50%] top-[50%] z-50 max-h-[85vh] w-[90vw] max-w-4xl translate-x-[-50%] translate-y-[-50%] rounded-xl p-0 overflow-hidden outline-none bg-background border border-border shadow-[0_25px_50px_-12px_rgba(0,0,0,0.25)] dark:bg-surface-1 dark:border-border-subtle dark:shadow-[0_25px_60px_-12px_rgba(0,0,0,0.5),0_0_0_1px_rgba(255,255,255,0.05)] duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-98 data-[state=open]:zoom-in-98"
        >
          <div className="relative h-full max-h-[85vh] overflow-hidden">
            <div className="absolute end-4 top-4 z-10 flex items-center gap-2">
              {notice && isCompact && (
                <InfoBox title={notice.description} className="rounded-md p-1.25 text-primary">
                  {notice.icon}
                  <span className="sr-only">{notice.description}</span>
                </InfoBox>
              )}
              <DialogPrimitive.Close className="rounded-md p-1.5 opacity-40 ring-offset-background transition-[opacity,background-color] hover:opacity-100 bg-transparent hover:bg-muted dark:hover:bg-surface-raised outline-none focus-visible:ring-2 focus-visible:ring-ring/30 focus-visible:ring-offset-1">
                <X className="h-3.5 w-3.5 text-muted-foreground" />
                <span className="sr-only">{t("common.close")}</span>
              </DialogPrimitive.Close>
            </div>

            <div ref={containerRef} className="flex h-[85vh]">
              <DialogPrimitive.Title className="sr-only">{title}</DialogPrimitive.Title>

              {/* Sidebar */}
              <div
                className={`${actualSidebarWidth} shrink-0 border-e border-border/70 dark:border-border-subtle flex flex-col bg-surface-1 dark:bg-surface-0 transition-[width] duration-200 ease-out`}
              >
                {/* Identity / custom header */}
                {header && !isCompact && <div className="px-4 pt-5 pb-1">{header}</div>}
                {notice && !isCompact && (
                  <div className="flex justify-center px-4 pb-1">
                    <span
                      aria-hidden="true"
                      title={notice.description}
                      className="inline-flex items-center gap-1.5 text-center text-[11px] text-primary"
                    >
                      {notice.icon}
                      {notice.label}
                    </span>
                    <span className="sr-only">{notice.description}</span>
                  </div>
                )}

                {/* Navigation */}
                <nav
                  className={`relative flex-1 pb-2 overflow-y-auto ${
                    isCompact ? "px-1.5 pt-4" : "px-2 pt-3"
                  }`}
                >
                  {groupedItems.map((group, groupIndex) => (
                    <div key={groupIndex} className={groupIndex > 0 ? "mt-4" : ""}>
                      {!isCompact && group.label && (
                        <div className="px-2 pb-1">
                          <span className="text-[11px] font-medium text-muted-foreground/70 dark:text-muted-foreground/65">
                            {group.label}
                          </span>
                        </div>
                      )}
                      <div className="space-y-px">
                        {group.items.map((item) => {
                          const Icon = item.icon;
                          const isActive = activeSection === item.id;

                          return (
                            <button
                              key={item.id}
                              data-section-id={item.id}
                              onClick={() => onSectionChange(item.id)}
                              title={isCompact ? item.label : undefined}
                              className={`group relative w-full flex items-center text-start text-xs rounded-md transition-colors duration-100 outline-none ${
                                isCompact ? "justify-center px-0 py-2" : "gap-2 px-2 py-1.5"
                              } ${
                                isActive
                                  ? "text-foreground bg-muted dark:bg-surface-raised"
                                  : "text-muted-foreground dark:text-foreground/75 hover:text-foreground hover:bg-muted/50 dark:hover:bg-surface-2"
                              }`}
                            >
                              <Icon
                                className={`h-4 w-4 shrink-0 transition-colors duration-100 ${
                                  isActive
                                    ? "text-primary"
                                    : "text-muted-foreground/70 dark:text-foreground/55 group-hover:text-foreground/80"
                                }`}
                              />
                              {!isCompact && (
                                <>
                                  <span
                                    className={`flex-1 truncate leading-tight ${isActive ? "font-medium" : "font-normal"}`}
                                  >
                                    {item.label}
                                  </span>
                                  {renderBadge(item)}
                                  {item.shortcut && !item.badge && (
                                    <kbd
                                      dir="ltr"
                                      className="ms-auto text-xs text-muted-foreground/70 font-mono shrink-0"
                                    >
                                      {item.shortcut}
                                    </kbd>
                                  )}
                                </>
                              )}
                              {isCompact && item.badgeVariant === "dot" && (
                                <span className="absolute top-1.5 end-1.5 h-1.5 w-1.5 rounded-full bg-primary" />
                              )}
                            </button>
                          );
                        })}
                      </div>
                    </div>
                  ))}
                </nav>

                {/* Footer / version */}
                {version && (
                  <div
                    className={`border-t border-border/70 dark:border-border-subtle ${
                      isCompact ? "flex justify-center py-2.5" : "px-3 py-2.5"
                    }`}
                  >
                    <div className="flex items-center gap-1.5">
                      <div className="h-1 w-1 rounded-full bg-success/60" />
                      {!isCompact && (
                        <span
                          dir="ltr"
                          className="text-xs text-muted-foreground/70 tabular-nums tracking-wide"
                        >
                          v{version}
                        </span>
                      )}
                    </div>
                  </div>
                )}
              </div>

              {/* Main Content */}
              <div className="flex-1 overflow-y-auto bg-background dark:bg-surface-1">
                <SettingsLayoutProvider value={{ isCompact }}>
                  {/* The close button (and compact notice badge) float over this column's top corner
                      (top-4, 26px tall); pt-[22px] centres a text-xs heading (line-height 1.15) on that row. */}
                  <div className={`${isCompact ? "p-4" : "p-6"} pt-[22px]`}>{children}</div>
                </SettingsLayoutProvider>
              </div>
            </div>
          </div>
        </DialogPrimitive.Content>
      </DialogPrimitive.Portal>
    </DialogPrimitive.Root>
  );
}