Open History to see the last commit
1import { EnvironmentId, type ServerConfig } from "@t3tools/contracts";
2import { clampFileAttachmentUploadBytes } from "@t3tools/client-runtime/state/attachments";
3import type { AttachmentUploadStatus } from "@t3tools/client-runtime/state/attachment-uploads";
4import { msg } from "gt-react-native";
5import { m, type MessageResolver } from "../i18n/messages";
6
7import { parseLegacyNewTaskDraftKey } from "../state/new-task-draft-key";
8import type { DraftComposerAttachment } from "./composerImages";
9
10export interface ComposerAttachmentUploadRequest {
11 readonly environmentId: EnvironmentId;
12 readonly attachment: DraftComposerAttachment;
13}
14
15export type ComposerAttachmentUploadState = AttachmentUploadStatus;
16
17export function composerAttachmentUploadKey(
18 environmentId: EnvironmentId,
19 attachmentId: string,
20): string {
21 return `${environmentId}:${attachmentId}`;
22}
23
24/** Environment and thread ids may both contain colons, so a thread draft key resolves to the longest known environment it starts with. */
25export function composerDraftEnvironmentId(
26 draftKey: string,
27 environmentIds: Iterable<EnvironmentId>,
28 queuedMessages: ReadonlyArray<{
29 readonly messageId: string;
30 readonly environmentId: EnvironmentId;
31 }>,
32 draft?: { readonly project?: { readonly environmentId: EnvironmentId } },
33): EnvironmentId | null {
34 if (draftKey.startsWith("pending-task:")) {
35 return (
36 queuedMessages.find((message) => `pending-task:${message.messageId}` === draftKey)
37 ?.environmentId ?? null
38 );
39 }
40 if (draftKey.startsWith("new-task:")) {
41 if (draft?.project) {
42 return draft.project.environmentId;
43 }
44 const legacy = parseLegacyNewTaskDraftKey(draftKey);
45 return legacy === null ? null : EnvironmentId.make(legacy.environmentId);
46 }
47 let match: EnvironmentId | null = null;
48 for (const environmentId of environmentIds) {
49 if (
50 draftKey.startsWith(`${environmentId}:`) &&
51 (match === null || environmentId.length > match.length)
52 ) {
53 match = environmentId;
54 }
55 }
56 return match;
57}
58
59type UploadServerConfig = {
60 readonly environment: {
61 readonly capabilities: Pick<ServerConfig["environment"]["capabilities"], "fileAttachments">;
62 };
63};
64
65export function canUploadComposerAttachment(
66 attachment: DraftComposerAttachment,
67 config: UploadServerConfig | null | undefined,
68): boolean {
69 const capabilities = config?.environment.capabilities;
70 return (
71 capabilities !== undefined &&
72 (attachment.type === "image" ||
73 (capabilities.fileAttachments !== undefined &&
74 attachment.sizeBytes <=
75 clampFileAttachmentUploadBytes(capabilities.fileAttachments.maxUploadBytes)))
76 );
77}
78
79/** Render callers pass `useMessages()` so the reason follows a language switch. */
80export function composerAttachmentUploadBlockReason(
81 input: {
82 readonly environmentId: EnvironmentId;
83 readonly attachments: ReadonlyArray<DraftComposerAttachment>;
84 readonly connected: boolean;
85 readonly serverConfig: UploadServerConfig | null;
86 readonly states: Readonly<Record<string, ComposerAttachmentUploadState>>;
87 },
88 resolve: MessageResolver = m,
89): string | null {
90 if (!input.connected) return null;
91 for (const attachment of input.attachments) {
92 if (!canUploadComposerAttachment(attachment, input.serverConfig)) continue;
93 const state = input.states[composerAttachmentUploadKey(input.environmentId, attachment.id)];
94 if (state?.status === "failed") return resolve(msg("Retry or remove the failed attachment"));
95 }
96 return null;
97}
98
99export function composerAttachmentsStillUploading(input: {
100 readonly environmentId: EnvironmentId;
101 readonly attachments: ReadonlyArray<DraftComposerAttachment>;
102 readonly serverConfig: UploadServerConfig | null;
103 readonly states: Readonly<Record<string, ComposerAttachmentUploadState>>;
104}): boolean {
105 return input.attachments.some((attachment) => {
106 if (!canUploadComposerAttachment(attachment, input.serverConfig)) return false;
107 const state = input.states[composerAttachmentUploadKey(input.environmentId, attachment.id)];
108 return state?.status !== "ready" && state?.status !== "failed";
109 });
110}