ComponentsFiles
Object Card
Card for one stored file, with download and delete actions.
ObjectCard shows a file you already stored, such as an attachment on a
record, with a download and a delete action. Pass the object key and the
hooks for the actions you want. While its file downloads it shows the
progress, and while it is deleted, a spinner.
Preview
Download a file to see its progress, or delete it.
Install
Complete UI Setup first: the stylesheet and one
<Toaster />. Then add this component:
import { ObjectCard } from "@dimah-s3/ui";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} />
</>
);
}- The name defaults to the last segment of the key. Pass
fileNameto show, and save, another one. downloadtakesuseDownloadoruseFetchDownload. With a fetch download the card shows its progress ring and the bytes received.deleteadds the delete action and its confirm dialog. The card stays until you remove it, so drop it inonDeleted.- Pass
previewUrlfor an image thumbnail, anddescriptionto replace the size line. - Each card is its own
DownloadandDeleteroot, and does not toast: mountDownloadToastsandDeleteToastsonce for the hooks.
Examples
Your own actions
actions render before the download and delete actions. Here a Copy link
action signs a URL with the same download hook.
Props
import type { ObjectCardProps } from "@dimah-s3/ui";Prop
Type