Skip to content
Merged
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
37 changes: 37 additions & 0 deletions src/hooks/useSearchManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,5 +387,42 @@ describe("useSearchManager", () => {

expect(trackSearch).toHaveBeenCalledWith("해리포터", "강남도서관", "single", 1);
});

it("북마클릿으로 들어와 내 도서관을 자동 검색해도 group 으로 한 번 보낸다", () => {
vi.mocked(getUrlParams).mockReturnValue({ title: "해리포터", library: DEFAULT_LIBRARY, ref: "bm" });

const { rerender } = renderHook(() => useSearchManager(createMockParams({ myLibraries: ["강남도서관"] })));
rerender();

expect(trackSearch).toHaveBeenCalledTimes(1);
expect(trackSearch).toHaveBeenCalledWith("해리포터", DEFAULT_LIBRARY, "group", 1);
});

it("내 도서관이 없어 자동 검색하지 않으면 보내지 않는다", () => {
vi.mocked(getUrlParams).mockReturnValue({ title: "해리포터", library: DEFAULT_LIBRARY, ref: "bm" });

renderHook(() => useSearchManager(createMockParams({ myLibraries: [] })));

expect(trackSearch).not.toHaveBeenCalled();
});

it("공유 링크로 도서관과 함께 들어오면 single 로 보낸다", () => {
vi.mocked(getUrlParams).mockReturnValue({ title: "해리포터", library: "강남도서관" });

renderHook(() => useSearchManager(createMockParams()));

expect(trackSearch).toHaveBeenCalledWith("해리포터", "강남도서관", "single", 1);
});

it("뒤로 가기로 다시 검색할 때는 보내지 않는다", () => {
renderHook(() => useSearchManager(createMockParams()));
vi.mocked(getUrlParams).mockReturnValue({ title: "해리포터", library: DEFAULT_LIBRARY });

act(() => {
window.dispatchEvent(new PopStateEvent("popstate"));
});

expect(trackSearch).not.toHaveBeenCalled();
});
});
});
31 changes: 18 additions & 13 deletions src/hooks/useSearchManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ import { DEFAULT_LIBRARY } from "../constants";
import { getUrlParams, updateUrl } from "../utils/url";
import type { Library } from "../types";

/** 검색 측정용 범위. 도서관 하나면 single, 전체면 all, 그 밖(필터·내 도서관)은 group */
const searchScope = (libraryName: string, libraryCount: number, totalCount: number): SearchScope =>
libraryName !== DEFAULT_LIBRARY ? "single" : libraryCount === totalCount ? "all" : "group";

/** 검색 이벤트를 보낸다. single 이면 도서관 수는 1 */
const trackSearchScope = (title: string, libraryName: string, libraryCount: number, totalCount: number) => {
const scope = searchScope(libraryName, libraryCount, totalCount);
trackSearch(title, libraryName, scope, scope === "single" ? 1 : libraryCount);
return scope;
};

export const useSearchManager = (params: {
libraryNames: Library[];
filteredLibraries: Library[];
Expand Down Expand Up @@ -61,19 +72,24 @@ export const useSearchManager = (params: {

useEffect(() => {
if (libraryNames.length > 0 && !initialSearchFiredRef.current) {
// 첫 검색 판단은 도서관 목록이 준비됐을 때 한 번만 한다.
// 제목 없이 열린 뒤 뒤로 가기로 제목이 생겨도 여기서 다시 검색하지 않는다 (popstate 가 맡는다)
initialSearchFiredRef.current = true;
const { title, library, ref } = getUrlParams();
if (title) {
initialSearchFiredRef.current = true;
if (ref && library === DEFAULT_LIBRARY) {
// 북마클릿·확장은 도서관을 모른다. 전체(1,342곳) 대신 내 도서관을 검색하고,
// 내 도서관이 없으면 제목만 채워 두고 도서관을 고르게 한다.
const mine = libraryNames.filter((lib) => myLibraries.includes(lib.name));
if (mine.length > 0) {
setMyScope({ libraries: mine, libraryName: DEFAULT_LIBRARY, filteredLibraries });
trackSearchScope(title, DEFAULT_LIBRARY, mine.length, libraryNames.length);
performSearch(title, DEFAULT_LIBRARY, mine);
}
return;
}
// URL 로 들어온 자동 검색(북마클릿·공유 링크)도 검색 버튼과 같이 측정한다
trackSearchScope(title, library, libraryNames.length, libraryNames.length);
performSearch(title, library, libraryNames);
}
}
Expand All @@ -87,21 +103,10 @@ export const useSearchManager = (params: {
resetFilters();
addToHistory(searchText);
updateUrl(searchText, libraryName);
const scope: SearchScope =
libraryName !== DEFAULT_LIBRARY
? "single"
: libraries.length === libraryNames.length
? "all"
: "group";
const scope = trackSearchScope(searchText, libraryName, libraries.length, libraryNames.length);
if (scope === "single") {
addToMyLibraries?.(libraryName);
}
trackSearch(
searchText,
libraryName,
scope,
scope === "single" ? 1 : libraries.length,
);

performSearch(searchText, libraryName, libraries);
}, [
Expand Down
Loading