@@ -9,12 +9,12 @@ import useDeepCompareEffect from 'use-deep-compare-effect';
9
9
import { useDeepCompareCallback } from 'use-deep-compare' ;
10
10
11
11
import {
12
- getContentFromFile , saveFile , ensureFile , deleteFile ,
12
+ saveFile , ensureFile , deleteFile ,
13
13
} from './helpers' ;
14
14
import {
15
15
FileCard , FileForm , useBlob , RepositoryContext ,
16
16
} from '..' ;
17
- import { fetchCatalogContent } from './dcsCatalogNextApis ' ;
17
+ import useFileContent from './useFileContent ' ;
18
18
19
19
function useFile ( {
20
20
authentication,
@@ -37,6 +37,13 @@ function useFile({
37
37
const { actions : { updateBranch } , config : repositoryConfig } = useContext ( RepositoryContext ) ;
38
38
39
39
const config = _config || repositoryConfig ;
40
+ const { state : { content, publishedContent } } = useFileContent ( {
41
+ authentication,
42
+ repository,
43
+ config,
44
+ file,
45
+ onLoadCache,
46
+ } ) ;
40
47
const branch = repository && ( repository . branch || repository . default_branch ) ;
41
48
const [ deleted , setDeleted ] = useState ( ) ;
42
49
@@ -77,42 +84,29 @@ function useFile({
77
84
const load = useDeepCompareCallback ( async ( ) => {
78
85
if ( config && repository && filepath ) {
79
86
const _file = await ensureFile ( {
80
- filepath, defaultContent, authentication, config, repository, branch, onOpenValidation,
87
+ authentication,
88
+ branch,
89
+ config,
90
+ defaultContent,
91
+ filepath,
92
+ repository,
93
+ onOpenValidation,
81
94
} ) ;
82
-
83
- console . log ( "ensureFile:" , _file ) ;
84
-
85
- let defaultCachedContentFile ;
86
- if ( onLoadCache && _file && _file . html_url ) {
87
- defaultCachedContentFile = await onLoadCache ( { authentication, repository, branch, html_url : _file . html_url , file : _file } ) ;
88
- }
89
-
90
- // console.log("GRT defaultContent", '|', defaultContent);
91
- // console.log("GRT defaultCachedContent", '|', defaultCachedContentFile);
92
-
93
- let content ;
94
- let _publishedContent ;
95
-
96
- if ( defaultCachedContentFile && defaultCachedContentFile . content ) {
97
- // Load autosaved content:
98
- content = defaultCachedContentFile . content ;
99
- } else {
100
- // Get SERVER content: Overwrite cache:
101
- content = await getContentFromFile ( _file ) ;
102
-
103
- // Check catalog next:
104
- const prodTag = repository . catalog ?. prod ?. branch_or_tag_name ;
105
- if ( prodTag ) {
106
- _publishedContent = await fetchCatalogContent ( 'unfoldingword' , repository . name , prodTag , filepath , config ) ;
107
- }
108
- }
109
-
95
+ // console.log("\nuseFile.load():", _file);
110
96
update ( {
111
- ..._file , branch, content, filepath : _file . path , publishedContent : _publishedContent ,
97
+ ..._file ,
98
+ branch,
99
+ filepath : _file . path ,
112
100
} ) ;
113
- }
114
- } , [ config , repository , filepath , onLoadCache , ensureFile , update ,
115
- defaultContent , authentication , branch , onOpenValidation
101
+ } ;
102
+ } , [
103
+ authentication ,
104
+ branch ,
105
+ config ,
106
+ defaultContent ,
107
+ filepath ,
108
+ repository ,
109
+ update ,
116
110
] ) ;
117
111
118
112
const createFile = useDeepCompareCallback ( async ( {
@@ -145,16 +139,16 @@ function useFile({
145
139
update ( ) ;
146
140
} , [ update , blobActions , onFilepath ] ) ;
147
141
148
- const saveCache = useDeepCompareCallback ( async ( content ) => {
142
+ const saveCache = useDeepCompareCallback ( async ( _content ) => {
149
143
if ( onSaveCache ) {
150
- await onSaveCache ( { authentication, repository, branch, file, content} ) ;
144
+ await onSaveCache ( { authentication, repository, branch, file, content : _content } ) ;
151
145
}
152
146
} , [ writeable , authentication , repository , branch , file , onSaveCache ] ) ;
153
147
154
- const save = useDeepCompareCallback ( async ( content ) => {
148
+ const save = useDeepCompareCallback ( async ( _content ) => {
155
149
//console.log("GRT save // will save file");
156
150
await saveFile ( {
157
- authentication, repository, branch, file, content,
151
+ authentication, repository, branch, file, content : _content ,
158
152
} ) . then (
159
153
// Empty cache if user has saved this file
160
154
// (save() will not happen for "OFFLINE" system files)
@@ -182,6 +176,7 @@ function useFile({
182
176
const loadNew = ( file && filepath && file . filepath !== filepath ) ;
183
177
184
178
if ( notLoaded || loadNew ) {
179
+ // console.log("useFile.useDeepCompareEffect(): notLoaded || loadNew", file);
185
180
load ( ) ;
186
181
}
187
182
} , [ deleted , filepath , load , file ] ) ;
@@ -247,10 +242,12 @@ function useFile({
247
242
} else {
248
243
component = components . browse ;
249
244
}
250
- }
245
+ } ;
246
+
247
+ const state = file && { ...file , content, publishedContent } ;
251
248
252
249
return {
253
- state : file ,
250
+ state,
254
251
stateValues : { isChanged} ,
255
252
actions,
256
253
component,
0 commit comments