Skip to content

Commit 29f2054

Browse files
highlight search terms in results and messages (#6702)
**Category:** fix **User Impact:** Search terms are now highlighted in yellow in Cmd+F results and in the message opened from a result. **Problem:** Search returned relevant messages, but users still had to reread each preview and destination message to discover where the query appeared. Search navigation could also lose or misapply highlighting during rapid typing, repeated navigation, thread opening, and forum navigation. **Solution:** Render match-focused previews with a shared yellow treatment, carry the result query through navigation, and apply it only to the clicked destination. The implementation binds highlights to the debounced result set, supports token and prefix matching, and handles channels, threads, forums, diffs, and wave messages consistently. <details> <summary>File changes</summary> **desktop/src/app/AppShell.tsx** Carries the active result query into search-hit navigation so the destination can preserve the user's context. **desktop/src/app/navigation/searchHitEventCache.ts** Adds a bounded, one-shot cache for result queries and navigation IDs alongside cached search-hit events. **desktop/src/app/navigation/searchHitNavigation.test.mjs** Covers query retention, one-shot consumption, repeated same-route activations, forum posts, forced routing, and cancellation. **desktop/src/app/navigation/searchHitNavigation.ts** Associates each search-result activation with a unique navigation ID and forwards it to channel or forum destinations. **desktop/src/app/navigation/useAppNavigation.ts** Extends channel and forum navigation to carry search navigation state without changing normal navigation behavior. **desktop/src/app/routes/ChannelRouteScreen.tsx** Consumes search highlight state after navigation and retains it for the selected message while clearing it on community context changes. **desktop/src/app/routes/channels.$channelId.posts.$postId.tsx** Validates and forwards forum search-navigation IDs. **desktop/src/app/routes/channels.$channelId.tsx** Validates and forwards channel search-navigation IDs. **desktop/src/features/channels/ui/ChannelPane.tsx** Passes the selected result ID and query into the main timeline and open thread panel. **desktop/src/features/channels/ui/ChannelPane.types.ts** Defines the destination-highlight contract for channel panes. **desktop/src/features/channels/ui/ChannelScreen.tsx** Routes destination highlighting to either forum content or the channel timeline. **desktop/src/features/channels/ui/ChannelScreen.types.ts** Defines route-level search-highlight inputs. **desktop/src/features/channels/ui/ForumChannelContent.tsx** Forwards search context into expanded forum threads. **desktop/src/features/forum/ui/ForumThreadPanel.tsx** Highlights the matching text in the selected forum post or reply. **desktop/src/features/forum/ui/ForumView.tsx** Carries selected-result context from forum routing into the thread panel. **desktop/src/features/messages/ui/DiffMessage.tsx** Forwards the query into diff descriptions and rendered diff content. **desktop/src/features/messages/ui/DiffViewer.tsx** Provides a highlighted match excerpt for structured diffs and highlights fallback raw diff text. **desktop/src/features/messages/ui/MessageRow.tsx** Passes destination queries into normal Markdown, diff, and wave message renderers. **desktop/src/features/messages/ui/MessageThreadPanel.tsx** Applies highlighting to the selected thread head or reply without tinting unrelated messages. **desktop/src/features/messages/ui/WaveMessageAttachment.tsx** Highlights matches in wave-message fallback text. **desktop/src/features/search/lib/searchMatch.test.mjs** Covers case-insensitive, literal, multi-term, prefix, one-character, and late-preview matches. **desktop/src/features/search/lib/searchMatch.ts** Centralizes token extraction, case-insensitive match splitting, and match-focused preview generation. **desktop/src/features/search/ui/HighlightedSearchText.tsx** Adds the reusable accessible mark renderer used by result and specialized message surfaces. **desktop/src/features/search/ui/TopbarSearch.tsx** Highlights result previews, centers excerpts around matches, strips search operators, and hides stale results during debounce transitions. **desktop/src/features/sidebar/ui/AppSidebar.types.ts** Updates the sidebar search callback contract to include the result query. **desktop/src/features/sidebar/ui/AppSidebarPinnedHeader.tsx** Passes query-aware result selection through the pinned search entry point. **desktop/src/shared/lib/rehypeSearchHighlight.ts** Uses the shared matcher to mark every matching Markdown text segment while leaving code blocks untouched. **desktop/src/shared/lib/searchHighlightStyle.ts** Defines one yellow highlight treatment for light and dark themes. **desktop/src/shared/ui/markdown/nodeCache.test.mjs** Verifies all Markdown matches, one-character scoped searches, code exclusion, and transient cache behavior. **desktop/src/shared/ui/markdown/nodeCache.ts** Enables transient highlighting for one-character scoped searches without polluting the Markdown cache. **desktop/tests/e2e/smoke.spec.ts** Exercises result and destination highlighting, same-route forum activation, and stale-query suppression end to end. </details> ## Reproduction steps 1. Open a channel containing a longer message and press Cmd+F. 2. Search for a word that appears later in the message, such as `mentions`. 3. Confirm each matching result shows the word in yellow and keeps the match visible in its preview. 4. Open a result and confirm the same term is highlighted in yellow in the destination message. 5. Repeat with a thread reply or forum post, and quickly change the query to confirm stale results are not selectable. ## Demo - **Before:** Search results returned the correct message but offered no visual indication of where the query matched; opening the message also showed no match treatment. - **After:** Every matching term is marked in yellow in the result preview and in the exact channel, thread, or forum message opened from that result. --------- Signed-off-by: tulsi <[email protected]>
1 parent 9b6a637 commit 29f2054

37 files changed

Lines changed: 1205 additions & 282 deletions

‎desktop/src/app/AppShell.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -648,8 +648,8 @@ export function AppShell() {
648648
);
649649

650650
const handleOpenSearchResult = React.useCallback(
651-
(hit: SearchHit) => {
652-
void openSearchHit(hit);
651+
(hit: SearchHit, query: string) => {
652+
void openSearchHit(hit, { query });
653653
},
654654
[openSearchHit],
655655
);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import assert from "node:assert/strict";
2+
import test from "node:test";
3+
4+
const { createSearchHighlightNavigation, parseSearchHighlightNavigation } =
5+
await import("./searchHighlightNavigation.ts");
6+
7+
test("creates trimmed transient state with a unique activation id", () => {
8+
const first = createSearchHighlightNavigation("message", " Mentions ");
9+
const second = createSearchHighlightNavigation("message", "Mentions");
10+
11+
assert.deepEqual(
12+
{ messageId: first.messageId, query: first.query },
13+
{ messageId: "message", query: "Mentions" },
14+
);
15+
assert.notEqual(first.activationId, second.activationId);
16+
});
17+
18+
test("does not create highlight state for an empty query", () => {
19+
assert.equal(createSearchHighlightNavigation("message", " "), undefined);
20+
assert.equal(
21+
createSearchHighlightNavigation("message", undefined),
22+
undefined,
23+
);
24+
});
25+
26+
test("parses only complete highlight navigation state", () => {
27+
const state = {
28+
activationId: "activation",
29+
messageId: "message",
30+
query: "mentions",
31+
};
32+
33+
assert.deepEqual(parseSearchHighlightNavigation(state), state);
34+
assert.equal(
35+
parseSearchHighlightNavigation({ messageId: "message", query: "mentions" }),
36+
null,
37+
);
38+
assert.equal(parseSearchHighlightNavigation(null), null);
39+
});
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
export type SearchHighlightNavigation = {
2+
activationId: string;
3+
messageId: string;
4+
query: string;
5+
};
6+
7+
export function createSearchHighlightNavigation(
8+
messageId: string,
9+
query: string | undefined,
10+
): SearchHighlightNavigation | undefined {
11+
const trimmedQuery = query?.trim();
12+
if (!trimmedQuery) {
13+
return undefined;
14+
}
15+
16+
return {
17+
activationId: crypto.randomUUID(),
18+
messageId,
19+
query: trimmedQuery,
20+
};
21+
}
22+
23+
export function parseSearchHighlightNavigation(
24+
value: unknown,
25+
): SearchHighlightNavigation | null {
26+
if (!value || typeof value !== "object") {
27+
return null;
28+
}
29+
30+
const candidate = value as Partial<SearchHighlightNavigation>;
31+
if (
32+
typeof candidate.activationId !== "string" ||
33+
candidate.activationId.length === 0 ||
34+
typeof candidate.messageId !== "string" ||
35+
candidate.messageId.length === 0 ||
36+
typeof candidate.query !== "string" ||
37+
candidate.query.length === 0
38+
) {
39+
return null;
40+
}
41+
42+
return {
43+
activationId: candidate.activationId,
44+
messageId: candidate.messageId,
45+
query: candidate.query,
46+
};
47+
}

‎desktop/src/app/navigation/searchHitNavigation.test.mjs‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,53 @@ test("search-hit navigation preserves forced message routing while active", asyn
5151
options: {
5252
force: true,
5353
messageId: "message",
54+
searchHighlight: undefined,
5455
threadRootId: "thread-root",
5556
},
5657
},
5758
]);
5859
assert.equal(getCachedSearchHitEvent("message")?.id, "message");
5960
});
6061

62+
test("search-hit navigation carries trimmed highlight state and forces repeated activations", async () => {
63+
clearSearchHitEventCache();
64+
const calls = [];
65+
66+
await openSearchHitWithNavigation(plainMessage, {
67+
goChannel: async (channelId, options) => {
68+
calls.push({ channelId, options });
69+
return true;
70+
},
71+
goForumPost: async () => false,
72+
query: " Mentions ",
73+
});
74+
75+
assert.equal(calls[0].options.force, true);
76+
assert.equal(calls[0].options.searchHighlight.messageId, "message");
77+
assert.equal(calls[0].options.searchHighlight.query, "Mentions");
78+
assert.match(calls[0].options.searchHighlight.activationId, /.+/);
79+
});
80+
81+
test("forum-post search navigation carries transient same-route activation state", async () => {
82+
clearSearchHitEventCache();
83+
const forumPost = { ...forumComment, eventId: "post", kind: 45001 };
84+
const calls = [];
85+
86+
await openSearchHitWithNavigation(forumPost, {
87+
goChannel: async () => false,
88+
goForumPost: async (channelId, postId, options) => {
89+
calls.push({ channelId, postId, options });
90+
return true;
91+
},
92+
query: "mentions",
93+
});
94+
95+
assert.equal(calls[0].options.force, true);
96+
assert.equal(calls[0].options.searchHighlight.messageId, "post");
97+
assert.equal(calls[0].options.searchHighlight.query, "mentions");
98+
assert.match(calls[0].options.searchHighlight.activationId, /.+/);
99+
});
100+
61101
test("cancelled search-hit navigation cannot repopulate cache or route", async () => {
62102
clearSearchHitEventCache();
63103
let resolveLookup;

‎desktop/src/app/navigation/searchHitNavigation.ts‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
import { resolveSearchHitDestination } from "@/app/navigation/resolveSearchHitDestination";
2+
import { createSearchHighlightNavigation } from "@/app/navigation/searchHighlightNavigation";
23
import { cacheSearchHitEvent } from "@/app/navigation/searchHitEventCache";
34
import type { SearchHit } from "@/shared/api/types";
45

56
type SearchHitNavigationActions = {
67
force?: boolean;
8+
query?: string;
79
goChannel: (
810
channelId: string,
911
options?: {
1012
force?: boolean;
1113
messageId?: string;
14+
searchHighlight?: ReturnType<typeof createSearchHighlightNavigation>;
1215
threadRootId?: string | null;
1316
},
1417
) => Promise<unknown>;
1518
goForumPost: (
1619
channelId: string,
1720
postId: string,
18-
options?: { force?: boolean; replyId?: string },
21+
options?: {
22+
force?: boolean;
23+
replyId?: string;
24+
searchHighlight?: ReturnType<typeof createSearchHighlightNavigation>;
25+
},
1926
) => Promise<unknown>;
2027
signal?: AbortSignal;
2128
};
@@ -30,6 +37,10 @@ export async function openSearchHitWithNavigation(
3037
}
3138

3239
const isLifecycleBound = Boolean(actions.signal);
40+
const searchHighlight = createSearchHighlightNavigation(
41+
hit.eventId,
42+
actions.query,
43+
);
3344
if (!isLifecycleBound) {
3445
cacheSearchHitEvent(hit);
3546
}
@@ -47,14 +58,16 @@ export async function openSearchHitWithNavigation(
4758

4859
if (destination.kind === "forum-post") {
4960
return actions.goForumPost(destination.channelId, destination.postId, {
50-
force: actions.force,
61+
force: actions.force || Boolean(searchHighlight),
5162
replyId: destination.replyId,
63+
searchHighlight,
5264
});
5365
}
5466

5567
return actions.goChannel(destination.channelId, {
56-
force: actions.force,
68+
force: actions.force || Boolean(searchHighlight),
5769
messageId: destination.messageId,
70+
searchHighlight,
5871
threadRootId: destination.threadRootId,
5972
});
6073
}

‎desktop/src/app/navigation/useAppNavigation.ts‎

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
useRouter,
77
} from "@tanstack/react-router";
88

9+
import type { SearchHighlightNavigation } from "@/app/navigation/searchHighlightNavigation";
910
import { openSearchHitWithNavigation } from "@/app/navigation/searchHitNavigation";
1011
import {
1112
allowNavigation,
@@ -32,14 +33,23 @@ export function useAppNavigation() {
3233
to: string;
3334
params?: Record<string, string>;
3435
search?: Record<string, string | undefined>;
35-
state?: Record<string, unknown>;
36+
state?:
37+
| Record<string, unknown>
38+
| ((
39+
previousState: Record<string, unknown>,
40+
) => Record<string, unknown>);
3641
},
3742
behavior: NavigationBehavior = {},
3843
guardedTarget?: GuardedNavigation,
3944
) => {
4045
const nextLocation = router.buildLocation(next as never);
46+
const hasStateUpdate = next.state !== undefined;
4147

42-
if (location.href === nextLocation.href && !behavior.force) {
48+
if (
49+
location.href === nextLocation.href &&
50+
!behavior.force &&
51+
!hasStateUpdate
52+
) {
4353
return false;
4454
}
4555

@@ -265,6 +275,9 @@ export function useAppNavigation() {
265275
* silently swallowed (block/buzz#3509). */
266276
force?: boolean;
267277
messageId?: string;
278+
/** Preserve an active search highlight; ordinary navigation clears it. */
279+
preserveSearchHighlight?: boolean;
280+
searchHighlight?: SearchHighlightNavigation;
268281
replace?: boolean;
269282
/** Open this thread panel directly without waiting for a timeline row. */
270283
thread?: string;
@@ -290,6 +303,12 @@ export function useAppNavigation() {
290303
...(options?.thread ? { thread: options.thread } : {}),
291304
...(options?.autoSend ? { autoSend: options.autoSend } : {}),
292305
},
306+
state: options?.preserveSearchHighlight
307+
? undefined
308+
: (previousState: Record<string, unknown>) => ({
309+
...previousState,
310+
searchHighlight: options?.searchHighlight ?? null,
311+
}),
293312
},
294313
{
295314
force: options?.force,
@@ -329,6 +348,9 @@ export function useAppNavigation() {
329348
force?: boolean;
330349
replace?: boolean;
331350
replyId?: string;
351+
/** Preserve an active search highlight; ordinary navigation clears it. */
352+
preserveSearchHighlight?: boolean;
353+
searchHighlight?: SearchHighlightNavigation;
332354
},
333355
) => {
334356
return commitNavigation(
@@ -338,7 +360,15 @@ export function useAppNavigation() {
338360
channelId,
339361
postId,
340362
},
341-
search: options?.replyId ? { replyId: options.replyId } : {},
363+
search: {
364+
...(options?.replyId ? { replyId: options.replyId } : {}),
365+
},
366+
state: options?.preserveSearchHighlight
367+
? undefined
368+
: (previousState: Record<string, unknown>) => ({
369+
...previousState,
370+
searchHighlight: options?.searchHighlight ?? null,
371+
}),
342372
},
343373
{
344374
force: options?.force,
@@ -406,6 +436,8 @@ export function useAppNavigation() {
406436
* Used by desktop-notification activation so a click is never
407437
* silently swallowed (block/buzz#3509). */
408438
force?: boolean;
439+
/** Search text to highlight after opening this result. */
440+
query?: string;
409441
/** Stop notification-driven routing when its owning lifecycle ends. */
410442
signal?: AbortSignal;
411443
},
@@ -414,6 +446,7 @@ export function useAppNavigation() {
414446
force: behavior?.force,
415447
goChannel,
416448
goForumPost,
449+
query: behavior?.query,
417450
signal: behavior?.signal,
418451
}),
419452
[goChannel, goForumPost],

0 commit comments

Comments
 (0)