Skip to content

Commit 7ac60e7

Browse files
committed
fix(search): show the PR number for multi-token searches
The command palette matches each whitespace-separated token, but the PR number shown on a result required the whole query as one substring, so 'owner/repo #123' found the thread without naming the PR.
1 parent dcb32dd commit 7ac60e7

3 files changed

Lines changed: 47 additions & 11 deletions

File tree

‎apps/web/src/components/Sidebar.logic.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
resolveSidebarThreadStatus,
2828
resolveThreadStatusPill,
2929
resolveWorkingStartedAt,
30+
pullRequestMatchesSearchQuery,
3031
searchSidebarThreads,
3132
formatWorkingDurationLabel,
3233
shouldClearThreadSelectionOnMouseDown,
@@ -844,6 +845,27 @@ describe("resolveSidebarThreadStatus", () => {
844845
});
845846
});
846847

848+
describe("pullRequestMatchesSearchQuery", () => {
849+
const branchPullRequest = {
850+
projectId: ProjectId.make("project"),
851+
repository: "pingdotgg/t3code",
852+
number: 123,
853+
url: "https://github.com/pingdotgg/t3code/pull/123",
854+
};
855+
856+
it("matches when every query token appears in the PR's terms", () => {
857+
expect(pullRequestMatchesSearchQuery({ branchPullRequest }, "123")).toBe(true);
858+
expect(pullRequestMatchesSearchQuery({ branchPullRequest }, "pingdotgg/t3code #123")).toBe(
859+
true,
860+
);
861+
});
862+
863+
it("rejects queries with a token the PR does not carry, and blank queries", () => {
864+
expect(pullRequestMatchesSearchQuery({ branchPullRequest }, "rebase 123")).toBe(false);
865+
expect(pullRequestMatchesSearchQuery({ branchPullRequest }, " ")).toBe(false);
866+
});
867+
});
868+
847869
describe("searchSidebarThreads", () => {
848870
const searchThread = (id: string, title: string, project: string) => ({
849871
environmentId: localEnvironmentId,

‎apps/web/src/components/Sidebar.logic.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
type ThreadSortInput,
2222
} from "../lib/threadSort";
2323
import type { SidebarThreadSummary, Thread } from "../types";
24-
import { cn } from "../lib/utils";
24+
import { normalizeSearchText } from "../lib/utils";
2525
import { isLatestTurnSettled } from "../session-logic";
2626

2727
export function shouldNavigateAfterThreadPark(input: {
@@ -916,6 +916,21 @@ export function searchSidebarThreads<
916916
return [...titleMatches, ...contentMatches];
917917
}
918918

919+
/**
920+
* Whether a PR's search terms contain every whitespace-separated token of the
921+
* query. Search results use this to pick the PR number to show, so it has to
922+
* accept the multi-token queries the command palette matches on.
923+
*/
924+
export function pullRequestMatchesSearchQuery(
925+
pullRequest: Parameters<typeof threadPullRequestSearchTerms>[0],
926+
query: string,
927+
): boolean {
928+
const normalizedQuery = normalizeSearchText(query);
929+
if (normalizedQuery.length === 0) return false;
930+
const haystack = normalizeSearchText(threadPullRequestSearchTerms(pullRequest).join(" "));
931+
return normalizedQuery.split(" ").every((token) => haystack.includes(token));
932+
}
933+
919934
export function filterSidebarProjectScopeItems<TItem extends { readonly value: string }>(input: {
920935
items: readonly TItem[];
921936
query: string;

‎apps/web/src/components/ThreadStatusIndicators.tsx‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
resolveThreadPullRequestChains,
1414
visibleThreadPullRequests,
1515
type ThreadPullRequestBadge,
16-
threadPullRequestSearchTerms,
1716
} from "@t3tools/shared/threadPullRequests";
1817
import { FolderGit2Icon, TerminalIcon } from "lucide-react";
1918
import { useRender } from "@base-ui/react/use-render";
@@ -33,7 +32,11 @@ import { linkedPullRequestDetailAtom, useSharedPullRequestSummary } from "../sta
3332
import { useThreadRunningTerminalIds } from "../state/terminalSessions";
3433
import { useUiStateStore } from "../uiStateStore";
3534
import { resolveChangeRequestPresentation } from "../sourceControlPresentation";
36-
import { resolveThreadStatusPill, type ThreadStatusPill } from "./Sidebar.logic";
35+
import {
36+
pullRequestMatchesSearchQuery,
37+
resolveThreadStatusPill,
38+
type ThreadStatusPill,
39+
} from "./Sidebar.logic";
3740
import type { SidebarThreadSummary } from "../types";
3841
import { formatWorktreePathForDisplay } from "../worktreeCleanup";
3942
import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";
@@ -670,22 +673,18 @@ export function ThreadSearchPullRequestNumber({
670673
enabled?: boolean;
671674
settled?: boolean;
672675
}) {
673-
const normalizedQuery = query.trim().toLowerCase();
674-
const matches = (candidate: Parameters<typeof threadPullRequestSearchTerms>[0]) =>
675-
normalizedQuery.length > 0 &&
676-
threadPullRequestSearchTerms(candidate).some((term) =>
677-
term.toLowerCase().includes(normalizedQuery),
678-
);
679676
const matchedLink = visibleThreadPullRequests(thread.pullRequests).find((pr) =>
680-
matches({ pullRequests: [pr] }),
677+
pullRequestMatchesSearchQuery({ pullRequests: [pr] }, query),
681678
);
682679
const fallbackCandidates =
683680
thread.pullRequests.length === 0
684681
? [thread.linkedPullRequest, thread.branchPullRequest]
685682
: [thread.branchPullRequest];
686683
const legacyReference = matchedLink
687684
? undefined
688-
: fallbackCandidates.find((pr) => pr != null && matches({ linkedPullRequest: pr }));
685+
: fallbackCandidates.find(
686+
(pr) => pr != null && pullRequestMatchesSearchQuery({ linkedPullRequest: pr }, query),
687+
);
689688
const legacyStatus = useLinkedThreadPullRequest(
690689
thread.environmentId,
691690
legacyReference,

0 commit comments

Comments
 (0)