{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "media-file-upload",
  "type": "registry:component",
  "title": "FileUpload",
  "description": "A queue of files with progress, retry and remove",
  "author": "sudo-kw <https://github.com/sudo-kw>",
  "dependencies": [
    "@gpuix/native@0.11.0",
    "@gpuix/react@0.11.0",
    "effect@4.1.2",
    "react@^19.2.4"
  ],
  "registryDependencies": [
    "https://gpuixui.sudokw.com/gpuix-icons.json",
    "https://gpuixui.sudokw.com/gpuix-primitives.json",
    "https://gpuixui.sudokw.com/gpuix-tokens.json",
    "https://gpuixui.sudokw.com/gpuix-tw.json"
  ],
  "files": [
    {
      "path": "packages/media/src/file-upload.tsx",
      "target": "@ui/gpuixui/media/file-upload.tsx",
      "type": "registry:component",
      "content": "/** @jsxImportSource @gpuix/react */\nimport type { Props } from \"@gpuix/react\";\nimport { mergeStyles } from \"@gpuix/react/floating\";\nimport { Icon } from \"../icons/index.js\";\nimport { Progress } from \"../primitives/index.js\";\nimport { Button, IconButton } from \"../primitives/button.js\";\nimport { cn } from \"../tw/index.js\";\nimport { Match } from \"effect\";\nimport * as Arr from \"effect/Array\";\nimport * as Option from \"effect/Option\";\nimport { useCallback, useEffect, useState, type ReactElement } from \"react\";\nimport { recipeStyle, useTheme, type Theme } from \"./internal.js\";\nimport { accessibleName } from \"./name.js\";\nimport {\n  complete,\n  emptyQueue,\n  enqueue,\n  fail,\n  filesOf,\n  findItem,\n  remove,\n  reportProgress,\n  start,\n  type UploadFile,\n  type UploadItem,\n  type UploadQueue,\n} from \"./upload/queue.js\";\n\nexport type UploadFunction = (\n  file: UploadFile,\n  onProgress: (progress: number) => void,\n) => Promise<void>;\n\nexport interface FileUploadProps extends Omit<Props, \"children\"> {\n  readonly upload: UploadFunction;\n\n  readonly files: ReadonlyArray<UploadFile>;\n\n  readonly onRemoveFile?: (id: string) => void;\n\n  readonly label?: string;\n\n  readonly emptyLabel?: string;\n\n  readonly retryLabel?: string;\n\n  readonly removeLabel?: string;\n  readonly className?: string;\n}\n\nexport interface FileUploadActions {\n  readonly onRetry: (id: string) => void;\n  readonly onRemove: (id: string) => void;\n}\n\nconst percent = (progress: number): number => Math.round(progress * 100);\n\nconst statusGlyph = (item: UploadItem, theme: Theme): ReactElement | null =>\n  Match.value(item.status).pipe(\n    Match.when(\"idle\", () => (\n      <Icon name=\"file\" color={theme.colors.mutedForeground} />\n    )),\n    Match.when(\"uploading\", () => null),\n    Match.when(\"done\", () => (\n      <Icon name=\"check\" color={theme.colors.success} />\n    )),\n    Match.when(\"error\", () => (\n      <Icon name=\"error\" color={theme.colors.destructive} />\n    )),\n    Match.exhaustive,\n  );\n\nconst renderRow = (\n  item: UploadItem,\n  theme: Theme,\n  actions: FileUploadActions,\n  retryLabel: string,\n  removeLabel: string,\n): ReactElement => (\n  <div\n    key={item.id}\n    style={{\n      display: \"flex\",\n      flexDirection: \"column\",\n      gap: theme.spacing.xs,\n      padding: theme.spacing.sm,\n      borderRadius: theme.radius.md,\n      borderWidth: theme.borderWidth.thin,\n      borderColor: theme.colors.border,\n    }}\n  >\n    <div\n      style={{\n        display: \"flex\",\n        flexDirection: \"row\",\n        alignItems: \"center\",\n        gap: theme.spacing.sm,\n      }}\n    >\n      {statusGlyph(item, theme)}\n      <text style={{ flexGrow: 1, color: theme.colors.foreground }}>\n        {item.name}\n      </text>\n      {item.status === \"error\" ? (\n        <Button\n          variant=\"secondary\"\n          size=\"sm\"\n          testId={`upload-retry-${item.id}`}\n          onActivate={() => actions.onRetry(item.id)}\n        >\n          {retryLabel}\n        </Button>\n      ) : null}\n      <IconButton\n        label={`${removeLabel} ${item.name}`}\n        testId={`upload-remove-${item.id}`}\n        onActivate={() => actions.onRemove(item.id)}\n        icon={<Icon name=\"trash\" color={theme.colors.mutedForeground} />}\n      />\n    </div>\n    {item.status === \"uploading\" ? (\n      <Progress\n        value={percent(item.progress)}\n        label={`Uploading ${item.name}`}\n      />\n    ) : null}\n  </div>\n);\n\nexport const renderFileUpload = (\n  props: FileUploadProps,\n  theme: Theme,\n  items: ReadonlyArray<UploadItem>,\n  actions: FileUploadActions,\n): ReactElement => {\n  const {\n    label,\n    emptyLabel,\n    retryLabel,\n    removeLabel,\n    className,\n    style,\n    ...rest\n  } = props;\n  const name = accessibleName(label, \"Upload files\");\n  const empty = accessibleName(emptyLabel, \"No files\");\n  const retry = accessibleName(retryLabel, \"Retry\");\n  const remove = accessibleName(removeLabel, \"Remove\");\n\n  return (\n    <div\n      {...rest}\n      role=\"group\"\n      aria-label={name}\n      style={mergeStyles(\n        recipeStyle(cn(\"flex flex-col gap-2\", className), theme),\n        style,\n      )}\n    >\n      {Arr.match(items, {\n        onEmpty: () => (\n          <text style={{ color: theme.colors.mutedForeground }}>{empty}</text>\n        ),\n        onNonEmpty: (rows) =>\n          Arr.map(rows, (item) =>\n            renderRow(item, theme, actions, retry, remove),\n          ),\n      })}\n    </div>\n  );\n};\n\nexport const FileUpload = (props: FileUploadProps): ReactElement => {\n  const theme = useTheme();\n  const { upload, files, onRemoveFile } = props;\n  const [queue, setQueue] = useState<UploadQueue>(emptyQueue);\n\n  const launch = useCallback(\n    (item: UploadItem): void => {\n      const attempt = Promise.resolve().then(() =>\n        upload({ id: item.id, name: item.name }, (progress) =>\n          setQueue((current) =>\n            reportProgress(current, item.id, item.token, progress),\n          ),\n        ),\n      );\n      attempt.then(\n        () => setQueue((current) => complete(current, item.id, item.token)),\n        () => setQueue((current) => fail(current, item.id, item.token)),\n      );\n    },\n    [upload],\n  );\n\n  useEffect(() => {\n    const requested = filesOf(files);\n    const retained = Arr.filter(queue.items, (item) =>\n      Arr.some(requested, (file) => file.id === item.id),\n    );\n    const seeded = enqueue(\n      { attempts: queue.attempts, items: retained },\n      requested,\n    );\n    const idle = Arr.filter(seeded.items, (item) => item.status === \"idle\");\n    const started = Arr.reduce(idle, seeded, (state, item) =>\n      start(state, item.id),\n    );\n    setQueue(started);\n    Arr.forEach(idle, (item) => {\n      Option.match(findItem(started, item.id), {\n        onNone: () => undefined,\n        onSome: (after) => launch(after),\n      });\n    });\n  }, [files, launch]);\n\n  const retry = (id: string): void => {\n    const before = findItem(queue, id);\n    const started = start(queue, id);\n    setQueue(started);\n    Option.match(findItem(started, id), {\n      onNone: () => undefined,\n      onSome: (after) => {\n        if (Option.exists(before, (item) => after.token > item.token)) {\n          launch(after);\n        }\n      },\n    });\n  };\n\n  const removeFile = (id: string): void => {\n    setQueue(remove(queue, id));\n    onRemoveFile?.(id);\n  };\n\n  return renderFileUpload(props, theme, queue.items, {\n    onRetry: retry,\n    onRemove: removeFile,\n  });\n};\n"
    },
    {
      "path": "packages/media/src/internal.ts",
      "target": "@ui/gpuixui/media/internal.ts",
      "type": "registry:lib",
      "content": "import type { StyleDesc } from \"@gpuix/react\";\nimport { classesToStyle } from \"../tw/index.js\";\nimport type { Theme } from \"../tokens/index.js\";\n\nexport { ThemeProvider, useTheme } from \"../tokens/react.js\";\nexport type { Theme } from \"../tokens/index.js\";\n\nexport const recipeStyle = (recipe: string, theme: Theme): StyleDesc =>\n  classesToStyle(recipe, theme).style;\n"
    },
    {
      "path": "packages/media/src/name.ts",
      "target": "@ui/gpuixui/media/name.ts",
      "type": "registry:lib",
      "content": "import * as Str from \"effect/String\";\n\nexport const accessibleName = (label: unknown, fallback: string): string => {\n  const text = typeof label === \"string\" ? Str.trim(label) : \"\";\n  return Str.isEmpty(text) ? fallback : text;\n};\n\nexport const PICTURE_NAME = \"Picture\";\n"
    },
    {
      "path": "packages/media/src/upload/queue.ts",
      "target": "@ui/gpuixui/media/upload/queue.ts",
      "type": "registry:lib",
      "content": "import * as Arr from \"effect/Array\";\nimport * as Option from \"effect/Option\";\nimport * as Schema from \"effect/Schema\";\nimport { accessibleName } from \"../name.js\";\n\nexport type UploadStatus = \"idle\" | \"uploading\" | \"done\" | \"error\";\n\nexport interface UploadFile {\n  readonly id: string;\n  readonly name: string;\n}\n\nexport interface UploadItem {\n  readonly id: string;\n  readonly name: string;\n  readonly status: UploadStatus;\n\n  readonly progress: number;\n\n  readonly token: number;\n}\n\nexport interface UploadQueue {\n  readonly items: ReadonlyArray<UploadItem>;\n\n  readonly attempts: number;\n}\n\nexport const emptyQueue: UploadQueue = { attempts: 0, items: [] };\n\nclass StoredFile extends Schema.Class<StoredFile>(\"StoredFile\")({\n  id: Schema.NonEmptyString,\n  name: Schema.optionalKey(Schema.NullOr(Schema.String)),\n}) {}\n\nconst decodeStoredFile = Schema.decodeUnknownOption(StoredFile);\n\nexport const filesOf = (value: unknown): ReadonlyArray<UploadFile> =>\n  Arr.isArray(value)\n    ? Arr.getSomes(\n        Arr.map(value, (entry) =>\n          Option.map(decodeStoredFile(entry), (file) => ({\n            id: file.id,\n            name: Option.getOrElse(\n              Option.fromNullishOr(file.name),\n              () => file.id,\n            ),\n          })),\n        ),\n      )\n    : [];\n\nconst withItems = (\n  state: UploadQueue,\n  items: ReadonlyArray<UploadItem>,\n): UploadQueue => ({ attempts: state.attempts, items });\n\nconst has = (state: UploadQueue, id: string): boolean =>\n  Arr.some(state.items, (item) => item.id === id);\n\nexport const enqueue = (\n  state: UploadQueue,\n  files: ReadonlyArray<UploadFile>,\n): UploadQueue =>\n  Arr.reduce(files, state, (queue, file) =>\n    has(queue, file.id)\n      ? queue\n      : withItems(\n          queue,\n          Arr.append(queue.items, {\n            id: file.id,\n\n            name: accessibleName(file.name, file.id),\n            status: \"idle\",\n            progress: 0,\n            token: 0,\n          }),\n        ),\n  );\n\nexport const findItem = (\n  state: UploadQueue,\n  id: string,\n): Option.Option<UploadItem> =>\n  Arr.findFirst(state.items, (item) => item.id === id);\n\nexport const start = (state: UploadQueue, id: string): UploadQueue => {\n  const [attempts, items] = Arr.mapAccum(\n    state.items,\n    state.attempts,\n    (attempts, item): readonly [number, UploadItem] =>\n      item.id === id && (item.status === \"idle\" || item.status === \"error\")\n        ? [\n            attempts + 1,\n            {\n              ...item,\n              status: \"uploading\",\n              progress: 0,\n              token: attempts + 1,\n            },\n          ]\n        : [attempts, item],\n  );\n  return { attempts, items };\n};\n\nconst clamp01 = (value: number): number => Math.min(1, Math.max(0, value));\n\nexport const reportProgress = (\n  state: UploadQueue,\n  id: string,\n  token: number,\n  progress: number,\n): UploadQueue =>\n  withItems(\n    state,\n    Arr.map(state.items, (item) => {\n      if (\n        item.id !== id ||\n        item.token !== token ||\n        item.status !== \"uploading\"\n      ) {\n        return item;\n      }\n      const next = clamp01(progress);\n      return Number.isNaN(next) || next <= item.progress\n        ? item\n        : { ...item, progress: next };\n    }),\n  );\n\nexport const complete = (\n  state: UploadQueue,\n  id: string,\n  token: number,\n): UploadQueue =>\n  withItems(\n    state,\n    Arr.map(state.items, (item) =>\n      item.id === id && item.token === token && item.status === \"uploading\"\n        ? { ...item, status: \"done\", progress: 1 }\n        : item,\n    ),\n  );\n\nexport const fail = (\n  state: UploadQueue,\n  id: string,\n  token: number,\n): UploadQueue =>\n  withItems(\n    state,\n    Arr.map(state.items, (item) =>\n      item.id === id && item.token === token && item.status === \"uploading\"\n        ? { ...item, status: \"error\" }\n        : item,\n    ),\n  );\n\nexport const remove = (state: UploadQueue, id: string): UploadQueue =>\n  withItems(\n    state,\n    Arr.filter(state.items, (item) => item.id !== id),\n  );\n"
    }
  ],
  "docs": "[FileUpload](https://gpuixui.sudokw.com/media/file-upload) — These are GPUix native components: the files render through GPUix's reconciler against `@gpuix/react`, not the DOM, and need the per-platform `@gpuix/native` binding. The CLI installs the `@gpuixui/*` runtime the components import as registry items, so the copied tree resolves on its own; wrap it in `ThemeProvider` before it renders.",
  "meta": {
    "url": "/media/file-upload",
    "kind": "component",
    "runtime": "gpuix",
    "registry": "gpuixui"
  }
}