Open History to see the last commit
1import type { EnvironmentId } from "@t3tools/contracts";
2
3export type ReadyAttachmentUpload = {
4 readonly status: "ready";
5 readonly environmentId: EnvironmentId;
6 readonly attachmentId: string;
7};
8
9export type AttachmentUploadState =
10 | {
11 readonly status: "uploading";
12 readonly environmentId: EnvironmentId;
13 readonly progress: number;
14 readonly previous?: ReadyAttachmentUpload;
15 }
16 | ReadyAttachmentUpload
17 | {
18 readonly status: "failed";
19 readonly environmentId: EnvironmentId;
20 readonly reason: string;
21 /**
22 * Retry and release delete it, so it must never carry a draft's persisted
23 * attachment id.
24 */
25 readonly attachmentId?: string;
26 readonly previous?: ReadyAttachmentUpload;
27 };
28
29export function attachmentUploadBlockReason(input: {
30 readonly imageIds: ReadonlyArray<string>;
31 readonly uploadsByImageId: Readonly<Record<string, AttachmentUploadState>>;
32 readonly environmentId: EnvironmentId;
33}): string | null {
34 let pending = 0;
35 let failed = 0;
36
37 for (const imageId of input.imageIds) {
38 const upload = input.uploadsByImageId[imageId];
39 if (upload?.status === "failed" && upload.environmentId === input.environmentId) {
40 failed += 1;
41 } else if (upload?.status !== "ready" || upload.environmentId !== input.environmentId) {
42 pending += 1;
43 }
44 }
45
46 if (failed > 0) {
47 return failed === 1
48 ? "Retry or remove the failed attachment"
49 : "Retry or remove the failed attachments";
50 }
51 if (pending > 0) {
52 return pending === 1 ? "Attachment still uploading" : "Attachments still uploading";
53 }
54 return null;
55}
56
57export function formatAttachmentUploadProgress(progress: number): string {
58 const bounded = Math.max(0, Math.min(1, Number.isFinite(progress) ? progress : 0));
59 return `${Math.floor(bounded * 100)}%`;
60}