{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "files-04",
  "description": "Folders as tiles over a list of files with their size and last change, a path back to the top, and a menu on each file.",
  "registryDependencies": [
    "https://www.dashboardblocks.com/r/radix/activity-feed.json",
    "https://www.dashboardblocks.com/r/radix/block-state.json",
    "https://www.dashboardblocks.com/r/radix/details.json",
    "https://www.dashboardblocks.com/r/radix/files.json",
    "https://www.dashboardblocks.com/r/radix/format.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/files/files-04.tsx",
      "content": "'use client'\n\nimport {\n  BlockMessage,\n  BlockReady,\n  BlockStateProvider,\n  BlockStateRows,\n  type BlockStateProps,\n  getBlockState,\n} from '@/registry/components/dashboardblocks/block-state'\nimport { Details } from '@/registry/components/dashboardblocks/details'\nimport {\n  type FileAction,\n  FileActionsMenu,\n  FileBreadcrumbs,\n  FileKindIcon,\n  fileKindConfig,\n  formatFileSize,\n} from '@/registry/components/dashboardblocks/files'\nimport { IconPlaceholder } from '@/registry/icons/icon-placeholder'\nimport { formatRelativeTime, joinDetails } from '@/registry/lib/dashboardblocks/format'\nimport { startTransition, useEffect, useMemo, useRef, useState } from 'react'\n\ninterface BrowserFile {\n  href: string\n  id: string\n  modified: Date\n  name: string\n  /** In bytes. */\n  size: number\n  type: 'file'\n}\n\ninterface BrowserFolder {\n  children: BrowserItem[]\n  id: string\n  name: string\n  type: 'folder'\n}\n\ntype BrowserItem = BrowserFile | BrowserFolder\n\ninterface Files4Props extends BlockStateProps {\n  onCopyLink?: (file: BrowserFile) => Promise<void>\n  onDelete?: (file: BrowserFile) => Promise<void>\n  /** Pass a fixed date, so times render the same on the server and in the browser. */\n  now: Date\n  /** Pass a root with no children to show the empty state. */\n  root: BrowserFolder\n  timeZone?: string\n}\n\nconst exampleNow = new Date(Date.UTC(2026, 8, 26, 15, 30))\nconst MB = 1024 * 1024\n\nlet exampleId = 0\nconst file = (name: string, size: number, hoursAgo: number): BrowserFile => ({\n  href: '#',\n  id: `file-${++exampleId}`,\n  modified: new Date(exampleNow.getTime() - hoursAgo * 3_600_000),\n  name,\n  size: Math.round(size),\n  type: 'file',\n})\nconst folder = (name: string, children: BrowserItem[]): BrowserFolder => ({\n  children,\n  id: `folder-${++exampleId}`,\n  name,\n  type: 'folder',\n})\n\nconst exampleProps: Files4Props = {\n  now: exampleNow,\n  root: folder('Marketing', [\n    folder('Brand', [\n      folder('Logos', [\n        file('logo-primary.svg', 24 * 1024, 900),\n        file('logo-mark@2x.png', 186 * 1024, 900),\n        file('logo-white.svg', 22 * 1024, 880),\n      ]),\n      file('Brand guidelines v4.pdf', 12.1 * MB, 74),\n      file('Typography specimen.pdf', 3.2 * MB, 410),\n      file('Color tokens.json', 8 * 1024, 52),\n    ]),\n    folder('Campaigns', [\n      folder('Fall launch', [\n        file('Fall launch brief.pdf', 2.4 * MB, 5),\n        file('hero-banner@2x.png', 6.8 * MB, 30),\n        file('Launch teaser 30s.mp4', 142 * MB, 98),\n        file('Media plan.xlsx', 312 * 1024, 20),\n      ]),\n      folder('Spring promo', []),\n    ]),\n    folder('Events', [\n      folder('Summit 2026', [\n        file('Keynote slides.pptx', 48.3 * MB, 300),\n        file('Speaker headshots.zip', 96.4 * MB, 320),\n      ]),\n    ]),\n    file('Q3 board deck.pptx', 18.4 * MB, 1.5),\n    file('Campaign budget FY27.xlsx', 486 * 1024, 26),\n    file('Press release draft.docx', 92 * 1024, 140),\n    file('Podcast ep 12 master.wav', 318 * MB, 220),\n    file('Team offsite.jpg', 4.6 * MB, 600),\n  ]),\n}\n\nasync function copyLink(target: BrowserFile) {\n  await navigator.clipboard.writeText(new URL(target.href, window.location.href).href)\n}\n\nasync function deleteFile() {\n  await new Promise((resolve) => setTimeout(resolve, 400))\n}\n\nfunction findPath(root: BrowserFolder, id: string): BrowserFolder[] {\n  if (root.id === id) return [root]\n  for (const child of root.children) {\n    if (child.type !== 'folder') continue\n    const path = findPath(child, id)\n    if (path.length > 0) return [root, ...path]\n  }\n  return []\n}\n\nfunction removeItem(folder: BrowserFolder, id: string): BrowserFolder {\n  return {\n    ...folder,\n    children: folder.children\n      .filter((child) => child.id !== id)\n      .map((child) => (child.type === 'folder' ? removeItem(child, id) : child)),\n  }\n}\n\nconst plural = (count: number, noun: string) =>\n  `${count} ${count === 1 ? noun : `${noun}s`}`\n\nconst byName = (a: BrowserItem, b: BrowserItem) => a.name.localeCompare(b.name, 'en-US')\n\nconst Files4 = (props: Files4Props) => {\n  const {\n    now,\n    onCopyLink = copyLink,\n    onDelete = deleteFile,\n    root: storedRoot,\n    errorMessage,\n    status,\n    timeZone = 'UTC',\n  } = props\n  const [deleted, setDeleted] = useState<ReadonlySet<string>>(() => new Set())\n  const root = useMemo(\n    () => [...deleted].reduce(removeItem, storedRoot),\n    [deleted, storedRoot],\n  )\n  const [folderId, setFolderId] = useState(storedRoot.id)\n  const state = getBlockState(status, storedRoot.children)\n  const [message, setMessage] = useState('')\n  const contentRef = useRef<HTMLElement>(null)\n  const moved = useRef(false)\n\n  const path = findPath(root, folderId)\n  const current = path.at(-1) ?? root\n  const folders = current.children\n    .filter((item): item is BrowserFolder => item.type === 'folder')\n    .sort(byName)\n  const files = current.children\n    .filter((item): item is BrowserFile => item.type === 'file')\n    .sort((a, b) => b.modified.getTime() - a.modified.getTime())\n\n  useEffect(() => {\n    if (!moved.current) return\n    moved.current = false\n    contentRef.current?.focus()\n  }, [folderId])\n\n  function open(id: string) {\n    moved.current = true\n    setFolderId(id)\n    setMessage('')\n  }\n\n  async function remove(target: BrowserFile) {\n    setMessage(`Deleting ${target.name}…`)\n    await onDelete(target)\n    startTransition(() => {\n      setDeleted((current) => new Set([...current, target.id]))\n      setMessage(`Deleted ${target.name}`)\n    })\n  }\n\n  async function share(target: BrowserFile) {\n    try {\n      await onCopyLink(target)\n      startTransition(() => setMessage(`Copied a link to ${target.name}`))\n    } catch {\n      startTransition(() => setMessage(`Couldn't copy a link to ${target.name}`))\n    }\n  }\n\n  const actionsFor = (target: BrowserFile): FileAction[] => [\n    {\n      icon: (\n        <IconPlaceholder\n          lucide='DownloadIcon'\n          tabler='IconDownload'\n          hugeicons='Download01Icon'\n          phosphor='DownloadIcon'\n          remixicon='RiDownloadLine'\n        />\n      ),\n      label: 'Download',\n      onSelect: () => setMessage(`Downloading ${target.name}`),\n    },\n    {\n      icon: (\n        <IconPlaceholder\n          lucide='LinkIcon'\n          tabler='IconLink'\n          hugeicons='Link01Icon'\n          phosphor='LinkIcon'\n          remixicon='RiLinkM'\n        />\n      ),\n      label: 'Copy link',\n      onSelect: () => void share(target),\n    },\n    {\n      icon: (\n        <IconPlaceholder\n          lucide='Trash2Icon'\n          tabler='IconTrash'\n          hugeicons='Delete02Icon'\n          phosphor='TrashIcon'\n          remixicon='RiDeleteBinLine'\n        />\n      ),\n      label: 'Delete',\n      onSelect: () => void remove(target),\n      variant: 'destructive',\n    },\n  ]\n\n  const summary =\n    joinDetails(\n      folders.length > 0 && plural(folders.length, 'folder'),\n      files.length > 0 && plural(files.length, 'file'),\n      files.length > 0 && formatFileSize(files.reduce((sum, item) => sum + item.size, 0)),\n    ) || 'Empty'\n\n  return (\n    <BlockStateProvider errorMessage={errorMessage} state={state}>\n      <section className='@container flex flex-col gap-3'>\n        <header className='flex flex-col gap-0.5'>\n          <FileBreadcrumbs\n            items={path.map((item) => ({ id: item.id, label: item.name }))}\n            onNavigate={open}\n            className='-mx-1 [&_ol]:text-base [&_[aria-current]]:font-semibold [&_[aria-current]]:tracking-tight'\n          />\n          <BlockReady className='text-sm'>\n            <p className='text-muted-foreground text-sm tabular-nums'>{summary}</p>\n          </BlockReady>\n        </header>\n        <section\n          ref={contentRef}\n          tabIndex={-1}\n          aria-label={current.name}\n          className='flex flex-col gap-4 outline-none'\n        >\n          <BlockStateRows className='border-t' rows={5} />\n          {!state && folders.length > 0 && (\n            <ul\n              aria-label='Folders'\n              className='grid grid-cols-1 gap-2 @xs:grid-cols-2 @2xl:grid-cols-3 @4xl:grid-cols-4'\n            >\n              {folders.map((item) => (\n                <li key={item.id}>\n                  <button\n                    type='button'\n                    onClick={() => open(item.id)}\n                    className='bg-muted/50 hover:bg-muted focus-visible:ring-ring/50 flex w-full items-center gap-3 rounded-xl p-3 text-left transition-colors outline-none focus-visible:ring-3'\n                  >\n                    <FileKindIcon kind='folder' size='sm' className='bg-background' />\n                    <span className='flex min-w-0 flex-col'>\n                      <span className='truncate text-sm font-medium'>{item.name}</span>\n                      <span className='text-muted-foreground text-sm tabular-nums'>\n                        {item.children.length === 0\n                          ? 'Empty'\n                          : plural(item.children.length, 'item')}\n                      </span>\n                    </span>\n                  </button>\n                </li>\n              ))}\n            </ul>\n          )}\n          {!state && files.length > 0 && (\n            <ul aria-label='Files' className='flex flex-col border-t'>\n              {files.map((item) => (\n                <li\n                  key={item.id}\n                  className='relative flex items-center gap-3 border-b py-2.5'\n                >\n                  <FileKindIcon name={item.name} size='sm' />\n                  <div className='flex min-w-0 flex-1 flex-col @md:flex-row @md:items-baseline @md:gap-4'>\n                    <a\n                      href={item.href}\n                      className='truncate text-sm font-medium underline-offset-4 outline-none hover:underline focus-visible:underline @md:flex-1'\n                    >\n                      <span aria-hidden className='absolute inset-0' />\n                      {item.name}\n                    </a>\n                    <span className='text-muted-foreground truncate text-sm tabular-nums @md:w-28 @md:shrink-0'>\n                      <Details className='@md:hidden'>\n                        {formatFileSize(item.size)}\n                        <time dateTime={item.modified.toISOString()}>\n                          {formatRelativeTime(item.modified, now, { timeZone })}\n                        </time>\n                      </Details>\n                      <time\n                        dateTime={item.modified.toISOString()}\n                        className='hidden @md:inline'\n                      >\n                        {formatRelativeTime(item.modified, now, { timeZone })}\n                      </time>\n                    </span>\n                    <span className='hidden w-20 shrink-0 text-right text-sm tabular-nums @md:inline'>\n                      {formatFileSize(item.size)}\n                    </span>\n                  </div>\n                  <FileActionsMenu\n                    actions={actionsFor(item)}\n                    label={`Actions for ${item.name}`}\n                    className='relative -mr-2'\n                  />\n                </li>\n              ))}\n            </ul>\n          )}\n          {!state && folders.length === 0 && files.length === 0 && (\n            <BlockMessage\n              className='border-t'\n              icon={fileKindConfig.folder.icon}\n              title={`${current.name} is empty`}\n              description='Files and folders added here show up in this list.'\n            />\n          )}\n        </section>\n        <p aria-live='polite' className='text-muted-foreground min-h-5 text-sm'>\n          {message}\n        </p>\n      </section>\n    </BlockStateProvider>\n  )\n}\n\nexport {\n  Files4,\n  exampleProps as files4ExampleProps,\n  type BrowserFile,\n  type BrowserFolder,\n  type BrowserItem,\n  type Files4Props,\n}\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/files/files-04.tsx"
    }
  ],
  "meta": {
    "shows": "Folders as tiles over a list of files with their size and last change, a path back to the top, and a menu on each file.",
    "size": "full",
    "usedWith": [
      "files-01",
      "files-02"
    ],
    "component": "Files4",
    "props": "{ onCopyLink?: (file: { href: string; id: string; modified: Date; name: string; size: number; type: \"file\" }) => Promise<void>; onDelete?: (file: { href: string; id: string; modified: Date; name: string; size: number; type: \"file\" }) => Promise<void>; now: Date; root: { children: ({ href: string; id: string; modified: Date; name: string; size: number; type: \"file\" } | BrowserFolder)[]; id: string; name: string; type: \"folder\" }; timeZone?: string; errorMessage?: string; status?: \"loading\" | \"error\" }"
  },
  "type": "registry:component"
}
