Skip to content

Commit 861b8cb

Browse files
committed
fix(repo-view): Sync repo ref from URL and default branch
- Use ref from URL to load image file tree and update repo store when available - Fallback to default branch when ref is missing in URL to keep repo state consistent - Prevent mismatched repo ref between URL, store and image loading pipeline
1 parent 4957b65 commit 861b8cb

1 file changed

Lines changed: 12 additions & 33 deletions

File tree

src/hooks/features/repo-view/useRepoLoading.ts

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,65 +15,44 @@ export function useRepoLoading() {
1515
const getDefaultBranch = usePromise(useGithubDefaultBranch())[1]
1616
const isLoadImagePaths = usePromise(useGithubImageFileTree())[0]
1717
const getImagePaths = usePromise(useGithubImageFileTree())[1]
18-
const repoFromStore = useRepoStore(state => state.repo)
19-
const repoFromUrl = useTargetRepository()[0]
18+
const targetRepository = useTargetRepository()[0]
2019
const setRepo = useRepoStore(state => state.setRepo)
2120
const setImageFiles = useRepoStore(state => state.setImageFiles)
2221
const setError = useRepoStore(state => state.setError)
2322

2423
useEffect(() => {
25-
// Only proceed if we have owner and name
26-
if (!repoFromStore.owner || !repoFromStore.name) {
27-
return
28-
}
29-
30-
// Check if ref is provided in URL first (most reliable source)
31-
// This prevents unnecessary defaultBranch lookup when branch is already in URL
32-
const refFromUrl = repoFromUrl.ref?.trim()
33-
const refFromStore = repoFromStore.ref?.trim()
24+
const ref = targetRepository.ref?.trim()
3425

35-
if (refFromUrl) {
36-
// Ref is provided in URL, directly fetch image files
37-
// Ensure store is synced with URL
38-
if (refFromUrl !== refFromStore) {
39-
setRepo({...repoFromStore, ref: refFromUrl})
40-
}
41-
getImagePaths({...repoFromStore, ref: refFromUrl})
42-
.then(imageFileTree => {
43-
setError(null)
44-
setImageFiles(imageFileTree)
45-
})
46-
.catch(err => {
47-
setError(err instanceof Error ? err : new Error(String(err)))
48-
})
49-
} else if (refFromStore) {
50-
// Ref is in store but not in URL, use store ref
51-
getImagePaths(repoFromStore)
26+
// Get image files if ref is provided in URL
27+
if (ref) {
28+
setRepo(targetRepository)
29+
getImagePaths(targetRepository)
5230
.then(imageFileTree => {
5331
setError(null)
5432
setImageFiles(imageFileTree)
5533
})
5634
.catch(err => {
5735
setError(err instanceof Error ? err : new Error(String(err)))
5836
})
37+
return
5938
} else {
6039
// No ref provided, need to fetch default branch
61-
getDefaultBranch(repoFromUrl)
40+
getDefaultBranch(targetRepository)
6241
.then(defaultBranch => {
6342
setError(null)
6443
setTargetRepository(
65-
repoFromUrl.owner,
66-
repoFromUrl.name,
44+
targetRepository.owner,
45+
targetRepository.name,
6746
defaultBranch,
6847
)
48+
setRepo({...targetRepository, ref: defaultBranch})
6949
})
7050
.catch(err => {
7151
setError(err instanceof Error ? err : new Error(String(err)))
7252
})
7353
}
7454
}, [
75-
repoFromStore,
76-
repoFromUrl,
55+
targetRepository,
7756
getDefaultBranch,
7857
getImagePaths,
7958
setTargetRepository,

0 commit comments

Comments
 (0)