{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "files-02",
  "description": "A table of files with type, owner, last change and size, with search, type filter, sorting and bulk download or delete.",
  "registryDependencies": [
    "button",
    "https://www.dashboardblocks.com/r/radix/activity-feed.json",
    "https://www.dashboardblocks.com/r/radix/block-state.json",
    "https://www.dashboardblocks.com/r/radix/data-table.json",
    "https://www.dashboardblocks.com/r/radix/files.json",
    "https://www.dashboardblocks.com/r/radix/table.json",
    "https://www.dashboardblocks.com/r/radix/team.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/files/files-02.tsx",
      "content": "'use client'\n\nimport { ActivityTime } from '@/registry/components/dashboardblocks/activity-feed'\nimport {\n  BlockReady,\n  BlockStateProvider,\n  type BlockStateProps,\n  getBlockState,\n} from '@/registry/components/dashboardblocks/block-state'\nimport {\n  createDataTableColumnHelper,\n  createDataTableSelectColumn,\n  DataTableContent,\n  DataTableFacetFilter,\n  type DataTableFacetOption,\n  DataTableReset,\n  DataTableSearch,\n  DataTableSelectionBar,\n  useDataTable,\n} from '@/registry/components/dashboardblocks/data-table'\nimport {\n  type FileAction,\n  FileActionsMenu,\n  FileKindIcon,\n  type FileKind,\n  fileKindConfig,\n  formatFileSize,\n  getFileKind,\n} from '@/registry/components/dashboardblocks/files'\nimport { DataTableRowLink } from '@/registry/components/dashboardblocks/table'\nimport { PersonAvatar } from '@/registry/components/dashboardblocks/team'\nimport { IconPlaceholder } from '@/registry/icons/icon-placeholder'\nimport { createContext, startTransition, useContext, useMemo, useState } from 'react'\n\nimport { Button } from '@/components/ui/button'\n\ninterface DriveFile {\n  href: string\n  id: string\n  modified: Date\n  name: string\n  owner: string\n  ownerAvatar?: string\n  /** In bytes. */\n  size: number\n}\n\ninterface FileRow extends DriveFile {\n  kind: FileKind\n}\n\ninterface Files2Props extends BlockStateProps {\n  /** Leave out to describe the files shown. */\n  description?: string\n  /** Leave out, or pass none, to show the empty state. */\n  files?: DriveFile[]\n  /** Pass a fixed date, so times render the same on the server and in the browser. */\n  now: Date\n  onCopyLink?: (file: DriveFile) => Promise<void>\n  onDelete?: (files: DriveFile[]) => Promise<void>\n  onDownload?: (files: DriveFile[]) => void\n  timeZone?: string\n  title: string\n}\n\nconst exampleNow = new Date(Date.UTC(2026, 8, 26, 15, 30))\nconst hoursAgo = (hours: number) => new Date(exampleNow.getTime() - hours * 3_600_000)\nconst MB = 1024 * 1024\n\nconst avatars: Record<string, string> = {\n  'Hannah Okafor': '/images/avatar-12.webp',\n  'Jonas Berg': '/images/avatar-07.webp',\n  'Lena Fischer': '/images/avatar-03.webp',\n  'Mateo Silva': '/images/avatar-06.webp',\n  'Priya Nair': '/images/avatar-02.webp',\n}\n\nconst exampleProps: Files2Props = {\n  files: [\n    ['Q3 board deck.pptx', 'Priya Nair', 1.5, 18.4 * MB],\n    ['Fall launch brief.pdf', 'Mateo Silva', 5, 2.4 * MB],\n    ['Campaign budget FY27.xlsx', 'Hannah Okafor', 26, 486 * 1024],\n    ['hero-banner@2x.png', 'Lena Fischer', 30, 6.8 * MB],\n    ['Brand guidelines v4.pdf', 'Priya Nair', 74, 12.1 * MB],\n    ['Launch teaser 30s.mp4', 'Lena Fischer', 98, 142 * MB],\n    ['Press release draft.docx', 'Mateo Silva', 140, 92 * 1024],\n    ['Podcast ep 12 master.wav', 'Jonas Berg', 220, 318 * MB],\n    ['Customer logos.zip', 'Hannah Okafor', 410, 54.6 * MB],\n    ['utm-builder.json', 'Jonas Berg', 760, 6 * 1024],\n  ].map(([name, owner, hours, size], index) => ({\n    href: '#',\n    id: `file-${index + 1}`,\n    modified: hoursAgo(hours as number),\n    name: name as string,\n    owner: owner as string,\n    ownerAvatar: avatars[owner as string],\n    size: Math.round(size as number),\n  })),\n  now: exampleNow,\n  title: 'Marketing drive',\n}\n\nasync function copyLink(file: DriveFile) {\n  await navigator.clipboard.writeText(new URL(file.href, window.location.href).href)\n}\n\nasync function deleteFiles() {\n  await new Promise((resolve) => setTimeout(resolve, 400))\n}\n\nconst noFiles: DriveFile[] = []\n\nconst plural = (count: number) => `${count} ${count === 1 ? 'file' : 'files'}`\n\nfunction summarize(rows: DriveFile[]) {\n  if (rows.length === 0) return 'No files yet.'\n  const owners = new Set(rows.map((row) => row.owner)).size\n  const total = rows.reduce((sum, row) => sum + row.size, 0)\n  return `${plural(rows.length)} from ${owners} ${owners === 1 ? 'person' : 'people'}, ${formatFileSize(total)} in all, newest first.`\n}\n\nconst KIND_OPTIONS: DataTableFacetOption[] = (\n  [\n    'pdf',\n    'document',\n    'spreadsheet',\n    'presentation',\n    'image',\n    'video',\n    'audio',\n    'archive',\n    'code',\n    'other',\n  ] as const\n).map((kind) => ({\n  icon: fileKindConfig[kind].icon,\n  label: fileKindConfig[kind].label,\n  value: kind,\n}))\n\nconst downloadIcon = (\n  <IconPlaceholder\n    lucide='DownloadIcon'\n    tabler='IconDownload'\n    hugeicons='Download01Icon'\n    phosphor='DownloadIcon'\n    remixicon='RiDownloadLine'\n  />\n)\n\nconst deleteIcon = (\n  <IconPlaceholder\n    lucide='Trash2Icon'\n    tabler='IconTrash'\n    hugeicons='Delete02Icon'\n    phosphor='TrashIcon'\n    remixicon='RiDeleteBinLine'\n  />\n)\n\ninterface FileTableContext {\n  actionsFor: (file: DriveFile) => FileAction[]\n  now: Date\n  timeZone: string\n}\n\nconst FileTable = createContext<FileTableContext>({\n  actionsFor: () => [],\n  now: new Date(0),\n  timeZone: 'UTC',\n})\n\nfunction ModifiedCell({ date }: { date: Date }) {\n  const { now, timeZone } = useContext(FileTable)\n  return (\n    <ActivityTime\n      date={date}\n      now={now}\n      timeZone={timeZone}\n      className='text-foreground text-sm'\n    />\n  )\n}\n\nfunction ActionsCell({ file }: { file: DriveFile }) {\n  const { actionsFor } = useContext(FileTable)\n  return <FileActionsMenu actions={actionsFor(file)} label={`Actions for ${file.name}`} />\n}\n\nconst columnHelper = createDataTableColumnHelper<FileRow>()\n\nconst columns = columnHelper.columns([\n  createDataTableSelectColumn<FileRow>({ getRowLabel: (row) => row.name }),\n  columnHelper.accessor('name', {\n    cell: ({ row }) => (\n      <span className='flex min-w-0 items-center gap-3'>\n        <FileKindIcon kind={row.original.kind} size='sm' />\n        <DataTableRowLink href={row.original.href}>{row.original.name}</DataTableRowLink>\n      </span>\n    ),\n    header: 'Name',\n    meta: { primary: true, truncate: true },\n  }),\n  columnHelper.accessor('kind', {\n    cell: ({ getValue }) => fileKindConfig[getValue()].label,\n    enableGlobalFilter: false,\n    header: 'Type',\n    meta: { cellClassName: 'text-muted-foreground' },\n  }),\n  columnHelper.accessor('owner', {\n    cell: ({ row }) => (\n      <span className='flex items-center gap-2 whitespace-nowrap'>\n        <PersonAvatar\n          person={{ avatar: row.original.ownerAvatar, name: row.original.owner }}\n          size='sm'\n        />\n        {row.original.owner}\n      </span>\n    ),\n    header: 'Owner',\n  }),\n  columnHelper.accessor((row) => row.modified.getTime(), {\n    cell: ({ row }) => <ModifiedCell date={row.original.modified} />,\n    enableGlobalFilter: false,\n    header: 'Modified',\n    id: 'modified',\n    sortDescFirst: true,\n  }),\n  columnHelper.accessor('size', {\n    cell: ({ getValue }) => formatFileSize(getValue()),\n    enableGlobalFilter: false,\n    header: 'Size',\n    meta: { align: 'end', cellClassName: 'tabular-nums' },\n    sortDescFirst: true,\n  }),\n  columnHelper.display({\n    cell: ({ row }) => <ActionsCell file={row.original} />,\n    header: () => <span className='sr-only'>Actions</span>,\n    id: 'actions',\n    meta: {\n      cellClassName: 'w-px pl-0',\n      headerClassName: 'w-px',\n    },\n  }),\n])\n\nconst Files2 = (props: Files2Props) => {\n  const {\n    description,\n    files = noFiles,\n    now,\n    onCopyLink = copyLink,\n    onDelete = deleteFiles,\n    onDownload = () => {},\n    errorMessage,\n    status,\n    timeZone = 'UTC',\n    title,\n  } = props\n  const [deleted, setDeleted] = useState<ReadonlySet<string>>(() => new Set())\n  const rows = useMemo<FileRow[]>(\n    () =>\n      files\n        .filter((file) => !deleted.has(file.id))\n        .map((file) => ({ ...file, kind: getFileKind(file.name) })),\n    [deleted, files],\n  )\n  const [message, setMessage] = useState('')\n\n  async function remove(targets: DriveFile[]) {\n    const ids = new Set(targets.map((file) => file.id))\n    setMessage(\n      `Deleting ${targets.length === 1 ? targets[0].name : plural(targets.length)}…`,\n    )\n    await onDelete(targets)\n    startTransition(() => {\n      setDeleted((current) => new Set([...current, ...ids]))\n      table.setRowSelection((current) =>\n        Object.fromEntries(Object.entries(current).filter(([id]) => !ids.has(id))),\n      )\n      setMessage(\n        `Deleted ${targets.length === 1 ? targets[0].name : plural(targets.length)}`,\n      )\n    })\n  }\n\n  function download(targets: DriveFile[]) {\n    onDownload(targets)\n    setMessage(\n      `Downloading ${targets.length === 1 ? targets[0].name : plural(targets.length)}`,\n    )\n  }\n\n  async function share(file: DriveFile) {\n    try {\n      await onCopyLink(file)\n      startTransition(() => setMessage(`Copied a link to ${file.name}`))\n    } catch {\n      startTransition(() => setMessage(`Couldn't copy a link to ${file.name}`))\n    }\n  }\n\n  const actionsFor = (file: DriveFile): FileAction[] => [\n    { icon: downloadIcon, label: 'Download', onSelect: () => download([file]) },\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(file),\n    },\n    {\n      icon: deleteIcon,\n      label: 'Delete',\n      onSelect: () => void remove([file]),\n      variant: 'destructive',\n    },\n  ]\n\n  const table = useDataTable({\n    columns,\n    data: rows,\n    getRowId: (row) => row.id,\n    initialState: { sorting: [{ desc: true, id: 'modified' }] },\n  })\n  const selected = table.getSelectedRowModel().rows.map((row) => row.original)\n  const shownDescription = description ?? summarize(rows)\n\n  return (\n    <BlockStateProvider errorMessage={errorMessage} state={getBlockState(status, files)}>\n      <FileTable value={{ actionsFor, now, timeZone }}>\n        <section className='@container/data-table flex flex-col gap-3'>\n          <header className='flex flex-col gap-0.5'>\n            <h2 className='font-semibold tracking-tight'>{title}</h2>\n            <BlockReady className='text-sm'>\n              <p className='text-muted-foreground text-sm'>{shownDescription}</p>\n            </BlockReady>\n          </header>\n          <div className='flex min-h-9 flex-wrap items-center gap-2'>\n            {selected.length > 0 ? (\n              <DataTableSelectionBar className='w-full' table={table}>\n                <Button variant='outline' size='sm' onClick={() => download(selected)}>\n                  {downloadIcon}\n                  Download\n                </Button>\n                <Button variant='outline' size='sm' onClick={() => void remove(selected)}>\n                  {deleteIcon}\n                  Delete\n                </Button>\n              </DataTableSelectionBar>\n            ) : (\n              <>\n                <DataTableSearch table={table} placeholder='Search files' />\n                <DataTableFacetFilter\n                  table={table}\n                  columnId='kind'\n                  options={KIND_OPTIONS.filter((option) =>\n                    rows.some((row) => row.kind === option.value),\n                  )}\n                />\n                <DataTableReset table={table} />\n              </>\n            )}\n          </div>\n          <DataTableContent caption={`${title}: ${shownDescription}`} table={table} />\n          <p aria-live='polite' className='text-muted-foreground min-h-5 text-sm'>\n            {message}\n          </p>\n        </section>\n      </FileTable>\n    </BlockStateProvider>\n  )\n}\n\nexport { Files2, exampleProps as files2ExampleProps, type DriveFile, type Files2Props }\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/files/files-02.tsx"
    }
  ],
  "meta": {
    "shows": "A table of files with type, owner, last change and size, with search, type filter, sorting and bulk download or delete.",
    "size": "full",
    "usedWith": [
      "files-01",
      "files-03"
    ],
    "component": "Files2",
    "props": "{ description?: string; files?: { href: string; id: string; modified: Date; name: string; owner: string; ownerAvatar?: string; size: number }[]; now: Date; onCopyLink?: (file: { href: string; id: string; modified: Date; name: string; owner: string; ownerAvatar?: string; size: number }) => Promise<void>; onDelete?: (files: { href: string; id: string; modified: Date; name: string; owner: string; ownerAvatar?: string; size: number }[]) => Promise<void>; onDownload?: (files: { href: string; id: string; modified: Date; name: string; owner: string; ownerAvatar?: string; size: number }[]) => void; timeZone?: string; title: string; errorMessage?: string; status?: \"loading\" | \"error\" }"
  },
  "type": "registry:component"
}
