Skip to content

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

74.9 KB
Raw Blame
import { useMemo, useRef, useState } from "react";
import { useTranslation } from "react-i18next";
import {
  Check,
  ChevronRight,
  ExternalLink,
  FileText,
  Folder,
  FolderOpen,
  Info,
  Loader2,
  Lock,
  MoreHorizontal,
  Pencil,
  Plus,
  Search,
  Settings,
  Share2,
  Trash2,
  Users,
} from "../icons";
import { FolderNotesPopover } from "./FolderNotesPopover";
import { sidebarFolderNotes } from "./sidebarFolderNotes";
import { Button } from "../ui/button";
import ThemedEmptyIllustration from "../ui/ThemedEmptyIllustration";
import sharedSpaceEmptyLight from "../../assets/empty-states/notes-shared-space-light.svg";
import sharedSpaceEmptyDark from "../../assets/empty-states/notes-shared-space-dark.svg";
import {
  DropdownMenu,
  DropdownMenuTrigger,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuSub,
  DropdownMenuSubTrigger,
  DropdownMenuSubContent,
  DropdownMenuSeparator,
} from "../ui/dropdown-menu";
import { ConfirmDialog } from "../ui/dialog";
import { useDialogs } from "../../hooks/useDialogs";
import { useToast } from "../ui/useToast";
import { useNoteDragAndDrop, type NoteMoveTarget } from "../../hooks/useNoteDragAndDrop";
import { useTeamSpacesCapability } from "../../hooks/useTeamSpacesCapability";
import { useCanCreateTeamSpace } from "../../hooks/useCanCreateTeamSpace";
import { useAuth } from "../../hooks/useAuth";
import { useWorkspace } from "../../hooks/useWorkspace";
import SpaceSettingsDialog from "./SpaceSettingsDialog";
import {
  canChangeSpaceNoteScope,
  canDeleteSpaceNote,
  canManageSpace,
  canManageWorkspace,
  canMoveBetweenSpaces,
  canMoveOrDeleteSpaceFolder,
} from "../../lib/spacePermissions";
import {
  canOrganizeNote as canOrganizeSharedNote,
  resolveNotePermission,
  sharedNoteBlocksDelete,
} from "../../lib/notePermissions";
import { groupTeamSpacesByWorkspace } from "../../lib/workspaceSelection";
import { localMutationErrorKey } from "../../lib/localMutationError";
import { useSettingsStore } from "../../stores/settingsStore";
import { cn } from "../lib/utils";
import { getCachedPlatform } from "../../utils/platform";
import CreateSpaceDialog from "./CreateSpaceDialog";
import DeleteSpaceDialog from "./DeleteSpaceDialog";
import { treeHorizontalIntent, treeRowActionClearanceStyle } from "./treeDirection";
import { defaultFolderDisplayName } from "./shared";
import type { FolderItem, NoteItem, SpaceItem, WorkspaceRole } from "../../types/electron";
import {
  folderContainerKey,
  spaceContainerKey,
  useSpaces,
  useFolders,
  useFolderCounts,
  useSpaceRootCounts,
  useNotesByContainer,
  useExpandedContainers,
  useActiveContext,
  useActiveNoteId,
  useActiveNote,
  useIsTreeLoading,
  setActiveContext,
  setActiveNoteId,
  setContainerExpanded,
  toggleContainerExpanded,
  revealContainer,
  createFolder,
  renameFolder,
  deleteFolder,
  moveFolderToSpace,
  getNoteFromStore,
  getFoldersValue,
  getSpacesValue,
  useShareCache,
} from "../../stores/noteStore";

const FOLDER_INPUT_CLASS =
  "w-full h-6 bg-foreground/5 dark:bg-white/5 rounded px-2 text-xs text-foreground outline-none border border-primary/30 focus:border-primary/50";

const ROW_BASE_CLASS =
  "group relative flex items-center gap-2 rounded-md cursor-pointer select-none " +
  "transition-colors duration-150 outline-none focus-visible:ring-1 focus-visible:ring-ring/30";

// Button forces svg children to 16px; these 20px controls want the 12px icon they pass.
const KEBAB_BUTTON_CLASS =
  "h-5 w-5 rounded-sm [&_svg]:size-3! opacity-0 group-hover:opacity-100 focus-visible:opacity-100 data-[state=open]:opacity-100 " +
  "transition-opacity text-muted-foreground/70 dark:text-muted-foreground/70 " +
  "hover:text-foreground/60 hover:bg-foreground/5 active:bg-foreground/8";

const KEBAB_TRIGGER_CLASS = cn(KEBAB_BUTTON_CLASS, "absolute end-1.5");

const HOVER_REVEAL_BUTTON_CLASS =
  "h-5 w-5 rounded-sm [&_svg]:size-3! opacity-0 focus-visible:opacity-100 transition-opacity " +
  "text-muted-foreground/70 dark:text-muted-foreground/70 hover:text-foreground/60 " +
  "hover:bg-foreground/5 active:bg-foreground/8";

const MENU_ITEM_CLASS = "text-xs gap-2 rounded-md px-2 py-1";

const SUB_CONTENT_CLASS = "min-w-36 rounded-xl border border-border p-1";

const DROP_TARGET_CLASS = "bg-primary/12 dark:bg-primary/15 ring-1 ring-primary/25";
const DROP_SUCCESS_CLASS = "bg-emerald-500/10 dark:bg-emerald-400/10 ring-1 ring-emerald-500/20";
const SUB_TRIGGER_CLASS = cn(
  MENU_ITEM_CLASS,
  "cursor-pointer focus:bg-foreground/5 data-[state=open]:bg-foreground/5"
);

type TFn = (key: string, options?: Record<string, unknown>) => string;

type TreeRow =
  | { type: "more"; key: string; folder: FolderItem; parentKey: string; level: 2 | 3 }
  | { type: "space"; key: string; space: SpaceItem; parentKey?: undefined }
  | {
      type: "folder";
      key: string;
      folder: FolderItem;
      parentKey?: string;
      level: 1 | 2;
    }
  | {
      type: "note";
      key: string;
      note: NoteItem;
      parentKey?: string;
      level: 1 | 2 | 3;
    };

interface DropHandlers {
  onDragOver: (e: React.DragEvent) => void;
  onDragEnter: (e: React.DragEvent) => void;
  onDragLeave: () => void;
  onDrop: (e: React.DragEvent) => void;
}

export interface RowA11yProps {
  tabIndex: number;
  rowRef: (el: HTMLDivElement | null) => void;
  onKeyDown: (e: React.KeyboardEvent<HTMLDivElement>) => void;
  onFocus: () => void;
}

interface SpacesTreeProps {
  onDeleteNote: (id: number) => void;
  onMoveNote: (noteId: number, target: NoteMoveTarget) => Promise<void>;
  onCreateFolderAndMove: (noteId: number, folderName: string) => void;
  onNewNote: (spaceId: number, folderId: number | null) => void;
  onShowStructureIntro?: () => void;
}

function spaceDisplayName(space: SpaceItem, t: TFn): string {
  return space.kind === "private" ? t("notes.spaces.personal") : space.name;
}

function getFileManagerName(): string {
  const platform = getCachedPlatform();
  return platform === "darwin" ? "Finder" : platform === "win32" ? "Explorer" : "Files";
}

function SectionHeader({
  label,
  icon,
  action,
  className,
  expanded,
  onToggle,
  toggleRef,
  dropHandlers,
  isDragOver,
  isDropSuccess,
}: {
  label: string;
  /** Resting icon shown in the chevron slot; the chevron appears on hover. */
  icon?: React.ReactNode;
  action?: React.ReactNode;
  className?: string;
  expanded?: boolean;
  onToggle?: () => void;
  toggleRef?: React.Ref<HTMLButtonElement>;
  dropHandlers?: DropHandlers;
  isDragOver?: boolean;
  isDropSuccess?: boolean;
}) {
  // All-caps has no descenders, so its optical centre sits ~1px above the line box.
  const labelClassName =
    "translate-y-px text-[11px] font-medium uppercase tracking-[0.08em] text-foreground/55 select-none";

  return (
    <div
      role="none"
      {...dropHandlers}
      className={cn(
        "group flex items-center justify-between h-7 px-2 rounded-md",
        isDragOver && DROP_TARGET_CLASS,
        isDropSuccess && DROP_SUCCESS_CLASS,
        className
      )}
    >
      {onToggle ? (
        <button
          ref={toggleRef}
          type="button"
          aria-expanded={expanded}
          onClick={onToggle}
          className="flex h-full min-w-0 items-center gap-2 rounded-sm outline-none hover:text-foreground focus-visible:ring-1 focus-visible:ring-ring/30"
        >
          <span
            aria-hidden="true"
            className="relative h-4 w-4 flex items-center justify-center shrink-0"
          >
            {icon && (
              <span className="absolute inset-0 flex items-center justify-center transition-opacity duration-150 group-hover:opacity-0">
                {icon}
              </span>
            )}
            <ChevronRight
              size={12}
              className={cn(
                "text-foreground/60 transition-all duration-150",
                expanded ? "rotate-90" : "rtl:rotate-180",
                icon && "opacity-0 group-hover:opacity-100"
              )}
            />
          </span>
          <span className={labelClassName}>{label}</span>
        </button>
      ) : (
        <span className={labelClassName}>{label}</span>
      )}
      {action}
    </div>
  );
}

function TreeChildren({
  open,
  children,
  grouped = true,
}: {
  open: boolean;
  children: React.ReactNode;
  grouped?: boolean;
}) {
  return (
    <div
      role="none"
      className={cn(
        "grid transition-[grid-template-rows] duration-[160ms] ease-out",
        open ? "grid-rows-[1fr]" : "grid-rows-[0fr]"
      )}
    >
      <div
        role={grouped ? "group" : "none"}
        className={cn(
          "min-h-0 overflow-hidden transition-opacity duration-[80ms]",
          open ? "opacity-100" : "opacity-0"
        )}
      >
        {children}
      </div>
    </div>
  );
}

/**
 * Expand/collapse toggle. With `icon`, the icon is the resting state and the
 * chevron fades in when the row (a `group`) is hovered.
 */
function RowToggle({
  isExpanded,
  onToggle,
  icon,
}: {
  isExpanded: boolean;
  onToggle: () => void;
  icon?: React.ReactNode;
}) {
  return (
    <span
      aria-hidden="true"
      onClick={(e) => {
        e.stopPropagation();
        onToggle();
      }}
      className="relative h-4 w-4 flex items-center justify-center shrink-0 rounded-sm text-foreground/50 hover:text-foreground/80 transition-colors duration-150"
    >
      {icon && (
        <span className="absolute inset-0 flex items-center justify-center transition-opacity duration-150 group-hover:opacity-0">
          {icon}
        </span>
      )}
      <ChevronRight
        size={12}
        className={cn(
          "transition-all duration-150",
          isExpanded ? "rotate-90" : "rtl:rotate-180",
          icon && "opacity-0 group-hover:opacity-100"
        )}
      />
    </span>
  );
}

function DropSuccessCheck({ isDropSuccess }: { isDropSuccess: boolean }) {
  if (!isDropSuccess) return null;
  return (
    <Check
      size={10}
      className="text-emerald-500 dark:text-emerald-400 shrink-0 animate-[scale-in_200ms_ease-out]"
    />
  );
}

function SearchableMoveSubmenu({
  icon,
  label,
  itemCount,
  search,
  onSearchChange,
  searchPlaceholder,
  children,
  footer,
}: {
  icon: React.ReactNode;
  label: string;
  itemCount: number;
  search: string;
  onSearchChange: (value: string) => void;
  searchPlaceholder: string;
  children: React.ReactNode;
  footer?: React.ReactNode;
}) {
  return (
    <DropdownMenuSub>
      <DropdownMenuSubTrigger className={SUB_TRIGGER_CLASS}>
        {icon}
        {label}
      </DropdownMenuSubTrigger>
      <DropdownMenuSubContent sideOffset={4} className={SUB_CONTENT_CLASS}>
        {itemCount > 5 && (
          <>
            <div className="relative px-1.5 py-0.5">
              <Search
                size={12}
                className="absolute start-3 top-1/2 -translate-y-1/2 text-foreground/45 pointer-events-none"
              />
              <input
                dir="auto"
                value={search}
                onChange={(event) => onSearchChange(event.target.value)}
                onKeyDown={(event) => event.stopPropagation()}
                placeholder={searchPlaceholder}
                className="input-inline w-full ps-8 pe-1 py-1 text-xs text-foreground placeholder:text-foreground/45 outline-none border-none appearance-none"
              />
            </div>
            <DropdownMenuSeparator />
          </>
        )}
        <div className="overflow-y-auto max-h-40">{children}</div>
        {footer && (
          <>
            <DropdownMenuSeparator />
            {footer}
          </>
        )}
      </DropdownMenuSubContent>
    </DropdownMenuSub>
  );
}

function SpaceMenuIcon({ space }: { space: SpaceItem }) {
  if (space.kind === "private") {
    return <Lock size={11} className="text-muted-foreground/70 shrink-0" />;
  }
  if (space.emoji) {
    return (
      <span className="text-[11px] leading-none shrink-0" aria-hidden="true">
        {space.emoji}
      </span>
    );
  }
  return <Users size={11} className="text-muted-foreground/70 shrink-0" />;
}

function SpaceRow({
  space,
  displayName,
  isExpanded,
  isActive,
  count,
  isDragOver,
  isDropSuccess,
  dropHandlers,
  onActivate,
  onToggle,
  onNewFolder,
  onSettings,
  a11y,
  t,
}: {
  space: SpaceItem;
  displayName: string;
  isExpanded: boolean;
  isActive: boolean;
  count: number;
  isDragOver: boolean;
  isDropSuccess: boolean;
  dropHandlers: DropHandlers;
  onActivate: () => void;
  onToggle: () => void;
  onNewFolder: () => void;
  onSettings: () => void;
  a11y: RowA11yProps;
  t: TFn;
}) {
  const isPrivate = space.kind === "private";
  return (
    <div
      role="treeitem"
      aria-level={1}
      aria-expanded={isExpanded}
      aria-selected={isActive}
      aria-label={
        count > 0 ? `${displayName}, ${t("notes.spaces.noteCount", { count })}` : displayName
      }
      tabIndex={a11y.tabIndex}
      ref={a11y.rowRef}
      onKeyDown={a11y.onKeyDown}
      onFocus={a11y.onFocus}
      onClick={onActivate}
      title={isPrivate ? t("notes.spaces.privateTooltip") : displayName}
      {...dropHandlers}
      style={treeRowActionClearanceStyle()}
      className={cn(
        ROW_BASE_CLASS,
        "h-7 px-2",
        isActive
          ? "bg-primary/8 dark:bg-primary/10"
          : "hover:bg-foreground/4 dark:hover:bg-white/4",
        isDragOver && DROP_TARGET_CLASS,
        isDropSuccess && DROP_SUCCESS_CLASS
      )}
    >
      <RowToggle
        isExpanded={isExpanded}
        onToggle={onToggle}
        icon={
          isPrivate ? (
            <Lock
              size={14}
              className={cn(
                "transition-colors duration-150",
                isActive ? "text-primary" : "text-foreground/55 dark:text-foreground/45"
              )}
            />
          ) : space.emoji ? (
            <span className="text-[12px] leading-none" aria-hidden="true">
              {space.emoji}
            </span>
          ) : (
            <Users
              size={14}
              className={cn(
                "transition-colors duration-150",
                isDragOver || isActive
                  ? "text-primary"
                  : "text-foreground/55 dark:text-foreground/45"
              )}
            />
          )
        }
      />
      <span
        dir="auto"
        className={cn(
          "text-[13px] truncate flex-1 transition-colors duration-150",
          isDragOver || isActive ? "text-foreground font-medium" : "text-foreground/85"
        )}
      >
        {displayName}
      </span>
      <DropSuccessCheck isDropSuccess={isDropSuccess} />
      <span className="absolute end-1.5 flex items-center gap-px">
        <Button
          variant="ghost"
          size="icon"
          aria-label={t("notes.context.newFolder")}
          onClick={(e) => {
            e.stopPropagation();
            onNewFolder();
          }}
          className={KEBAB_BUTTON_CLASS}
        >
          <Plus size={12} />
        </Button>
        {!isPrivate && (
          <DropdownMenu>
            <DropdownMenuTrigger asChild>
              <Button
                variant="ghost"
                size="icon"
                aria-label={t("common.actions")}
                onClick={(e) => e.stopPropagation()}
                className={KEBAB_BUTTON_CLASS}
              >
                <MoreHorizontal size={12} />
              </Button>
            </DropdownMenuTrigger>
            <DropdownMenuContent align="end" sideOffset={4} className="min-w-36">
              <DropdownMenuItem
                onClick={(e) => {
                  e.stopPropagation();
                  onSettings();
                }}
                className={MENU_ITEM_CLASS}
              >
                <Settings size={11} className="text-muted-foreground/70" />
                {t("notes.spaces.settings")}
              </DropdownMenuItem>
            </DropdownMenuContent>
          </DropdownMenu>
        )}
      </span>
    </div>
  );
}

function FolderRow({
  folder,
  level,
  spaces,
  isExpanded,
  isActive,
  count,
  isDragOver,
  isDropSuccess,
  dropHandlers,
  noteFilesEnabled,
  fileManagerName,
  canManageDestructive,
  onActivate,
  onToggle,
  onNewNote,
  onRename,
  onMoveToSpace,
  onDelete,
  a11y,
  t,
}: {
  folder: FolderItem;
  level: 1 | 2;
  spaces: SpaceItem[];
  isExpanded: boolean;
  isActive: boolean;
  count: number;
  isDragOver: boolean;
  isDropSuccess: boolean;
  dropHandlers: DropHandlers;
  noteFilesEnabled: boolean;
  fileManagerName: string;
  canManageDestructive: boolean;
  onActivate: () => void;
  onToggle: () => void;
  onNewNote: () => void;
  onRename: () => void;
  onMoveToSpace: (space: SpaceItem) => void;
  onDelete: () => void;
  a11y: RowA11yProps;
  t: TFn;
}) {
  const [spaceSearch, setSpaceSearch] = useState("");
  const displayName = defaultFolderDisplayName(folder, t);
  const canMoveToSpace = canManageDestructive && !folder.is_default && spaces.length > 1;
  const filteredSpaces = useMemo(
    () =>
      spaceSearch
        ? spaces.filter((s) =>
            spaceDisplayName(s, t).toLowerCase().includes(spaceSearch.toLowerCase())
          )
        : spaces,
    [spaces, spaceSearch, t]
  );

  return (
    <div
      role="treeitem"
      aria-level={level}
      aria-expanded={isExpanded}
      aria-selected={isActive}
      aria-label={
        count > 0 ? `${displayName}, ${t("notes.spaces.noteCount", { count })}` : displayName
      }
      tabIndex={a11y.tabIndex}
      ref={a11y.rowRef}
      onKeyDown={a11y.onKeyDown}
      onFocus={a11y.onFocus}
      onClick={onActivate}
      title={displayName}
      {...dropHandlers}
      style={treeRowActionClearanceStyle()}
      className={cn(
        ROW_BASE_CLASS,
        "h-7 pe-2",
        level === 1 ? "ps-2" : "ps-[14px]",
        isActive
          ? "bg-primary/8 dark:bg-primary/10"
          : "hover:bg-foreground/4 dark:hover:bg-white/4",
        isDragOver && DROP_TARGET_CLASS,
        isDropSuccess && DROP_SUCCESS_CLASS
      )}
    >
      <RowToggle
        isExpanded={isExpanded}
        onToggle={onToggle}
        icon={
          <Folder
            size={14}
            className={cn(
              "transition-colors duration-150",
              isDragOver || isActive ? "text-primary" : "text-foreground/55 dark:text-foreground/45"
            )}
          />
        }
      />
      <span
        dir="auto"
        className={cn(
          "text-[13px] truncate flex-1 transition-colors duration-150",
          isDragOver || isActive
            ? "text-foreground font-medium"
            : "text-foreground/85 group-hover:text-foreground"
        )}
      >
        {displayName}
      </span>
      <DropSuccessCheck isDropSuccess={isDropSuccess} />
      <span className="absolute end-1.5 flex items-center gap-px">
        <Button
          variant="ghost"
          size="icon"
          aria-label={t("notes.list.newNote")}
          onClick={(e) => {
            e.stopPropagation();
            onNewNote();
          }}
          className={KEBAB_BUTTON_CLASS}
        >
          <Plus size={12} />
        </Button>
        {(!folder.is_default || noteFilesEnabled) && (
          <DropdownMenu onOpenChange={(open) => !open && setSpaceSearch("")}>
            <DropdownMenuTrigger asChild>
              <Button
                variant="ghost"
                size="icon"
                aria-label={t("common.actions")}
                onClick={(e) => e.stopPropagation()}
                className={KEBAB_BUTTON_CLASS}
              >
                <MoreHorizontal size={12} />
              </Button>
            </DropdownMenuTrigger>
            <DropdownMenuContent align="end" sideOffset={4} className="min-w-32">
              {noteFilesEnabled && (
                <DropdownMenuItem
                  onClick={(e) => {
                    e.stopPropagation();
                    window.electronAPI?.showFolderInExplorer?.(folder.name);
                  }}
                  className={MENU_ITEM_CLASS}
                >
                  <ExternalLink size={11} className="text-muted-foreground/70" />
                  {t("notes.context.showInFileManager", { manager: fileManagerName })}
                </DropdownMenuItem>
              )}
              {!folder.is_default && (
                <>
                  {noteFilesEnabled && <DropdownMenuSeparator />}
                  <DropdownMenuItem
                    onClick={(e) => {
                      e.stopPropagation();
                      onRename();
                    }}
                    className={MENU_ITEM_CLASS}
                  >
                    <Pencil size={11} className="text-muted-foreground/70" />
                    {t("notes.context.rename")}
                  </DropdownMenuItem>
                  {canMoveToSpace && (
                    <SearchableMoveSubmenu
                      icon={<Users size={11} className="text-muted-foreground/70" />}
                      label={t("notes.spaces.moveToSpace")}
                      itemCount={spaces.length}
                      search={spaceSearch}
                      onSearchChange={setSpaceSearch}
                      searchPlaceholder={t("notes.spaces.searchSpaces")}
                    >
                      {filteredSpaces.map((space) => {
                        const isCurrent = space.id === folder.space_id;
                        return (
                          <DropdownMenuItem
                            key={space.id}
                            disabled={isCurrent}
                            onClick={(e) => {
                              e.stopPropagation();
                              onMoveToSpace(space);
                            }}
                            className={MENU_ITEM_CLASS}
                          >
                            <SpaceMenuIcon space={space} />
                            <span dir="auto" className="truncate flex-1">
                              {spaceDisplayName(space, t)}
                            </span>
                            {isCurrent && <Check size={9} className="text-primary shrink-0" />}
                          </DropdownMenuItem>
                        );
                      })}
                      {spaceSearch && filteredSpaces.length === 0 && (
                        <p className="text-xs text-foreground/50 text-center py-1.5">
                          {t("notes.spaces.noSpacesFound")}
                        </p>
                      )}
                    </SearchableMoveSubmenu>
                  )}
                  {canManageDestructive && (
                    <>
                      <DropdownMenuSeparator />
                      <DropdownMenuItem
                        onClick={(e) => {
                          e.stopPropagation();
                          onDelete();
                        }}
                        className={cn(
                          MENU_ITEM_CLASS,
                          "text-destructive focus:text-destructive focus:bg-destructive/10"
                        )}
                      >
                        <Trash2 size={11} />
                        {t("notes.context.delete")}
                      </DropdownMenuItem>
                    </>
                  )}
                </>
              )}
            </DropdownMenuContent>
          </DropdownMenu>
        )}
      </span>
    </div>
  );
}

interface MoveOption {
  key: string;
  label: string;
  space: SpaceItem;
  target: NoteMoveTarget;
  isCurrent: boolean;
}

function NoteLeaf({
  note,
  level,
  indentClassName,
  isActive,
  isDragging,
  dragHandlers,
  spaces,
  folders,
  noteFilesEnabled,
  fileManagerName,
  canDelete,
  canMove,
  onOpen,
  onMove,
  onCreateFolderAndMove,
  onDelete,
  a11y,
  t,
}: {
  note: NoteItem;
  level: 1 | 2 | 3;
  indentClassName?: string;
  isActive: boolean;
  isDragging: boolean;
  dragHandlers: {
    draggable: true;
    onDragStart: (e: React.DragEvent) => void;
    onDragEnd: () => void;
  };
  spaces: SpaceItem[];
  folders: FolderItem[];
  noteFilesEnabled: boolean;
  fileManagerName: string;
  canDelete: boolean;
  canMove: boolean;
  onOpen: () => void;
  onMove: (target: NoteMoveTarget) => void;
  onCreateFolderAndMove: (noteId: number, folderName: string) => void;
  onDelete: (id: number) => void;
  a11y: RowA11yProps;
  t: TFn;
}) {
  const [moveSearch, setMoveSearch] = useState("");
  const [newFolderName, setNewFolderName] = useState("");
  const [isCreating, setIsCreating] = useState(false);

  const multiSpace = spaces.length > 1;

  const moveOptions = useMemo<MoveOption[]>(() => {
    const options: MoveOption[] = [];
    for (const space of spaces) {
      if (space.kind === "team") {
        options.push({
          key: spaceContainerKey(space.id),
          label: spaceDisplayName(space, t),
          space,
          target: { spaceId: space.id, folderId: null },
          isCurrent: note.folder_id == null && note.space_id === space.id,
        });
      }
      for (const folder of folders.filter((f) => f.space_id === space.id)) {
        options.push({
          key: folderContainerKey(folder.id),
          label: defaultFolderDisplayName(folder, t),
          space,
          target: { spaceId: space.id, folderId: folder.id },
          isCurrent: note.folder_id === folder.id,
        });
      }
    }
    return options;
  }, [spaces, folders, note.folder_id, note.space_id, t]);

  const filteredOptions = useMemo(() => {
    if (!moveSearch) return moveOptions;
    const query = moveSearch.toLowerCase();
    return moveOptions.filter(
      (option) =>
        option.label.toLowerCase().includes(query) ||
        spaceDisplayName(option.space, t).toLowerCase().includes(query)
    );
  }, [moveOptions, moveSearch, t]);

  const renderOption = (option: MoveOption, label: string) => (
    <DropdownMenuItem
      key={option.key}
      disabled={option.isCurrent}
      onClick={(e) => {
        e.stopPropagation();
        onMove(option.target);
      }}
      className={MENU_ITEM_CLASS}
    >
      <span dir="auto" className="truncate flex-1">
        {label}
      </span>
      {option.isCurrent && <Check size={9} className="text-primary shrink-0" />}
    </DropdownMenuItem>
  );

  const title = note.title || t("notes.list.untitled");
  const hasActions = noteFilesEnabled || canMove || canDelete;

  return (
    <div
      role="treeitem"
      aria-level={level}
      aria-selected={isActive}
      tabIndex={a11y.tabIndex}
      ref={a11y.rowRef}
      onKeyDown={a11y.onKeyDown}
      onFocus={a11y.onFocus}
      onClick={onOpen}
      title={title}
      {...(canMove ? dragHandlers : {})}
      style={hasActions ? treeRowActionClearanceStyle(1) : undefined}
      className={cn(
        ROW_BASE_CLASS,
        "h-7 pe-2",
        indentClassName ?? (level === 3 ? "ps-10" : "ps-[14px]"),
        isActive
          ? "bg-primary/8 dark:bg-primary/10"
          : "hover:bg-foreground/4 dark:hover:bg-white/4",
        isDragging && "opacity-40"
      )}
    >
      <FileText
        size={14}
        className={cn(
          "shrink-0 transition-colors duration-150",
          isActive
            ? "text-primary"
            : "text-foreground/50 dark:text-foreground/45 group-hover:text-foreground/70 dark:group-hover:text-foreground/55"
        )}
      />
      <span
        dir="auto"
        className={cn(
          "text-[13px] truncate flex-1 transition-colors duration-150",
          isActive
            ? "text-foreground font-medium"
            : "text-foreground/85 group-hover:text-foreground"
        )}
      >
        {title}
      </span>
      {Boolean(note.is_shared) && (
        <Share2
          size={11}
          role="img"
          aria-label={t("notes.list.shared")}
          className="text-foreground/45 shrink-0 transition-opacity group-hover:opacity-0"
        />
      )}
      {hasActions && (
        <DropdownMenu
          onOpenChange={(open) => {
            if (!open) {
              setMoveSearch("");
              setIsCreating(false);
              setNewFolderName("");
            }
          }}
        >
          <DropdownMenuTrigger asChild>
            <Button
              variant="ghost"
              size="icon"
              aria-label={t("common.actions")}
              onClick={(e) => e.stopPropagation()}
              className={KEBAB_TRIGGER_CLASS}
            >
              <MoreHorizontal size={12} />
            </Button>
          </DropdownMenuTrigger>
          <DropdownMenuContent align="end" sideOffset={4} className="min-w-40">
            {noteFilesEnabled && (
              <>
                <DropdownMenuItem
                  onClick={(e) => {
                    e.stopPropagation();
                    window.electronAPI?.showNoteFile?.(note.id);
                  }}
                  className={MENU_ITEM_CLASS}
                >
                  <ExternalLink size={11} className="text-muted-foreground/70" />
                  {t("notes.context.showInFileManager", { manager: fileManagerName })}
                </DropdownMenuItem>
                {(canMove || canDelete) && <DropdownMenuSeparator />}
              </>
            )}
            {canMove && (
              <SearchableMoveSubmenu
                icon={<FolderOpen size={11} className="text-muted-foreground/70" />}
                label={multiSpace ? t("notes.spaces.moveTo") : t("notes.context.moveToFolder")}
                itemCount={moveOptions.length}
                search={moveSearch}
                onSearchChange={setMoveSearch}
                searchPlaceholder={t("notes.context.searchFolders")}
                footer={
                  isCreating ? (
                    <div className="px-1">
                      <input
                        dir="auto"
                        autoFocus
                        value={newFolderName}
                        onChange={(e) => setNewFolderName(e.target.value)}
                        onKeyDown={(e) => {
                          e.stopPropagation();
                          if (e.key === "Enter" && newFolderName.trim()) {
                            onCreateFolderAndMove(note.id, newFolderName.trim());
                            setNewFolderName("");
                            setIsCreating(false);
                          }
                          if (e.key === "Escape") {
                            setIsCreating(false);
                            setNewFolderName("");
                          }
                        }}
                        placeholder={t("notes.folders.folderName")}
                        className="input-inline w-full px-2 py-1.5 rounded-md bg-transparent text-xs text-foreground placeholder:text-foreground/45 outline-none border-none appearance-none"
                      />
                    </div>
                  ) : (
                    <DropdownMenuItem
                      onSelect={(e) => {
                        e.preventDefault();
                        setIsCreating(true);
                      }}
                      className={cn(MENU_ITEM_CLASS, "text-foreground/70")}
                    >
                      <Plus size={10} />
                      {t("notes.context.newFolder")}
                    </DropdownMenuItem>
                  )
                }
              >
                {moveSearch ? (
                  <>
                    {filteredOptions.map((option) =>
                      renderOption(
                        option,
                        multiSpace && option.target.folderId != null
                          ? `${spaceDisplayName(option.space, t)} / ${option.label}`
                          : option.label
                      )
                    )}
                    {filteredOptions.length === 0 && (
                      <p className="text-xs text-foreground/50 text-center py-1.5">
                        {t("notes.context.noResults")}
                      </p>
                    )}
                  </>
                ) : multiSpace ? (
                  spaces.map((space) => {
                    const spaceOptions = moveOptions.filter(
                      (option) => option.space.id === space.id
                    );
                    if (spaceOptions.length === 0) return null;
                    return (
                      <DropdownMenuSub key={space.id}>
                        <DropdownMenuSubTrigger className={SUB_TRIGGER_CLASS}>
                          <SpaceMenuIcon space={space} />
                          <span dir="auto" className="truncate flex-1">
                            {spaceDisplayName(space, t)}
                          </span>
                        </DropdownMenuSubTrigger>
                        <DropdownMenuSubContent
                          sideOffset={4}
                          className={cn(SUB_CONTENT_CLASS, "max-h-40 overflow-y-auto")}
                        >
                          {spaceOptions.map((option) =>
                            renderOption(
                              option,
                              option.target.folderId == null
                                ? t("notes.spaces.spaceRoot")
                                : option.label
                            )
                          )}
                        </DropdownMenuSubContent>
                      </DropdownMenuSub>
                    );
                  })
                ) : (
                  moveOptions.map((option) => renderOption(option, option.label))
                )}
              </SearchableMoveSubmenu>
            )}
            {canDelete && (
              <>
                {canMove && <DropdownMenuSeparator />}
                <DropdownMenuItem
                  onClick={(e) => {
                    e.stopPropagation();
                    onDelete(note.id);
                  }}
                  className={cn(
                    MENU_ITEM_CLASS,
                    "text-destructive focus:text-destructive focus:bg-destructive/10"
                  )}
                >
                  <Trash2 size={11} />
                  {t("notes.context.delete")}
                </DropdownMenuItem>
              </>
            )}
          </DropdownMenuContent>
        </DropdownMenu>
      )}
    </div>
  );
}

function SkeletonRows() {
  return (
    <div className="space-y-px" aria-hidden="true">
      {["w-3/5", "w-2/5", "w-1/2"].map((width) => (
        <div key={width} className="flex items-center h-7 ps-[18px] pe-2">
          <div
            className={cn(
              "h-2.5 rounded-full bg-foreground/6 dark:bg-white/6 animate-pulse",
              width
            )}
          />
        </div>
      ))}
    </div>
  );
}

export default function SpacesTree({
  onDeleteNote,
  onMoveNote,
  onCreateFolderAndMove,
  onNewNote,
  onShowStructureIntro,
}: SpacesTreeProps) {
  const { t, i18n } = useTranslation();
  const { toast, dismiss } = useToast();
  const fileManagerName = getFileManagerName();

  const spaces = useSpaces();
  const folders = useFolders();
  const folderCounts = useFolderCounts();
  const spaceRootCounts = useSpaceRootCounts();
  const notesByContainer = useNotesByContainer();
  const [openFolderId, setOpenFolderId] = useState<number | null>(null);
  const expanded = useExpandedContainers();
  const activeContext = useActiveContext();
  const activeNoteId = useActiveNoteId();
  const activeNote = useActiveNote();

  const isTreeLoading = useIsTreeLoading();
  const { isSignedIn, user } = useAuth();
  const teamCapability = useTeamSpacesCapability(isSignedIn);
  const canCreateTeamSpace = useCanCreateTeamSpace();
  const { workspaces, loaded: workspacesLoaded } = useWorkspace();
  const noteFilesEnabled = useSettingsStore((s) => s.noteFilesEnabled);
  const shareByCloudId = useShareCache();

  const [creatingFolderSpaceId, setCreatingFolderSpaceId] = useState<number | null>(null);
  const [newFolderName, setNewFolderName] = useState("");
  const [renamingFolderId, setRenamingFolderId] = useState<number | null>(null);
  const [renameValue, setRenameValue] = useState("");
  const [settingsSpaceId, setSettingsSpaceId] = useState<number | null>(null);
  const [settingsOpen, setSettingsOpen] = useState(false);
  const [showCreateSpace, setShowCreateSpace] = useState(false);
  const [createSpaceWorkspaceId, setCreateSpaceWorkspaceId] = useState<string | null>(null);
  const [deleteSpaceTarget, setDeleteSpaceTarget] = useState<SpaceItem | null>(null);
  const [focusedKey, setFocusedKey] = useState<string | null>(null);
  const rowRefs = useRef(new Map<string, HTMLDivElement>());
  const privateSectionToggleRef = useRef<HTMLButtonElement>(null);
  const undoToastIdRef = useRef<string | null>(null);

  const { confirmDialog, showConfirmDialog, hideConfirmDialog } = useDialogs();

  const privateSpaces = useMemo(() => spaces.filter((s) => s.kind === "private"), [spaces]);
  const privateSpace = privateSpaces[0];
  const privateSectionExpanded = privateSpace
    ? expanded.has(spaceContainerKey(privateSpace.id))
    : false;
  const teamSpaces = useMemo(() => spaces.filter((s) => s.kind === "team"), [spaces]);
  const showWorkspaceGroups = workspaces.length > 1;
  const {
    groups: teamSpaceGroups,
    ungrouped: ungroupedTeamSpaces,
    ordered: groupedTeamSpaces,
  } = useMemo(() => groupTeamSpacesByWorkspace(workspaces, teamSpaces), [teamSpaces, workspaces]);
  const orderedTeamSpaces = showWorkspaceGroups ? groupedTeamSpaces : teamSpaces;
  const visibleSpaces = teamCapability ? spaces : privateSpaces;
  const spacesById = useMemo(() => new Map(spaces.map((space) => [space.id, space])), [spaces]);
  // Valid move destinations per source space (the source itself stays listed —
  // menus render it as the disabled "current" row).
  const moveTargetsBySpace = useMemo(() => {
    const targets = new Map<number, SpaceItem[]>();
    for (const source of spaces) {
      targets.set(
        source.id,
        visibleSpaces.filter(
          (target) => target.id === source.id || canMoveBetweenSpaces(source, target)
        )
      );
    }
    return targets;
  }, [spaces, visibleSpaces]);

  // Until the workspace fetch settles, grouping is unknown — showing spaces
  // flat and regrouping on arrival reads as a glitch, so skeleton instead.
  // Signed-out users never load workspaces (mirrored team spaces render flat);
  // errors still flip `loaded`, so this can't skeleton forever.
  const workspacesPending = isSignedIn && !workspacesLoaded;

  const currentUserId = user?.id ?? null;
  const workspaceRoleFor = (space: SpaceItem | undefined): WorkspaceRole | null =>
    workspaces.find((w) => w.id === space?.workspace_id)?.role ?? null;

  // Local-only spaces (no cloud id) stay fully manageable; cloud spaces
  // follow space/workspace roles. Cosmetic — the server enforces.
  const canManageTeamSpace = (space: SpaceItem): boolean =>
    !space.cloud_space_id || canManageSpace(space, workspaceRoleFor(space));

  // Note-level ACL for cloud-backed personal notes shared with this user.
  // Without a cache entry (note never opened this session, offline) the
  // owner fallback keeps today's behavior; once the ACL loads, editors and
  // viewers lose owner-only actions. Team notes follow space roles instead.
  const sharedNoteScope = (note: NoteItem) => ({
    isTeamNote: spacesById.get(note.space_id)?.kind === "team",
    hasCloudCopy: !!note.cloud_id,
  });
  const sharedNotePermission = (note: NoteItem) => {
    const entry = note.cloud_id ? shareByCloudId.get(note.cloud_id) : null;
    return resolveNotePermission({
      cachedPermission: entry?.access?.my_permission,
      aclState: entry ? "loaded" : "unavailable",
      isTeamNote: sharedNoteScope(note).isTeamNote,
    });
  };

  // Destructive/scope-changing note and folder actions mirror the server's
  // rules (spacePermissions + per-note ACLs). Menus, keyboard, drag/drop,
  // and undo all route through these so no path bypasses them; the server
  // enforces regardless.
  const canDeleteNote = (note: NoteItem): boolean => {
    if (sharedNoteBlocksDelete(sharedNotePermission(note), sharedNoteScope(note))) return false;
    const space = spacesById.get(note.space_id);
    return canDeleteSpaceNote(note, space, currentUserId, workspaceRoleFor(space));
  };
  const canChangeNoteScope = (note: NoteItem): boolean => {
    const space = spacesById.get(note.space_id);
    return canChangeSpaceNoteScope(note, space, currentUserId, workspaceRoleFor(space));
  };
  // Any local re-filing (same-space folder moves included): owner-only on
  // shared personal notes — a denied folder_id PATCH would fork an
  // unexpected Personal copy.
  const canMoveNote = (note: NoteItem): boolean =>
    canOrganizeSharedNote(sharedNotePermission(note), sharedNoteScope(note));
  const canManageFolderDestructive = (folder: FolderItem): boolean => {
    const space = spacesById.get(folder.space_id);
    return canMoveOrDeleteSpaceFolder(space, workspaceRoleFor(space));
  };

  // Cross-space targets require scope-change permission; same-space folder
  // targets stay available to every member.
  const noteMoveTargets = (note: NoteItem): SpaceItem[] => {
    const targets = moveTargetsBySpace.get(note.space_id) ?? [];
    return canChangeNoteScope(note) ? targets : targets.filter((s) => s.id === note.space_id);
  };

  const requestDeleteNote = (note: NoteItem): void => {
    if (!canDeleteNote(note)) return;
    onDeleteNote(note.id);
  };

  const openCreateSpace = (workspaceId: string | null = null): void => {
    setCreateSpaceWorkspaceId(workspaceId);
    setShowCreateSpace(true);
  };

  const targetLabel = (target: NoteMoveTarget): string => {
    if (target.folderId != null) {
      const folder = folders.find((f) => f.id === target.folderId);
      return folder ? defaultFolderDisplayName(folder, t) : "";
    }
    const space = spaces.find((s) => s.id === target.spaceId);
    return space ? spaceDisplayName(space, t) : "";
  };

  const showUndoToast = (title: string, target: string, onUndo: () => void): void => {
    if (undoToastIdRef.current) dismiss(undoToastIdRef.current);
    undoToastIdRef.current = toast({
      title: t("notes.spaces.moved", { title, target }),
      duration: 8000,
      action: (
        <Button
          variant="ghost"
          size="sm"
          onClick={() => {
            if (undoToastIdRef.current) dismiss(undoToastIdRef.current);
            undoToastIdRef.current = null;
            onUndo();
          }}
          className="h-6 px-2 text-xs text-white/70 hover:text-white hover:bg-white/10"
        >
          {t("notes.spaces.undo")}
        </Button>
      ),
    });
  };

  // Every permitted cross-space move enters a team space (private→team or
  // team→team): moving OUT of a team space is policy-blocked in
  // canMoveBetweenSpaces, and there is only one private space.
  const confirmCrossSpaceMove = (
    toSpaceId: number,
    isFolder: boolean,
    onConfirm: () => void
  ): void => {
    const intoSpace = spaces.find((s) => s.id === toSpaceId);
    if (!intoSpace) return;
    showConfirmDialog({
      title: t("notes.spaces.confirmMoveInTitle", { space: intoSpace.name }),
      description: t(isFolder ? "notes.spaces.confirmMoveInFolder" : "notes.spaces.confirmMoveIn", {
        space: intoSpace.name,
      }),
      confirmText: t("notes.spaces.moveConfirm"),
      onConfirm,
    });
  };

  const moveNoteSafely = async (noteId: number, target: NoteMoveTarget): Promise<boolean> => {
    try {
      await onMoveNote(noteId, target);
      return true;
    } catch (err: unknown) {
      toast({
        title: t("notes.spaces.couldNotMoveNote"),
        description: t(localMutationErrorKey(err instanceof Error ? err.message : undefined)),
        variant: "destructive",
      });
      return false;
    }
  };

  // The 8s undo window outlives the tree snapshot: the restore target may
  // have been deleted (folder) or purged (space) meanwhile. Never restore a
  // note into a container nothing renders — degrade to the space root.
  const undoMoveNote = async (
    noteId: number,
    title: string,
    prev: NoteMoveTarget
  ): Promise<void> => {
    const prevSpace = getSpacesValue().find((s) => s.id === prev.spaceId);
    if (!prevSpace) {
      toast({ title: t("notes.spaces.couldNotMoveNote"), variant: "destructive" });
      return;
    }
    // Undo restores across spaces outside the normal one-way policy, but it
    // must not bypass move/scope permissions after a role or ACL change.
    const current = getNoteFromStore(noteId);
    if (
      current &&
      (!canMoveNote(current) || (current.space_id !== prev.spaceId && !canChangeNoteScope(current)))
    ) {
      toast({ title: t("notes.spaces.couldNotMoveNote"), variant: "destructive" });
      return;
    }
    const folderGone =
      prev.folderId != null && !getFoldersValue().some((f) => f.id === prev.folderId);
    const restore = folderGone ? { spaceId: prev.spaceId, folderId: null } : prev;
    const moved = await moveNoteSafely(noteId, restore);
    if (moved && folderGone) {
      toast({ title: t("notes.spaces.moved", { title, target: spaceDisplayName(prevSpace, t) }) });
    }
  };

  const commitMoveNote = async (noteId: number, target: NoteMoveTarget): Promise<void> => {
    const note = getNoteFromStore(noteId);
    // Defense in depth behind the menu/drag gating: shared personal notes
    // are re-filed by their owner only.
    if (note && !canMoveNote(note)) return;
    const prev: NoteMoveTarget | null = note
      ? { spaceId: note.space_id, folderId: note.folder_id }
      : null;
    const moved = await moveNoteSafely(noteId, target);
    if (moved && prev) {
      const title = note?.title || t("notes.list.untitled");
      // Undo silently restores the previous space/folder — no confirm, no new toast.
      showUndoToast(title, targetLabel(target), () => void undoMoveNote(noteId, title, prev));
    }
  };

  const allowsCrossSpaceMove = (fromSpaceId: number, toSpaceId: number): boolean => {
    const from = spacesById.get(fromSpaceId);
    const to = spacesById.get(toSpaceId);
    return !!from && !!to && canMoveBetweenSpaces(from, to);
  };

  const requestMoveNote = (noteId: number, target: NoteMoveTarget): void => {
    const note = getNoteFromStore(noteId);
    if (!note || !canMoveNote(note)) return;
    if (note.space_id !== target.spaceId) {
      if (!allowsCrossSpaceMove(note.space_id, target.spaceId)) return;
      if (!canChangeNoteScope(note)) return;
      confirmCrossSpaceMove(target.spaceId, false, () => void commitMoveNote(noteId, target));
    } else {
      void commitMoveNote(noteId, target);
    }
  };

  const moveFolderSafely = async (
    folderId: number,
    spaceId: number
  ): Promise<{ success: boolean; error?: string }> => {
    const result = await moveFolderToSpace(folderId, spaceId).catch((err: unknown) => ({
      success: false,
      error: (err as Error).message,
    }));
    if (!result.success) {
      toast({
        title: t("notes.spaces.couldNotMove"),
        description: t(localMutationErrorKey(result.error)),
        variant: "destructive",
      });
    }
    return result;
  };

  const commitMoveFolder = async (folder: FolderItem, space: SpaceItem): Promise<void> => {
    const prevSpaceId = folder.space_id;
    const result = await moveFolderSafely(folder.id, space.id);
    if (!result.success) return;
    revealContainer(space.id, null);
    showUndoToast(folder.name, spaceDisplayName(space, t), () => {
      // Undo is a folder move like any other: after a role change it must
      // not bypass the destructive-folder-action rules in the new space.
      const current = getFoldersValue().find((f) => f.id === folder.id);
      if (!current || !canManageFolderDestructive(current)) {
        toast({ title: t("notes.spaces.couldNotMove"), variant: "destructive" });
        return;
      }
      void moveFolderSafely(folder.id, prevSpaceId);
    });
  };

  const requestMoveFolder = (folder: FolderItem, space: SpaceItem): void => {
    if (space.id === folder.space_id) return;
    if (!allowsCrossSpaceMove(folder.space_id, space.id)) return;
    if (!canManageFolderDestructive(folder)) return;
    confirmCrossSpaceMove(space.id, true, () => void commitMoveFolder(folder, space));
  };

  const { dragState, noteDragHandlers, dropTargetHandlers } = useNoteDragAndDrop({
    untitledLabel: t("notes.list.untitled"),
    onMoveToTarget: commitMoveNote,
    onCrossSpaceDrop: (_note, target, commit) => {
      confirmCrossSpaceMove(target.spaceId, false, commit);
    },
    canCrossSpaceDrop: (note, target) => {
      if (!allowsCrossSpaceMove(note.spaceId, target.spaceId)) return false;
      const stored = getNoteFromStore(note.id);
      return !!stored && canMoveNote(stored) && canChangeNoteScope(stored);
    },
    onCrossSpaceVeto: (note) => {
      const stored = getNoteFromStore(note.id);
      const scopeDenied = !!stored && !canChangeNoteScope(stored);
      toast({
        title: t(scopeDenied ? "notes.spaces.cantMoveTeammate" : "notes.spaces.cantMoveOut"),
        duration: 4000,
      });
    },
    onHoverTarget: (key) => setContainerExpanded(key, true),
  });

  const visibleRows = useMemo<TreeRow[]>(() => {
    const rows: TreeRow[] = [];
    const pushPrivateSpace = (space: SpaceItem) => {
      const spaceKey = spaceContainerKey(space.id);
      if (!expanded.has(spaceKey)) return;
      folders
        .filter((f) => f.space_id === space.id)
        .forEach((folder) => {
          const folderKey = folderContainerKey(folder.id);
          rows.push({ type: "folder", key: folderKey, folder, level: 1 });
          if (expanded.has(folderKey)) {
            const folderNotes = notesByContainer[folderKey] ?? [];
            const { notes: shownNotes, hiddenCount } = sidebarFolderNotes(
              folderNotes,
              activeNote,
              folder.id,
              folderCounts[folder.id] ?? folderNotes.length
            );
            shownNotes.forEach((note) => {
              rows.push({
                type: "note",
                key: `n:${note.id}`,
                note,
                parentKey: folderKey,
                level: 2,
              });
            });
            if (hiddenCount > 0) {
              rows.push({
                type: "more",
                key: `more:${folder.id}`,
                folder,
                parentKey: folderKey,
                level: 2,
              });
            }
          }
        });
      (notesByContainer[spaceKey] ?? []).forEach((note) => {
        rows.push({ type: "note", key: `n:${note.id}`, note, level: 1 });
      });
    };
    const pushSpace = (space: SpaceItem) => {
      const spaceKey = spaceContainerKey(space.id);
      rows.push({ type: "space", key: spaceKey, space });
      if (!expanded.has(spaceKey)) return;
      folders
        .filter((f) => f.space_id === space.id)
        .forEach((folder) => {
          const folderKey = folderContainerKey(folder.id);
          rows.push({ type: "folder", key: folderKey, folder, parentKey: spaceKey, level: 2 });
          if (expanded.has(folderKey)) {
            const folderNotes = notesByContainer[folderKey] ?? [];
            const { notes: shownNotes, hiddenCount } = sidebarFolderNotes(
              folderNotes,
              activeNote,
              folder.id,
              folderCounts[folder.id] ?? folderNotes.length
            );
            shownNotes.forEach((note) => {
              rows.push({
                type: "note",
                key: `n:${note.id}`,
                note,
                parentKey: folderKey,
                level: 3,
              });
            });
            if (hiddenCount > 0) {
              rows.push({
                type: "more",
                key: `more:${folder.id}`,
                folder,
                parentKey: folderKey,
                level: 3,
              });
            }
          }
        });
      (notesByContainer[spaceKey] ?? []).forEach((note) => {
        rows.push({ type: "note", key: `n:${note.id}`, note, parentKey: spaceKey, level: 2 });
      });
    };
    privateSpaces.forEach(pushPrivateSpace);
    if (teamCapability) orderedTeamSpaces.forEach(pushSpace);
    return rows;
  }, [
    privateSpaces,
    orderedTeamSpaces,
    folders,
    notesByContainer,
    expanded,
    teamCapability,
    activeNote,
    folderCounts,
  ]);

  const effectiveFocusKey =
    focusedKey && visibleRows.some((r) => r.key === focusedKey)
      ? focusedKey
      : (visibleRows[0]?.key ?? null);

  const focusRow = (key: string | undefined) => {
    if (!key) return;
    setFocusedKey(key);
    rowRefs.current.get(key)?.focus();
  };

  /** Restore focus to a row after an inline input closes (keyboard paths only). */
  const focusRowSoon = (key: string) => {
    setFocusedKey(key);
    requestAnimationFrame(() => rowRefs.current.get(key)?.focus());
  };

  const activateRow = (row: TreeRow) => {
    if (row.type === "more") {
      setOpenFolderId(row.folder.id);
    } else if (row.type === "space") {
      // Deselect the note so the main pane shows the container overview.
      setActiveNoteId(null);
      setActiveContext(row.space.id, null);
      toggleContainerExpanded(row.key);
    } else if (row.type === "folder") {
      setActiveNoteId(null);
      setActiveContext(row.folder.space_id, row.folder.id);
      toggleContainerExpanded(row.key);
    } else {
      setActiveNoteId(row.note.id);
    }
  };

  const startRenameFolder = (folder: FolderItem) => {
    setRenamingFolderId(folder.id);
    setRenameValue(folder.name);
  };

  const openSpaceSettings = (space: SpaceItem) => {
    setSettingsSpaceId(space.id);
    setSettingsOpen(true);
  };

  const requestDeleteFolder = (folder: FolderItem) => {
    if (!canManageFolderDestructive(folder)) return;
    const count = folderCounts[folder.id] ?? 0;
    showConfirmDialog({
      title: t("notes.folders.deleteTitle"),
      description:
        count > 0
          ? t("notes.folders.deleteDescription", { name: folder.name, count })
          : t("notes.folders.deleteDescriptionEmpty", { name: folder.name }),
      confirmText: t("notes.folders.deleteConfirm"),
      variant: "destructive",
      onConfirm: async () => {
        const result = await deleteFolder(folder.id);
        if (!result.success && result.error) {
          toast({
            title: t("notes.folders.couldNotDelete"),
            description: t(localMutationErrorKey(result.error)),
            variant: "destructive",
          });
        }
      },
    });
  };

  const requestDeleteSpace = (space: SpaceItem) => {
    setDeleteSpaceTarget(space);
  };

  const handleRowKeyDown = (e: React.KeyboardEvent<HTMLDivElement>, row: TreeRow) => {
    if (e.target !== e.currentTarget) return;
    const idx = visibleRows.findIndex((r) => r.key === row.key);
    if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === "n") {
      e.preventDefault();
      if (activeContext) onNewNote(activeContext.spaceId, activeContext.folderId);
      return;
    }
    const horizontalIntent = treeHorizontalIntent(e.key, i18n.dir());
    if (horizontalIntent === "inward") {
      e.preventDefault();
      if (row.type === "note" || row.type === "more") return;
      if (!expanded.has(row.key)) {
        setContainerExpanded(row.key, true);
      } else if (visibleRows[idx + 1]?.parentKey === row.key) {
        focusRow(visibleRows[idx + 1]?.key);
      }
      return;
    }
    if (horizontalIntent === "outward") {
      e.preventDefault();
      if (row.type !== "note" && row.type !== "more" && expanded.has(row.key)) {
        setContainerExpanded(row.key, false);
      } else if (row.parentKey) {
        focusRow(row.parentKey);
      }
      return;
    }
    switch (e.key) {
      case "ArrowDown":
        e.preventDefault();
        focusRow(visibleRows[idx + 1]?.key);
        break;
      case "ArrowUp":
        e.preventDefault();
        focusRow(visibleRows[idx - 1]?.key);
        break;
      case "F2":
        e.preventDefault();
        if (row.type === "folder" && !row.folder.is_default) {
          startRenameFolder(row.folder);
        } else if (
          row.type === "space" &&
          row.space.kind === "team" &&
          canManageTeamSpace(row.space)
        ) {
          openSpaceSettings(row.space);
        }
        break;
      case "Delete":
      case "Backspace":
        // Bare Backspace stays inert; Cmd/Ctrl+Backspace matches the native delete gesture.
        if (e.key === "Backspace" && !(e.metaKey || e.ctrlKey)) break;
        e.preventDefault();
        if (row.type === "note") {
          if (!canDeleteNote(row.note)) break;
          focusRow(visibleRows[idx + 1]?.key ?? visibleRows[idx - 1]?.key);
          requestDeleteNote(row.note);
        } else if (row.type === "folder" && !row.folder.is_default) {
          requestDeleteFolder(row.folder);
        } else if (
          row.type === "space" &&
          row.space.kind === "team" &&
          canManageTeamSpace(row.space)
        ) {
          requestDeleteSpace(row.space);
        }
        break;
      case "Enter":
      case " ":
        e.preventDefault();
        activateRow(row);
        break;
    }
  };

  const a11yFor = (key: string): RowA11yProps => ({
    tabIndex: key === effectiveFocusKey ? 0 : -1,
    rowRef: (el) => {
      if (el) rowRefs.current.set(key, el);
      else rowRefs.current.delete(key);
    },
    onKeyDown: (e) => {
      const row = visibleRows.find((r) => r.key === key);
      if (row) handleRowKeyDown(e, row);
    },
    onFocus: () => setFocusedKey(key),
  });

  const startCreateFolder = (space: SpaceItem) => {
    setContainerExpanded(spaceContainerKey(space.id), true);
    setCreatingFolderSpaceId(space.id);
    setNewFolderName("");
  };

  const confirmCreateFolder = async (): Promise<string | null> => {
    const spaceId = creatingFolderSpaceId;
    const trimmed = newFolderName.trim();
    setCreatingFolderSpaceId(null);
    setNewFolderName("");
    if (spaceId == null || !trimmed) return null;
    const result = await createFolder(trimmed, spaceId);
    if (result.success && result.folder) {
      setActiveContext(spaceId, result.folder.id);
      return folderContainerKey(result.folder.id);
    }
    if (result.error) {
      toast({
        title: t("notes.folders.couldNotCreate"),
        description: t(localMutationErrorKey(result.error)),
        variant: "destructive",
      });
    }
    return null;
  };

  const confirmRename = async () => {
    const folderId = renamingFolderId;
    const trimmed = renameValue.trim();
    setRenamingFolderId(null);
    setRenameValue("");
    if (folderId == null || !trimmed) return;
    const result = await renameFolder(folderId, trimmed);
    if (!result.success && result.error) {
      toast({
        title: t("notes.folders.couldNotRename"),
        description: t(localMutationErrorKey(result.error)),
        variant: "destructive",
      });
    }
  };

  const renderNote = (
    note: NoteItem,
    level: 1 | 2 | 3,
    parentKey?: string,
    indentClassName?: string
  ) => (
    <NoteLeaf
      key={note.id}
      note={note}
      level={level}
      indentClassName={indentClassName}
      isActive={note.id === activeNoteId}
      isDragging={dragState.draggingNoteId === note.id}
      dragHandlers={noteDragHandlers({
        id: note.id,
        title: note.title,
        folderId: note.folder_id,
        spaceId: note.space_id,
      })}
      spaces={noteMoveTargets(note)}
      folders={folders}
      noteFilesEnabled={noteFilesEnabled}
      fileManagerName={fileManagerName}
      canDelete={canDeleteNote(note)}
      canMove={canMoveNote(note)}
      onOpen={() => activateRow({ type: "note", key: `n:${note.id}`, note, parentKey, level })}
      onMove={(target) => requestMoveNote(note.id, target)}
      onCreateFolderAndMove={onCreateFolderAndMove}
      onDelete={() => requestDeleteNote(note)}
      a11y={a11yFor(`n:${note.id}`)}
      t={t}
    />
  );

  const renderFolder = (folder: FolderItem, parentKey?: string, level: 1 | 2 = 2) => {
    const folderKey = folderContainerKey(folder.id);
    const isExpanded = expanded.has(folderKey);
    const isRenaming = renamingFolderId === folder.id;
    const folderNotes = notesByContainer[folderKey] ?? [];
    const total = folderCounts[folder.id] ?? folderNotes.length;
    const { notes: shownNotes, hiddenCount } = sidebarFolderNotes(
      folderNotes,
      activeNote,
      folder.id,
      total
    );

    if (isRenaming) {
      return (
        <div key={folder.id} role="none" className={cn(level === 1 ? "ps-2" : "ps-[14px]", "pe-2")}>
          <input
            dir="auto"
            autoFocus
            value={renameValue}
            onChange={(e) => setRenameValue(e.target.value)}
            onFocus={(e) => e.currentTarget.select()}
            onKeyDown={(e) => {
              if (e.key === "Enter") {
                confirmRename();
                focusRowSoon(folderKey);
              }
              if (e.key === "Escape") {
                setRenamingFolderId(null);
                setRenameValue("");
                focusRowSoon(folderKey);
              }
            }}
            onBlur={confirmRename}
            className={FOLDER_INPUT_CLASS}
          />
        </div>
      );
    }

    return (
      <div key={folder.id} role="none">
        <FolderRow
          folder={folder}
          level={level}
          spaces={moveTargetsBySpace.get(folder.space_id) ?? []}
          isExpanded={isExpanded}
          isActive={activeNoteId == null && activeContext?.folderId === folder.id}
          count={folderCounts[folder.id] ?? 0}
          isDragOver={dragState.dragOverKey === folderKey}
          isDropSuccess={dragState.dropSuccessKey === folderKey}
          dropHandlers={dropTargetHandlers({
            spaceId: folder.space_id,
            folderId: folder.id,
            folderName: folder.name,
            isDefaultFolder: Boolean(folder.is_default),
          })}
          noteFilesEnabled={noteFilesEnabled}
          fileManagerName={fileManagerName}
          canManageDestructive={canManageFolderDestructive(folder)}
          onActivate={() =>
            activateRow({ type: "folder", key: folderKey, folder, parentKey, level })
          }
          onToggle={() => toggleContainerExpanded(folderKey)}
          onNewNote={() => onNewNote(folder.space_id, folder.id)}
          onRename={() => startRenameFolder(folder)}
          onMoveToSpace={(space) => requestMoveFolder(folder, space)}
          onDelete={() => requestDeleteFolder(folder)}
          a11y={a11yFor(folderKey)}
          t={t}
        />
        <TreeChildren open={isExpanded}>
          <div className="space-y-px">
            {shownNotes.map((note) =>
              level === 1 ? renderNote(note, 2, folderKey, "ps-8") : renderNote(note, 3, folderKey)
            )}
            {hiddenCount > 0 && (
              <FolderNotesPopover
                folder={folder}
                total={total}
                hiddenCount={hiddenCount}
                activeNoteId={activeNoteId}
                level={level === 1 ? 2 : 3}
                open={openFolderId === folder.id}
                onOpenChange={(open) => setOpenFolderId(open ? folder.id : null)}
                a11y={a11yFor(`more:${folder.id}`)}
                onPick={(note, keyboard) => {
                  setActiveNoteId(note.id);
                  if (keyboard) focusRowSoon(`n:${note.id}`);
                  else setFocusedKey(`n:${note.id}`);
                }}
                onOpenFolder={() => {
                  setActiveNoteId(null);
                  setActiveContext(folder.space_id, folder.id);
                }}
              />
            )}
          </div>
        </TreeChildren>
      </div>
    );
  };

  const renderSpaceContents = (space: SpaceItem, flattened = false) => {
    const spaceKey = spaceContainerKey(space.id);
    const spaceFolders = folders.filter((f) => f.space_id === space.id);
    const rootNotes = notesByContainer[spaceKey];
    const showSkeletons =
      space.kind === "team" &&
      space.sync_status === "pending" &&
      spaceFolders.length === 0 &&
      rootNotes === undefined;
    const showEmptySpace =
      space.kind === "team" && spaceFolders.length === 0 && rootNotes?.length === 0;

    return (
      <div className="space-y-px">
        {spaceFolders.map((folder) =>
          flattened ? renderFolder(folder, undefined, 1) : renderFolder(folder, spaceKey)
        )}
        {creatingFolderSpaceId === space.id && (
          <div className={cn(flattened ? "ps-2" : "ps-[14px]", "pe-2")}>
            <input
              dir="auto"
              autoFocus
              value={newFolderName}
              onChange={(e) => setNewFolderName(e.target.value)}
              onKeyDown={(e) => {
                if (e.key === "Enter") {
                  void confirmCreateFolder().then((key) => key && focusRowSoon(key));
                }
                if (e.key === "Escape") {
                  setCreatingFolderSpaceId(null);
                  setNewFolderName("");
                  if (flattened) privateSectionToggleRef.current?.focus();
                  else focusRowSoon(spaceKey);
                }
              }}
              onBlur={confirmCreateFolder}
              placeholder={t("notes.folders.folderName")}
              className={cn(FOLDER_INPUT_CLASS, "placeholder:text-foreground/45")}
            />
          </div>
        )}
        {(rootNotes ?? []).map((note) =>
          flattened ? renderNote(note, 1, undefined, "ps-[30px]") : renderNote(note, 2, spaceKey)
        )}
        {showSkeletons && <SkeletonRows />}
        {showEmptySpace && (
          <div className="ps-[18px] pe-2 py-1">
            <p className="text-xs text-foreground/60 leading-relaxed mb-1.5">
              {t("notes.spaces.emptySpace", { space: space.name })}
            </p>
            <Button
              variant="ghost"
              size="sm"
              onClick={() => onNewNote(space.id, null)}
              className="h-6 px-2 text-xs gap-1 text-primary/70 hover:text-primary hover:bg-primary/8"
            >
              <Plus size={11} />
              {t("notes.list.newNote")}
            </Button>
          </div>
        )}
      </div>
    );
  };

  const renderSpace = (space: SpaceItem) => {
    const spaceKey = spaceContainerKey(space.id);
    const isExpanded = expanded.has(spaceKey);
    const spaceFolders = folders.filter((f) => f.space_id === space.id);
    const displayName = spaceDisplayName(space, t);
    // DB-backed counts: space-root notes count before their container loads,
    // and the root contribution isn't capped at the container's page size.
    const noteCount =
      spaceFolders.reduce((sum, f) => sum + (folderCounts[f.id] ?? 0), 0) +
      (spaceRootCounts[space.id] ?? 0);
    return (
      <div key={space.id} role="none">
        <SpaceRow
          space={space}
          displayName={displayName}
          isExpanded={isExpanded}
          isActive={
            activeNoteId == null &&
            activeContext?.spaceId === space.id &&
            activeContext.folderId == null
          }
          count={noteCount}
          isDragOver={dragState.dragOverKey === spaceKey}
          isDropSuccess={dragState.dropSuccessKey === spaceKey}
          dropHandlers={dropTargetHandlers({ spaceId: space.id, folderId: null })}
          onActivate={() => activateRow({ type: "space", key: spaceKey, space })}
          onToggle={() => toggleContainerExpanded(spaceKey)}
          onNewFolder={() => startCreateFolder(space)}
          onSettings={() => openSpaceSettings(space)}
          a11y={a11yFor(spaceKey)}
          t={t}
        />
        <TreeChildren open={isExpanded}>{renderSpaceContents(space)}</TreeChildren>
      </div>
    );
  };

  const settingsSpace =
    settingsSpaceId != null ? spaces.find((s) => s.id === settingsSpaceId) : undefined;

  if (isTreeLoading && spaces.length === 0) {
    return (
      <div className="flex-1 flex items-start justify-center py-8">
        <Loader2 size={12} className="animate-spin text-foreground/45" />
      </div>
    );
  }

  return (
    <>
      <div
        role="tree"
        aria-label={t("notes.list.title")}
        className="scrollbar-hidden flex-1 overflow-y-auto px-1.5 pb-2 space-y-px"
      >
        <div role="none" className="group/section">
          <SectionHeader
            label={t("notes.spaces.privateSpaces")}
            icon={<Lock size={12} className="text-foreground/55" />}
            expanded={privateSectionExpanded}
            onToggle={() => {
              if (privateSpace) toggleContainerExpanded(spaceContainerKey(privateSpace.id));
            }}
            toggleRef={privateSectionToggleRef}
            dropHandlers={
              privateSpace
                ? dropTargetHandlers({ spaceId: privateSpace.id, folderId: null })
                : undefined
            }
            isDragOver={
              privateSpace && dragState.dragOverKey === spaceContainerKey(privateSpace.id)
            }
            isDropSuccess={
              privateSpace && dragState.dropSuccessKey === spaceContainerKey(privateSpace.id)
            }
            action={
              privateSpace ? (
                <Button
                  variant="ghost"
                  size="icon"
                  aria-label={t("notes.context.newFolder")}
                  onClick={() => startCreateFolder(privateSpace)}
                  className={cn(HOVER_REVEAL_BUTTON_CLASS, "group-hover/section:opacity-100")}
                >
                  <Plus size={12} />
                </Button>
              ) : undefined
            }
          />
          {privateSpace && (
            <TreeChildren open={privateSectionExpanded} grouped={false}>
              {/* The private space remains the storage/sync boundary but is flattened in the UI. */}
              {renderSpaceContents(privateSpace, true)}
            </TreeChildren>
          )}
        </div>
        {teamCapability && (
          <div role="none" className="group/section">
            <SectionHeader
              label={
                showWorkspaceGroups
                  ? t("workspaces.switcher.workspaces")
                  : t("notes.spaces.teamSpaces")
              }
              className="mt-4"
              action={
                <div className="flex items-center gap-px">
                  {onShowStructureIntro && (
                    <Button
                      variant="ghost"
                      size="icon"
                      aria-label={t("notes.structureIntro.reopen")}
                      onClick={onShowStructureIntro}
                      className={cn(HOVER_REVEAL_BUTTON_CLASS, "group-hover/section:opacity-100")}
                    >
                      <Info size={11} />
                    </Button>
                  )}
                  {/* With one workspace the header + is unambiguous; with several,
                      each workspace row carries its own + instead. */}
                  {canCreateTeamSpace && !showWorkspaceGroups && (
                    <Button
                      variant="ghost"
                      size="icon"
                      aria-label={t("notes.spaces.newSpace")}
                      onClick={() => openCreateSpace()}
                      className={cn(HOVER_REVEAL_BUTTON_CLASS, "group-hover/section:opacity-100")}
                    >
                      <Plus size={12} />
                    </Button>
                  )}
                </div>
              }
            />
            {workspacesPending ? (
              <SkeletonRows />
            ) : (
              <>
                {showWorkspaceGroups
                  ? teamSpaceGroups.map(({ workspace, spaces: workspaceSpaces }) => (
                      <div
                        key={workspace.id}
                        role="group"
                        aria-label={workspace.name}
                        className="mt-1 group/workspace"
                      >
                        <div
                          role="none"
                          className="flex items-center justify-between h-5 ps-4 pe-2"
                        >
                          <span
                            dir="auto"
                            title={workspace.name}
                            className="min-w-0 text-[10px] font-medium text-foreground/60 truncate"
                          >
                            {workspace.name}
                          </span>
                          {canManageWorkspace(workspace.role) && (
                            <Button
                              variant="ghost"
                              size="icon"
                              aria-label={t("notes.spaces.newSpaceInWorkspace", {
                                workspace: workspace.name,
                              })}
                              onClick={() => openCreateSpace(workspace.id)}
                              className={cn(
                                HOVER_REVEAL_BUTTON_CLASS,
                                "group-hover/workspace:opacity-100"
                              )}
                            >
                              <Plus size={12} />
                            </Button>
                          )}
                        </div>
                        {workspaceSpaces.map(renderSpace)}
                      </div>
                    ))
                  : teamSpaces.map(renderSpace)}
                {showWorkspaceGroups && ungroupedTeamSpaces.map(renderSpace)}
                {teamSpaces.length === 0 && (
                  <div role="none" className="flex flex-col items-center px-3 py-6 text-center">
                    <ThemedEmptyIllustration
                      light={sharedSpaceEmptyLight}
                      dark={sharedSpaceEmptyDark}
                      width={134}
                      height={139}
                    />
                    <p className="mt-1 text-sm font-semibold text-foreground">
                      {t("notes.spaces.emptyTeamTitle")}
                    </p>
                    <p className="mt-2 text-xs text-muted-foreground">
                      {t("notes.spaces.emptyTeamHint")}
                    </p>
                    {canCreateTeamSpace && !showWorkspaceGroups && (
                      <Button
                        variant="ghost"
                        size="sm"
                        onClick={() => openCreateSpace()}
                        className="mt-3 h-7 gap-1 text-xs text-primary"
                      >
                        <Plus size={11} />
                        {t("notes.spaces.newSpace")}
                      </Button>
                    )}
                  </div>
                )}
              </>
            )}
          </div>
        )}
      </div>

      <CreateSpaceDialog
        open={showCreateSpace}
        onOpenChange={(open) => {
          setShowCreateSpace(open);
          if (!open) setCreateSpaceWorkspaceId(null);
        }}
        initialWorkspaceId={createSpaceWorkspaceId}
      />

      {settingsSpace && (
        <SpaceSettingsDialog
          space={settingsSpace}
          open={settingsOpen}
          onOpenChange={setSettingsOpen}
          onCloseAutoFocus={(event) => {
            // No trigger element to return to: the row the settings belong to is the natural target.
            event.preventDefault();
            focusRow(spaceContainerKey(settingsSpace.id));
          }}
        />
      )}

      <DeleteSpaceDialog space={deleteSpaceTarget} onClose={() => setDeleteSpaceTarget(null)} />

      <ConfirmDialog
        open={confirmDialog.open}
        onOpenChange={(open) => !open && hideConfirmDialog()}
        title={confirmDialog.title}
        description={confirmDialog.description}
        confirmText={confirmDialog.confirmText}
        cancelText={confirmDialog.cancelText}
        onConfirm={confirmDialog.onConfirm}
        variant={confirmDialog.variant}
      />
    </>
  );
}