Skip to content

Commit e7ae7b1

Browse files
matt2eclaude
andauthored
fix(search): hold an early ⌘K Enter until results land (#1910)
Signed-off-by: Matt Toohey <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 7edaec0 commit e7ae7b1

7 files changed

Lines changed: 1134 additions & 44 deletions

File tree

‎src/app/shell/PageSearch.test.tsx‎

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import {
1919
} from "../../features/shortcuts/preferences";
2020
import { ShortcutsService } from "../../features/shortcuts/service";
2121
import { isApplePlatform } from "../../features/shortcuts/format";
22+
import { createRelaySession } from "../../features/relay/session";
23+
import { keypair, scriptedTransport } from "../../features/relay/testing";
2224

2325
// jsdom lacks scrollIntoView; the palette reveals its typed-text selection.
2426
beforeEach(() => {
@@ -391,6 +393,123 @@ it("opens conversation-scoped search with Command F and returns to overall searc
391393
}
392394
});
393395

396+
it("does not open a DM that finishes starting after search closed and reopened", async () => {
397+
const root = new Context();
398+
root.provide("pluginStatus", {
399+
isActive: () => true,
400+
subscribe: () => () => {},
401+
});
402+
const bindings = createShortcutBindings(window);
403+
const shortcuts = new ShortcutsService(root, window, bindings);
404+
const relay = keypair(),
405+
viewer = keypair(),
406+
bob = keypair();
407+
const owner = createRelaySession({
408+
...scriptedTransport(viewer.pubkey, relay.pubkey).transport,
409+
query: async () => [],
410+
});
411+
let started!: (channelId: string) => void;
412+
let attempt!: AbortSignal;
413+
const session = {
414+
...owner.session,
415+
channels: {
416+
...owner.session.channels,
417+
// The started DM is known, so only the closed palette stops it.
418+
get: (id: string) =>
419+
id === "bob-dm"
420+
? { id, name: "", channelType: "dm" }
421+
: owner.session.channels.get?.(id),
422+
},
423+
directMessages: {
424+
...owner.session.directMessages,
425+
available: true,
426+
people: async () => ({
427+
people: [{ pubkey: bob.pubkey, name: "Bob" }],
428+
hasMore: false,
429+
}),
430+
open: (_pubkeys: readonly string[], signal: AbortSignal) => {
431+
attempt = signal;
432+
return new Promise<string>((resolve) => (started = resolve));
433+
},
434+
},
435+
} as unknown as typeof owner.session;
436+
const connection = { status: "ready", generation: 0, session, scope: "s" };
437+
const community = {
438+
selected: "wss://relay.example",
439+
viewer: viewer.pubkey,
440+
};
441+
const open = vi.fn(async () => {});
442+
const services = {
443+
shortcuts,
444+
shortcutBindings: bindings,
445+
navigation: {
446+
subscribe: () => () => {},
447+
snapshot: () => ({ entry: { target: null } }),
448+
open,
449+
},
450+
communities: {
451+
subscribe: () => () => {},
452+
snapshot: () => community,
453+
relay: { subscribe: () => () => {}, snapshot: () => connection },
454+
},
455+
} as unknown as SearchServices;
456+
const user = userEvent.setup();
457+
try {
458+
render(
459+
<PageSearch
460+
pages={[
461+
{
462+
key: "buzz.channels/channels",
463+
pluginId: "buzz.channels",
464+
id: "channels",
465+
title: "Channels",
466+
revision: "bundled",
467+
component: () => null,
468+
},
469+
]}
470+
onSelect={vi.fn()}
471+
services={services}
472+
/>,
473+
);
474+
await user.click(screen.getByRole("button", { name: "Search Buzz" }));
475+
const input = await screen.findByRole("combobox", { name: "Search Buzz" });
476+
await user.type(input, "bob");
477+
await user.click(await screen.findByRole("option", { name: /Bob/ }));
478+
expect(
479+
await screen.findByText("Opening your conversation with Bob…"),
480+
).toBeVisible();
481+
// Hold the dialog's close animation, so search reopens with the same
482+
// results still mounted before the DM resolves.
483+
let release!: () => void;
484+
const finished = new Promise<void>((resolve) => (release = resolve));
485+
const animations = vi.fn(() => [{ finished }]);
486+
const dialog = screen.getByRole("dialog", { name: "Search Buzz" });
487+
Object.defineProperty(dialog, "getAnimations", { value: animations });
488+
try {
489+
fireEvent.keyDown(input, { key: "Escape" });
490+
await vi.waitFor(() => expect(animations).toHaveBeenCalled());
491+
expect(attempt.aborted).toBe(true);
492+
fireEvent.click(
493+
screen.getByRole("button", { name: "Search Buzz", hidden: true }),
494+
);
495+
expect(input).toBeInTheDocument();
496+
expect(input).toHaveValue("");
497+
await act(async () => started("bob-dm"));
498+
expect(open).not.toHaveBeenCalled();
499+
expect(
500+
screen.queryByText("Opening your conversation with Bob…"),
501+
).toBeNull();
502+
} finally {
503+
await act(async () => release());
504+
}
505+
} finally {
506+
cleanup();
507+
owner.dispose();
508+
bindings.dispose();
509+
await root.fiber.dispose();
510+
}
511+
});
512+
394513
it("ranks pages by match when no community is connected", async () => {
395514
const root = new Context();
396515
root.provide("pluginStatus", {

‎src/app/shell/PageSearch.tsx‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ export function PageSearch({
238238
setScopedChannelId(channelId);
239239
input.current?.focus();
240240
}}
241+
open={open}
242+
isOpen={() => openRef.current}
241243
close={(channelId) => {
242244
composerChannel.current = channelId;
243245
returnFocus.current = document.getElementById("main-content");
@@ -263,10 +265,12 @@ function CommunitySearch({
263265
query,
264266
onQueryChange,
265267
input,
268+
open,
266269
enabled,
267270
scopedChannelId,
268271
currentChannelId,
269272
onScopeChange,
273+
isOpen,
270274
close,
271275
}: {
272276
services: SearchServices;
@@ -275,6 +279,9 @@ function CommunitySearch({
275279
scopedChannelId?: string | undefined;
276280
currentChannelId?: string | undefined;
277281
onScopeChange: (channelId?: string) => void;
282+
/** Whether the palette is still open. A choice that resolves after it
283+
* closed, such as a DM still opening, must not navigate. */
284+
isOpen: () => boolean;
278285
close: (channelId?: string) => void;
279286
} & SearchInputProps) {
280287
const inlineSettings = useSearchSettings({
@@ -371,6 +378,7 @@ function CommunitySearch({
371378
query={query}
372379
onQueryChange={onQueryChange}
373380
input={input}
381+
open={open}
374382
pages={pages}
375383
scopedChannelId={scopedChannelId}
376384
currentChannelId={currentChannelId}
@@ -380,6 +388,7 @@ function CommunitySearch({
380388
openConversation={(channelId, messageId) => {
381389
const current = services.communities.snapshot();
382390
if (
391+
!isOpen() ||
383392
current.viewer !== scope.viewer ||
384393
current.selected !== client.selected ||
385394
services.communities.relay.snapshot().session !==
@@ -400,6 +409,7 @@ function CommunitySearch({
400409
const current = services.communities.snapshot();
401410
const relay = services.communities.relay.snapshot();
402411
if (
412+
!isOpen() ||
403413
current.viewer !== scope.viewer ||
404414
current.selected !== client.selected ||
405415
relay.session !== connection.session ||

‎src/app/shell/SearchChoices.tsx‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ export type SearchInputProps = {
5555
| undefined;
5656
displayQuery?: string | undefined;
5757
onDisplayQueryChange?: ((value: string) => void) | undefined;
58+
/** False while the palette closes, so a held Enter chooses nothing. */
59+
open?: boolean | undefined;
5860
};
5961

6062
export function SearchChoices({
@@ -70,9 +72,18 @@ export function SearchChoices({
7072
scopeAction,
7173
children,
7274
inlineSettings,
75+
open = true,
76+
pending = false,
77+
isAction,
7378
}: SearchInputProps & {
7479
groups: readonly SearchGroup[];
7580
children?: ReactNode;
81+
/** A row that should lead may still arrive, so Enter before any row
82+
* waits until it has. */
83+
pending?: boolean;
84+
/** Whether a row acts on the search rather than being a result, so Enter
85+
* on it can wait for `pending` like Enter before any row. */
86+
isAction?: (key: string) => boolean;
7687
}) {
7788
const resultsViewport = useRef<HTMLDivElement>(null);
7889
const resultsContent = useRef<HTMLDivElement>(null);
@@ -137,6 +148,9 @@ export function SearchChoices({
137148
query,
138149
keys: destinations.map(({ key }) => key),
139150
onChoose: (key) => destinations.find((row) => row.key === key)?.run(),
151+
open,
152+
pending,
153+
isAction,
140154
});
141155
const selected = highlight.active;
142156
useEffect(() => {
@@ -210,7 +224,7 @@ export function SearchChoices({
210224
const destination = destinations[Number(shortcutNumber) - 1];
211225
if (destination) {
212226
event.preventDefault();
213-
if (!event.repeat) destination.run();
227+
if (!event.repeat) highlight.choose(destination.key);
214228
}
215229
return;
216230
}
@@ -259,7 +273,6 @@ export function SearchChoices({
259273
icon,
260274
image,
261275
avatar,
262-
run,
263276
}) =>
264277
content ? (
265278
// biome-ignore lint/a11y/useSemanticElements: Interactive search grid, not a data table.
@@ -287,7 +300,7 @@ export function SearchChoices({
287300
)
288301
)
289302
return;
290-
run();
303+
highlight.choose(key);
291304
}}
292305
onKeyDownCapture={(event) => {
293306
const target = event.target as HTMLElement;
@@ -308,7 +321,7 @@ export function SearchChoices({
308321
event.key === " "
309322
) {
310323
event.preventDefault();
311-
run();
324+
highlight.choose(key);
312325
} else highlight.rowProps(key).onKeyDown(event);
313326
}}
314327
>
@@ -345,7 +358,7 @@ export function SearchChoices({
345358
event.key === "Enter"
346359
) {
347360
event.preventDefault();
348-
run();
361+
highlight.choose(key);
349362
} else
350363
highlight
351364
.rowProps(key)
@@ -412,7 +425,7 @@ export function SearchChoices({
412425
)}
413426
</>
414427
}
415-
onClick={run}
428+
onClick={() => highlight.choose(key)}
416429
/>
417430
</div>
418431
</div>

0 commit comments

Comments
 (0)