Skip to content

Commit 6bead6f

Browse files
Crypto-Virusclaude
andcommitted
feat(mobile): Add long press to copy message text
Long press on user or assistant message bubbles opens a form sheet with selectable text and a "Copy all" button. Extracts a reusable createExternalStore utility and refactors both message copy and review comment stores to use it. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
1 parent acdfb6a commit 6bead6f

7 files changed

Lines changed: 298 additions & 107 deletions

File tree

‎apps/mobile/src/app/threads/[environmentId]/[threadId]/_layout.tsx‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,17 @@ export default function ThreadLayout() {
7575
headerShadowVisible: false,
7676
}}
7777
/>
78+
<Stack.Screen
79+
name="message-copy"
80+
options={{
81+
contentStyle: { backgroundColor: "transparent" },
82+
gestureEnabled: true,
83+
headerShown: false,
84+
presentation: "formSheet" as const,
85+
sheetAllowedDetents: [0.5, 1.0],
86+
sheetGrabberVisible: true,
87+
}}
88+
/>
7889
</Stack>
7990
);
8091
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { MessageCopySheet } from "../../../../features/threads/MessageCopySheet";
2+
3+
export default function MessageCopyRoute() {
4+
return <MessageCopySheet />;
5+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import * as Clipboard from "expo-clipboard";
2+
import * as Haptics from "expo-haptics";
3+
import { useNavigation, useRouter } from "expo-router";
4+
import { SymbolView } from "expo-symbols";
5+
import { useCallback, useEffect, useRef, useState } from "react";
6+
import { Pressable, ScrollView, TextInput, View } from "react-native";
7+
import Animated, { FadeIn, FadeOut } from "react-native-reanimated";
8+
import { useSafeAreaInsets } from "react-native-safe-area-context";
9+
10+
import { AppText as Text } from "../../components/AppText";
11+
import { useThemeColor } from "../../lib/useThemeColor";
12+
import { clearMessageCopyText, useMessageCopyText } from "./messageCopySelection";
13+
14+
export function MessageCopySheet() {
15+
const router = useRouter();
16+
const navigation = useNavigation();
17+
const insets = useSafeAreaInsets();
18+
const messageText = useMessageCopyText();
19+
const textColor = useThemeColor("--color-md-body");
20+
const primaryBg = useThemeColor("--color-primary");
21+
const primaryFg = useThemeColor("--color-primary-foreground");
22+
const selectionColor = useThemeColor("--color-primary");
23+
const [copied, setCopied] = useState(false);
24+
const dismissTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
25+
26+
useEffect(() => {
27+
const unsubscribe = navigation.addListener("beforeRemove", () => {
28+
if (dismissTimerRef.current) {
29+
clearTimeout(dismissTimerRef.current);
30+
dismissTimerRef.current = null;
31+
}
32+
clearMessageCopyText();
33+
});
34+
return unsubscribe;
35+
}, [navigation]);
36+
37+
useEffect(() => {
38+
return () => {
39+
if (dismissTimerRef.current) {
40+
clearTimeout(dismissTimerRef.current);
41+
}
42+
};
43+
}, []);
44+
45+
const onCopyAll = useCallback(() => {
46+
if (messageText) {
47+
void Clipboard.setStringAsync(messageText);
48+
void Haptics.notificationAsync(Haptics.NotificationFeedbackType.Success);
49+
setCopied(true);
50+
dismissTimerRef.current = setTimeout(() => {
51+
dismissTimerRef.current = null;
52+
router.back();
53+
}, 600);
54+
}
55+
}, [messageText, router]);
56+
57+
if (!messageText) {
58+
return null;
59+
}
60+
61+
const iconName = copied ? "checkmark.circle.fill" : "doc.on.doc";
62+
const label = copied ? "Copied" : "Copy all";
63+
const tint = copied ? "#fff" : primaryFg;
64+
65+
return (
66+
<View style={{ flex: 1 }}>
67+
<ScrollView
68+
style={{ flex: 1 }}
69+
contentContainerStyle={{ padding: 20, gap: 12 }}
70+
keyboardShouldPersistTaps="handled"
71+
bounces
72+
>
73+
<TextInput
74+
multiline
75+
editable={false}
76+
scrollEnabled={false}
77+
value={messageText}
78+
selectionColor={selectionColor as string}
79+
dataDetectorTypes="none"
80+
style={{
81+
fontSize: 15,
82+
lineHeight: 22,
83+
fontFamily: "DMSans_400Regular",
84+
color: textColor as string,
85+
padding: 0,
86+
}}
87+
/>
88+
</ScrollView>
89+
90+
<View
91+
style={{
92+
paddingHorizontal: 20,
93+
paddingTop: 12,
94+
paddingBottom: Math.max(insets.bottom, 18) + 8,
95+
}}
96+
>
97+
<Pressable
98+
onPress={onCopyAll}
99+
disabled={copied}
100+
style={{
101+
minHeight: 48,
102+
flexDirection: "row",
103+
alignItems: "center",
104+
justifyContent: "center",
105+
gap: 8,
106+
borderRadius: 18,
107+
borderCurve: "continuous",
108+
backgroundColor: copied ? "#34d399" : primaryBg,
109+
}}
110+
>
111+
<Animated.View
112+
key={copied ? "copied" : "default"}
113+
entering={FadeIn.duration(200)}
114+
exiting={FadeOut.duration(150)}
115+
style={{ flexDirection: "row", alignItems: "center", gap: 8 }}
116+
>
117+
<SymbolView
118+
name={iconName}
119+
size={16}
120+
tintColor={tint}
121+
type="monochrome"
122+
animationSpec={copied ? { effect: { type: "bounce", direction: "up" } } : undefined}
123+
/>
124+
<Text
125+
className="text-[12px] font-t3-bold uppercase"
126+
style={{ color: tint, letterSpacing: 0.9 }}
127+
>
128+
{label}
129+
</Text>
130+
</Animated.View>
131+
</Pressable>
132+
</View>
133+
</View>
134+
);
135+
}

‎apps/mobile/src/features/threads/ThreadFeed.tsx‎

Lines changed: 96 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as Clipboard from "expo-clipboard";
22
import * as Haptics from "expo-haptics";
33
import { KeyboardAvoidingLegendList } from "@legendapp/list/keyboard";
44
import { type LegendListRef } from "@legendapp/list/react-native";
5+
import { useLocalSearchParams, useRouter } from "expo-router";
56
import { SymbolView } from "expo-symbols";
67
import { memo, useCallback, useEffect, useMemo, useRef, useState } from "react";
78
import {
@@ -22,6 +23,7 @@ import { TouchableOpacity } from "react-native-gesture-handler";
2223
import ImageViewing from "react-native-image-viewing";
2324
import { useSafeAreaInsets } from "react-native-safe-area-context";
2425
import { useThemeColor } from "../../lib/useThemeColor";
26+
import { setMessageCopyText } from "./messageCopySelection";
2527

2628
import { AppText as Text } from "../../components/AppText";
2729
import { EmptyState } from "../../components/EmptyState";
@@ -325,6 +327,7 @@ function renderFeedEntry(
325327
readonly expandedWorkGroups: Record<string, boolean>;
326328
readonly onCopyWorkRow: (rowId: string, value: string) => void;
327329
readonly onToggleWorkGroup: (groupId: string) => void;
330+
readonly onLongPressMessage: (text: string) => void;
328331
readonly onPressImage: (uri: string, headers?: Record<string, string>) => void;
329332
readonly iconSubtleColor: string | import("react-native").ColorValue;
330333
readonly markdownStyles: MarkdownStyleSets;
@@ -343,41 +346,43 @@ function renderFeedEntry(
343346
if (isUser) {
344347
return (
345348
<View className="mb-3.5 items-end gap-1.5">
346-
<View className="max-w-[85%] gap-2 rounded-[22px] rounded-br-[10px] border border-blue-300/50 bg-blue-50/80 px-4 py-4 dark:border-blue-400/20 dark:bg-blue-500/12">
347-
{message.text.trim().length > 0 ? (
348-
<Markdown
349-
options={{ gfm: true }}
350-
renderers={styles.renderers}
351-
styles={styles.styles}
352-
theme={styles.theme}
353-
>
354-
{message.text}
355-
</Markdown>
356-
) : null}
357-
{attachments.map((attachment) => {
358-
const uri = messageImageUrl(props.httpBaseUrl, attachment.id);
359-
if (!uri) {
360-
return null;
361-
}
362-
const headers = props.bearerToken
363-
? { Authorization: `Bearer ${props.bearerToken}` }
364-
: undefined;
365-
366-
return (
367-
<TouchableOpacity
368-
key={attachment.id}
369-
activeOpacity={0.7}
370-
onPress={() => props.onPressImage(uri, headers)}
349+
<Pressable onLongPress={() => props.onLongPressMessage(message.text)}>
350+
<View className="max-w-[85%] gap-2 rounded-[22px] rounded-br-[10px] border border-blue-300/50 bg-blue-50/80 px-4 py-4 dark:border-blue-400/20 dark:bg-blue-500/12">
351+
{message.text.trim().length > 0 ? (
352+
<Markdown
353+
options={{ gfm: true }}
354+
renderers={styles.renderers}
355+
styles={styles.styles}
356+
theme={styles.theme}
371357
>
372-
<Image
373-
source={{ uri, ...(headers ? { headers } : {}) }}
374-
className="aspect-[1.3] w-full rounded-[18px] bg-neutral-200 dark:bg-neutral-800"
375-
resizeMode="cover"
376-
/>
377-
</TouchableOpacity>
378-
);
379-
})}
380-
</View>
358+
{message.text}
359+
</Markdown>
360+
) : null}
361+
{attachments.map((attachment) => {
362+
const uri = messageImageUrl(props.httpBaseUrl, attachment.id);
363+
if (!uri) {
364+
return null;
365+
}
366+
const headers = props.bearerToken
367+
? { Authorization: `Bearer ${props.bearerToken}` }
368+
: undefined;
369+
370+
return (
371+
<TouchableOpacity
372+
key={attachment.id}
373+
activeOpacity={0.7}
374+
onPress={() => props.onPressImage(uri, headers)}
375+
>
376+
<Image
377+
source={{ uri, ...(headers ? { headers } : {}) }}
378+
className="aspect-[1.3] w-full rounded-[18px] bg-neutral-200 dark:bg-neutral-800"
379+
resizeMode="cover"
380+
/>
381+
</TouchableOpacity>
382+
);
383+
})}
384+
</View>
385+
</Pressable>
381386
<Text className="px-1 text-right font-t3-medium text-xs text-neutral-500 dark:text-neutral-500">
382387
{timestampLabel}
383388
</Text>
@@ -392,44 +397,46 @@ function renderFeedEntry(
392397
}
393398

394399
return (
395-
<View className="mb-3.5 gap-1.5 px-1">
396-
{message.text.trim().length > 0 ? (
397-
<Markdown
398-
options={{ gfm: true }}
399-
renderers={styles.renderers}
400-
styles={styles.styles}
401-
theme={styles.theme}
402-
>
403-
{message.text}
404-
</Markdown>
405-
) : null}
406-
{attachments.map((attachment) => {
407-
const uri = messageImageUrl(props.httpBaseUrl, attachment.id);
408-
if (!uri) {
409-
return null;
410-
}
411-
const headers = props.bearerToken
412-
? { Authorization: `Bearer ${props.bearerToken}` }
413-
: undefined;
414-
415-
return (
416-
<TouchableOpacity
417-
key={attachment.id}
418-
activeOpacity={0.7}
419-
onPress={() => props.onPressImage(uri, headers)}
400+
<Pressable onLongPress={() => props.onLongPressMessage(message.text)}>
401+
<View className="mb-3.5 gap-1.5 px-1">
402+
{message.text.trim().length > 0 ? (
403+
<Markdown
404+
options={{ gfm: true }}
405+
renderers={styles.renderers}
406+
styles={styles.styles}
407+
theme={styles.theme}
420408
>
421-
<Image
422-
source={{ uri, ...(headers ? { headers } : {}) }}
423-
className="aspect-[1.3] w-full rounded-[18px] bg-neutral-200 dark:bg-neutral-800"
424-
resizeMode="cover"
425-
/>
426-
</TouchableOpacity>
427-
);
428-
})}
429-
<Text className="font-t3-medium text-xs text-neutral-500 dark:text-neutral-500">
430-
{timestampLabel}
431-
</Text>
432-
</View>
409+
{message.text}
410+
</Markdown>
411+
) : null}
412+
{attachments.map((attachment) => {
413+
const uri = messageImageUrl(props.httpBaseUrl, attachment.id);
414+
if (!uri) {
415+
return null;
416+
}
417+
const headers = props.bearerToken
418+
? { Authorization: `Bearer ${props.bearerToken}` }
419+
: undefined;
420+
421+
return (
422+
<TouchableOpacity
423+
key={attachment.id}
424+
activeOpacity={0.7}
425+
onPress={() => props.onPressImage(uri, headers)}
426+
>
427+
<Image
428+
source={{ uri, ...(headers ? { headers } : {}) }}
429+
className="aspect-[1.3] w-full rounded-[18px] bg-neutral-200 dark:bg-neutral-800"
430+
resizeMode="cover"
431+
/>
432+
</TouchableOpacity>
433+
);
434+
})}
435+
<Text className="font-t3-medium text-xs text-neutral-500 dark:text-neutral-500">
436+
{timestampLabel}
437+
</Text>
438+
</View>
439+
</Pressable>
433440
);
434441
}
435442

@@ -524,6 +531,9 @@ const IOS_NAV_BAR_HEIGHT = 44;
524531

525532
export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
526533
const listRef = useRef<LegendListRef>(null);
534+
const router = useRouter();
535+
const { environmentId } = useLocalSearchParams<{ environmentId?: string | string[] }>();
536+
const resolvedEnvironmentId = Array.isArray(environmentId) ? environmentId[0] : environmentId;
527537
const copyFeedbackTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
528538
const [copiedRowId, setCopiedRowId] = useState<string | null>(null);
529539
const [expandedWorkGroups, setExpandedWorkGroups] = useState<Record<string, boolean>>({});
@@ -563,6 +573,19 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
563573
};
564574
}, []);
565575

576+
const onLongPressMessage = useCallback(
577+
(text: string) => {
578+
if (!resolvedEnvironmentId) return;
579+
void Haptics.selectionAsync();
580+
setMessageCopyText(text);
581+
router.push({
582+
pathname: "/threads/[environmentId]/[threadId]/message-copy",
583+
params: { environmentId: resolvedEnvironmentId, threadId: props.threadId },
584+
});
585+
},
586+
[resolvedEnvironmentId, props.threadId, router],
587+
);
588+
566589
const onCopyWorkRow = useCallback((rowId: string, value: string) => {
567590
void Clipboard.setStringAsync(value);
568591
void Haptics.selectionAsync();
@@ -594,6 +617,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
594617
copiedRowId,
595618
httpBaseUrl: props.httpBaseUrl,
596619
expandedWorkGroups,
620+
onLongPressMessage,
597621
onCopyWorkRow,
598622
onToggleWorkGroup,
599623
onPressImage,
@@ -605,6 +629,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
605629
expandedWorkGroups,
606630
iconSubtleColor,
607631
markdownStyles,
632+
onLongPressMessage,
608633
onCopyWorkRow,
609634
onPressImage,
610635
onToggleWorkGroup,
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { createExternalStore } from "../../lib/createExternalStore";
2+
3+
const store = createExternalStore<string | null>(null);
4+
5+
export const setMessageCopyText = (text: string) => store.set(text);
6+
export const clearMessageCopyText = () => store.set(null);
7+
export const useMessageCopyText = store.useValue;

0 commit comments

Comments
 (0)