@@ -9,12 +9,12 @@ import useDeepCompareEffect from 'use-deep-compare-effect';
99import { useDeepCompareCallback } from 'use-deep-compare' ;
1010
1111import {
12- getContentFromFile , saveFile , ensureFile , deleteFile ,
12+ saveFile , ensureFile , deleteFile ,
1313} from './helpers' ;
1414import {
1515 FileCard , FileForm , useBlob , RepositoryContext ,
1616} from '..' ;
17- import { fetchCatalogContent } from './dcsCatalogNextApis ' ;
17+ import useFileContent from './useFileContent ' ;
1818
1919function useFile ( {
2020 authentication,
@@ -37,6 +37,13 @@ function useFile({
3737 const { actions : { updateBranch } , config : repositoryConfig } = useContext ( RepositoryContext ) ;
3838
3939 const config = _config || repositoryConfig ;
40+ const { state : { content, publishedContent } } = useFileContent ( {
41+ authentication,
42+ repository,
43+ config,
44+ file,
45+ onLoadCache,
46+ } ) ;
4047 const branch = repository && ( repository . branch || repository . default_branch ) ;
4148 const [ deleted , setDeleted ] = useState ( ) ;
4249
@@ -77,42 +84,29 @@ function useFile({
7784 const load = useDeepCompareCallback ( async ( ) => {
7885 if ( config && repository && filepath ) {
7986 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,
8194 } ) ;
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);
11096 update ( {
111- ..._file , branch, content, filepath : _file . path , publishedContent : _publishedContent ,
97+ ..._file ,
98+ branch,
99+ filepath : _file . path ,
112100 } ) ;
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 ,
116110 ] ) ;
117111
118112 const createFile = useDeepCompareCallback ( async ( {
@@ -145,16 +139,16 @@ function useFile({
145139 update ( ) ;
146140 } , [ update , blobActions , onFilepath ] ) ;
147141
148- const saveCache = useDeepCompareCallback ( async ( content ) => {
142+ const saveCache = useDeepCompareCallback ( async ( _content ) => {
149143 if ( onSaveCache ) {
150- await onSaveCache ( { authentication, repository, branch, file, content} ) ;
144+ await onSaveCache ( { authentication, repository, branch, file, content : _content } ) ;
151145 }
152146 } , [ writeable , authentication , repository , branch , file , onSaveCache ] ) ;
153147
154- const save = useDeepCompareCallback ( async ( content ) => {
148+ const save = useDeepCompareCallback ( async ( _content ) => {
155149 //console.log("GRT save // will save file");
156150 await saveFile ( {
157- authentication, repository, branch, file, content,
151+ authentication, repository, branch, file, content : _content ,
158152 } ) . then (
159153 // Empty cache if user has saved this file
160154 // (save() will not happen for "OFFLINE" system files)
@@ -182,6 +176,7 @@ function useFile({
182176 const loadNew = ( file && filepath && file . filepath !== filepath ) ;
183177
184178 if ( notLoaded || loadNew ) {
179+ // console.log("useFile.useDeepCompareEffect(): notLoaded || loadNew", file);
185180 load ( ) ;
186181 }
187182 } , [ deleted , filepath , load , file ] ) ;
@@ -247,10 +242,12 @@ function useFile({
247242 } else {
248243 component = components . browse ;
249244 }
250- }
245+ } ;
246+
247+ const state = file && { ...file , content, publishedContent } ;
251248
252249 return {
253- state : file ,
250+ state,
254251 stateValues : { isChanged} ,
255252 actions,
256253 component,
0 commit comments