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

thread-search-match.tsx

92 lines2.7 KB

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;
45​
46export function ThreadSearchMatchExcerpt(props: {
47 readonly match: EnvironmentThreadSearchMatch;
48 readonly query: string;
49 readonly selected?: boolean;
50 readonly compact?: boolean;
51}) {
52 const isUser = props.match.source === "user";
53 const parts = splitHighlightParts(props.match.snippet, props.query);
54 return (
55 <Text
56 className={cn(
57 props.compact ? "text-sm" : "text-xs",
58 props.selected ? "text-user-bubble-foreground-muted" : "text-foreground-muted",
59 )}
60 numberOfLines={1}
61 >
62 <Text
63 className={cn(
64 props.compact ? "text-sm font-t3-medium" : "text-xs font-t3-medium",
65 props.selected
66 ? "text-user-bubble-foreground"
67 : isUser
68 ? "text-blue-500 dark:text-blue-400"
69 : "text-emerald-600 dark:text-emerald-400",
70 )}
71 >
72 {isUser ? "You:" : "Agent:"}{" "}
73 </Text>
74 {parts.map((part) => (
75 <Text
76 className={cn(
77 props.compact ? "text-sm" : "text-xs",
78 part.highlighted && "font-t3-bold",
79 props.selected
80 ? "text-user-bubble-foreground"
81 : part.highlighted
82 ? "text-foreground"
83 : "text-foreground-muted",
84 )}
85 key={part.start}
86 >
87 {part.text}
88 </Text>
89 ))}
90 </Text>
91 );