@@ -27,44 +27,46 @@ export const ObjectsDiff = ({diffType, repoId, leftRef, rightRef, path}) => {
27
27
if ( hooksError ) return < AlertError error = { hooksError } /> ;
28
28
29
29
const readable = readableObject ( path ) ;
30
- let left ;
31
- let right ;
32
- switch ( diffType ) {
33
- case 'changed' :
34
- case 'conflict' :
35
- left = useAPI ( async ( ) => objects . getStat ( repoId , leftRef , path ) ,
36
- [ repoId , leftRef , path ] ) ;
37
- right = useAPI ( async ( ) => objects . getStat ( repoId , rightRef , path ) ,
38
- [ repoId , rightRef , path ] ) ;
39
- break ;
40
- case 'added' :
41
- right = useAPI ( async ( ) => objects . getStat ( repoId , rightRef , path ) ,
42
- [ repoId , rightRef , path ] ) ;
43
- break ;
44
- case 'removed' :
45
- left = useAPI ( async ( ) => objects . getStat ( repoId , leftRef , path ) ,
46
- [ repoId , leftRef , path ] ) ;
47
- break ;
48
- default :
49
- return < AlertError error = { "Unsupported diff type " + diffType } /> ;
30
+ // Always call hooks at the top level
31
+ const leftStatResponse = useAPI ( async ( ) => {
32
+ if ( diffType === 'changed' || diffType === 'conflict' || diffType === 'removed' ) {
33
+ return objects . getStat ( repoId , leftRef , path ) ;
34
+ }
35
+ return null ;
36
+ } , [ repoId , leftRef , path , diffType ] ) ;
37
+
38
+ const rightStatResponse = useAPI ( async ( ) => {
39
+ if ( diffType === 'changed' || diffType === 'conflict' || diffType === 'added' ) {
40
+ return objects . getStat ( repoId , rightRef , path ) ;
41
+ }
42
+ return null ;
43
+ } , [ repoId , rightRef , path , diffType ] ) ;
44
+
45
+ if ( ! [ 'changed' , 'conflict' , 'added' , 'removed' ] . includes ( diffType ) ) {
46
+ return < AlertError error = { "Unsupported diff type " + diffType } /> ;
50
47
}
51
48
49
+ let left = leftStatResponse ;
50
+ let right = rightStatResponse ;
51
+
52
+ const hooksLoading = ( left && left . loading ) || ( right && right . loading ) ;
53
+
52
54
if ( hooksLoading || ( left && left . loading ) || ( right && right . loading ) ) return < Loading /> ;
53
55
const err = ( left && left . error ) || ( right && right . err ) ;
54
56
if ( err ) return < AlertError error = { err } /> ;
55
57
56
- const leftStat = left && left . response ;
57
- const rightStat = right && right . response ;
58
+ const leftStatResponseResponse = left && left . response ;
59
+ const rightStatResponseResponse = right && right . response ;
58
60
if ( ! readable ) {
59
- return < NoContentDiff left = { leftStat } right = { rightStat } diffType = { diffType } /> ;
61
+ return < NoContentDiff left = { leftStatResponseResponse } right = { rightStatResponseResponse } diffType = { diffType } /> ;
60
62
}
61
- const objectTooBig = ( leftStat && leftStat . size_bytes > maxDiffSizeBytes ) || ( rightStat && rightStat . size_bytes > maxDiffSizeBytes ) ;
63
+ const objectTooBig = ( leftStatResponse && leftStatResponse . size_bytes > maxDiffSizeBytes ) || ( rightStatResponse && rightStatResponse . size_bytes > maxDiffSizeBytes ) ;
62
64
if ( objectTooBig ) {
63
65
return < AlertError error = { path + " is too big (> " + humanSize ( maxDiffSizeBytes ) + "). To view its diff please download" +
64
66
" the objects and use an external diff tool." } />
65
67
}
66
- const leftSize = leftStat && leftStat . size_bytes ;
67
- const rightSize = rightStat && rightStat . size_bytes ;
68
+ const leftSize = leftStatResponse && leftStatResponse . size_bytes ;
69
+ const rightSize = rightStatResponse && rightStatResponse . size_bytes ;
68
70
return < ContentDiff
69
71
config = { storageConfig }
70
72
repoId = { repoId }
@@ -122,10 +124,19 @@ const ContentDiff = ({config, repoId, path, leftRef, rightRef, leftSize, rightSi
122
124
123
125
124
126
const TextDiff = ( { config, repoId, path, leftRef, rightRef, leftSize, rightSize, diffType, isDarkMode } ) => {
125
- const left = leftRef && useAPI ( async ( ) => objects . get ( repoId , leftRef , path , config . pre_sign_support_ui ) ,
126
- [ repoId , leftRef , path ] ) ;
127
- const right = rightRef && useAPI ( async ( ) => objects . get ( repoId , rightRef , path , config . pre_sign_support_ui ) ,
128
- [ repoId , rightRef , path ] ) ;
127
+ const left = useAPI ( async ( ) => {
128
+ if ( leftRef ) {
129
+ return objects . get ( repoId , leftRef , path , config . pre_sign_support_ui ) ;
130
+ }
131
+ return null ;
132
+ } , [ repoId , leftRef , path , config . pre_sign_support_ui ] ) ;
133
+
134
+ const right = useAPI ( async ( ) => {
135
+ if ( rightRef ) {
136
+ return objects . get ( repoId , rightRef , path , config . pre_sign_support_ui ) ;
137
+ }
138
+ return null ;
139
+ } , [ repoId , rightRef , path , config . pre_sign_support_ui ] ) ;
129
140
130
141
if ( ( left && left . loading ) || ( right && right . loading ) ) return < Loading /> ;
131
142
const err = ( left && left . error ) || ( right && right . error ) ;
0 commit comments