The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { EnvironmentId } from "@t3tools/contracts"; |
| 2 | import { msg } from "gt-react"; |
| 3 | |
| 4 | import { m, type MessageResolver } from "../i18n/messages"; |
| 5 | |
| 6 | export type ReadyAttachmentUpload = { |
| 7 | readonly status: "ready"; |
| 8 | readonly environmentId: EnvironmentId; |
| 9 | readonly attachmentId: string; |
| 10 | }; |
| 11 | |
| 12 | export type AttachmentUploadState = |
| 13 | | { |
| 14 | readonly status: "uploading"; |
| 15 | readonly environmentId: EnvironmentId; |
| 16 | readonly progress: number; |
| 17 | readonly previous?: ReadyAttachmentUpload; |
| 18 | } |
| 19 | | ReadyAttachmentUpload |
| 20 | | { |
| 21 | readonly status: "failed"; |
| 22 | readonly environmentId: EnvironmentId; |
| 23 | readonly reason: string; |
| 24 | /** `reason` as a `msg()`, so the composer shows it in the current language. */ |
| 25 | readonly reasonMessage?: string; |
| 26 | /** Retry/release deletes this id; never use a persisted draft attachment id. */ |
| 27 | readonly attachmentId?: string; |
| 28 | readonly previous?: ReadyAttachmentUpload; |
| 29 | }; |
| 30 | |
| 31 | export function attachmentUploadFailureText( |
| 32 | upload: Extract<AttachmentUploadState, { status: "failed" }>, |
| 33 | resolve: MessageResolver, |
| 34 | ): string { |
| 35 | return upload.reasonMessage === undefined ? upload.reason : resolve(upload.reasonMessage); |
| 36 | } |
| 37 | |
| 38 | export function attachmentUploadBlockReason( |
| 39 | input: { |
| 40 | readonly imageIds: ReadonlyArray<string>; |
| 41 | readonly uploadsByImageId: Readonly<Record<string, AttachmentUploadState>>; |
| 42 | readonly environmentId: EnvironmentId; |
| 43 | }, |
| 44 | resolve: MessageResolver = m, |
| 45 | ): string | null { |
| 46 | let pending = 0; |
| 47 | |
| 48 | for (const imageId of input.imageIds) { |
| 49 | const upload = input.uploadsByImageId[imageId]; |
| 50 | // Send retries failed uploads, so they must not disable it. |
| 51 | if (upload?.status === "failed" && upload.environmentId === input.environmentId) continue; |
| 52 | if (upload?.status !== "ready" || upload.environmentId !== input.environmentId) { |
| 53 | pending += 1; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | if (pending > 0) { |
| 58 | return pending === 1 |
| 59 | ? resolve(msg("Attachment still uploading")) |
| 60 | : resolve(msg("Attachments still uploading")); |
| 61 | } |
| 62 | return null; |
| 63 | } |
| 64 | |
| 65 | export function formatAttachmentUploadProgress(progress: number): string { |
| 66 | const bounded = Math.max(0, Math.min(1, Number.isFinite(progress) ? progress : 0)); |
| 67 | return `${Math.floor(bounded * 100)}%`; |
| 68 | } |