The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 77 lines 2.61 KB
Raw
1 import * as Dialog from "../../components/ui/Dialog";
2 import { SymbolView } from "../../components/ui/AppSymbol";
3 import { useGT } from "gt-react-native";
4 import { memo, useState } from "react";
5 import { Pressable, type ColorValue } from "react-native";
6
7 import { useDeadlinePassed } from "../../lib/clock";
8 import { tryCopyTextWithHaptic } from "../../lib/copyTextWithHaptic";
9
10 const COPY_FEEDBACK_DURATION_MS = 1200;
11
12 export const CopyTextButton = memo(function CopyTextButton(props: {
13 readonly accessibilityLabel: string;
14 readonly text: string;
15 readonly onCopy?: () => Promise<void>;
16 /** Lets a long-press menu wrap the button (`Menu` injects it on Android). */
17 readonly onLongPress?: () => void;
18 readonly tintColor?: ColorValue;
19 readonly copiedTintColor?: ColorValue;
20 readonly backgroundColor?: ColorValue;
21 readonly borderColor?: ColorValue;
22 readonly iconSize?: number;
23 readonly buttonSize?: number;
24 }) {
25 const gt = useGT();
26 const [copiedAtMs, setCopiedAtMs] = useState<number | null>(null);
27 const feedbackEnded = useDeadlinePassed(
28 copiedAtMs === null ? null : copiedAtMs + COPY_FEEDBACK_DURATION_MS,
29 );
30 const copied = copiedAtMs !== null && !feedbackEnded;
31
32 return (
33 <Pressable
34 accessibilityRole="button"
35 accessibilityLabel={copied ? gt("Copied") : props.accessibilityLabel}
36 disabled={props.text.length === 0}
37 hitSlop={8}
38 onLongPress={props.onLongPress}
39 onPress={async () => {
40 try {
41 if (props.onCopy) await props.onCopy();
42 else if (!(await tryCopyTextWithHaptic(props.text))) {
43 Dialog.alert(gt("Could not copy"), gt("Try again."));
44 return;
45 }
46 } catch {
47 Dialog.alert(gt("Could not copy"), gt("Try again."));
48 return;
49 }
50 setCopiedAtMs(Date.now());
51 }}
52 style={({ pressed }) => ({
53 width: props.buttonSize ?? 30,
54 height: props.buttonSize ?? 30,
55 alignItems: "center",
56 justifyContent: "center",
57 borderRadius: 9,
58 borderWidth: props.borderColor ? 1 : 0,
59 borderColor: props.borderColor,
60 backgroundColor: props.backgroundColor,
61 opacity: pressed ? 0.52 : 1,
62 })}
63 >
64 <SymbolView
65 name={
66 copied
67 ? { ios: "checkmark", android: "check" }
68 : { ios: "doc.on.doc", android: "content_copy" }
69 }
70 size={props.iconSize ?? 13}
71 tintColor={copied ? (props.copiedTintColor ?? props.tintColor) : props.tintColor}
72 tintColorClassName={props.tintColor ? undefined : "accent-foreground"}
73 type="monochrome"
74 />
75 </Pressable>
76 );
77 });