Object Card
Card for one stored file, with download and delete actions and their progress.
Show a file you already stored, such as an attachment on a record.
ObjectCard renders its name, size, and type, with a download action and a
delete action. It shows the progress of each.
For a whole folder of files, use Object List.
Installation
import { ObjectCard } from "@dimah-s3/ui";Complete Installation first: the package or registry
setup, the stylesheet, and one <Toaster /> in your layout.
Usage
"use client";
import { useDelete, useFetchDownload } from "@dimah-s3/react";
import { DeleteToasts, DownloadToasts, ObjectCard } from "@dimah-s3/ui";
type Attachment = { key: string; size: number; type: string };
export function Attachments({
files,
onRemoved,
}: {
files: Attachment[];
onRemoved: (key: string) => void;
}) {
const download = useFetchDownload({ route: "attachments" });
const remove = useDelete({ route: "attachments" });
return (
<>
{files.map((file) => (
<ObjectCard
key={file.key}
objectKey={file.key}
fileSize={file.size}
contentType={file.type}
download={download}
delete={remove}
onDeleted={(result) => {
if (result.deleted.includes(file.key)) onRemoved(file.key);
}}
/>
))}
<DownloadToasts download={download} />
<DeleteToasts delete={remove} />
</>
);
}- Pass only the hooks for the actions you want. Many cards share one hook.
- The card stays after a delete. Remove it in
onDeleted. - Mount
DownloadToastsandDeleteToastsonce for the hooks. - With
useFetchDownload, the card shows the download progress and the action cancels.
Examples
Your own actions
actions render before the built-in ones. This Copy link action signs a URL
with the same download hook.
API Reference
Also accepts the FileAttachment props it
does not set itself, such as description, previewUrl, actions, and
trigger.
| Prop | Type | Default | Description |
|---|---|---|---|
objectKey | string | required | Key of the stored object |
fileName | string | Last segment of key | Name shown, and the name the browser saves |
fileSize | number | null | — | Size in bytes, shown on the second line |
contentType | string | null | — | MIME type. Picks the type glyph without a preview |
previewUrl | string | null | — | Image thumbnail in place of the glyph |
download | AnyDownloadReturn | — | A useDownload or useFetchDownload return. Adds the download action |
delete | UseDeleteReturn | — | Adds the delete action and its confirm dialog |
onDeleted | (result: DeleteResult) => void | — | Runs once a confirmed delete settles |
description | ReactNode | The size | Replaces the second line |
actions | ReactNode | — | Your own AttachmentActions, before the built-in ones |