Skip to content

TagSpaces is an offline, open source, document manager with tagging support

import location tagLibrary optimizations (#1820)

Stanimirc26f446c1f8c
diff --git a/app/components/TagLibrary.tsx b/app/components/TagLibrary.tsx
index 9c933751d..2bf287bf3 100644
--- a/app/components/TagLibrary.tsx
+++ b/app/components/TagLibrary.tsx
@@ -75,6 +75,7 @@ import {
removeTagGroup,
sortTagGroup
} from '-/services/taglibrary-utils';
+import useFirstRender from '-/utils/useFirstRender';
interface Props {
classes?: any;
@@ -162,6 +163,7 @@ function TagLibrary(props: Props) {
const [isDeleteTagDialogOpened, setIsDeleteTagDialogOpened] = useState<
boolean
>(false);
+ const firstRender = useFirstRender();
useEffect(() => {
if (Pro && props.saveTagInLocation) {
@@ -170,7 +172,9 @@ function TagLibrary(props: Props) {
}, []);
useEffect(() => {
- setTagGroups(getTagLibrary());
+ if (!firstRender) {
+ setTagGroups(getTagLibrary());
+ }
}, [props.isTagLibraryChanged]);
const refreshTagsFromLocation = () => {
@@ -182,10 +186,13 @@ function TagLibrary(props: Props) {
...group,
locationId: location.uuid
}));
- setTagGroups(importTagGroups(newGroups, getTagLibrary(), false));
- } else {
+ const oldGroups = getTagLibrary();
+ if (checkTagGroupModified(location.uuid, newGroups, oldGroups)) {
+ setTagGroups(importTagGroups(newGroups, oldGroups, false));
+ }
+ } /*else {
setTagGroups(getTagLibrary());
- }
+ }*/
return true;
})
.catch(err => {
@@ -194,6 +201,23 @@ function TagLibrary(props: Props) {
);
};
+ function checkTagGroupModified(
+ locationId: string,
+ newGroups: Array<TS.TagGroup>,
+ oldGroups: Array<TS.TagGroup>
+ ) {
+ if (!oldGroups.some(group => group.locationId === locationId)) {
+ return true;
+ }
+ return !oldGroups.some(group =>
+ newGroups.some(
+ newGroup =>
+ newGroup.modified_date === group.modified_date &&
+ newGroup.locationId === group.locationId
+ )
+ );
+ }
+
const isTagLibraryReadOnly =
window.ExtTagLibrary && window.ExtTagLibrary.length > 0;
diff --git a/app/services/taglibrary-utils.ts b/app/services/taglibrary-utils.ts
index 6c003eedf..9124c8bb1 100644
--- a/app/services/taglibrary-utils.ts
+++ b/app/services/taglibrary-utils.ts
@@ -234,22 +234,20 @@ export function createTagGroup(
tagGroups: Array<TS.TagGroup>,
locations: Array<TS.Location>
): Array<TS.TagGroup> {
+ const newEntry = {
+ ...entry,
+ created_date: new Date().getTime(),
+ modified_date: new Date().getTime()
+ };
if (Pro && entry.locationId) {
const location: TS.Location = locations.find(
l => l.uuid === entry.locationId
);
if (location) {
- Pro.MetaOperations.createTagGroup(location.path, entry);
+ Pro.MetaOperations.createTagGroup(location.path, newEntry);
}
}
- return setTagLibrary([
- ...tagGroups,
- {
- ...entry,
- created_date: new Date().getTime(),
- modified_date: new Date().getTime()
- }
- ]);
+ return setTagLibrary([...tagGroups, newEntry]);
}
export function editTag(
@@ -265,8 +263,9 @@ export function editTag(
if (indexForEditing > -1) {
const tagGroup: TS.TagGroup = tagGroups[indexForEditing];
- const newTagGroup = {
+ const newTagGroup: TS.TagGroup = {
...tagGroup,
+ modified_date: new Date().getTime(),
children: tagGroup.children.map(t => {
if (t.title === origTitle) {
return tag;
@@ -360,15 +359,20 @@ export function editTagGroup(
tagGroups: Array<TS.TagGroup>,
locations: Array<TS.Location>
) {
+ const editedTagGroup = {
+ ...entry,
+ modified_date: new Date().getTime()
+ };
+
if (Pro && entry.locationId) {
const location: TS.Location = locations.find(
l => l.uuid === entry.locationId
);
if (location) {
- Pro.MetaOperations.editTagGroup(location.path, entry);
+ Pro.MetaOperations.editTagGroup(location.path, editedTagGroup);
}
}
- return updateTagGroup(entry, tagGroups);
+ return updateTagGroup(editedTagGroup, tagGroups);
}
export function deleteTag(
@@ -385,8 +389,9 @@ export function deleteTag(
tag => tag.title === tagTitle
);
if (tagIndexForRemoving >= 0) {
- const editedTagGroup = {
+ const editedTagGroup: TS.TagGroup = {
...tagGroup,
+ modified_date: new Date().getTime(),
children: [
...tagGroup.children.slice(0, tagIndexForRemoving),
...tagGroup.children.slice(tagIndexForRemoving + 1)
@@ -550,9 +555,11 @@ function updateTagGroup(
);
if (indexForEditing >= 0) {
- const modifiedEntry = extend({ created_date: new Date() }, entry, {
- modified_date: new Date()
- });
+ const modifiedEntry = {
+ ...entry,
+ ...(!entry.created_date && { created_date: new Date().getTime() }),
+ ...(!entry.modified_date && { modified_date: new Date().getTime() })
+ };
return setTagLibrary([
...tagGroups.slice(0, indexForEditing),
modifiedEntry,