1- import { useEffect , useRef } from "react" ;
1+ import { useCallback } from "react" ;
22
33interface UseSearchDrawerInitProps {
4- open : boolean ;
54 currentPath : string ;
65 search : {
76 setKeyword : ( value : string ) => void ;
@@ -19,44 +18,34 @@ const SEARCH_INPUT_ID = "repo-search-keyword";
1918
2019/**
2120 * 搜索抽屉初始化 Hook
22- * 处理打开/关闭时的状态重置和自动填充逻辑
21+ * 返回打开完成后的初始化处理函数
2322 */
2423export const useSearchDrawerInit = ( {
25- open,
2624 currentPath,
2725 search,
2826 setPathPrefix,
2927 setExtensionInput,
3028 setFiltersExpanded,
31- } : UseSearchDrawerInitProps ) : void => {
32- const previousOpenRef = useRef ( false ) ;
33-
34- useEffect ( ( ) => {
35- const wasOpen = previousOpenRef . current ;
36-
37- if ( open && ! wasOpen ) {
38- // 初始化搜索索引(懒加载)
39- search . initializeIndex ( ) ;
40-
41- // 重置所有筛选条件
42- search . setKeyword ( "" ) ;
43- search . setBranchFilter ( [ ] ) ;
44- search . setExtensionFilter ( [ ] ) ;
45- setExtensionInput ( "" ) ;
46- search . clearResults ( ) ;
47- setFiltersExpanded ( false ) ;
48-
49- // 自动填充当前路径
50- const trimmedCurrentPath = currentPath . trim ( ) ;
51- setPathPrefix ( trimmedCurrentPath . length > 0 ? trimmedCurrentPath : "" ) ;
52-
53- // 聚焦搜索框
54- setTimeout ( ( ) => {
55- const input = document . getElementById ( SEARCH_INPUT_ID ) as HTMLInputElement | null ;
56- input ?. focus ( ) ;
57- } , 100 ) ;
58- }
59-
60- previousOpenRef . current = open ;
61- } , [ open , currentPath , setPathPrefix , search , setExtensionInput , setFiltersExpanded ] ) ;
62- } ;
29+ } : UseSearchDrawerInitProps ) : ( ( ) => void ) =>
30+ useCallback ( ( ) => {
31+ // 初始化搜索索引(懒加载)
32+ search . initializeIndex ( ) ;
33+
34+ // 重置所有筛选条件
35+ search . setKeyword ( "" ) ;
36+ search . setBranchFilter ( [ ] ) ;
37+ search . setExtensionFilter ( [ ] ) ;
38+ setExtensionInput ( "" ) ;
39+ search . clearResults ( ) ;
40+ setFiltersExpanded ( false ) ;
41+
42+ // 自动填充当前路径
43+ const trimmedCurrentPath = currentPath . trim ( ) ;
44+ setPathPrefix ( trimmedCurrentPath . length > 0 ? trimmedCurrentPath : "" ) ;
45+
46+ // 聚焦搜索框
47+ setTimeout ( ( ) => {
48+ const input = document . getElementById ( SEARCH_INPUT_ID ) as HTMLInputElement | null ;
49+ input ?. focus ( ) ;
50+ } , 100 ) ;
51+ } , [ currentPath , search , setExtensionInput , setFiltersExpanded , setPathPrefix ] ) ;
0 commit comments