Skip to content

Commit 5ff81e4

Browse files
committed
use-deep-compare
1 parent 1f83c32 commit 5ff81e4

File tree

8 files changed

+56
-30
lines changed

8 files changed

+56
-30
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"localforage": "1.7.3",
1919
"markdown-translatable": "1.3.1-rc.1",
2020
"prop-types": "15.7.2",
21+
"use-deep-compare": "^1.1.0",
2122
"use-deep-compare-effect": "^1.3.1",
2223
"utf8": "3.0.0"
2324
},

src/components/authentication/useAuthentication.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
ERROR_NETWORK_DISCONNECTED,
1212
} from '../../core';
1313
import { LoginForm } from '.';
14+
import { useDeepCompareCallback, useDeepCompareEffect, useDeepCompareMemo } from 'use-deep-compare';
1415

1516
function useAuthentication({
1617
messages,
@@ -58,7 +59,7 @@ function useAuthentication({
5859
}
5960
}, [onAuthentication, saveAuthentication]);
6061

61-
useEffect(() => {
62+
useDeepCompareEffect(() => {
6263
if (!authentication && loadAuthentication) {
6364
loadAuthentication().then(_authentication => {
6465
if (_authentication) {
@@ -68,7 +69,7 @@ function useAuthentication({
6869
}
6970
}, [authentication, loadAuthentication, update]);
7071

71-
const onSubmitLogin = useCallback(async ({
72+
const onSubmitLogin = useDeepCompareCallback(async ({
7273
username, password, remember,
7374
}) => {
7475
try {
@@ -105,7 +106,7 @@ function useAuthentication({
105106
messages.genericError, messages.networkError, messages.passwordError, messages.serverError, messages.usernameError,
106107
]);
107108

108-
const onSubmit = useCallback(async ({
109+
const onSubmit = useDeepCompareCallback(async ({
109110
username, password, remember,
110111
}) => {
111112
if (authentication) {
@@ -116,7 +117,7 @@ function useAuthentication({
116117
}
117118
}, [authentication, config, logout, update, onSubmitLogin]);
118119

119-
const component = useMemo(() => (
120+
const component = useDeepCompareMemo(() => (
120121
config && (
121122
<LoginForm
122123
config={config}

src/components/file/File.context.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,15 @@ export function FileContextProvider({
3333
config: _config || contextConfig,
3434
authentication: _authentication || contextAuthentication,
3535
repository: _repository || contextRepository,
36-
filepath, onFilepath, defaultContent, create, onOpenValidation, onConfirmClose,
37-
onLoadCache, onSaveCache, releaseFlag,
36+
filepath,
37+
onFilepath,
38+
defaultContent,
39+
create,
40+
onOpenValidation,
41+
onConfirmClose,
42+
onLoadCache,
43+
onSaveCache,
44+
releaseFlag,
3845
});
3946

4047
const context = {

src/components/file/useFile.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import React, {
2-
useState, useCallback, useContext,
3-
useEffect, useMemo
2+
useState,
3+
useCallback,
4+
useContext,
5+
useEffect,
46
} from 'react';
57
import PropTypes from 'prop-types';
68
import useDeepCompareEffect from 'use-deep-compare-effect';
9+
import { useDeepCompareCallback } from 'use-deep-compare';
710

811
import {
912
getContentFromFile, saveFile, ensureFile, deleteFile,
@@ -37,7 +40,7 @@ function useFile({
3740
const branch = repository && (repository.branch || repository.default_branch);
3841
const [deleted, setDeleted] = useState();
3942

40-
const _setBlob = useCallback(async (_blob) => {
43+
const _setBlob = useDeepCompareCallback(async (_blob) => {
4144
if (blob && _blob && typeof onConfirmClose == 'function') {
4245
const confirm = await onConfirmClose()
4346

@@ -71,7 +74,7 @@ function useFile({
7174
};
7275
}, [onFilepath]);
7376

74-
const load = useCallback(async () => {
77+
const load = useDeepCompareCallback(async () => {
7578
if (config && repository && filepath) {
7679
const _file = await ensureFile({
7780
filepath, defaultContent, authentication, config, repository, branch, onOpenValidation,
@@ -80,8 +83,7 @@ function useFile({
8083
console.log("ensureFile:", _file);
8184

8285
let defaultCachedContentFile;
83-
if (onLoadCache && _file && _file.html_url)
84-
{
86+
if (onLoadCache && _file && _file.html_url) {
8587
defaultCachedContentFile = await onLoadCache({authentication, repository, branch, html_url: _file.html_url, file: _file});
8688
}
8789

@@ -113,7 +115,7 @@ function useFile({
113115
defaultContent, authentication, branch, onOpenValidation
114116
]);
115117

116-
const createFile = useCallback(async ({
118+
const createFile = useDeepCompareCallback(async ({
117119
branch: _branch, filepath: _filepath, defaultContent: _defaultContent, onOpenValidation,
118120
}) => {
119121
if (config && repository) {
@@ -132,7 +134,7 @@ function useFile({
132134
}
133135
}, [authentication, config, repository, updateBranch, onFilepath]);
134136

135-
const close = useCallback(() => {
137+
const close = useDeepCompareCallback(() => {
136138
if (blobActions && blobActions.close) {
137139
blobActions.close();
138140
};
@@ -143,13 +145,13 @@ function useFile({
143145
update();
144146
}, [update, blobActions, onFilepath]);
145147

146-
const saveCache = useCallback(async (content) => {
148+
const saveCache = useDeepCompareCallback(async (content) => {
147149
if (onSaveCache) {
148150
await onSaveCache({authentication, repository, branch, file, content});
149151
}
150152
}, [writeable, authentication, repository, branch, file, onSaveCache]);
151153

152-
const save = useCallback(async (content) => {
154+
const save = useDeepCompareCallback(async (content) => {
153155
//console.log("GRT save // will save file");
154156
await saveFile({
155157
authentication, repository, branch, file, content,
@@ -163,7 +165,7 @@ function useFile({
163165
);
164166
}, [writeable, authentication, repository, branch, file, load, saveFile, saveCache]);
165167

166-
const dangerouslyDelete = useCallback(async () => {
168+
const dangerouslyDelete = useDeepCompareCallback(async () => {
167169
if (writeable) {
168170
const _deleted = await deleteFile({
169171
authentication, repository, file, branch,

src/components/organization/useOrganization.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
33

44
import { Organization, CurrentUserOrganizations } from '../..';
55
import { getCurrentUserOrgs } from '../../core';
6+
import { useDeepCompareCallback } from 'use-deep-compare';
67

78
function useOrganization({
89
organization,
@@ -14,7 +15,7 @@ function useOrganization({
1415
onOrganization(_organization);
1516
}, [onOrganization]);
1617

17-
const list = useCallback(async () => {
18+
const list = useDeepCompareCallback(async () => {
1819
const _organizations = await getCurrentUserOrgs({ config });
1920
return _organizations;
2021
}, [config]);

src/components/repository/useRepository.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
removeRepositoryZip,
1919
getFileFromRepositoryZip,
2020
} from './helpers';
21+
import { useDeepCompareCallback, useDeepCompareEffect } from 'use-deep-compare';
2122

2223
function useRepository({
2324
repositories,
@@ -63,26 +64,26 @@ function useRepository({
6364
setBranch(_branch);
6465
}, []);
6566

66-
const create = useCallback(async (settings) => {
67+
const create = useDeepCompareCallback(async (settings) => {
6768
const _repository = await createRepository({ settings, config });
6869
update(_repository);
6970
}, [config, update]);
7071

71-
const dangerouslyDelete = useCallback(() => {
72+
const dangerouslyDelete = useDeepCompareCallback(() => {
7273
if (user && user.username === repository.owner.username) {
7374
deleteRepository({ repository, config });
7475
window.setTimeout(update, 500);
7576
};
7677
}, [user, repository, config, update]);
7778

78-
const fork = useCallback(async () => {
79+
const fork = useDeepCompareCallback(async () => {
7980
if ((user && user.username) !== repository.owner.username) {
8081
const _repo = await forkRepository({ repository, config });
8182
update(_repo);
8283
};
8384
}, [config, repository, update, user]);
8485

85-
const save = useCallback(async (settings) => {
86+
const save = useDeepCompareCallback(async (settings) => {
8687
let _repository;
8788

8889
if (repository.permissions.admin) {
@@ -93,36 +94,36 @@ function useRepository({
9394
};
9495
}, [config, repository, update]);
9596

96-
const storeZip = useCallback(async () => {
97+
const storeZip = useDeepCompareCallback(async () => {
9798
await storeRepositoryZip({ repository, config });
9899
}, [repository, config]);
99100

100-
const removeZip = useCallback(async () =>{
101+
const removeZip = useDeepCompareCallback(async () =>{
101102
await removeRepositoryZip({ repository, config });
102103
}, [repository, config]);
103104

104-
const fileFromZip = useCallback(async (filepath) => {
105+
const fileFromZip = useDeepCompareCallback(async (filepath) => {
105106
const file = await getFileFromRepositoryZip({
106107
repository, filepath, config,
107108
});
108109
return file;
109110
}, [repository, config]);
110111

111-
const forks = useCallback(() => {
112+
const forks = useDeepCompareCallback(() => {
112113
repositoryForks({ repository, config });
113114
}, [repository, config]);
114115

115116
const close = useCallback(() => {
116117
update();
117118
}, [update]);
118119

119-
useEffect(() => {
120+
useDeepCompareEffect(() => {
120121
if (__branch !== branch) {
121122
setBranch(__branch);
122123
}
123124
}, [__branch, branch]);
124125

125-
useEffect(() => {
126+
useDeepCompareEffect(() => {
126127
if (repository && branch !== repository.branch) {
127128
update({ ...repository, branch });
128129
}

src/components/tree-blob/useBlob.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import deepFreeze from 'deep-freeze';
55
import {manifestFileComparer} from '../file/helpers';
66

77
import { Tree } from '.';
8+
import { useDeepCompareCallback, useDeepCompareMemo } from 'use-deep-compare';
89

910
function useBlob({
1011
config,
@@ -35,11 +36,11 @@ function useBlob({
3536
update();
3637
}, [update]);
3738

38-
const tsvManifestFileComparer = useCallback((item1,item2) => {
39+
const tsvManifestFileComparer = useDeepCompareCallback((item1,item2) => {
3940
return manifestFileComparer({repository, item1, item2});
4041
}, [repository, repository?.books, manifestFileComparer]);
4142

42-
const browse = useMemo(() => {
43+
const browse = useDeepCompareMemo(() => {
4344
return (tree || url) ? (
4445
<Tree
4546
tree={tree}

yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5919,6 +5919,11 @@ deps-sort@^2.0.0:
59195919
subarg "^1.0.0"
59205920
through2 "^2.0.0"
59215921

5922+
dequal@1.0.0:
5923+
version "1.0.0"
5924+
resolved "https://registry.yarnpkg.com/dequal/-/dequal-1.0.0.tgz#41c6065e70de738541c82cdbedea5292277a017e"
5925+
integrity sha512-/Nd1EQbQbI9UbSHrMiKZjFLrXSnU328iQdZKPQf78XQI6C+gutkFUeoHpG5J08Ioa6HeRbRNFpSIclh1xyG0mw==
5926+
59225927
dequal@^2.0.2:
59235928
version "2.0.2"
59245929
resolved "https://registry.yarnpkg.com/dequal/-/dequal-2.0.2.tgz#85ca22025e3a87e65ef75a7a437b35284a7e319d"
@@ -17206,6 +17211,13 @@ use-deep-compare-effect@^1.3.1:
1720617211
"@types/react" "^17.0.0"
1720717212
dequal "^2.0.2"
1720817213

17214+
use-deep-compare@^1.1.0:
17215+
version "1.1.0"
17216+
resolved "https://registry.yarnpkg.com/use-deep-compare/-/use-deep-compare-1.1.0.tgz#85580dde751f68400bf6ef7e043c7f986595cef8"
17217+
integrity sha512-6yY3zmKNCJ1jjIivfZMZMReZjr8e6iC6Uqtp701jvWJ6ejC/usXD+JjmslZDPJQgX8P4B1Oi5XSLHkOLeYSJsA==
17218+
dependencies:
17219+
dequal "1.0.0"
17220+
1720917221
use@^3.1.0:
1721017222
version "3.1.1"
1721117223
resolved "https://registry.yarnpkg.com/use/-/use-3.1.1.tgz#d50c8cac79a19fbc20f2911f56eb973f4e10070f"

0 commit comments

Comments
 (0)