-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #120 from unfoldingWord/bugfix-klappy-multiple-loa…
…d-content Bugfix klappy multiple load content
- Loading branch information
Showing
4 changed files
with
337 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { useCallback, useState } from "react"; | ||
import { useDeepCompareCallback } from "use-deep-compare"; | ||
import useDeepCompareEffect from "use-deep-compare-effect"; | ||
import deepEqual from "deep-equal"; | ||
|
||
import { fetchCatalogContent } from "./dcsCatalogNextApis"; | ||
import { getContentFromFile } from "./helpers"; | ||
|
||
export default function useFileContent ({ | ||
authentication, | ||
repository, | ||
config, | ||
file, | ||
onLoadCache, | ||
}) { | ||
const defaultState = { | ||
file: undefined, | ||
content: undefined, | ||
publishedContent: undefined, | ||
}; | ||
const [state, setState] = useState(defaultState); | ||
|
||
const _onLoadCache = useDeepCompareCallback( async () => { | ||
let cachedFile; | ||
if (onLoadCache && file?.html_url) { | ||
cachedFile = await onLoadCache({ | ||
authentication, | ||
repository, | ||
branch: file.branch, | ||
html_url: file.html_url, | ||
file: file | ||
}); | ||
}; | ||
// console.log("useFileContent._onLoadCache():", cachedFile); | ||
return cachedFile; | ||
}, [ | ||
authentication, | ||
repository, | ||
file, | ||
]); | ||
|
||
const _fetchCatalogContent = useDeepCompareCallback( async ({prodTag}) => { | ||
const publishedContent = await fetchCatalogContent( | ||
'unfoldingword', | ||
repository.name, | ||
prodTag, | ||
file.filepath, | ||
config, | ||
); | ||
// console.log("useFileContent._fetchCatalogContent():", publishedContent); | ||
return publishedContent; | ||
}, [ | ||
repository, | ||
file, | ||
config, | ||
]); | ||
|
||
const load = useDeepCompareCallback( async () => { | ||
if (file) { | ||
const cachedFile = await _onLoadCache(); | ||
// Load autosaved content: | ||
let content = cachedFile?.content; | ||
let publishedContent; | ||
|
||
if (!content) content = await getContentFromFile(file); | ||
|
||
if (!publishedContent) { | ||
// Check catalog next: | ||
const prodTag = repository?.catalog?.prod?.branch_or_tag_name; | ||
if ( prodTag ) { | ||
publishedContent = await _fetchCatalogContent({prodTag}); | ||
} | ||
}; | ||
// console.log("\nuseFileContent.load()\n"); | ||
setState({ content, publishedContent }); | ||
}; | ||
}, [file, repository, _onLoadCache, _fetchCatalogContent]); | ||
|
||
const reset = useCallback(() => { | ||
setState(defaultState); | ||
}, []); | ||
|
||
useDeepCompareEffect(() => { | ||
if (!file && state.file) reset(); | ||
if (state.file && !deepEqual(file, state.file)) reset(); | ||
if (file && !state.file) { | ||
load(); | ||
}; | ||
}, [file, state.file, reset, load]); | ||
|
||
return { state }; | ||
}; |
Oops, something went wrong.