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

thread-search-match.tsx

101 lines3.0 KB

1import { Platform } from "react-native";
2import type { EnvironmentThreadSearchMatch } from "@t3tools/client-runtime/state/thread-search";
3​
4import { AppText as Text } from "../../components/AppText";
5import { cn } from "../../lib/cn";
6​
7function foldAsciiCase(value: string): string {
8 return value.replace(/[A-Z]/g, (character) => character.toLowerCase());
9}
10​
11function splitHighlightParts(text: string, query: string) {
12 const normalizedText = foldAsciiCase(text);
13 const normalizedQuery = foldAsciiCase(query.trim());
14 if (normalizedQuery.length === 0) {
15 return [{ text, highlighted: false, start: 0 }];
16 }
17​
18 const parts: Array<{
19 readonly text: string;
20 readonly highlighted: boolean;
21 readonly start: number;
22 }> = [];
23 let cursor = 0;
24 while (cursor < text.length) {
25 const matchIndex = normalizedText.indexOf(normalizedQuery, cursor);
26 if (matchIndex === -1) {
27 parts.push({ text: text.slice(cursor), highlighted: false, start: cursor });
28 break;
29 }
30 if (matchIndex > cursor) {
31 parts.push({
32 text: text.slice(cursor, matchIndex),
33 highlighted: false,
34 start: cursor,
35 });
36 }
37 parts.push({
38 text: text.slice(matchIndex, matchIndex + normalizedQuery.length),
39 highlighted: true,
40 start: matchIndex,
41 });
42 cursor = matchIndex + normalizedQuery.length;
43 }
44 return parts;
46​
47export function ThreadSearchMatchExcerpt(props: {
48 readonly match: EnvironmentThreadSearchMatch;
49 readonly query: string;
50 readonly selected?: boolean;
51 readonly compact?: boolean;
52}) {
53 const isUser = props.match.source === "user";
54 const parts = splitHighlightParts(props.match.snippet, props.query);
55 return (
56 <Text
57 className={cn(
58 props.compact ? "text-sm" : "text-xs",
59 props.selected
60 ? Platform.OS === "android"
61 ? "text-thread-selected-foreground-muted"
62 : "text-user-bubble-foreground-muted"
63 : "text-foreground-muted",
64 )}
65 numberOfLines={1}
66 >
67 <Text
68 className={cn(
69 props.compact ? "text-sm font-t3-medium" : "text-xs font-t3-medium",
70 props.selected
71 ? Platform.OS === "android"
72 ? "text-thread-selected-foreground"
73 : "text-user-bubble-foreground"
74 : isUser
75 ? "text-foreground-secondary"
76 : "text-adaptive-emerald-600-400",
77 )}
78 >
79 {isUser ? "You:" : "Agent:"}{" "}
80 </Text>
81 {parts.map((part) => (
82 <Text
83 className={cn(
84 props.compact ? "text-sm" : "text-xs",
85 part.highlighted && "font-t3-bold",
86 props.selected
87 ? Platform.OS === "android"
88 ? "text-thread-selected-foreground"
89 : "text-user-bubble-foreground"
90 : part.highlighted
91 ? "text-foreground"
92 : "text-foreground-muted",
93 )}
94 key={part.start}
95 >
96 {part.text}
97 </Text>
98 ))}
99 </Text>
100 );