import { toast } from "sonner"; export interface MediaUploadToastResult { uploadedCount: number; assetNames?: string[]; } function getAssetLabel({ count }: { count: number }): string { return count === 1 ? "media asset" : "media assets"; } function waitForNextPaint(): Promise { return new Promise((resolve) => { requestAnimationFrame(() => { requestAnimationFrame(() => resolve()); }); }); } export async function showMediaUploadToast({ filesCount, promise, }: { filesCount: number; promise: Promise | (() => Promise); }) { const run = typeof promise === "function" ? promise : () => promise; const toastPromise = toast.promise(async () => { await waitForNextPaint(); return run(); }, { loading: `Uploading ${getAssetLabel({ count: filesCount })}...`, success: ({ uploadedCount, assetNames }) => { if (uploadedCount === 1) { const assetName = assetNames?.[0]; return assetName ? `${assetName} has been uploaded` : "1 media asset has been uploaded"; } if (uploadedCount > 1) { return `${uploadedCount} media assets have been uploaded`; } return "No media assets were uploaded"; }, error: `Failed to upload ${getAssetLabel({ count: filesCount })}`, }); return toastPromise.unwrap(); }