Skip to content

Commit 95351f9

Browse files
committed
清理派生状态 Effect
1 parent 7ae9a98 commit 95351f9

11 files changed

Lines changed: 391 additions & 272 deletions

File tree

src/components/file/FileList.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,7 @@ const FileList = React.memo<FileListProps>(
6464
const [showAlphabetIndex, setShowAlphabetIndex] = React.useState(false);
6565
const [highlightedIndex, setHighlightedIndex] = React.useState<number | null>(null);
6666
const highlightTimeoutRef = React.useRef<NodeJS.Timeout | null>(null);
67-
68-
// 当预览状态变化时,自动关闭字母索引
69-
React.useEffect(() => {
70-
if (isPreviewActive) {
71-
setShowAlphabetIndex(false);
72-
}
73-
}, [isPreviewActive]);
67+
const visibleAlphabetIndex = showAlphabetIndex && !isPreviewActive;
7468

7569
// 计算每个文件项的高度(包括间距)
7670
// 这个计算需要与 FileListItem 的实际高度保持一致
@@ -390,7 +384,7 @@ const FileList = React.memo<FileListProps>(
390384
<AlphabetIndex
391385
contents={contents}
392386
onScrollToIndex={handleScrollToIndex}
393-
visible={showAlphabetIndex}
387+
visible={visibleAlphabetIndex}
394388
/>
395389
</Box>
396390
</Box>
Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { useEffect, useRef } from "react";
1+
import { useCallback } from "react";
22

33
interface 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
*/
2423
export 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]);

src/components/interactions/SearchDrawer/index.tsx

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,7 @@ export const SearchDrawer: React.FC<SearchDrawerProps> = ({ open, onClose }) =>
5252
const { selectFile } = usePreviewContext();
5353

5454
// 解构搜索相关状态
55-
const {
56-
branchFilter,
57-
availableBranches,
58-
indexStatus,
59-
pathPrefix,
60-
refreshIndexStatus,
61-
setPathPrefix,
62-
} = search;
55+
const { branchFilter, availableBranches, indexStatus, pathPrefix, refreshIndexStatus } = search;
6356
const { enabled, loading, error, ready, indexedBranches, lastUpdatedAt } = indexStatus;
6457

6558
// 使用自定义 hooks
@@ -80,11 +73,10 @@ export const SearchDrawer: React.FC<SearchDrawerProps> = ({ open, onClose }) =>
8073
});
8174

8275
// 初始化逻辑
83-
useSearchDrawerInit({
84-
open,
76+
const handleDialogEntered = useSearchDrawerInit({
8577
currentPath,
8678
search,
87-
setPathPrefix,
79+
setPathPrefix: search.setPathPrefix,
8880
setExtensionInput,
8981
setFiltersExpanded,
9082
});
@@ -163,6 +155,11 @@ export const SearchDrawer: React.FC<SearchDrawerProps> = ({ open, onClose }) =>
163155
m: isSmallScreen ? 2 : 3,
164156
},
165157
},
158+
transition: {
159+
onEntered: () => {
160+
handleDialogEntered();
161+
},
162+
},
166163
}}
167164
>
168165
<DialogTitle

src/components/layout/ToolbarButtons.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,8 @@ const ToolbarButtons: React.FC<ToolbarButtonsProps> = ({
9292
const storedRefreshStateRef = useRef<RefreshSessionState | null>(null);
9393
const branchValueRef = useRef(currentBranch);
9494
const pathValueRef = useRef(currentPath);
95-
96-
useEffect(() => {
97-
branchValueRef.current = currentBranch;
98-
}, [currentBranch]);
99-
100-
useEffect(() => {
101-
pathValueRef.current = currentPath;
102-
}, [currentPath]);
95+
branchValueRef.current = currentBranch;
96+
pathValueRef.current = currentPath;
10397

10498
const buildRefreshSessionState = useCallback(
10599
(): RefreshSessionState => ({

src/hooks/github/useBranchManagement.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ export function useBranchManagement(): BranchManagementState {
3232
const [branchError, setBranchError] = useState<string | null>(null);
3333

3434
const currentBranchRef = useRef<string>(currentBranch);
35-
36-
useEffect(() => {
37-
currentBranchRef.current = currentBranch;
38-
}, [currentBranch]);
35+
currentBranchRef.current = currentBranch;
3936

4037
const mergeBranchList = useCallback((branchesToMerge: string[]) => {
4138
setBranches((prev) => {

src/hooks/github/useContentLoading.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,6 @@ export function useContentLoading(path: string, branch: string): ContentLoadingS
3939
const forceRefreshRef = useRef<boolean>(false);
4040
const revalidateKeyRef = useRef<string | null>(null);
4141

42-
useEffect(() => {
43-
currentPathRef.current = path;
44-
}, [path]);
45-
46-
useEffect(() => {
47-
currentBranchRef.current = branch;
48-
}, [branch]);
49-
5042
const buildContentsSignature = useCallback((data: GitHubContent[]): string => {
5143
if (data.length === 0) {
5244
return "";
@@ -55,10 +47,10 @@ export function useContentLoading(path: string, branch: string): ContentLoadingS
5547
return data.map((item) => `${item.path}:${item.sha}:${item.type}`).join("|");
5648
}, []);
5749

58-
useEffect(() => {
59-
contentsRef.current = contents;
60-
lastSignatureRef.current = buildContentsSignature(contents);
61-
}, [contents, buildContentsSignature]);
50+
currentPathRef.current = path;
51+
currentBranchRef.current = branch;
52+
contentsRef.current = contents;
53+
lastSignatureRef.current = buildContentsSignature(contents);
6254

6355
const displayError = useCallback((message: string) => {
6456
setError(message);

src/hooks/github/usePathManagement.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,8 @@ export function usePathManagement(branch: string): PathManagementState {
6161
const currentBranchRef = useRef<string>(branch);
6262
const isRefreshInProgressRef = useRef(false);
6363
const refreshTargetPathRef = useRef<string | null>(null);
64-
65-
useEffect(() => {
66-
currentPathRef.current = currentPath;
67-
}, [currentPath]);
68-
69-
useEffect(() => {
70-
currentBranchRef.current = branch;
71-
}, [branch]);
64+
currentPathRef.current = currentPath;
65+
currentBranchRef.current = branch;
7266

7367
const setCurrentPath = useCallback((path: string, direction: NavigationDirection = "none") => {
7468
if (isRefreshInProgressRef.current && refreshTargetPathRef.current !== null) {

src/hooks/github/useReadmeContent.ts

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,18 @@ export function useReadmeContent(
3636
const readmeRequestControllerRef = useRef<AbortController | null>(null);
3737
const readmeRequestIdRef = useRef<number>(0);
3838

39+
currentPathRef.current = currentPath;
40+
loadingReadmeRef.current = loadingReadme;
41+
readmeLoadedRef.current = readmeLoaded;
42+
readmeContentRef.current = readmeContent;
43+
3944
const cancelActiveReadmeRequest = useCallback(() => {
4045
if (readmeRequestControllerRef.current !== null) {
4146
readmeRequestControllerRef.current.abort();
4247
readmeRequestControllerRef.current = null;
4348
}
4449
}, []);
4550

46-
useEffect(() => {
47-
currentPathRef.current = currentPath;
48-
}, [currentPath]);
49-
5051
useEffect(() => {
5152
if (currentBranchRef.current !== currentBranch) {
5253
cancelActiveReadmeRequest();
@@ -183,19 +184,6 @@ export function useReadmeContent(
183184
}
184185
}, [cancelActiveReadmeRequest, contents, currentBranch, loadReadmeContent]);
185186

186-
// 同步加载状态到 ref,避免将其加入主 effect 依赖
187-
useEffect(() => {
188-
loadingReadmeRef.current = loadingReadme;
189-
}, [loadingReadme]);
190-
191-
useEffect(() => {
192-
readmeLoadedRef.current = readmeLoaded;
193-
}, [readmeLoaded]);
194-
195-
useEffect(() => {
196-
readmeContentRef.current = readmeContent;
197-
}, [readmeContent]);
198-
199187
return {
200188
readmeContent,
201189
loadingReadme,

0 commit comments

Comments
 (0)