The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { ASSISTANT_CITATION_MAX_COMMENT_LENGTH, type AssistantCitation } from "@t3tools/contracts"; |
| 2 | import { scopedThreadKey } from "@t3tools/client-runtime/environment"; |
| 3 | import { useState } from "react"; |
| 4 | import { T, useGT } from "gt-react-native"; |
| 5 | import { Platform, ScrollView, TextInput, View } from "react-native"; |
| 6 | import { useSafeAreaInsets } from "react-native-safe-area-context"; |
| 7 | import { useOpenLinkedThread } from "../features/threads/useOpenLinkedThread"; |
| 8 | import { requestThreadMessageJump } from "../features/threads/threadMessageJump"; |
| 9 | import { copyTextWithHaptic } from "../lib/copyTextWithHaptic"; |
| 10 | import { AppText as Text } from "./ui/AppText"; |
| 11 | import { ListActionRow } from "./ui/ListActionRow"; |
| 12 | import { ListSection } from "./ui/ListSection"; |
| 13 | import { Sheet } from "./ui/Sheet"; |
| 14 | |
| 15 | const CONTEXT_CHARS = 80; |
| 16 | |
| 17 | function 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)}`; |
| 22 | } |
| 23 | |
| 24 | function 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(" "))}…`; |
| 29 | } |
| 30 | |
| 31 | export 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("Quote", { $context: "noun, a quoted passage" })} |
| 55 | onClose={close} |
| 56 | dismissDisabled={commentTooLong} |
| 57 | > |
| 58 | <ScrollView |
| 59 | contentInsetAdjustmentBehavior={Platform.OS === "ios" ? "automatic" : "never"} |
| 60 | contentContainerStyle={{ |
| 61 | paddingHorizontal: 20, |
| 62 | paddingTop: 12, |
| 63 | gap: 24, |
| 64 | paddingBottom: Math.max(20, insets.bottom), |
| 65 | }} |
| 66 | > |
| 67 | <View className="flex-row gap-3 rounded-2xl bg-grouped-card p-4"> |
| 68 | <View className="w-1 rounded-full bg-primary" /> |
| 69 | <Text selectable textRole="body" className="min-w-0 flex-1 text-foreground-muted"> |
| 70 | {contextBefore(citation.prefix)} |
| 71 | <Text className="font-t3-medium text-foreground">{citation.text}</Text> |
| 72 | {contextAfter(citation.suffix)} |
| 73 | </Text> |
| 74 | </View> |
| 75 | {props.onCommentChange ? ( |
| 76 | <ListSection title={gt("Comment", { $context: "noun" })}> |
| 77 | <TextInput |
| 78 | accessibilityLabel={gt("Comment on quote")} |
| 79 | placeholder={gt("Add an optional comment…")} |
| 80 | placeholderTextColorClassName="accent-foreground-muted" |
| 81 | multiline |
| 82 | maxLength={ASSISTANT_CITATION_MAX_COMMENT_LENGTH} |
| 83 | value={comment} |
| 84 | onChangeText={setComment} |
| 85 | className="min-h-24 px-4 py-3 text-base text-foreground" |
| 86 | style={{ textAlignVertical: "top" }} |
| 87 | /> |
| 88 | {commentTooLong ? ( |
| 89 | <Text textRole="body" className="px-4 pb-3 text-foreground-muted"> |
| 90 | <T> |
| 91 | This comment makes the message too long to send. Shorten it to save and close. |
| 92 | </T> |
| 93 | </Text> |
| 94 | ) : null} |
| 95 | </ListSection> |
| 96 | ) : citation.comment ? ( |
| 97 | <ListSection title={gt("Comment", { $context: "noun" })}> |
| 98 | <Text selectable textRole="body" className="px-4 py-3 text-foreground"> |
| 99 | {citation.comment} |
| 100 | </Text> |
| 101 | </ListSection> |
| 102 | ) : null} |
| 103 | <ListSection> |
| 104 | <ListActionRow |
| 105 | icon="arrow.up.right" |
| 106 | label={gt("Go to message")} |
| 107 | onPress={() => { |
| 108 | if (commentTooLong) return; |
| 109 | close(); |
| 110 | requestThreadMessageJump(scopedThreadKey(citation), citation.messageId); |
| 111 | openLinkedThread(citation); |
| 112 | }} |
| 113 | /> |
| 114 | <ListActionRow |
| 115 | icon="doc.on.doc" |
| 116 | label={gt("Copy quote")} |
| 117 | onPress={() => copyTextWithHaptic(citation.text)} |
| 118 | /> |
| 119 | {props.onRemove ? ( |
| 120 | <ListActionRow |
| 121 | icon="trash" |
| 122 | label={gt("Remove from draft")} |
| 123 | tone="danger" |
| 124 | onPress={props.onRemove} |
| 125 | /> |
| 126 | ) : null} |
| 127 | </ListSection> |
| 128 | </ScrollView> |
| 129 | </Sheet> |
| 130 | ); |
| 131 | } |