Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 61 additions & 1 deletion frontend/src/components/SearchLayout.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,64 @@ describe("SearchLayout product events", () => {
expect(resultLookupCount).toBe(lookupCountAfterResults);
findSpy.mockRestore();
});
});

it("keeps the first search result authoritative when duplicate ids arrive", async () => {
vi.stubGlobal("fetch", vi.fn((input: RequestInfo | URL) => {
const url = String(input);
if (url.endsWith("/api/search")) {
return Promise.resolve(jsonResponse({
results: [
{
id: 303,
source_message_id: "<first-source@example.com>",
subject: "첫 번째 중복 ID 결과",
sender: "first@example.com",
date: "2026-05-20T09:00:00Z",
snippet: "첫 번째 검색 근거",
thread_id: "thread-first",
reply_count: 2,
score: 0.91,
},
{
id: 303,
source_message_id: "<second-source@example.com>",
subject: "두 번째 중복 ID 결과",
sender: "second@example.com",
date: "2026-05-20T09:01:00Z",
snippet: "두 번째 검색 근거",
thread_id: "thread-second",
reply_count: 3,
score: 0.82,
},
],
}));
}
if (url.endsWith("/api/search/answer")) {
return Promise.resolve(jsonResponse({ answer: null, citations: [] }));
}
if (url.includes("/api/ontology/relationships?")) {
return Promise.resolve(jsonResponse([]));
}
throw new Error(`Unexpected fetch: ${url}`);
}));

container = document.createElement("div");
document.body.appendChild(container);
root = createRoot(container);
await act(async () => {
root?.render(<SearchLayout />);
});

await waitForCondition(() =>
container?.querySelector("section[aria-label='맥락 검색 결과 상세'] h2") !== null,
);
expect(
container.querySelector("section[aria-label='맥락 검색 결과 상세'] h2")?.textContent,
).toBe("첫 번째 중복 ID 결과");
expect(getRecordedProductEvents().some((event) =>
event.name === "context_search_result_opened" &&
event.payload.result_id === 303 &&
event.payload.rank_bucket === "top_1",
)).toBe(true);
});
});
21 changes: 14 additions & 7 deletions frontend/src/components/SearchLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -462,12 +462,19 @@ export function SearchLayout() {
return results;
}, [activeFilter, results]);

// ⚡ Bolt: Memoize activeResult to prevent O(N) re-renders
const activeResult = useMemo(() => (
filteredResults.find((result) => result.id === activeResultId) ??
const resultById = useMemo(() => {
const map = new Map<number, { result: SearchResultItem; index: number }>();
filteredResults.forEach((result, index) => {
// Preserve the first-match semantics of the previous find/findIndex path.
if (!map.has(result.id)) map.set(result.id, { result, index });
});
Comment thread
seonghobae marked this conversation as resolved.
return map;
}, [filteredResults]);

const activeResult =
(activeResultId !== null ? resultById.get(activeResultId)?.result : null) ??
filteredResults[0] ??
null
), [filteredResults, activeResultId]);
null;
const activeOntologySourceKey = ontologySourceKey(activeResult);
const activeOntologyUrl = activeResult
? buildOntologyUrl(activeResult)
Expand Down Expand Up @@ -496,7 +503,7 @@ export function SearchLayout() {
useEffect(() => {
if (!activeResult || loading) return;

const resultIndex = filteredResults.findIndex((result) => result.id === activeResult.id);
const resultIndex = resultById.get(activeResult.id)?.index ?? -1;
const eventKey = `${searchSessionIdRef.current}:${activeResult.id}`;
if (lastOpenedResultKeyRef.current === eventKey) return;
lastOpenedResultKeyRef.current = eventKey;
Expand All @@ -509,7 +516,7 @@ export function SearchLayout() {
rank_bucket: bucketSearchRank(resultIndex < 0 ? 0 : resultIndex),
confidence: activeConfidence,
});
}, [activeConfidence, activeResult, filteredResults, loading]);
}, [activeConfidence, activeResult, resultById, loading]);

useEffect(() => {
if (!activeOntologyUrl || !activeOntologySourceKey) return;
Expand Down