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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Server will start on port 3000 (or use PORT environment variable to customize)
- `VITE_GA_ID`: Optional GA4 measurement ID for the Vite web app. Example: `G-XXXXXXXXXX`. If unset, production builds use the project's default ID in `src/analytics.ts`. Development builds send nothing.
- Events: `page_view`, `search` (`search_scope`: `single`/`group`/`all`, `library_count`), `search_complete` (`library_count`, `failed_count`, `result_count`, `available_count`, `duration_ms`), `search_cancel` (`library_count`, `completed_count`, `duration_ms`), `my_library_select`
- Entry source: open the app with `?ref=<source>` (e.g. `?ref=ext` for the Chrome extension, `?ref=bm` for the bookmarklet; lowercase letters, digits, `-`, `_`, up to 20 chars). The first `page_view` gets `utm_source=<source>` in `page_location`, so GA4 shows it as the session source. `page_view` and `search` also carry `entry_ref`; register it as an event-scoped custom dimension in GA4 to break down searches by source.
- Bookmarklet: the bottom of the page shows a "동네 도서관에서 찾기" button to drag to the bookmarks bar. On any site, select a book title (or type one when nothing is selected) and click it to open `/?title=<title>&ref=bm`. When the app is opened with `ref` but no `library`, it searches the user's "내 도서관" (up to 5 recently searched libraries, stored in this site's localStorage) instead of every library; with no saved libraries it only fills in the title. Older bookmarklets that include `library` keep searching that library.
- Bookmarklet: the bottom of the page shows a "동네 도서관에서 찾기" button to drag to the bookmarks bar. On any site, select a book title (or type one when nothing is selected) and click it to open `/?title=<title>&ref=bm`. When the app is opened with `ref` but no `library`, it searches the user's "내 도서관" (up to 5 recently searched libraries, stored in this site's localStorage) instead of every library; with no saved libraries it only fills in the title. The search wizard shows that scope as "내 도서관 (…)" and the search button keeps searching those libraries until the library, region, module or filter selection changes. Older bookmarklets that include `library` keep searching that library.

## Library Regions

Expand Down
2 changes: 2 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const App = () => {
libraryName,
setLibraryName,
handleSearch,
scopeLibraries,
} = useSearchManager({
libraryNames,
filteredLibraries,
Expand Down Expand Up @@ -168,6 +169,7 @@ const App = () => {
onSearch={handleSearch}
onCancel={cancelSearch}
isLoading={isLoading}
scopeLibraries={scopeLibraries}
/>
<SearchProgressBar progress={searchProgress} />
<LibraryTagFilter
Expand Down
17 changes: 17 additions & 0 deletions src/components/StepWizard.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,23 @@ describe("StepWizard", () => {
expect(screen.getByTestId("step-3-content")).toHaveTextContent("전체 도서관");
});

it("shows '내 도서관' with names in step 3 summary when my-library scope is active", async () => {
const user = userEvent.setup();
const scopeLibraries = [
{ id: 0, name: "가양도서관" },
{ id: 1, name: "판교도서관" },
];
render(
<StepWizard {...defaultProps} searchText="해리포터" filterText="" scopeLibraries={scopeLibraries} />,
);
fireEvent.keyDown(screen.getByTestId("search-input"), { key: "Enter" });
await user.click(screen.getByTestId("step-2-next"));

const step3 = screen.getByTestId("step-3-content");
expect(step3).toHaveTextContent("내 도서관 (가양도서관, 판교도서관)");
expect(step3).not.toHaveTextContent("전체 도서관");
});

it("shows filtered library names in step 2 summary when filter is active", async () => {
const user = userEvent.setup();
const filteredLibraries = [
Expand Down
7 changes: 6 additions & 1 deletion src/components/StepWizard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ interface StepWizardProps {
onSearch: () => void;
onCancel: () => void;
isLoading: boolean;
/** 내 도서관 범위로 검색 중이면 그 도서관들 (북마클릿·확장으로 들어왔을 때) */
scopeLibraries?: Library[];
}

export const StepWizard = ({
Expand All @@ -65,6 +67,7 @@ export const StepWizard = ({
onSearch,
onCancel,
isLoading,
scopeLibraries,
}: StepWizardProps) => {
const { currentStep, isStepCompleted, completeStep, goToStep } =
useStepWizard();
Expand Down Expand Up @@ -130,7 +133,9 @@ export const StepWizard = ({
const parts = [];
if (selectedSido) parts.push([selectedSido, selectedSigungu].filter(Boolean).join(" "));
if (selectedModule) parts.push(selectedModule);
if (selectedLibrary === DEFAULT_LIBRARY) {
if (scopeLibraries) {
parts.push(`내 도서관 (${scopeLibraries.map((lib) => lib.name).join(", ")})`);
} else if (selectedLibrary === DEFAULT_LIBRARY) {
if (filterText?.trim() && filteredLibraries.length > 0) {
parts.push(filteredLibraries.map((lib) => lib.name).join(", "));
} else {
Expand Down
32 changes: 32 additions & 0 deletions src/hooks/useSearchManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,38 @@ describe("useSearchManager", () => {
expect(result.current.searchText).toBe("해리포터");
});

it("북마클릿 범위가 살아 있으면 검색 버튼도 내 도서관만 검색한다", () => {
vi.mocked(getUrlParams).mockReturnValue({ title: "해리포터", library: DEFAULT_LIBRARY, ref: "bm" });

const params = createMockParams({ myLibraries: ["강남도서관"] });
const { result } = renderHook(() => useSearchManager(params));
expect(result.current.scopeLibraries).toEqual([{ id: 2, name: "강남도서관" }]);

act(() => result.current.handleSearch());

expect(params.performSearch).toHaveBeenLastCalledWith("해리포터", DEFAULT_LIBRARY, [
{ id: 2, name: "강남도서관" },
]);
});

it("도서관이나 필터 목록을 바꾸면 내 도서관 범위가 풀린다", () => {
vi.mocked(getUrlParams).mockReturnValue({ title: "해리포터", library: DEFAULT_LIBRARY, ref: "bm" });

const params = createMockParams({ myLibraries: ["강남도서관"] });
const { result, rerender } = renderHook((p: SearchManagerParams) => useSearchManager(p), {
initialProps: params,
});

rerender({ ...params, filteredLibraries: filteredYeoju });
expect(result.current.scopeLibraries).toBeUndefined();
act(() => result.current.handleSearch());
expect(params.performSearch).toHaveBeenLastCalledWith("해리포터", DEFAULT_LIBRARY, filteredYeoju);

rerender(params);
act(() => result.current.setLibraryName("서울도서관"));
expect(result.current.scopeLibraries).toBeUndefined();
});

it("북마클릿으로 들어왔는데 내 도서관이 없으면 제목만 채우고 검색하지 않는다", () => {
vi.mocked(getUrlParams).mockReturnValue({ title: "해리포터", library: DEFAULT_LIBRARY, ref: "bm" });

Expand Down
29 changes: 24 additions & 5 deletions src/hooks/useSearchManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ export const useSearchManager = (params: {
const initialParams = getUrlParams();
const [searchText, setSearchText] = useState(initialParams.title);
const [libraryName, setLibraryName] = useState(initialParams.library);
// 북마클릿·확장으로 들어와 내 도서관을 검색한 범위. 그때의 선택(도서관·필터 목록)을 함께 기억해,
// 사용자가 도서관·지역·모듈·필터를 바꾸면 자연히 풀린다 (effect 로 지우지 않는다).
const [myScope, setMyScope] = useState<{
libraries: Library[];
libraryName: string;
filteredLibraries: Library[];
} | null>(null);
const scopeLibraries =
myScope && myScope.libraryName === libraryName && myScope.filteredLibraries === filteredLibraries
? myScope.libraries
: undefined;
const initialSearchFiredRef = useRef(false);

useEffect(() => {
Expand Down Expand Up @@ -57,24 +68,29 @@ export const useSearchManager = (params: {
// 북마클릿·확장은 도서관을 모른다. 전체(1,342곳) 대신 내 도서관을 검색하고,
// 내 도서관이 없으면 제목만 채워 두고 도서관을 고르게 한다.
const mine = libraryNames.filter((lib) => myLibraries.includes(lib.name));
if (mine.length > 0) performSearch(title, DEFAULT_LIBRARY, mine);
if (mine.length > 0) {
setMyScope({ libraries: mine, libraryName: DEFAULT_LIBRARY, filteredLibraries });
performSearch(title, DEFAULT_LIBRARY, mine);
}
return;
}
performSearch(title, library, libraryNames);
}
}
}, [libraryNames, performSearch, myLibraries]);
}, [libraryNames, filteredLibraries, performSearch, myLibraries]);

const handleSearch = useCallback(() => {
if (!searchText?.length) return;

// 내 도서관 범위가 살아 있으면 그 도서관들을 다시 검색한다
const libraries = scopeLibraries ?? filteredLibraries;
resetFilters();
addToHistory(searchText);
updateUrl(searchText, libraryName);
const scope: SearchScope =
libraryName !== DEFAULT_LIBRARY
? "single"
: filteredLibraries.length === libraryNames.length
: libraries.length === libraryNames.length
? "all"
: "group";
if (scope === "single") {
Expand All @@ -84,15 +100,16 @@ export const useSearchManager = (params: {
searchText,
libraryName,
scope,
scope === "single" ? 1 : filteredLibraries.length,
scope === "single" ? 1 : libraries.length,
);

performSearch(searchText, libraryName, filteredLibraries);
performSearch(searchText, libraryName, libraries);
}, [
searchText,
libraryName,
libraryNames,
filteredLibraries,
scopeLibraries,
performSearch,
resetFilters,
addToHistory,
Expand All @@ -105,5 +122,7 @@ export const useSearchManager = (params: {
libraryName,
setLibraryName,
handleSearch,
/** 내 도서관 범위로 검색 중이면 그 도서관들 */
scopeLibraries,
};
};
Loading