Skip to content

Commit 1855cc1

Browse files
authored
Merge pull request #120 from unfoldingWord/bugfix-klappy-multiple-load-content
Bugfix klappy multiple load content
2 parents 76a4fa0 + d26234c commit 1855cc1

File tree

4 files changed

+337
-48
lines changed

4 files changed

+337
-48
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gitea-react-toolkit",
3-
"version": "2.0.2-rc.2",
3+
"version": "2.0.2-rc.13",
44
"license": "MIT",
55
"description": "A Gitea API React Toolkit Component Library",
66
"homepage": "https://gitea-react-toolkit.netlify.com/",
@@ -14,6 +14,7 @@
1414
"axios": "0.19.0",
1515
"axios-cache-adapter": "2.4.1",
1616
"base-64": "0.1.0",
17+
"deep-equal": "^2.0.5",
1718
"jszip": "^3.5.0",
1819
"localforage": "1.7.3",
1920
"markdown-translatable": "1.3.1-rc.1",

src/components/file/useFile.js

+38-41
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import useDeepCompareEffect from 'use-deep-compare-effect';
99
import { useDeepCompareCallback } from 'use-deep-compare';
1010

1111
import {
12-
getContentFromFile, saveFile, ensureFile, deleteFile,
12+
saveFile, ensureFile, deleteFile,
1313
} from './helpers';
1414
import {
1515
FileCard, FileForm, useBlob, RepositoryContext,
1616
} from '..';
17-
import {fetchCatalogContent} from './dcsCatalogNextApis';
17+
import useFileContent from './useFileContent';
1818

1919
function 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,

src/components/file/useFileContent.js

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { useCallback, useState } from "react";
2+
import { useDeepCompareCallback } from "use-deep-compare";
3+
import useDeepCompareEffect from "use-deep-compare-effect";
4+
import deepEqual from "deep-equal";
5+
6+
import { fetchCatalogContent } from "./dcsCatalogNextApis";
7+
import { getContentFromFile } from "./helpers";
8+
9+
export default function useFileContent ({
10+
authentication,
11+
repository,
12+
config,
13+
file,
14+
onLoadCache,
15+
}) {
16+
const defaultState = {
17+
file: undefined,
18+
content: undefined,
19+
publishedContent: undefined,
20+
};
21+
const [state, setState] = useState(defaultState);
22+
23+
const _onLoadCache = useDeepCompareCallback( async () => {
24+
let cachedFile;
25+
if (onLoadCache && file?.html_url) {
26+
cachedFile = await onLoadCache({
27+
authentication,
28+
repository,
29+
branch: file.branch,
30+
html_url: file.html_url,
31+
file: file
32+
});
33+
};
34+
// console.log("useFileContent._onLoadCache():", cachedFile);
35+
return cachedFile;
36+
}, [
37+
authentication,
38+
repository,
39+
file,
40+
]);
41+
42+
const _fetchCatalogContent = useDeepCompareCallback( async ({prodTag}) => {
43+
const publishedContent = await fetchCatalogContent(
44+
'unfoldingword',
45+
repository.name,
46+
prodTag,
47+
file.filepath,
48+
config,
49+
);
50+
// console.log("useFileContent._fetchCatalogContent():", publishedContent);
51+
return publishedContent;
52+
}, [
53+
repository,
54+
file,
55+
config,
56+
]);
57+
58+
const load = useDeepCompareCallback( async () => {
59+
if (file) {
60+
const cachedFile = await _onLoadCache();
61+
// Load autosaved content:
62+
let content = cachedFile?.content;
63+
let publishedContent;
64+
65+
if (!content) content = await getContentFromFile(file);
66+
67+
if (!publishedContent) {
68+
// Check catalog next:
69+
const prodTag = repository?.catalog?.prod?.branch_or_tag_name;
70+
if ( prodTag ) {
71+
publishedContent = await _fetchCatalogContent({prodTag});
72+
}
73+
};
74+
// console.log("\nuseFileContent.load()\n");
75+
setState({ content, publishedContent });
76+
};
77+
}, [file, repository, _onLoadCache, _fetchCatalogContent]);
78+
79+
const reset = useCallback(() => {
80+
setState(defaultState);
81+
}, []);
82+
83+
useDeepCompareEffect(() => {
84+
if (!file && state.file) reset();
85+
if (state.file && !deepEqual(file, state.file)) reset();
86+
if (file && !state.file) {
87+
load();
88+
};
89+
}, [file, state.file, reset, load]);
90+
91+
return { state };
92+
};

0 commit comments

Comments
 (0)