|
1 | 1 | import { |
2 | 2 | lazy, |
3 | 3 | Suspense, |
4 | | - useCallback, |
5 | 4 | useEffect, |
6 | | - useMemo, |
7 | 5 | useRef, |
8 | 6 | useState, |
9 | 7 | type ComponentType, |
@@ -113,22 +111,19 @@ export function SettingsSidebarNav({ pathname }: { pathname: string }) { |
113 | 111 | const navigate = useNavigate(); |
114 | 112 | const currentHash = useLocation({ select: (location) => location.hash }); |
115 | 113 | const currentSearch = useLocation({ select: (location) => location.search }); |
116 | | - const scopeSearch = useMemo(() => validateSettingsScopeSearch(currentSearch), [currentSearch]); |
| 114 | + const scopeSearch = validateSettingsScopeSearch(currentSearch); |
117 | 115 | const navItems = SETTINGS_NAV_ITEMS.filter( |
118 | 116 | (item) => item.to !== "/settings/projects" || isSettingsOverviewVisible(scopeSearch), |
119 | 117 | ); |
120 | 118 | const { isMobile, setOpenMobile, open, setOpen } = useSidebar(); |
121 | 119 | const searchInputRef = useRef<HTMLInputElement>(null); |
122 | 120 | const [query, setQuery] = useState(""); |
123 | | - const [activeResultIndex, setActiveResultIndex] = useState(0); |
| 121 | + const [storedActiveResultIndex, setActiveResultIndex] = useState(0); |
124 | 122 | const searchableItems = useAvailableSettingsSearchItems(scopeSearch); |
125 | | - const results = useMemo(() => searchSettings(query, searchableItems), [query, searchableItems]); |
| 123 | + const results = searchSettings(query, searchableItems); |
126 | 124 | const isSearching = query.trim().length > 0; |
127 | 125 | const hasResults = results.length > 0; |
128 | | - |
129 | | - useEffect(() => { |
130 | | - setActiveResultIndex((index) => Math.min(index, Math.max(results.length - 1, 0))); |
131 | | - }, [results.length]); |
| 126 | + const activeResultIndex = Math.min(storedActiveResultIndex, Math.max(results.length - 1, 0)); |
132 | 127 |
|
133 | 128 | useEffect(() => { |
134 | 129 | const result = results[activeResultIndex]; |
@@ -179,72 +174,66 @@ export function SettingsSidebarNav({ pathname }: { pathname: string }) { |
179 | 174 | return () => window.removeEventListener("keydown", handleKeyDown); |
180 | 175 | }, [isMobile, open, setOpen, setOpenMobile]); |
181 | 176 |
|
182 | | - const handleSectionClick = useCallback( |
183 | | - (to: SettingsPath) => { |
184 | | - if (isMobile) { |
185 | | - setOpenMobile(false); |
186 | | - } |
187 | | - void navigate({ |
188 | | - to, |
189 | | - hash: "", |
190 | | - replace: true, |
191 | | - hashScrollIntoView: false, |
192 | | - }); |
193 | | - }, |
194 | | - [isMobile, navigate, setOpenMobile], |
195 | | - ); |
196 | | - const clearSearch = useCallback(() => { |
| 177 | + const handleSectionClick = (to: SettingsPath) => { |
| 178 | + if (isMobile) { |
| 179 | + setOpenMobile(false); |
| 180 | + } |
| 181 | + void navigate({ |
| 182 | + to, |
| 183 | + hash: "", |
| 184 | + replace: true, |
| 185 | + hashScrollIntoView: false, |
| 186 | + }); |
| 187 | + }; |
| 188 | + |
| 189 | + const clearSearch = () => { |
197 | 190 | setQuery(""); |
198 | 191 | setActiveResultIndex(0); |
199 | | - }, []); |
200 | | - const handleSearchResultClick = useCallback( |
201 | | - (item: SettingsSearchItem) => { |
| 192 | + }; |
| 193 | + const handleSearchResultClick = (item: SettingsSearchItem) => { |
| 194 | + clearSearch(); |
| 195 | + if (isMobile) { |
| 196 | + setOpenMobile(false); |
| 197 | + } |
| 198 | + const targetId = item.targetId ?? item.id; |
| 199 | + if (pathname === item.to && currentHash.replace(/^#/, "") === targetId) { |
| 200 | + scrollToSettingsTarget(targetId); |
| 201 | + return; |
| 202 | + } |
| 203 | + void navigate({ |
| 204 | + to: item.to, |
| 205 | + hash: targetId, |
| 206 | + replace: true, |
| 207 | + hashScrollIntoView: false, |
| 208 | + state: { settingsTargetHighlight: true }, |
| 209 | + }); |
| 210 | + }; |
| 211 | + |
| 212 | + const handleSearchKeyDown = (event: KeyboardEvent<HTMLInputElement>) => { |
| 213 | + if (event.key === "Escape" && isSearching) { |
| 214 | + event.preventDefault(); |
| 215 | + event.stopPropagation(); |
202 | 216 | clearSearch(); |
203 | | - if (isMobile) { |
204 | | - setOpenMobile(false); |
205 | | - } |
206 | | - const targetId = item.targetId ?? item.id; |
207 | | - if (pathname === item.to && currentHash.replace(/^#/, "") === targetId) { |
208 | | - scrollToSettingsTarget(targetId); |
209 | | - return; |
210 | | - } |
211 | | - void navigate({ |
212 | | - to: item.to, |
213 | | - hash: targetId, |
214 | | - replace: true, |
215 | | - hashScrollIntoView: false, |
216 | | - state: { settingsTargetHighlight: true }, |
217 | | - }); |
218 | | - }, |
219 | | - [clearSearch, currentHash, isMobile, navigate, pathname, setOpenMobile], |
220 | | - ); |
221 | | - const handleSearchKeyDown = useCallback( |
222 | | - (event: KeyboardEvent<HTMLInputElement>) => { |
223 | | - if (event.key === "Escape" && isSearching) { |
224 | | - event.preventDefault(); |
225 | | - event.stopPropagation(); |
226 | | - clearSearch(); |
227 | | - return; |
228 | | - } |
229 | | - if (results.length === 0) return; |
230 | | - if (event.key === "ArrowDown") { |
231 | | - event.preventDefault(); |
232 | | - setActiveResultIndex((index) => (index + 1) % results.length); |
233 | | - return; |
234 | | - } |
235 | | - if (event.key === "ArrowUp") { |
236 | | - event.preventDefault(); |
237 | | - setActiveResultIndex((index) => (index - 1 + results.length) % results.length); |
238 | | - return; |
239 | | - } |
240 | | - if (event.key === "Enter") { |
241 | | - event.preventDefault(); |
242 | | - const result = results[activeResultIndex]; |
243 | | - if (result) handleSearchResultClick(result); |
244 | | - } |
245 | | - }, |
246 | | - [activeResultIndex, clearSearch, handleSearchResultClick, isSearching, results], |
247 | | - ); |
| 217 | + return; |
| 218 | + } |
| 219 | + if (results.length === 0) return; |
| 220 | + if (event.key === "ArrowDown") { |
| 221 | + event.preventDefault(); |
| 222 | + setActiveResultIndex((activeResultIndex + 1) % results.length); |
| 223 | + return; |
| 224 | + } |
| 225 | + if (event.key === "ArrowUp") { |
| 226 | + event.preventDefault(); |
| 227 | + setActiveResultIndex((activeResultIndex - 1 + results.length) % results.length); |
| 228 | + return; |
| 229 | + } |
| 230 | + if (event.key === "Enter") { |
| 231 | + event.preventDefault(); |
| 232 | + const result = results[activeResultIndex]; |
| 233 | + if (result) handleSearchResultClick(result); |
| 234 | + } |
| 235 | + }; |
| 236 | + |
248 | 237 | return ( |
249 | 238 | <> |
250 | 239 | <SidebarContent className="overflow-x-hidden"> |
|
0 commit comments