Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ComposerAttachmentStrip.tsx

261 lines8.8 KB

1import { SymbolView } from "../components/AppSymbol";
2import { videoMimeType } from "@t3tools/shared/video";
3import { useMemo } from "react";
4import { Image, Pressable, ScrollView, View } from "react-native";
5​
6import { AppText as Text } from "./AppText";
7import type { DraftComposerAttachment, DraftComposerFileAttachment } from "../lib/composerImages";
8import { VideoAttachmentTile } from "./VideoAttachmentTile";
9import type { MediaActionsSource } from "../lib/mediaActions";
10import { PresentationSource } from "./NativePresentation";
11import type { FilePreviewSource } from "./FilePreviewModal";
12import { isPdfFile } from "../lib/filePreview";
13import type { EnvironmentId } from "@t3tools/contracts";
14import {
15 retryComposerAttachmentUpload,
16 useComposerAttachmentUploadState,
17} from "../state/composer-attachment-uploads";
18​
19export interface ComposerAttachmentStripProps {
20 readonly environmentId?: EnvironmentId;
21 /** Attachments to display. */
22 readonly attachments: ReadonlyArray<DraftComposerAttachment>;
23 /** Called when the user removes an attachment. */
24 readonly onRemove: (imageId: string) => void;
25 /** Called when the user taps an image or PDF to preview it. */
26 readonly onPressPreview?: (source: FilePreviewSource) => void;
27 readonly onPressVideo?: (
28 attachment: DraftComposerFileAttachment,
29 sourceIdentifier: string,
30 ) => void;
31 /** Image thumbnail size in points. Defaults to 72. */
32 readonly imageSize?: number;
33 /** Border radius of each image thumbnail. Defaults to 16. */
34 readonly imageBorderRadius?: number;
35 /** Whether the remove button should sit in its own gutter instead of overlapping the image. */
36 readonly removeButtonPlacement?: "overlay" | "gutter";
38​
39type ComposerAttachmentThumbnailProps = {
40 readonly environmentId?: EnvironmentId;
41 readonly attachment: DraftComposerAttachment;
42 readonly size: number;
43 readonly borderRadius: number;
44 readonly compact?: boolean;
45 readonly onPressPreview?: (source: FilePreviewSource) => void;
46 readonly onPressVideo?: (
47 attachment: DraftComposerFileAttachment,
48 sourceIdentifier: string,
49 ) => void;
50};
51​
52export function ComposerAttachmentThumbnail(props: ComposerAttachmentThumbnailProps) {
53 const upload = useComposerAttachmentUploadState(props.environmentId, props.attachment.id);
54 return (
55 <View style={{ width: props.size, height: props.size }}>
56 <ComposerAttachmentContent {...props} />
57 {upload && upload.status !== "ready" ? (
58 <Pressable
59 accessibilityRole={upload.status === "failed" ? "button" : "text"}
60 accessibilityLabel={
61 upload.status === "failed"
62 ? `Retry uploading ${props.attachment.name}`
63 : `Uploading ${props.attachment.name}, ${Math.floor(upload.progress * 100)}%`
64 }
65 accessibilityHint={upload.status === "failed" ? upload.reason : undefined}
66 disabled={upload.status !== "failed"}
67 onPress={() =>
68 props.environmentId &&
69 retryComposerAttachmentUpload(props.environmentId, props.attachment.id)
70 }
71 className="absolute bottom-0.5 left-0.5 flex-row items-center gap-0.5 rounded-full bg-black/70 px-1 py-0.5"
72 >
73 <SymbolView
74 name={upload.status === "failed" ? "arrow.clockwise" : "arrow.up"}
75 size={props.compact ? 8 : 10}
76 tintColor="#ffffff"
77 type="monochrome"
78 />
79 {!props.compact ? (
80 <Text className="text-2xs text-white">
81 {upload.status === "failed" ? "Retry" : `${Math.floor(upload.progress * 100)}%`}
82 </Text>
83 ) : null}
84 </Pressable>
85 ) : null}
86 </View>
87 );
89​
90function ComposerAttachmentContent(props: ComposerAttachmentThumbnailProps) {
91 const { attachment } = props;
92 const style = { width: props.size, height: props.size, borderRadius: props.borderRadius };
93 if (attachment.type === "image") {
94 const sourceIdentifier = `draft-image:${attachment.id}`;
95 return (
96 <PresentationSource identifier={sourceIdentifier}>
97 <Pressable
98 accessibilityRole="imagebutton"
99 accessibilityLabel={`Open ${attachment.name}`}
100 disabled={!props.onPressPreview}
101 onPress={() =>
102 props.onPressPreview?.({
103 kind: "image",
104 uri: attachment.dataUrl,
105 name: attachment.name,
106 sourceIdentifier,
107 })
108 }
109 >
110 <Image
111 source={{ uri: attachment.previewUri }}
112 style={style}
113 className="bg-subtle"
114 resizeMode="cover"
115 />
116 </Pressable>
117 </PresentationSource>
118 );
119 }
120 const onPressVideo = props.onPressVideo;
121 if (onPressVideo && videoMimeType(attachment) !== null) {
122 return (
123 <ComposerVideoAttachment {...props} attachment={attachment} onPressVideo={onPressVideo} />
124 );
125 }
126 const canPreview = isPdfFile(attachment) && props.onPressPreview !== undefined;
127 const sourceIdentifier = `draft-file:${attachment.id}`;
128 return (
129 <PresentationSource identifier={sourceIdentifier}>
130 <Pressable
131 accessibilityRole="button"
132 accessibilityLabel={`Open ${attachment.name}`}
133 disabled={!canPreview}
134 onPress={() =>
135 props.onPressPreview?.({
136 kind: "pdf",
137 name: attachment.name,
138 attachment,
139 sourceIdentifier,
140 })
141 }
142 className={
143 props.compact
144 ? "items-center justify-center bg-subtle"
145 : "items-center justify-center gap-1 bg-subtle px-2"
146 }
147 style={style}
148 >
149 <SymbolView
150 name="doc.text"
151 size={props.compact ? 15 : 22}
152 tintColor="#a3a3a3"
153 type="monochrome"
154 />
155 {!props.compact ? (
156 <Text className="w-full text-center text-2xs text-foreground" numberOfLines={1}>
157 {attachment.name}
158 </Text>
159 ) : null}
160 </Pressable>
161 </PresentationSource>
162 );
164​
165function ComposerVideoAttachment(props: {
166 readonly attachment: DraftComposerFileAttachment;
167 readonly size: number;
168 readonly borderRadius: number;
169 readonly compact?: boolean;
170 readonly onPressVideo: (
171 attachment: DraftComposerFileAttachment,
172 sourceIdentifier: string,
173 ) => void;
174}) {
175 const { attachment } = props;
176 const sourceIdentifier = `draft:${attachment.id}`;
177 const style = { width: props.size, height: props.size, borderRadius: props.borderRadius };
178 const actionsSource = useMemo<MediaActionsSource>(
179 () => ({
180 name: attachment.name,
181 mimeType: videoMimeType(attachment) ?? attachment.mimeType,
182 sourceIdentifier,
183 attachment,
184 }),
185 [attachment, sourceIdentifier],
186 );
187​
188 return (
189 <VideoAttachmentTile
190 name={attachment.name}
191 sourceIdentifier={sourceIdentifier}
192 thumbnailSource={attachment}
193 compact={props.compact}
194 onPress={() => props.onPressVideo(attachment, sourceIdentifier)}
195 actionsSource={actionsSource}
196 style={style}
197 />
198 );
200​
201/**
202 * Attachment thumbnails used by the thread composer and the new-task draft screen.
203 */
204export function ComposerAttachmentStrip(props: ComposerAttachmentStripProps) {
205 const size = props.imageSize ?? 72;
206 const radius = props.imageBorderRadius ?? 16;
207 const removeButtonPlacement = props.removeButtonPlacement ?? "overlay";
208 const removeButtonGutter = removeButtonPlacement === "gutter" ? 10 : 0;
209​
210 if (props.attachments.length === 0) {
211 return null;
212 }
213​
214 return (
215 <ScrollView
216 horizontal
217 showsHorizontalScrollIndicator={false}
218 keyboardShouldPersistTaps="always"
219 className="grow-0"
220 >
221 <View className="flex-row gap-2.5">
222 {props.attachments.map((attachment) => (
223 <View
224 key={attachment.id}
225 className="relative"
226 style={{
227 paddingTop: removeButtonGutter,
228 paddingRight: removeButtonGutter,
229 }}
230 >
231 <ComposerAttachmentThumbnail
232 environmentId={props.environmentId}
233 attachment={attachment}
234 size={size}
235 borderRadius={radius}
236 onPressPreview={props.onPressPreview}
237 onPressVideo={props.onPressVideo}
238 />
239 <Pressable
240 className="absolute h-[22px] w-[22px] items-center justify-center rounded-[11px] bg-black/55"
241 style={{
242 top: removeButtonPlacement === "gutter" ? 0 : 4,
243 right: removeButtonPlacement === "gutter" ? 0 : 4,
244 }}
245 hitSlop={6}
246 onPress={() => props.onRemove(attachment.id)}
247 >
248 <SymbolView
249 name="xmark"
250 size={9}
251 tintColor="#ffffff"
252 type="monochrome"
253 weight="bold"
254 />
255 </Pressable>
256 </View>
257 ))}
258 </View>
259 </ScrollView>
260 );