{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"file-upload-list-with-per-file-progress-bars","type":"registry:component","title":"File Upload List With Per File Progress Bars","description":"","dependencies":[],"files":[{"path":"components/we-inc/file-upload-list-with-per-file-progress-bars.tsx","type":"registry:component","content":"import { useEffect, useState } from \"react\";\n\ntype Item = { name: string; size: number; pct: number; state: \"uploading\" | \"done\" | \"cancelled\" };\n// Demo files; sizes in megabytes.\nconst START: Item[] = [\n  { name: \"kitchen-before.jpg\", size: 4.2, pct: 100, state: \"done\" },\n  { name: \"kitchen-after-wide.jpg\", size: 6.8, pct: 64, state: \"uploading\" },\n  { name: \"floor-plan-v3.pdf\", size: 1.9, pct: 22, state: \"uploading\" },\n];\n\nexport default function FileUploadListWithProgress() {\n  const [items, setItems] = useState(START);\n\n  // Demo only: advance each upload a little every 400 ms, then start over.\n  useEffect(() => {\n    const t = setInterval(() => {\n      setItems((list) => {\n        if (list.every((f) => f.state !== \"uploading\")) return START;\n        return list.map((f) => {\n          if (f.state !== \"uploading\") return f;\n          const pct = Math.min(100, f.pct + Math.round(9 / f.size + 2));\n          return { ...f, pct, state: pct >= 100 ? \"done\" : \"uploading\" };\n        });\n      });\n    }, 400);\n    return () => clearInterval(t);\n  }, []);\n\n  const cancel = (name: string) => setItems((l) => l.map((f) => (f.name === name ? { ...f, state: \"cancelled\" } : f)));\n  const busy = items.filter((f) => f.state === \"uploading\").length;\n\n  return (\n    <section className=\"bg-slate-50 px-4 py-16\">\n      <div className=\"mx-auto max-w-xl rounded-2xl bg-white p-5 shadow-sm ring-1 ring-slate-200 sm:p-6\">\n        <div className=\"flex items-center justify-between\">\n          <h2 className=\"text-base font-semibold text-slate-900\">Project photos</h2>\n          <p className=\"text-sm text-slate-600\" aria-live=\"polite\">{busy ? `Uploading ${busy} of ${items.length}` : \"All uploads finished\"}</p>\n        </div>\n        <ul className=\"mt-4 divide-y divide-slate-100\">\n          {items.map((f) => {\n            const id = `fulp-${f.name.replace(/\\W/g, \"\")}`;\n            const colour = f.state === \"done\" ? \"bg-emerald-500\" : f.state === \"cancelled\" ? \"bg-slate-300\" : \"bg-indigo-600\";\n            return (\n              <li key={f.name} className=\"flex items-center gap-3 py-3.5\">\n                <span className={`grid h-10 w-10 shrink-0 place-items-center rounded-lg text-[10px] font-bold uppercase ${f.name.endsWith(\".pdf\") ? \"bg-rose-50 text-rose-700\" : \"bg-indigo-50 text-indigo-700\"}`} aria-hidden=\"true\">\n                  {f.name.split(\".\").pop()}\n                </span>\n                <div className=\"min-w-0 flex-1\">\n                  <div className=\"flex items-baseline justify-between gap-3\">\n                    <span id={id} className=\"truncate text-sm font-medium text-slate-900\">{f.name}</span>\n                    <span className=\"shrink-0 text-xs tabular-nums text-slate-600\">\n                      {f.state === \"done\" ? `${f.size} MB` : f.state === \"cancelled\" ? \"Cancelled\" : `${((f.size * f.pct) / 100).toFixed(1)} of ${f.size} MB`}\n                    </span>\n                  </div>\n                  <div role=\"progressbar\" aria-labelledby={id} aria-valuemin={0} aria-valuemax={100} aria-valuenow={f.pct} className=\"mt-2 h-1.5 overflow-hidden rounded-full bg-slate-100\">\n                    <div className={`h-full w-full origin-left rounded-full transition-transform duration-300 motion-reduce:transition-none ${colour}`} style={{ transform: `scaleX(${f.pct / 100})` }} />\n                  </div>\n                </div>\n                {f.state === \"uploading\" ? (\n                  <button type=\"button\" onClick={() => cancel(f.name)} aria-label={`Cancel upload of ${f.name}`} className=\"grid h-10 w-10 shrink-0 place-items-center rounded-lg text-slate-600 hover:bg-slate-100 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-indigo-600\">\n                    <svg viewBox=\"0 0 20 20\" width=\"16\" height=\"16\" aria-hidden=\"true\"><path d=\"M5 5l10 10M15 5L5 15\" stroke=\"currentColor\" strokeWidth=\"2\" strokeLinecap=\"round\" /></svg>\n                  </button>\n                ) : (\n                  <span className=\"grid h-10 w-10 shrink-0 place-items-center\" aria-label={f.state === \"done\" ? \"Uploaded\" : \"Cancelled\"} role=\"img\">\n                    {f.state === \"done\" ? (\n                      <svg viewBox=\"0 0 20 20\" width=\"20\" height=\"20\" aria-hidden=\"true\"><circle cx=\"10\" cy=\"10\" r=\"9\" fill=\"#10b981\" /><path d=\"M6 10.4l2.6 2.6L14 7.6\" fill=\"none\" stroke=\"#fff\" strokeWidth=\"2\" strokeLinecap=\"round\" strokeLinejoin=\"round\" /></svg>\n                    ) : (\n                      <svg viewBox=\"0 0 20 20\" width=\"20\" height=\"20\" aria-hidden=\"true\"><circle cx=\"10\" cy=\"10\" r=\"9\" fill=\"#cbd5e1\" /><path d=\"M6.5 10h7\" stroke=\"#fff\" strokeWidth=\"2\" strokeLinecap=\"round\" /></svg>\n                    )}\n                  </span>\n                )}\n              </li>\n            );\n          })}\n        </ul>\n      </div>\n    </section>\n  );\n}\n"}]}
