Skip to content

Commit d0b4d76

Browse files
committed
fix: 修复移动端刷新无法获取远程文件的问题
1 parent 85fba6b commit d0b4d76

3 files changed

Lines changed: 41 additions & 20 deletions

File tree

src/app/mobile/writing/custom-header.tsx

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export function WritingHeader() {
4545
fileTree,
4646
fileTreeLoading,
4747
loadFileTree,
48+
loadRemoteSyncFiles,
4849
loadCollapsibleFiles,
4950
loadFolderRemoteFiles,
5051
setCollapsibleList,
@@ -54,6 +55,7 @@ export function WritingHeader() {
5455
const [searchQuery, setSearchQuery] = useState('')
5556
const [currentDir, setCurrentDir] = useState('')
5657
const [folderLoading, setFolderLoading] = useState(false)
58+
const [isRefreshing, setIsRefreshing] = useState(false)
5759
const [entryMetaMap, setEntryMetaMap] = useState<Record<string, { modifiedAt?: string; size?: number }>>({})
5860
const hasInitializedDrawerRef = useRef(false)
5961

@@ -222,15 +224,33 @@ export function WritingHeader() {
222224
return modifiedLabel ? `${folderSummary} · ${modifiedLabel}` : folderSummary
223225
}, [entryMetaMap, formatDateTime, formatSize, tMobile])
224226

225-
const isBrowserLoading = fileTreeLoading || folderLoading || !!currentFolderNode?.loading
227+
const isBrowserLoading = fileTreeLoading || folderLoading || isRefreshing || !!currentFolderNode?.loading
226228

227229
const refreshTree = useCallback(async (dir: string) => {
228-
await loadFileTree()
229-
if (dir) {
230-
await setCollapsibleList(dir, true)
231-
await loadCollapsibleFiles(dir)
230+
setIsRefreshing(true)
231+
try {
232+
const parts = dir.split('/').filter(Boolean)
233+
const pathsToExpand = parts.map((_, index) => parts.slice(0, index + 1).join('/'))
234+
235+
for (const path of pathsToExpand) {
236+
await setCollapsibleList(path, true)
237+
}
238+
239+
await loadFileTree({ skipRemoteSync: true })
240+
await loadRemoteSyncFiles()
241+
242+
if (!dir) {
243+
return
244+
}
245+
246+
for (const path of pathsToExpand) {
247+
await loadCollapsibleFiles(path)
248+
await loadFolderRemoteFiles(path)
249+
}
250+
} finally {
251+
setIsRefreshing(false)
232252
}
233-
}, [loadFileTree, loadCollapsibleFiles, setCollapsibleList])
253+
}, [loadFileTree, loadRemoteSyncFiles, loadCollapsibleFiles, loadFolderRemoteFiles, setCollapsibleList])
234254

235255
useEffect(() => {
236256
if (!drawerOpen) {

src/stores/article-remote-sync.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ export function buildRemotePathsToLoad(expandedPaths: string[]): string[] {
22
const uniquePaths = new Set<string>([''])
33

44
for (const path of expandedPaths) {
5-
if (path) {
6-
uniquePaths.add(path)
5+
const parts = path.split('/').filter(Boolean)
6+
for (let index = 0; index < parts.length; index += 1) {
7+
uniquePaths.add(parts.slice(0, index + 1).join('/'))
78
}
89
}
910

src/stores/article.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ interface NoteState {
238238
removeLocalEntry: (relativePath: string) => boolean
239239
moveLocalEntry: (oldPath: string, newPath: string) => boolean
240240
syncOpenTabsForPathChange: (oldPath: string, newPath: string) => Promise<void>
241-
loadFileTree: () => Promise<void>
241+
loadFileTree: (options?: { skipRemoteSync?: boolean }) => Promise<void>
242242
loadRemoteSyncFiles: () => Promise<void>
243243
loadCollapsibleFiles: (folderName: string) => Promise<void>
244244
loadFolderRemoteFiles: (folderName: string) => Promise<void>
@@ -733,7 +733,7 @@ const useArticleStore = create<NoteState>((set, get) => ({
733733
set({ fileTree: [...fileTree] }) // 触发重新渲染
734734
},
735735

736-
loadFileTree: async () => {
736+
loadFileTree: async (options) => {
737737
set({ fileTreeLoading: true })
738738
set({ fileTree: [] })
739739

@@ -877,7 +877,9 @@ const useArticleStore = create<NoteState>((set, get) => ({
877877
get().initVectorIndexedFiles()
878878

879879
// 异步加载远程同步文件(不阻塞界面)
880-
get().loadRemoteSyncFiles()
880+
if (!options?.skipRemoteSync) {
881+
get().loadRemoteSyncFiles()
882+
}
881883
},
882884

883885
// 加载远程同步文件(后台任务)
@@ -923,8 +925,9 @@ const useArticleStore = create<NoteState>((set, get) => ({
923925
const collapsibleList = get().collapsibleList
924926
const pathsToLoad = buildRemotePathsToLoad(collapsibleList)
925927

926-
// 使用 Promise.all 并发请求所有路径的远程文件
927-
const loadPromises = pathsToLoad.map(async path => {
928+
// 目录树会在加载过程中逐步插入父级节点,因此这里必须按层级顺序加载。
929+
// 如果并发请求深层路径,远端子目录可能会在父目录节点尚未写入树时被跳过。
930+
for (const path of pathsToLoad) {
928931
try {
929932
let files;
930933
switch (primaryBackupMethod) {
@@ -1115,14 +1118,11 @@ const useArticleStore = create<NoteState>((set, get) => ({
11151118
}
11161119
});
11171120
}
1118-
set({ fileTree: dirs })
1121+
set({ fileTree: [...dirs] })
11191122
}
11201123
} catch {
11211124
}
1122-
});
1123-
1124-
// 等待所有远程文件加载完成
1125-
await Promise.all(loadPromises)
1125+
}
11261126
} catch {
11271127
}
11281128
},
@@ -1237,7 +1237,7 @@ const useArticleStore = create<NoteState>((set, get) => ({
12371237

12381238
// 设置子节点(可能为空)
12391239
currentFolder.children = children
1240-
set({ fileTree: cacheTree })
1240+
set({ fileTree: [...cacheTree] })
12411241

12421242
// 异步加载远程同步文件状态(不阻塞界面)
12431243
// 这将会填充仅存在于云端的文件
@@ -1406,7 +1406,7 @@ const useArticleStore = create<NoteState>((set, get) => ({
14061406

14071407
// 移除加载状态
14081408
currentFolder.loading = false
1409-
set({ fileTree: cacheTree })
1409+
set({ fileTree: [...cacheTree] })
14101410
}
14111411
}
14121412
} catch {

0 commit comments

Comments
 (0)