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
36 for (const imageId of input.imageIds) {
37 const upload = input.uploadsByImageId[imageId];
38 // Send retries failed uploads, so they must not disable it.
39 if (upload?.status === "failed" && upload.environmentId === input.environmentId) continue;
40 if (upload?.status !== "ready" || upload.environmentId !== input.environmentId) {
41 pending += 1;
42 }
43 }
44
45 if (pending > 0) {
46 return pending === 1 ? "Attachment still uploading" : "Attachments still uploading";
47 }
48 return null;
49}
50
51export function formatAttachmentUploadProgress(progress: number): string {
52 const bounded = Math.max(0, Math.min(1, Number.isFinite(progress) ? progress : 0));
53 return `${Math.floor(bounded * 100)}%`;
54}