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";
5import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider";
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;
45}
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 { materialYouStyleLayoutActive } = useAppearancePreferences();
55 const parts = splitHighlightParts(props.match.snippet, props.query);
56 return (
57 <Text
58 className={cn(
59 props.compact ? "text-sm" : "text-xs",
60 props.selected
61 ? materialYouStyleLayoutActive
62 ? "text-thread-selected-foreground-muted"
63 : "text-user-bubble-foreground-muted"
64 : "text-foreground-muted",
65 )}
66 numberOfLines={1}
67 >
68 <Text
69 className={cn(
70 props.compact ? "text-sm font-t3-medium" : "text-xs font-t3-medium",
71 props.selected
72 ? materialYouStyleLayoutActive
73 ? "text-thread-selected-foreground"
74 : "text-user-bubble-foreground"
75 : isUser
76 ? "text-foreground-secondary"
77 : "text-adaptive-emerald-600-400",
78 )}
79 >
80 {isUser ? "You:" : "Agent:"}{" "}
81 </Text>
82 {parts.map((part) => (
83 <Text
84 className={cn(
85 props.compact ? "text-sm" : "text-xs",
86 part.highlighted && "font-t3-bold",
87 props.selected
88 ? materialYouStyleLayoutActive
89 ? "text-thread-selected-foreground"
90 : "text-user-bubble-foreground"
91 : part.highlighted
92 ? "text-foreground"
93 : "text-foreground-muted",
94 )}
95 key={part.start}
96 >
97 {part.text}
98 </Text>
99 ))}
100 </Text>
101 );
102}