Skip to content

Open source CapCut alternative.

TypeScript
30.0 KB
Raw Blame
"use client";

import { Fragment, useCallback, useEffect, useMemo, useRef, useState } from "react";
import { Folder, MoreHorizontal, Pencil, Share2, Trash2 } from "lucide-react";
import { useLocale, useTranslations } from "next-intl";
import { Button } from "@/components/ui/button";
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuSeparator,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { Input } from "@/components/ui/input";
import { RenameInput } from "@/cut/components/RenameInput";
import { setObjectDragImage } from "@/cut/lib/assetDrag";
import { PICKED_RING } from "@/cut/lib/assetPick";
import { additiveClick } from "@/cut/lib/hostKeys";
import { clearRefDrag, setRefDragData, type AssetRef } from "@/cut/lib/assetRef";
import { reportSwallowed } from "@/cut/lib/report";
import { useDescribeError } from "@/i18n/describeError";
import { formatBytes } from "@/lib/bytes";
import { RefDropZone } from "./RefDropZone";
import { cn } from "@/lib/utils";

// A folder under a drag that would land in it, in the selection blue.
const DROP_OVER = "bg-[#0a84ff]/15 dark:bg-[#0a84ff]/25";

// A desktop-style folder surface shared by the projects home and the library:
// macOS folder tiles, marquee multi-select, drag a selection onto a folder with
// a ghost, and open-to-navigate. Both pages carry a selection as a JSON array of
// ids under their own MIME type, so one drag can move a whole collection.

export interface DeskFolder {
  id: string;
  name: string;
  /** A folder the host derives from the items themselves (the editor's Camera
   * Roll): it opens like any other but can't be renamed, deleted, or dropped
   * into. */
  locked?: boolean;
}

// A folder sits in the grid's pick beside the items, under an id of its own
// shape, so a folder and an item never collide in the one Set.
const FOLDER_SEL = "folder:";
// How long a settled create waits for its folder to reach the shelf before
// the name it held lets go: a host that patches its cache on a timer lands
// within it, one that bailed out never does.
const LANDING_GRACE_MS = 1000;
export const folderSelId = (id: string) => FOLDER_SEL + id;

/** A pick taken apart: the folder ids and the item ids it holds. */
export function splitPick(ids: Iterable<string>): { folders: string[]; items: string[] } {
  const folders: string[] = [];
  const items: string[] = [];
  for (const id of ids)
    if (id.startsWith(FOLDER_SEL)) folders.push(id.slice(FOLDER_SEL.length));
    else items.push(id);
  return { folders, items };
}

export function readDragIds(e: React.DragEvent, mime: string): string[] {
  const raw = e.dataTransfer.getData(mime);
  if (!raw) return [];
  try {
    const v = JSON.parse(raw);
    return Array.isArray(v) ? (v as string[]) : [];
  } catch {
    return raw ? [raw] : [];
  }
}

/** Folder tile glyph: the Lucide folder, filled blue. */
export function FolderGlyph({ className, ...rest }: { className?: string } & Record<string, unknown>) {
  return (
    <Folder className={cn("fill-[#8cc5ff] text-[#8cc5ff]", className)} aria-hidden="true" {...rest} />
  );
}

/**
 * Click selection over a grid of tiles, the desktop's rules: a plain click
 * picks one, ⌘/Ctrl toggles one in or out, ⇧ takes the run from the last pick
 * to this one. Pairs with `Marquee`, which sweeps the same set.
 */
export function useTilePicks(shown?: readonly string[]) {
  const [raw, setPicked] = useState<Set<string>>(new Set());
  const anchor = useRef<string | null>(null);
  // A pick reaches no further than the eye does. Navigating into a folder or
  // deleting a tile leaves ids in the set that are no longer on screen, and a
  // drag that carried those would file or lay down items nobody can see.
  const picked = useMemo(() => {
    if (!shown) return raw;
    const live = new Set(shown);
    return new Set([...raw].filter((id) => live.has(id)));
  }, [raw, shown]);
  const pick = useCallback((e: React.MouseEvent, id: string, order: string[]) => {
    if (e.metaKey || e.ctrlKey) {
      setPicked((prev) => {
        const next = new Set(prev);
        if (next.has(id)) next.delete(id);
        else next.add(id);
        return next;
      });
      anchor.current = id;
      return;
    }
    if (e.shiftKey && anchor.current) {
      const from = order.indexOf(anchor.current);
      const to = order.indexOf(id);
      if (from >= 0 && to >= 0) {
        const [lo, hi] = from < to ? [from, to] : [to, from];
        setPicked(new Set(order.slice(lo, hi + 1)));
        return;
      }
    }
    setPicked(new Set([id]));
    anchor.current = id;
  }, []);
  return { picked, setPicked, pick };
}

// Elements a press should not turn into a rubber-band: the cards themselves
// (they drag), folder tiles / breadcrumbs, anything else draggable sharing the
// scroll region (template and export rows), the scrollbar, and any
// interactive control.
const MARQUEE_SKIP =
  "[data-sel-id],[data-no-marquee],[draggable='true'],button,a,input,textarea,select," +
  "[role='button'],[role='menuitem'],[contenteditable='true']," +
  "[data-slot='scroll-area-scrollbar'],[data-slot='scroll-area-thumb']";

/** Rubber-band selection like a desktop: press-drag on empty space to sweep a
 * rectangle, and every tile (marked `data-sel-id`) it touches is selected. Armed
 * off the whole `<main>` arena — so it starts anywhere in the content area, not
 * just over the grid — while the left sidebar is left alone. The sweep reaches
 * every tile in the arena, so the folder shelf above the grid is swept with it.
 * ⇧/⌘ keeps the prior selection; a plain click on empty space clears it. */
export function Marquee({
  className,
  rootClassName = "relative min-h-[68vh] flex-1",
  scope = "page",
  selected,
  setSelected,
  children,
}: {
  className?: string;
  /** The band's own box. A page grid fills the arena; a panel grid takes the
   * column it is in. */
  rootClassName?: string;
  /** Where a press starts a sweep. "page" arms the whole content arena, so a
   * press anywhere beside the grid begins one; "self" arms the scroll region
   * this box sits in, which is what a side panel wants — the empty space
   * under the grid sweeps, while the preview and the timeline are left
   * alone. */
  scope?: "page" | "self";
  selected: Set<string>;
  setSelected: (s: Set<string>) => void;
  children: React.ReactNode;
}) {
  const ref = useRef<HTMLDivElement>(null);
  const selectedRef = useRef(selected);
  useEffect(() => {
    selectedRef.current = selected;
  });
  const [rect, setRect] = useState<{ left: number; top: number; width: number; height: number } | null>(
    null
  );

  useEffect(() => {
    const arena =
      scope === "self"
        ? (ref.current?.closest<HTMLElement>("[data-slot='scroll-area-viewport']") ?? ref.current)
        : (ref.current?.closest("main") ?? ref.current?.parentElement);
    if (!arena) return;
    const onPointerDown = (e: PointerEvent) => {
      if (e.button !== 0 || (e.target as HTMLElement).closest(MARQUEE_SKIP)) return;
      const startX = e.clientX;
      const startY = e.clientY;
      const additive = additiveClick(e);
      const base = new Set(additive ? selectedRef.current : []);
      let moved = false;

      const onMove = (ev: PointerEvent) => {
        if (!moved && Math.hypot(ev.clientX - startX, ev.clientY - startY) < 4) return;
        moved = true;
        const r = {
          left: Math.min(startX, ev.clientX),
          top: Math.min(startY, ev.clientY),
          width: Math.abs(ev.clientX - startX),
          height: Math.abs(ev.clientY - startY),
        };
        setRect(r);
        const hit = new Set(base);
        arena.querySelectorAll<HTMLElement>("[data-sel-id]").forEach((el) => {
          const b = el.getBoundingClientRect();
          const overlaps =
            b.left < r.left + r.width && b.right > r.left && b.top < r.top + r.height && b.bottom > r.top;
          if (overlaps) hit.add(el.dataset.selId!);
        });
        setSelected(hit);
      };
      const done = () => {
        window.removeEventListener("pointermove", onMove);
        window.removeEventListener("pointerup", onUp);
        window.removeEventListener("dragstart", onDrag, true);
        setRect(null);
      };
      const onUp = () => {
        done();
        if (!moved && !additive) setSelected(new Set());
      };
      // A press that turns into a native drag gets no pointerup at all, so the
      // sweep stands down when the drag takes over.
      const onDrag = () => done();
      window.addEventListener("pointermove", onMove);
      window.addEventListener("pointerup", onUp);
      window.addEventListener("dragstart", onDrag, true);
    };
    arena.addEventListener("pointerdown", onPointerDown);
    // A plain press anywhere beyond the arena lets the selection go too — the
    // desktop's rule: only a card, a control, or an open menu or dialog (which
    // acts on the pick) holds it.
    const onDocDown = (e: PointerEvent) => {
      if (e.button !== 0 || e.shiftKey || e.metaKey || e.ctrlKey) return;
      const t = e.target as HTMLElement;
      if (
        t.closest(MARQUEE_SKIP) ||
        t.closest("[role='menu'],[role='dialog'],[role='alertdialog']")
      )
        return;
      if (selectedRef.current.size) setSelected(new Set());
    };
    document.addEventListener("pointerdown", onDocDown);
    return () => {
      arena.removeEventListener("pointerdown", onPointerDown);
      document.removeEventListener("pointerdown", onDocDown);
    };
  }, [setSelected, scope]);

  return (
    <div ref={ref} className={rootClassName}>
      <div className={className}>{children}</div>
      {rect && (
        <div
          className="pointer-events-none fixed z-50 rounded-[3px] border-2 border-[#0a84ff]"
          style={{ left: rect.left, top: rect.top, width: rect.width, height: rect.height }}
        />
      )}
    </div>
  );
}

/** One step of the breadcrumb that can be gone back to: a button, and a drop
 * target for an item selection under `mime` and, when the host nests folders,
 * for folders under `folderMime`. */
function CrumbStep({
  label,
  mime,
  folderMime,
  onGo,
  onDrop,
  onDropFolders,
}: {
  label: string;
  mime: string;
  folderMime?: string;
  onGo: () => void;
  onDrop: (ids: string[]) => void;
  onDropFolders?: (ids: string[]) => void;
}) {
  const [over, setOver] = useState(false);
  // A pick that holds both drags both payloads, and the step takes each.
  const carried = (e: React.DragEvent) => {
    const types = Array.from(e.dataTransfer.types);
    return {
      items: types.includes(mime),
      folders: !!folderMime && !!onDropFolders && types.includes(folderMime),
    };
  };
  return (
    <button
      className={cn(
        "rounded-md px-1.5 py-0.5 text-muted-foreground transition-colors hover:text-foreground",
        over && "bg-primary/15 text-primary"
      )}
      onClick={onGo}
      onDragOver={(e) => {
        const kind = carried(e);
        if (!kind.items && !kind.folders) return;
        e.preventDefault();
        e.dataTransfer.dropEffect = "move";
        setOver(true);
      }}
      onDragLeave={() => setOver(false)}
      onDrop={(e) => {
        const kind = carried(e);
        if (!kind.items && !kind.folders) return;
        e.preventDefault();
        setOver(false);
        if (kind.folders) onDropFolders?.(readDragIds(e, folderMime!));
        if (kind.items) onDrop(readDragIds(e, mime));
      }}
    >
      {label}
    </button>
  );
}

function FolderTitle({ name, onRename }: { name: string; onRename: (name: string) => void | Promise<void> }) {
  const t = useTranslations("home.folders");
  const [editing, setEditing] = useState(false);
  const [error, setError] = useState(false);
  return (
    <span className="min-w-0">
      {editing ? (
        <RenameInput
          value={name}
          className="h-7 w-52 max-w-full px-2 text-sm"
          onDone={(next) => {
            setEditing(false);
            if (next) void Promise.resolve().then(() => onRename(next)).catch(() => setError(true));
          }}
        />
      ) : (
        <button type="button" title={t("renameFolder")} className="max-w-full cursor-text truncate rounded-md px-1 hover:bg-muted" onClick={() => { setError(false); setEditing(true); }}>{name}</button>
      )}
      {error && <span role="alert" className="ml-2 text-xs font-normal text-destructive">{t("renameFailed")}</span>}
    </span>
  );
}

/** The breadcrumb shown while a folder is open: the root, then every folder
 * on the way down to the open one. Each step above the open folder is a
 * button and a drop target, so a selection can be dragged back out to any
 * level. `id` is null for the root. */
export function FolderCrumb({
  root,
  trail,
  mime,
  folderMime,
  onGo,
  onRename,
  onDrop,
  onDropFolders,
  className,
}: {
  root: string;
  /** The folders from the top level down to the open one, the open one last. */
  trail: { id: string; name: string }[];
  mime: string;
  /** Set when folders themselves are dragged; steps then take those drops too. */
  folderMime?: string;
  onGo: (id: string | null) => void;
  onRename?: (id: string, name: string) => void | Promise<void>;
  onDrop: (ids: string[], id: string | null) => void;
  onDropFolders?: (ids: string[], id: string | null) => void;
  className?: string;
}) {
  const open = trail[trail.length - 1];
  return (
    <div
      className={cn("flex min-w-0 items-center gap-2 text-lg font-semibold tracking-tight", className)}
      data-no-marquee
    >
      <CrumbStep
        label={root}
        mime={mime}
        folderMime={folderMime}
        onGo={() => onGo(null)}
        onDrop={(ids) => onDrop(ids, null)}
        onDropFolders={onDropFolders && ((ids) => onDropFolders(ids, null))}
      />
      {trail.slice(0, -1).map((f) => (
        <Fragment key={f.id}>
          <span className="text-muted-foreground/50">/</span>
          <CrumbStep
            label={f.name}
            mime={mime}
            folderMime={folderMime}
            onGo={() => onGo(f.id)}
            onDrop={(ids) => onDrop(ids, f.id)}
            onDropFolders={onDropFolders && ((ids) => onDropFolders(ids, f.id))}
          />
        </Fragment>
      ))}
      {open && (
        <>
          <span className="text-muted-foreground/50">/</span>
          {onRename ? <FolderTitle key={open.id} name={open.name} onRename={(name) => onRename(open.id, name)} /> : <span className="truncate">{open.name}</span>}
        </>
      )}
    </div>
  );
}

/** The actions on a folder, shared by its ⋯ button and the grid's right-click
 * menu. `deleteLabel` says what Delete takes: the folder, or the pick it is
 * in. */
export function FolderMenuItems({
  onShare,
  onRename,
  onDelete,
  deleteLabel,
}: {
  onShare?: () => void;
  onRename: () => void;
  onDelete: () => void;
  deleteLabel?: string;
}) {
  const t = useTranslations("home.folders");
  return (
    <>
      {onShare && <DropdownMenuItem onClick={onShare}><Share2 /> {t("share")}</DropdownMenuItem>}
      <DropdownMenuItem onClick={onRename}>
        <Pencil /> {t("rename")}
      </DropdownMenuItem>
      <DropdownMenuSeparator />
      <DropdownMenuItem variant="destructive" onClick={onDelete}>
        <Trash2 /> {deleteLabel ?? t("deleteFolder")}
      </DropdownMenuItem>
    </>
  );
}

/** The desktop-style folder shelf: each folder as a blue folder icon and a
 * drop target for dragged items. Folder creation is driven by the host (e.g.
 * a header button) through `creating`/`onCreatingChange`. A host that nests
 * folders passes `folderMime`; tiles are then draggable, carrying their own
 * id under it, and every other tile takes that drop. */
export function FolderShelf<F extends DeskFolder>({
  folders,
  statOf,
  badgeOf,
  referenceOf,
  mime,
  folderMime,
  picked,
  onPick,
  renaming,
  onRenamingChange,
  onOpen,
  onCreate,
  onRename,
  onDelete,
  onShare,
  onDropIds,
  onDropFolders,
  onDropFiles,
  onRefDrop,
  creating = false,
  onCreatingChange,
  rows = false,
}: {
  folders: F[];
  statOf: (id: string) => { count: number; size?: number };
  /** Small marker beside the item count — where the library says which shelf
   * a folder is on. */
  badgeOf?: (id: string) => React.ReactNode;
  referenceOf?: (folder: F) => AssetRef;
  mime: string;
  /** The MIME a dragged folder tile carries its id under. */
  folderMime?: string;
  /** The grid's pick, when folder tiles join it: a tile is marked for the
   * marquee and the selection menu under `folderSelId`, wears the ring while
   * picked, and hands a ⌘/⇧ click to `onPick`; a plain click opens it. A
   * picked tile drags the whole pick — the folders under `folderMime`, the
   * items under `mime`. A locked folder stays out of the pick. */
  picked?: Set<string>;
  onPick?: (e: React.MouseEvent, id: string) => void;
  /** The folder whose name is being edited, when the host drives that (its
   * right-click menu offers Rename); unset, the tile keeps it itself. */
  renaming?: string | null;
  onRenamingChange?: (id: string | null) => void;
  onOpen: (id: string) => void;
  onCreate?: (name: string) => void | Promise<void>;
  onRename: (id: string, name: string) => void | Promise<void>;
  /** Delete from the tile's ⋯ menu. The host decides what a picked folder's
   * Delete takes: the tile labels it "Delete" when the folder is in a pick
   * with others. */
  onDelete: (id: string) => void | Promise<void>;
  onShare?: (id: string) => void;
  onDropIds: (ids: string[], folderId: string) => void;
  /** Folders dropped onto a folder tile — filed inside it. */
  onDropFolders?: (ids: string[], folderId: string) => void;
  /** Desktop files dropped onto a folder tile — dropped straight into it. */
  onDropFiles?: (files: FileList, folderId: string) => void;
  /** When set, folder tiles also take media drops from cards and timeline
   * clips — filter by `ref.scope` and file the media into the folder. */
  onRefDrop?: (ref: AssetRef, folderId: string) => void;
  creating?: boolean;
  onCreatingChange?: (creating: boolean) => void;
  /** Stacked full-width rows — glyph left, then name over an item-count
   * subtext — for narrow panels; default is the desktop tile grid. */
  rows?: boolean;
}) {
  // A folder tile accepts an internal selection (its MIME), another folder
  // tile when the host nests folders, and, when the host wires it up, OS
  // files dragged from the desktop.
  const t = useTranslations("home.folders");
  const locale = useLocale();
  const describeError = useDescribeError();
  const dragTypes = (e: React.DragEvent) => Array.from(e.dataTransfer.types);
  const nests = !!folderMime && !!onDropFolders;
  // The tile being dragged, so it does not light up as its own target. The
  // payload is drop-only, which is why the id is held here.
  const [dragging, setDragging] = useState<string | null>(null);
  const accepts = (e: React.DragEvent, target: string) =>
    dragTypes(e).includes(mime) ||
    (nests && dragTypes(e).includes(folderMime) && dragging !== target) ||
    (!!onDropFiles && dragTypes(e).includes("Files"));
  const [ownEditing, setOwnEditing] = useState<string | null>(null);
  const editingId = renaming ?? ownEditing;
  const setEditingId = onRenamingChange ?? setOwnEditing;
  const [draft, setDraft] = useState("");
  const [over, setOver] = useState<string | null>(null);
  // Every close path clears the draft, so the next create opens with an empty
  // name field.
  const closeCreate = () => {
    setDraft("");
    onCreatingChange?.(false);
  };
  const closeRename = () => setEditingId(null);

  // A name sent to the host stays on the shelf as the folder it becomes until
  // that folder lands in `folders`, so a create the server answers later
  // leaves no gap. A create that fails, or settles without a new folder,
  // lets the name go.
  const [landing, setLanding] = useState<{ name: string; known: Set<string> } | null>(null);
  if (landing && folders.some((f) => !landing.known.has(f.id))) setLanding(null);
  // A failed create says so on the shelf until the next one, and reaches
  // error tracking.
  const [failed, setFailed] = useState<string | null>(null);
  const commitCreate = (name: string) => {
    const pending = { name, known: new Set(folders.map((f) => f.id)) };
    setLanding(pending);
    setFailed(null);
    const release = () => setLanding((l) => (l === pending ? null : l));
    Promise.resolve(onCreate?.(name)).then(
      () => setTimeout(release, LANDING_GRACE_MS),
      (err: unknown) => {
        release();
        setFailed(describeError(err, t("createFailed")));
        reportSwallowed("[cut] folder create failed", err, name);
      },
    );
    closeCreate();
  };

  // Nothing filed here and nothing being made: the shelf takes no room. The
  // host keeps it mounted, so a name held for a create outlives the field.
  if (folders.length === 0 && !creating && !landing && !failed) return null;

  const editRowClass = rows
    ? "flex items-center gap-2.5 rounded-lg px-2 py-1.5"
    : "flex w-[92px] flex-col items-start gap-1 px-2 pt-1.5";
  const editGlyphClass = rows ? "size-7 shrink-0" : "size-[40px]";

  return (
    <div className={cn(rows ? "mb-4 flex flex-col" : "-ml-2 mb-7 flex flex-wrap gap-2")}>
      {folders.map((f) => {
        const s = statOf(f.id);
        const isOver = over === f.id;
        if (editingId === f.id) {
          return (
            <div key={f.id} className={editRowClass} data-no-marquee>
              <FolderGlyph className={editGlyphClass} />
              <RenameInput
                value={f.name}
                className={cn("text-[11px]", rows ? "flex-1" : "w-full")}
                onDone={(name) => {
                  closeRename();
                  if (name) void onRename(f.id, name);
                }}
              />
            </div>
          );
        }
        const sel = folderSelId(f.id);
        const pickable = !!picked && !!onPick && !f.locked;
        const isPicked = pickable && picked.has(sel);
        // The pick this tile drags: itself first, then the rest of the
        // folders, with the items riding under their own MIME.
        const carried = isPicked ? splitPick(picked) : { folders: [], items: [] };
        const carriedFolders = [f.id, ...carried.folders.filter((id) => id !== f.id)];
        // The tile, never the shelf around it, holds off the marquee: the
        // space between and beside tiles is page, where a press sweeps and a
        // right-click opens the page menu.
        const interact = {
          "data-no-marquee": "",
          ...(pickable ? { "data-sel-id": sel } : {}),
          onClick: (e: React.MouseEvent) => {
            if (pickable && additiveClick(e)) {
              e.preventDefault();
              onPick(e, f.id);
              return;
            }
            onOpen(f.id);
          },
          ...(f.locked
            ? {}
            : {
                onDoubleClick: () => setEditingId(f.id),
                onDragOver: (e: React.DragEvent) => {
                  if (!accepts(e, f.id)) return;
                  e.preventDefault();
                  e.dataTransfer.dropEffect = dragTypes(e).includes("Files") ? "copy" : "move";
                  setOver(f.id);
                },
                onDragLeave: (e: React.DragEvent) => {
                  // Crossing onto the tile's own glyph or label is still over it.
                  if (e.currentTarget.contains(e.relatedTarget as Node | null)) return;
                  setOver((o: string | null) => (o === f.id ? null : o));
                },
                onDrop: (e: React.DragEvent) => {
                  if (!accepts(e, f.id)) return;
                  e.preventDefault();
                  setOver(null);
                  // Files land in this folder; stop the drop bubbling to the page's
                  // catch-all so it isn't also imported at the current level.
                  if (onDropFiles && dragTypes(e).includes("Files") && e.dataTransfer.files.length) {
                    e.stopPropagation();
                    onDropFiles(e.dataTransfer.files, f.id);
                    return;
                  }
                  // A pick that holds both drops both: the folders file in,
                  // then the items.
                  if (nests && dragTypes(e).includes(folderMime))
                    onDropFolders(readDragIds(e, folderMime), f.id);
                  if (dragTypes(e).includes(mime)) onDropIds(readDragIds(e, mime), f.id);
                },
                // The tile itself is what a nesting host drags: its own id,
                // with the glyph as the ghost — and the rest of the pick
                // when it is in one.
                ...(nests || referenceOf
                  ? {
                      draggable: true,
                      onDragStart: (e: React.DragEvent) => {
                        if (nests) e.dataTransfer.setData(folderMime!, JSON.stringify(carriedFolders));
                        if (carried.items.length)
                          e.dataTransfer.setData(mime, JSON.stringify(carried.items));
                        e.dataTransfer.effectAllowed = referenceOf ? "copyMove" : "move";
                        if (referenceOf) setRefDragData(e, referenceOf(f));
                        setDragging(f.id);
                        setObjectDragImage(
                          e,
                          carriedFolders.length + carried.items.length,
                          [...carriedFolders.map(folderSelId), ...carried.items]
                        );
                      },
                      onDragEnd: () => { setDragging(null); clearRefDrag(); },
                    }
                  : {}),
              }),
        };
        const menu = f.locked ? null : (
          <DropdownMenu>
            <DropdownMenuTrigger
              render={
                <Button
                  variant="ghost"
                  size="icon-sm"
                  aria-label={t("options")}
                  className={cn(
                    "size-6 opacity-0 group-hover/f:opacity-100 data-[state=open]:opacity-100",
                    rows
                      ? "shrink-0 text-muted-foreground hover:text-foreground"
                      : "absolute top-1 right-1 bg-black/25 text-white hover:bg-black/40 hover:text-white"
                  )}
                  onClick={(e) => e.stopPropagation()}
                />
              }
            >
              <MoreHorizontal className="size-3.5" />
            </DropdownMenuTrigger>
            <DropdownMenuContent align="start" onClick={(e) => e.stopPropagation()}>
              <FolderMenuItems
                onShare={onShare && (() => onShare(f.id))}
                onRename={() => setEditingId(f.id)}
                onDelete={() => void onDelete(f.id)}
                deleteLabel={isPicked && picked.size > 1 ? t("delete") : t("deleteFolder")}
              />
            </DropdownMenuContent>
          </DropdownMenu>
        );
        const tile = rows ? (
          <div
            className={cn(
              "group/f flex cursor-pointer items-center gap-2.5 rounded-lg px-2 py-1.5 text-left transition-colors hover:bg-muted/60",
              isOver && DROP_OVER,
              isPicked && PICKED_RING
            )}
            {...interact}
          >
            <FolderGlyph
              className={cn(
                "size-7 shrink-0 drop-shadow-sm transition-transform",
                isOver && "scale-105 brightness-110"
              )}
              data-drag-object="bare"
            />
            <div className="min-w-0 flex-1">
              <div className="truncate text-xs font-medium">{f.name}</div>
              <div className="flex items-center gap-1 text-[10px] text-muted-foreground tabular-nums">
                {t("items", { count: s.count })}
                {s.size != null ? ` · ${formatBytes(s.size, locale)}` : ""}
                {badgeOf?.(f.id)}
              </div>
            </div>
            {menu}
          </div>
        ) : (
          <div
            className={cn(
              "group/f relative flex w-[92px] cursor-pointer flex-col items-start rounded-xl px-2 py-1.5 text-left transition-colors hover:bg-muted/60",
              isOver && DROP_OVER,
              isPicked && PICKED_RING
            )}
            {...interact}
          >
            <div
              className={cn("grid place-items-center transition-transform", isOver && "scale-105")}
              data-drag-object="bare"
            >
              <FolderGlyph className={cn("size-[40px] drop-shadow-sm", isOver && "brightness-110")} />
            </div>
            <span className="mt-0.5 line-clamp-2 max-w-full text-xs font-medium leading-tight">
              {f.name}
            </span>
            <span className="flex items-center gap-1 text-[10px] text-muted-foreground tabular-nums">
              {s.count}
              {s.size != null ? ` · ${formatBytes(s.size, locale)}` : ""}
              {badgeOf?.(f.id)}
            </span>
            {menu}
          </div>
        );
        return onRefDrop ? (
          <RefDropZone
            key={f.id}
            onRef={(r) => onRefDrop(r, f.id)}
            activeClassName={cn(DROP_OVER, rows ? "rounded-lg" : "rounded-xl")}
          >
            {tile}
          </RefDropZone>
        ) : (
          <Fragment key={f.id}>{tile}</Fragment>
        );
      })}

      {landing && (
        <div className={editRowClass} data-no-marquee>
          <FolderGlyph className={cn(editGlyphClass, "drop-shadow-sm")} />
          <span className={cn("truncate text-xs font-medium leading-tight", !rows && "mt-0.5 max-w-full")}>
            {landing.name}
          </span>
        </div>
      )}

      {creating && (
        <div className={editRowClass} data-no-marquee>
          <FolderGlyph className={cn(editGlyphClass, "opacity-60")} />
          <Input
            autoFocus
            value={draft}
            placeholder={t("namePlaceholder")}
            className={cn("h-6 text-[11px]", rows ? "flex-1" : "w-full")}
            onChange={(e) => setDraft(e.target.value)}
            onBlur={closeCreate}
            onKeyDown={(e) => {
              if (e.key === "Enter" && draft.trim()) {
                commitCreate(draft.trim());
              } else if (e.key === "Escape") closeCreate();
            }}
          />
        </div>
      )}

      {failed && (
        <p role="alert" className="w-full px-2 text-xs text-destructive">
          {failed}
        </p>
      )}
    </div>
  );
}