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

ComposerAttachmentStrip.tsx

106 lines3.7 KB

1import { SymbolView } from "../components/AppSymbol";
2import { Image, Pressable, ScrollView, View } from "react-native";
3​
4import { AppText as Text } from "./AppText";
5import type { DraftComposerAttachment } from "../lib/composerImages";
6​
7export interface ComposerAttachmentStripProps {
8 /** Attachments to display. */
9 readonly attachments: ReadonlyArray<DraftComposerAttachment>;
10 /** Called when the user removes an attachment. */
11 readonly onRemove: (imageId: string) => void;
12 /** Called when the user taps on an image thumbnail to preview it. */
13 readonly onPressImage?: (previewUri: string) => void;
14 /** Image thumbnail size in points. Defaults to 72. */
15 readonly imageSize?: number;
16 /** Border radius of each image thumbnail. Defaults to 16. */
17 readonly imageBorderRadius?: number;
18 /** Whether the remove button should sit in its own gutter instead of overlapping the image. */
19 readonly removeButtonPlacement?: "overlay" | "gutter";
21​
22/**
23 * Attachment thumbnails used by the thread composer and the new-task draft screen.
24 */
25export function ComposerAttachmentStrip(props: ComposerAttachmentStripProps) {
26 const size = props.imageSize ?? 72;
27 const radius = props.imageBorderRadius ?? 16;
28 const removeButtonPlacement = props.removeButtonPlacement ?? "overlay";
29 const removeButtonGutter = removeButtonPlacement === "gutter" ? 10 : 0;
30​
31 if (props.attachments.length === 0) {
32 return null;
33 }
34​
35 return (
36 <ScrollView
37 horizontal
38 showsHorizontalScrollIndicator={false}
39 keyboardShouldPersistTaps="always"
40 className="grow-0"
41 >
42 <View className="flex-row gap-2.5">
43 {props.attachments.map((attachment) => (
44 <View
45 key={attachment.id}
46 className="relative"
47 style={{
48 paddingTop: removeButtonGutter,
49 paddingRight: removeButtonGutter,
50 }}
51 >
52 {attachment.type === "image" ? (
53 <Pressable
54 onPress={
55 props.onPressImage ? () => props.onPressImage!(attachment.previewUri) : undefined
56 }
57 >
58 <Image
59 source={{ uri: attachment.previewUri }}
60 style={{
61 width: size,
62 height: size,
63 borderRadius: radius,
64 }}
65 className="bg-subtle"
66 resizeMode="cover"
67 />
68 </Pressable>
69 ) : (
70 <View
71 className="items-center justify-center gap-1 bg-subtle px-2"
72 style={{
73 width: size,
74 height: size,
75 borderRadius: radius,
76 }}
77 >
78 <SymbolView name="doc.text" size={22} tintColor="#a3a3a3" type="monochrome" />
79 <Text className="w-full text-center text-2xs text-foreground" numberOfLines={1}>
80 {attachment.name}
81 </Text>
82 </View>
83 )}
84 <Pressable
85 className="absolute h-[22px] w-[22px] items-center justify-center rounded-[11px] bg-black/55"
86 style={{
87 top: removeButtonPlacement === "gutter" ? 0 : 4,
88 right: removeButtonPlacement === "gutter" ? 0 : 4,
89 }}
90 hitSlop={6}
91 onPress={() => props.onRemove(attachment.id)}
92 >
93 <SymbolView
94 name="xmark"
95 size={9}
96 tintColor="#ffffff"
97 type="monochrome"
98 weight="bold"
99 />
100 </Pressable>
101 </View>
102 ))}
103 </View>
104 </ScrollView>
105 );