Skip to content

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

fix index not created if WS is down and search started on initial Sea… (#2236)

Stanimire336ac88f051
diff --git a/src/main/util.ts b/src/main/util.ts
index 21d63abb8..56fb96a1d 100644
--- a/src/main/util.ts
+++ b/src/main/util.ts
@@ -332,24 +332,30 @@ export function readMacOSTags(filename) {
* needs to run in init this function always return false first time
*/
export function isWorkerAvailable(): Promise<boolean> {
- if (settings.getToken() !== undefined) {
+ /*if (settings.getToken() !== undefined) {
return Promise.resolve(settings.getToken() !== 'not');
- }
+ }*/
try {
- fetch('http://127.0.0.1:' + settings.getUsedWsPort(), {
+ return fetch('http://127.0.0.1:' + settings.getUsedWsPort(), {
method: 'HEAD',
- }).then((res) => {
- if (res.status === 200) {
- const config = require('./config/config.json');
- if (config && config.jwt) {
- settings.setToken(config.jwt);
- return true;
- } else {
- console.error('jwt token not generated');
- settings.setToken('not');
+ })
+ .then((res) => {
+ if (res.status === 200) {
+ const config = require('./config/config.json');
+ if (config && config.jwt) {
+ settings.setToken(config.jwt);
+ return true;
+ } else {
+ console.error('jwt token not generated');
+ settings.setToken('not');
+ }
}
- }
- });
+ return false;
+ })
+ .catch((e) => {
+ console.debug('isWorkerAvailable:', e);
+ return false;
+ });
} catch (e) {
if (e && e.code && e.code === 'MODULE_NOT_FOUND') {
console.error('WS error MODULE_NOT_FOUND');
diff --git a/src/renderer/components/SearchAutocomplete.tsx b/src/renderer/components/SearchAutocomplete.tsx
index 824d77faf..a08ed0c02 100644
--- a/src/renderer/components/SearchAutocomplete.tsx
+++ b/src/renderer/components/SearchAutocomplete.tsx
@@ -71,6 +71,7 @@ import { removePrefix } from '-/services/utils-io';
import { isDesktopMode } from '-/reducers/settings';
import { useSavedSearchesContext } from '-/hooks/useSavedSearchesContext';
import { useBrowserHistoryContext } from '-/hooks/useBrowserHistoryContext';
+import useFirstRender from '-/utils/useFirstRender';
interface Props {
style?: any;
@@ -160,10 +161,12 @@ function SearchAutocomplete(props: Props) {
// const mainSearchField = useRef<HTMLInputElement>(null);
const isOpen = useRef<boolean>(true);
- // const firstRender = useFirstRender();
+ const firstRender = useFirstRender();
useEffect(() => {
- processSearchQuery();
+ if (!firstRender) {
+ processSearchQuery();
+ }
}, [searchQuery]);
function processSearchQuery() {
diff --git a/src/renderer/hooks/LocationIndexContextProvider.tsx b/src/renderer/hooks/LocationIndexContextProvider.tsx
index 4c3f03c82..a14f7cc9a 100644
--- a/src/renderer/hooks/LocationIndexContextProvider.tsx
+++ b/src/renderer/hooks/LocationIndexContextProvider.tsx
@@ -25,7 +25,10 @@ import { useNotificationContext } from '-/hooks/useNotificationContext';
import { Pro } from '-/pro';
import { getEnableWS } from '-/reducers/settings';
import Search from '-/services/search';
-import { executePromisesInBatches } from '-/services/utils-io';
+import {
+ executePromisesInBatches,
+ isWorkerAvailable,
+} from '-/services/utils-io';
import { TS } from '-/tagspaces.namespace';
import { CommonLocation } from '-/utils/CommonLocation';
import useFirstRender from '-/utils/useFirstRender';
@@ -346,69 +349,95 @@ export const LocationIndexContextProvider = ({
if (Pro && Pro.Watcher) {
Pro.Watcher.stopWatching();
}
- const loc = findLocation(param.locationID);
- const dirPath = cleanTrailingDirSeparator(param.path);
- if (
- enableWS &&
- !loc.haveObjectStoreSupport() &&
- !loc.haveWebDavSupport() &&
- !AppConfig.isCordova
- ) {
- // Start indexing in worker if not in the object store mode
- return loc
- .createDirectoryIndexInWorker(dirPath, extractText, ignorePatterns)
- .then((result) => {
- if (result && result.success) {
- return loadIndexFromDisk(dirPath, param.locationID);
- } else if (result && result.error) {
- console.error(
- 'createDirectoryIndexInWorker failed:' + result.error,
- );
- } else {
- console.error(
- 'createDirectoryIndexInWorker failed: unknown error',
- );
- }
- return undefined; // todo create index not in worker
- });
- }
-
- const mode = ['extractThumbPath'];
- if (extractText) {
- mode.push('extractTextContent');
- mode.push('extractLinks');
- }
- return createIndex(
- {
- ...param,
- listDirectoryPromise: loc.listDirectoryPromise,
- getFileContentPromise: loc.getFileContentPromise,
- },
- mode,
- ignorePatterns,
- isWalking,
- )
- .then((directoryIndex) => {
- if (!loc.isReadOnly) {
- persistIndex(param, directoryIndex).then((success) => {
- if (success) {
- console.log('Index generated in folder: ' + dirPath);
+ return isWorkerAvailable().then((isWorkerAvailable) => {
+ const loc = findLocation(param.locationID);
+ const dirPath = cleanTrailingDirSeparator(param.path);
+ if (
+ isWorkerAvailable &&
+ enableWS &&
+ !loc.haveObjectStoreSupport() &&
+ !loc.haveWebDavSupport() &&
+ !AppConfig.isCordova
+ ) {
+ // Start indexing in worker if not in the object store mode
+ return loc
+ .createDirectoryIndexInWorker(dirPath, extractText, ignorePatterns)
+ .then((result) => {
+ if (result && result.success) {
+ return loadIndexFromDisk(dirPath, param.locationID);
+ } else if (result && result.error) {
+ console.error(
+ 'createDirectoryIndexInWorker failed:' + result.error,
+ );
+ } else {
+ console.error(
+ 'createDirectoryIndexInWorker failed: unknown error',
+ );
}
+ return createNotWorkerIndex(
+ param,
+ loc,
+ extractText,
+ ignorePatterns,
+ isWalking,
+ );
});
- }
- return enhanceDirectoryIndex(
- directoryIndex,
- param.locationID,
- param.path,
- );
- })
- .catch((err) => {
- console.log('Error creating index: ', err);
- });
+ }
+
+ return createNotWorkerIndex(
+ param,
+ loc,
+ extractText,
+ ignorePatterns,
+ isWalking,
+ );
+ });
}
return Promise.resolve(undefined);
}
+ function createNotWorkerIndex(
+ param: any,
+ loc: CommonLocation,
+ extractText = false,
+ ignorePatterns: Array<string> = [],
+ isWalking = () => true,
+ ): Promise<TS.FileSystemEntry[]> {
+ const mode = ['extractThumbPath'];
+ if (extractText) {
+ mode.push('extractTextContent');
+ mode.push('extractLinks');
+ }
+ return createIndex(
+ {
+ ...param,
+ listDirectoryPromise: loc.listDirectoryPromise,
+ getFileContentPromise: loc.getFileContentPromise,
+ },
+ mode,
+ ignorePatterns,
+ isWalking,
+ )
+ .then((directoryIndex) => {
+ if (!loc.isReadOnly) {
+ persistIndex(param, directoryIndex).then((success) => {
+ if (success) {
+ console.log('Index generated in folder: ' + param.path);
+ }
+ });
+ }
+ return enhanceDirectoryIndex(
+ directoryIndex,
+ param.locationID,
+ param.path,
+ );
+ })
+ .catch((err) => {
+ console.log('Error creating index: ', err);
+ return undefined;
+ });
+ }
+
function createDirectoryIndexWrapper(
param: any,
extractText = false,
diff --git a/src/renderer/hooks/PaginationContextProvider.tsx b/src/renderer/hooks/PaginationContextProvider.tsx
index 511659def..0a773eea8 100644
--- a/src/renderer/hooks/PaginationContextProvider.tsx
+++ b/src/renderer/hooks/PaginationContextProvider.tsx
@@ -27,7 +27,7 @@ import { useEditedEntryMetaContext } from '-/hooks/useEditedEntryMetaContext';
type PaginationContextData = {
page: number;
pageFiles: TS.FileSystemEntry[];
- setCurrentPage: (page: number) => void;
+ setCurrentPage: (page?: number) => void;
};
export const PaginationContext = createContext<PaginationContextData>({
@@ -44,7 +44,7 @@ export const PaginationContextProvider = ({
children,
}: PaginationContextProviderProps) => {
const initPage = 1;
- const { currentDirectoryPath, isSearchMode } = useDirectoryContentContext();
+ const { currentDirectoryPath, searchQuery } = useDirectoryContentContext();
const { setReflectMetaActions } = useEditedEntryMetaContext();
const { settings, showDirectories } = usePerspectiveSettingsContext();
const { sortedDirContent } = useSortedDirContext();
@@ -63,7 +63,7 @@ export const PaginationContextProvider = ({
console.debug('meta loaded')
);
}*/
- }, [currentDirectoryPath, isSearchMode]); //, isMetaFolderExist]);
+ }, [currentDirectoryPath, searchQuery]); //, isSearchMode isMetaFolderExist]);
const pageFiles: TS.FileSystemEntry[] = useMemo(() => {
return getPageFiles(page, sortedDirContent);
@@ -91,15 +91,18 @@ export const PaginationContextProvider = ({
return files;
}
- function setCurrentPage(currentPage: number) {
- setPage(currentPage);
- const files = getPageFiles(currentPage, sortedDirContent);
- if (files && files.length > 0) {
- const actions: TS.EditMetaAction[] = files.map((file) => ({
- action: 'thumbGenerate',
- entry: file,
- }));
- setReflectMetaActions(...actions);
+ function setCurrentPage(currentPage?: number) {
+ const cPage = currentPage ? currentPage : initPage;
+ if (page !== cPage) {
+ setPage(cPage);
+ const files = getPageFiles(cPage, sortedDirContent);
+ if (files && files.length > 0) {
+ const actions: TS.EditMetaAction[] = files.map((file) => ({
+ action: 'thumbGenerate',
+ entry: file,
+ }));
+ setReflectMetaActions(...actions);
+ }
}
}