Open History to see the last commit
1import type { EnvironmentThreadSearchMatch } from "@t3tools/client-runtime/state/thread-search";
2
3import { AppText as Text } from "../../components/AppText";
4import { cn } from "../../lib/cn";
5
6function foldAsciiCase(value: string): string {
7 return value.replace(/[A-Z]/g, (character) => character.toLowerCase());
8}
9
10function splitHighlightParts(text: string, query: string) {
11 const normalizedText = foldAsciiCase(text);
12 const normalizedQuery = foldAsciiCase(query.trim());
13 if (normalizedQuery.length === 0) {
14 return [{ text, highlighted: false, start: 0 }];
15 }
16
17 const parts: Array<{
18 readonly text: string;
19 readonly highlighted: boolean;
20 readonly start: number;
21 }> = [];
22 let cursor = 0;
23 while (cursor < text.length) {
24 const matchIndex = normalizedText.indexOf(normalizedQuery, cursor);
25 if (matchIndex === -1) {
26 parts.push({ text: text.slice(cursor), highlighted: false, start: cursor });
27 break;
28 }
29 if (matchIndex > cursor) {
30 parts.push({
31 text: text.slice(cursor, matchIndex),
32 highlighted: false,
33 start: cursor,
34 });
35 }
36 parts.push({
37 text: text.slice(matchIndex, matchIndex + normalizedQuery.length),
38 highlighted: true,
39 start: matchIndex,
40 });
41 cursor = matchIndex + normalizedQuery.length;
42 }
43 return parts;
44}
45
46export function ThreadSearchMatchExcerpt(props: {
47 readonly match: EnvironmentThreadSearchMatch;
48 readonly query: string;
49 readonly selected?: boolean;
50 readonly compact?: boolean;
51 readonly sidebar?: 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 ? "text-thread-selected-foreground-muted"
61 : props.sidebar
62 ? "text-drawer-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 ? "text-thread-selected-foreground"
72 : isUser
73 ? props.sidebar
74 ? "text-drawer-foreground-muted"
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 ? "text-thread-selected-foreground"
88 : part.highlighted
89 ? props.sidebar
90 ? "text-drawer-foreground"
91 : "text-foreground"
92 : props.sidebar
93 ? "text-drawer-foreground-muted"
94 : "text-foreground-muted",
95 )}
96 key={part.start}
97 >
98 {part.text}
99 </Text>
100 ))}
101 </Text>
102 );
103}