From ce7a4deb55bb6a9f62fda62b5dcc5a3a14ac4f71 Mon Sep 17 00:00:00 2001 From: afrontend Date: Wed, 11 Nov 2026 22:54:02 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EB=B6=81=EB=A7=88=ED=81=B4=EB=A6=BF?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EB=93=A4=EC=96=B4=EC=98=A8=20=EB=92=A4=20?= =?UTF-8?q?=EA=B2=80=EC=83=89=ED=95=98=EA=B8=B0=EB=8F=84=20=EB=82=B4=20?= =?UTF-8?q?=EB=8F=84=EC=84=9C=EA=B4=80=EC=97=90=EC=84=9C=20=EC=B0=BE?= =?UTF-8?q?=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 북마클릿으로 들어와 내 도서관을 검색한 뒤에도 마법사 요약은 '전체 도서관'이었고, 검색하기를 누르면 전체 도서관을 검색했다. - useSearchManager 가 내 도서관 범위를 기억하고 검색하기도 그 도서관들로 검색한다 - 도서관·지역·모듈·필터를 바꾸면 범위가 풀린다 (그때의 선택과 비교하는 파생 상태) - 마법사 요약에 '내 도서관 (…)' 으로 보여 준다 --- README.md | 2 +- src/App.tsx | 2 ++ src/components/StepWizard.test.tsx | 17 ++++++++++++++++ src/components/StepWizard.tsx | 7 ++++++- src/hooks/useSearchManager.test.ts | 32 ++++++++++++++++++++++++++++++ src/hooks/useSearchManager.ts | 29 ++++++++++++++++++++++----- 6 files changed, 82 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index a932c67..b4b90be 100644 --- a/README.md +++ b/README.md @@ -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=` (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=` 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=&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 diff --git a/src/App.tsx b/src/App.tsx index 77273c1..68d2dcc 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -63,6 +63,7 @@ const App = () => { libraryName, setLibraryName, handleSearch, + scopeLibraries, } = useSearchManager({ libraryNames, filteredLibraries, @@ -168,6 +169,7 @@ const App = () => { onSearch={handleSearch} onCancel={cancelSearch} isLoading={isLoading} + scopeLibraries={scopeLibraries} /> <SearchProgressBar progress={searchProgress} /> <LibraryTagFilter diff --git a/src/components/StepWizard.test.tsx b/src/components/StepWizard.test.tsx index ea494d3..1bf0a4b 100644 --- a/src/components/StepWizard.test.tsx +++ b/src/components/StepWizard.test.tsx @@ -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 = [ diff --git a/src/components/StepWizard.tsx b/src/components/StepWizard.tsx index 3e29d72..60ef995 100644 --- a/src/components/StepWizard.tsx +++ b/src/components/StepWizard.tsx @@ -39,6 +39,8 @@ interface StepWizardProps { onSearch: () => void; onCancel: () => void; isLoading: boolean; + /** 내 도서관 범위로 검색 중이면 그 도서관들 (북마클릿·확장으로 들어왔을 때) */ + scopeLibraries?: Library[]; } export const StepWizard = ({ @@ -65,6 +67,7 @@ export const StepWizard = ({ onSearch, onCancel, isLoading, + scopeLibraries, }: StepWizardProps) => { const { currentStep, isStepCompleted, completeStep, goToStep } = useStepWizard(); @@ -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 { diff --git a/src/hooks/useSearchManager.test.ts b/src/hooks/useSearchManager.test.ts index 44e9f94..7cf421b 100644 --- a/src/hooks/useSearchManager.test.ts +++ b/src/hooks/useSearchManager.test.ts @@ -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" }); diff --git a/src/hooks/useSearchManager.ts b/src/hooks/useSearchManager.ts index dec8ea3..6663241 100644 --- a/src/hooks/useSearchManager.ts +++ b/src/hooks/useSearchManager.ts @@ -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(() => { @@ -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") { @@ -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, @@ -105,5 +122,7 @@ export const useSearchManager = (params: { libraryName, setLibraryName, handleSearch, + /** 내 도서관 범위로 검색 중이면 그 도서관들 */ + scopeLibraries, }; };