Skip to content

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

Fix updating index after tagging #2148 (#2149)

Stanimir1ffe0972ec4a
diff --git a/src/renderer/hooks/IOActionsContextProvider.tsx b/src/renderer/hooks/IOActionsContextProvider.tsx
index 927eda93b..4de58c0da 100644
--- a/src/renderer/hooks/IOActionsContextProvider.tsx
+++ b/src/renderer/hooks/IOActionsContextProvider.tsx
@@ -76,6 +76,7 @@ import { useCurrentLocationContext } from '-/hooks/useCurrentLocationContext';
import { Pro } from '-/pro';
import { useEditedKanBanMetaContext } from '-/hooks/useEditedKanBanMetaContext';
import { CommonLocation } from '-/utils/CommonLocation';
+import { useLocationIndexContext } from '-/hooks/useLocationIndexContext';
type IOActionsContextData = {
createDirectory: (directoryPath: string) => Promise<boolean>;
@@ -282,6 +283,7 @@ export const IOActionsContextProvider = ({
const { currentDirectoryPath, openDirectory, getAllPropertiesPromise } =
useDirectoryContentContext();
const { currentLocation, findLocation } = useCurrentLocationContext();
+ const { reflectUpdateSidecarMeta } = useLocationIndexContext();
const warningOpeningFilesExternally = useSelector(
getWarningOpeningFilesExternally,
);
@@ -1500,7 +1502,10 @@ export const IOActionsContextProvider = ({
{ path: metaFilePath, locationID: entry.locationID },
content,
true,
- ).then(() => meta);
+ ).then(() => {
+ reflectUpdateSidecarMeta(entry.path, meta);
+ return meta;
+ });
}
return Promise.reject(new Error('file not found' + entry.path));
}
diff --git a/src/renderer/hooks/LocationIndexContextProvider.tsx b/src/renderer/hooks/LocationIndexContextProvider.tsx
index 751ac6e31..cf791b61b 100644
--- a/src/renderer/hooks/LocationIndexContextProvider.tsx
+++ b/src/renderer/hooks/LocationIndexContextProvider.tsx
@@ -52,7 +52,7 @@ import { CommonLocation } from '-/utils/CommonLocation';
import { Pro } from '-/pro';
type LocationIndexContextData = {
- index: TS.FileSystemEntry[];
+ //index: TS.FileSystemEntry[];
indexLoadedOn: number;
isIndexing: string;
getIndex: () => TS.FileSystemEntry[];
@@ -62,13 +62,13 @@ type LocationIndexContextData = {
clearDirectoryIndex: () => void;
searchLocationIndex: (searchQuery: TS.SearchQuery) => void;
searchAllLocations: (searchQuery: TS.SearchQuery) => void;
- setIndex: (i: TS.FileSystemEntry[]) => void;
- indexUpdateSidecarTags: (path: string, tags: Array<TS.Tag>) => void;
+ setIndex: (i: TS.FileSystemEntry[], location?: CommonLocation) => void;
+ //indexUpdateSidecarTags: (path: string, tags: Array<TS.Tag>) => void;
reflectUpdateSidecarMeta: (path: string, entryMeta: Object) => void;
};
export const LocationIndexContext = createContext<LocationIndexContextData>({
- index: [],
+ //index: [],
indexLoadedOn: undefined,
isIndexing: undefined,
getIndex: undefined,
@@ -79,7 +79,7 @@ export const LocationIndexContext = createContext<LocationIndexContextData>({
searchLocationIndex: () => {},
searchAllLocations: () => {},
setIndex: () => {},
- indexUpdateSidecarTags: () => {},
+ //indexUpdateSidecarTags: () => {},
reflectUpdateSidecarMeta: () => {},
});
@@ -122,24 +122,33 @@ export const LocationIndexContextProvider = ({
} else if (action.action === 'delete') {
reflectDeleteEntry(action.entry.path);
} else if (action.action === 'update') {
- let i = index.current.findIndex(
+ reflectUpdateEntry(action.oldEntryPath, action.entry);
+ /*let i = index.current.findIndex(
(e) => e.path === action.oldEntryPath,
);
if (i !== -1) {
index.current[i] = action.entry;
- }
+ }*/
}
}
}
}, [actions]);
- function setIndex(i) {
+ function setIndex(i, location: CommonLocation = undefined) {
index.current = i;
if (index.current && index.current.length > 0) {
indexLoadedOn.current = new Date().getTime();
} else {
indexLoadedOn.current = undefined;
}
+ if (location) {
+ persistIndex(
+ { path: location.path, locationID: location.uuid },
+ index.current,
+ ).then(() => {
+ console.log('index persisted');
+ });
+ }
}
function getIndex() {
@@ -152,8 +161,9 @@ export const LocationIndexContextProvider = ({
}
for (let i = 0; i < index.current.length; i += 1) {
if (index.current[i].path === path) {
- index.current = index.current.splice(i, 1);
- i -= 1;
+ setIndex(index.current.splice(i, 1), currentLocation);
+ //i -= 1;
+ break;
}
}
}
@@ -166,12 +176,30 @@ export const LocationIndexContextProvider = ({
(entry) => entry.path === newEntry.path,
);
if (!entryFound) {
- index.current.push(newEntry);
+ setIndex([...index.current, newEntry], currentLocation);
+ //index.current.push(newEntry);
}
// else todo update index entry ?
}
- function indexUpdateSidecarTags(path: string, tags: Array<TS.Tag>) {
+ function reflectUpdateEntry(path: string, newEntry: TS.FileSystemEntry) {
+ if (!index.current || index.current.length < 1) {
+ return;
+ }
+ if (index.current.some((i) => i.path === path)) {
+ setIndex(
+ index.current.map((i) => {
+ if (i.path === path) {
+ return newEntry;
+ }
+ return i;
+ }),
+ currentLocation,
+ );
+ }
+ }
+
+ /*function indexUpdateSidecarTags(path: string, tags: Array<TS.Tag>) {
if (!index.current || index.current.length < 1) {
return;
}
@@ -183,20 +211,32 @@ export const LocationIndexContextProvider = ({
];
}
}
- }
+ }*/
function reflectUpdateSidecarMeta(path: string, entryMeta: Object) {
if (!index.current || index.current.length < 1) {
return;
}
- for (let i = 0; i < index.current.length; i += 1) {
+ setIndex(
+ index.current.map((i) => {
+ if (i.path === path) {
+ return {
+ ...i,
+ meta: { ...(i.meta && i.meta), ...entryMeta },
+ };
+ }
+ return i;
+ }),
+ currentLocation,
+ );
+ /*for (let i = 0; i < index.current.length; i += 1) {
if (index.current[i].path === path) {
index.current[i] = {
...index.current[i],
- ...entryMeta,
+ meta: {...(index.current[i].meta && index.current[i].meta), ...entryMeta},
};
}
- }
+ }*/
}
function isWalking() {
@@ -373,7 +413,7 @@ export const LocationIndexContextProvider = ({
function clearDirectoryIndex() {
isIndexing.current = undefined;
- setIndex([]);
+ setIndex([], currentLocation);
forceUpdate();
}
@@ -492,9 +532,8 @@ export const LocationIndexContextProvider = ({
);
setTimeout(async () => {
const currentPath = await getLocationPath(currentLocation);
- let directoryIndex = getIndex();
- if (!directoryIndex || directoryIndex.length < 1) {
- directoryIndex = await loadIndexFromDisk(
+ if (!index.current || index.current.length < 1) {
+ const directoryIndex = await loadIndexFromDisk(
currentPath,
currentLocation.uuid,
);
@@ -512,8 +551,8 @@ export const LocationIndexContextProvider = ({
if (
searchQuery.forceIndexing ||
(!currentLocation.disableIndexing &&
- (!directoryIndex ||
- directoryIndex.length < 1 ||
+ (!index.current ||
+ index.current.length < 1 ||
indexAge > maxIndexAge))
) {
console.log('Start creating index for : ' + currentPath);
@@ -529,7 +568,7 @@ export const LocationIndexContextProvider = ({
);
setIndex(newIndex);
}
- getSearchResults(getIndex(), searchQuery).then((results) => {
+ getSearchResults(index.current, searchQuery).then((results) => {
setSearchResults(results);
enhanceSearchEntries(results);
});
@@ -705,9 +744,9 @@ export const LocationIndexContextProvider = ({
});
}
- const context = useMemo(() => {
+ /*const context = useMemo(() => {
return {
- index: index.current,
+ //index: index.current,
indexLoadedOn: indexLoadedOn.current,
isIndexing: isIndexing.current,
cancelDirectoryIndexing,
@@ -722,10 +761,24 @@ export const LocationIndexContextProvider = ({
//reflectDeleteEntries,
//reflectCreateEntry,
//reflectRenameEntry,
- indexUpdateSidecarTags,
+ //indexUpdateSidecarTags,
reflectUpdateSidecarMeta,
};
- }, [currentLocation, index.current, isIndexing.current, enableWS]);
+ }, [currentLocation, index.current, isIndexing.current, enableWS]);*/
+
+ const context = {
+ indexLoadedOn: indexLoadedOn.current,
+ isIndexing: isIndexing.current,
+ cancelDirectoryIndexing,
+ createLocationIndex,
+ createLocationsIndexes,
+ clearDirectoryIndex,
+ searchLocationIndex,
+ searchAllLocations,
+ setIndex,
+ getIndex,
+ reflectUpdateSidecarMeta,
+ };
return (
<LocationIndexContext.Provider value={context}>
diff --git a/src/renderer/services/search.ts b/src/renderer/services/search.ts
index 92de54b2a..ba98a843a 100644
--- a/src/renderer/services/search.ts
+++ b/src/renderer/services/search.ts
@@ -24,6 +24,7 @@ import { parseGeoLocation } from '-/utils/geo';
import { extractTimePeriod } from '-/utils/dates';
import { Pro } from '../pro';
import { TS } from '-/tagspaces.namespace';
+import { getAllTags } from '-/services/utils-io';
// export type FileTypeGroups = 'images' | 'notes' | 'documents' | 'audio' | 'video' | 'archives';
@@ -290,7 +291,7 @@ function prepareIndex(
);
}
const enhancedIndex = filteredIndex.map((entry: any) => {
- const tags = [...entry.tags];
+ const tags = getAllTags(entry);
let lat = null;
let lon = null;
let fromTime = null;