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

AssistantCitationSheet.tsx

132 lines5.0 KB

1import { ASSISTANT_CITATION_MAX_COMMENT_LENGTH, type AssistantCitation } from "@t3tools/contracts";
2import { scopedThreadKey } from "@t3tools/client-runtime/environment";
3import { useState } from "react";
4import { T, useGT } from "gt-react-native";
5import { Platform, ScrollView, TextInput, View } from "react-native";
6import { useSafeAreaInsets } from "react-native-safe-area-context";
7import { useOpenLinkedThread } from "../features/threads/useOpenLinkedThread";
8import { requestThreadMessageJump } from "../features/threads/threadMessageJump";
9import { copyTextWithHaptic } from "../lib/copyTextWithHaptic";
10import { AppText as Text } from "./ui/AppText";
11import { ListActionRow } from "./ui/ListActionRow";
12import { ListSection } from "./ui/ListSection";
13import { Sheet } from "./ui/Sheet";
14​
15const CONTEXT_CHARS = 80;
16​
17function contextBefore(prefix: string) {
18 const flat = prefix.replace(/\s+/g, " ");
19 if (flat.length <= CONTEXT_CHARS) return flat.trimStart();
20 const cut = flat.slice(-CONTEXT_CHARS);
21 return `…${cut.slice(cut.indexOf(" ") + 1)}`;
23​
24function contextAfter(suffix: string) {
25 const flat = suffix.replace(/\s+/g, " ");
26 if (flat.length <= CONTEXT_CHARS) return flat.trimEnd();
27 const cut = flat.slice(0, CONTEXT_CHARS);
28 return `${cut.slice(0, cut.lastIndexOf(" "))}…`;
30​
31export function AssistantCitationSheet(props: {
32 readonly citation: AssistantCitation;
33 readonly onClose: () => void;
34 readonly onRemove?: () => void;
35 readonly onCommentChange?: (comment: string) => void;
36 readonly canChangeComment?: (comment: string) => boolean;
37}) {
38 const gt = useGT();
39 const openLinkedThread = useOpenLinkedThread();
40 const insets = useSafeAreaInsets();
41 const citation = props.citation;
42 const [comment, setComment] = useState(citation.comment ?? "");
43 const commentChanged = comment.trim() !== (citation.comment ?? "");
44 const commentTooLong = commentChanged && props.canChangeComment?.(comment) === false;
45 const close = () => {
46 if (commentTooLong) return;
47 if (props.onCommentChange && commentChanged) {
48 props.onCommentChange(comment);
49 }
50 props.onClose();
51 };
52 return (
53 <Sheet
54 title={gt({ context: "noun, a quoted passage" })`Quote`}
55 onClose={close}
56 dismissDisabled={commentTooLong}
57 >
58 <ScrollView
59 contentInsetAdjustmentBehavior={Platform.OS === "ios" ? "automatic" : "never"}
60 automaticallyAdjustKeyboardInsets
61 contentContainerStyle={{
62 paddingHorizontal: 20,
63 paddingTop: 12,
64 gap: 24,
65 paddingBottom: Math.max(20, insets.bottom),
66 }}
67 >
68 <View className="flex-row gap-3 rounded-2xl bg-grouped-card p-4">
69 <View className="w-1 rounded-full bg-primary" />
70 <Text selectable textRole="body" className="min-w-0 flex-1 text-foreground-muted">
71 {contextBefore(citation.prefix)}
72 <Text className="font-t3-medium text-foreground">{citation.text}</Text>
73 {contextAfter(citation.suffix)}
74 </Text>
75 </View>
76 {props.onCommentChange ? (
77 <ListSection title={gt({ context: "noun" })`Comment`}>
78 <TextInput
79 accessibilityLabel={gt`Comment on quote`}
80 placeholder={gt`Add an optional comment…`}
81 placeholderTextColorClassName="accent-foreground-muted"
82 multiline
83 maxLength={ASSISTANT_CITATION_MAX_COMMENT_LENGTH}
84 value={comment}
85 onChangeText={setComment}
86 className="min-h-24 px-4 py-3 text-base text-foreground"
87 style={{ textAlignVertical: "top" }}
88 />
89 {commentTooLong ? (
90 <Text textRole="body" className="px-4 pb-3 text-foreground-muted">
91 <T>
92 This comment makes the message too long to send. Shorten it to save and close.
93 </T>
94 </Text>
95 ) : null}
96 </ListSection>
97 ) : citation.comment ? (
98 <ListSection title={gt({ context: "noun" })`Comment`}>
99 <Text selectable textRole="body" className="px-4 py-3 text-foreground">
100 {citation.comment}
101 </Text>
102 </ListSection>
103 ) : null}
104 <ListSection>
105 <ListActionRow
106 icon="arrow.up.right"
107 label={gt`Go to message`}
108 onPress={() => {
109 if (commentTooLong) return;
110 close();
111 requestThreadMessageJump(scopedThreadKey(citation), citation.messageId);
112 openLinkedThread(citation);
113 }}
114 />
115 <ListActionRow
116 icon="doc.on.doc"
117 label={gt`Copy quote`}
118 onPress={() => copyTextWithHaptic(citation.text)}
119 />
120 {props.onRemove ? (
121 <ListActionRow
122 icon="trash"
123 label={gt`Remove from draft`}
124 tone="danger"
125 onPress={props.onRemove}
126 />
127 ) : null}
128 </ListSection>
129 </ScrollView>
130 </Sheet>
131 );