Skip to content

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

Add search history (#1879)

Stanimirb0329a87fcaf
diff --git a/app/components/EntryContainer.tsx b/app/components/EntryContainer.tsx
index 2dc76e171..4f63423cf 100644
--- a/app/components/EntryContainer.tsx
+++ b/app/components/EntryContainer.tsx
@@ -636,9 +636,11 @@ function EntryContainer(props: Props) {
if (Pro) {
Pro.history.saveHistory(
historyKeys.fileEditKey,
- openedFile.path,
- openedFile.url,
- openedFile.locationId,
+ {
+ path: openedFile.path,
+ url: openedFile.url,
+ lid: openedFile.locationId
+ },
settings[historyKeys.fileEditKey]
);
}
diff --git a/app/components/FolderContainer.tsx b/app/components/FolderContainer.tsx
index 33021b156..628881a62 100644
--- a/app/components/FolderContainer.tsx
+++ b/app/components/FolderContainer.tsx
@@ -214,6 +214,8 @@ interface Props {
progress?: Array<any>;
searchQuery: TS.SearchQuery;
setSearchQuery: (searchQuery: TS.SearchQuery) => void;
+ enterSearchMode: () => void;
+ exitSearchMode: () => void;
openURLExternally?: (url: string, skipConfirmation: boolean) => void;
language: string;
editedEntryPaths: Array<TS.EditedEntryPath>;
@@ -514,6 +516,16 @@ function FolderContainer(props: Props) {
// }
});
+ const toggleSearchMode = () => {
+ if (props.isSearchMode) {
+ props.setSearchQuery({});
+ props.exitSearchMode();
+ } else {
+ props.setSearchQuery({ textQuery: '' });
+ props.enterSearchMode();
+ }
+ };
+
return (
<div data-tid="folderContainerTID" style={{ position: 'relative' }}>
<div
@@ -607,16 +619,8 @@ function FolderContainer(props: Props) {
minWidth: 40,
width: 200
}}
- onKeyDown={() =>
- props.setSearchQuery(
- props.isSearchMode ? {} : { textQuery: '' }
- )
- }
- onClick={() =>
- props.setSearchQuery(
- props.isSearchMode ? {} : { textQuery: '' }
- )
- }
+ onKeyDown={toggleSearchMode}
+ onClick={toggleSearchMode}
margin="dense"
placeholder={i18n.t('core:searchTitle')}
InputProps={{
@@ -748,6 +752,8 @@ function mapActionCreatorsToProps(dispatch) {
setCurrentDirectoryPerspective: AppActions.setCurrentDirectoryPerspective,
updateCurrentDirEntry: AppActions.updateCurrentDirEntry,
setCurrentDirectoryColor: AppActions.setCurrentDirectoryColor,
+ enterSearchMode: AppActions.enterSearchMode,
+ exitSearchMode: AppActions.exitSearchMode,
setSearchQuery: LocationIndexActions.setSearchQuery,
// openCurrentDirectory: AppActions.openCurrentDirectory,
openURLExternally: AppActions.openURLExternally
diff --git a/app/components/LocationView.tsx b/app/components/LocationView.tsx
index d9d86e125..12903952e 100644
--- a/app/components/LocationView.tsx
+++ b/app/components/LocationView.tsx
@@ -88,6 +88,7 @@ interface Props {
selectedLocation: TS.Location;
setSelectedLocation: (loc: TS.Location) => void;
changeLocation: (loc: TS.Location) => void;
+ exitSearchMode: () => void;
}
function LocationView(props: Props) {
@@ -119,6 +120,7 @@ function LocationView(props: Props) {
} else {
// this.directoryTreeRef[location.uuid].loadSubDir(location, 1);
props.setSelectedEntries([]);
+ props.exitSearchMode();
props.openLocation(location);
if (props.hideDrawer) {
props.hideDrawer();
@@ -379,7 +381,8 @@ function mapDispatchToProps(dispatch) {
setSelectedEntries: AppActions.setSelectedEntries,
openLocation: AppActions.openLocation,
loadDirectoryContent: AppActions.loadDirectoryContent,
- changeLocation: AppActions.changeLocation
+ changeLocation: AppActions.changeLocation,
+ exitSearchMode: AppActions.exitSearchMode
},
dispatch
);
diff --git a/app/components/SearchAutocomplete.tsx b/app/components/SearchAutocomplete.tsx
index c83bf63bb..30b74f4f9 100644
--- a/app/components/SearchAutocomplete.tsx
+++ b/app/components/SearchAutocomplete.tsx
@@ -19,20 +19,16 @@
import React, { useEffect, useReducer, useRef, useState } from 'react';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
-import format from 'date-fns/format';
+import { format, formatDistanceToNow } from 'date-fns';
import makeStyles from '@mui/styles/makeStyles';
import HelpOutlineIcon from '@mui/icons-material/HelpOutline';
import Button from '@mui/material/Button';
import Tooltip from '-/components/Tooltip';
import IconButton from '@mui/material/IconButton';
import ClearSearchIcon from '@mui/icons-material/Close';
-import InputAdornment from '@mui/material/InputAdornment';
import {
actions as AppActions,
- getCurrentLocationId,
- getDirectoryContent,
getDirectoryPath,
- getEditedEntryPaths,
isSearchMode
} from '../reducers/app';
import {
@@ -97,12 +93,14 @@ interface Props {
locations: TS.Location[];
currentLocation: TS.Location;
openLocationById: (locationId: string) => void;
+ changeLocationByID: (locationId: string) => void;
isSearchMode: boolean;
exitSearchMode: () => void;
setSearchFilter: (filter) => void;
- currentLocationId: string;
+ // currentLocationId: string;
openFsEntry: (fsEntry: TS.FileSystemEntry) => void;
searches: Array<TS.SearchQuery>;
+ switchLocationTypeByID: (locationId: string) => Promise<string | null>;
// editedEntryPaths: Array<TS.EditedEntryPath>;
}
@@ -181,8 +179,9 @@ function SearchAutocomplete(props: Props) {
const isOpen = useRef<boolean>(true);
const firstRender = useFirstRender();
+ const historyKeys = Pro && Pro.history ? Pro.history.historyKeys : {};
- useEffect(() => {
+ /*useEffect(() => {
// Handle CHANGE LOCATION IN SEARCH MODE
if (!firstRender) {
if (props.isSearchMode && Object.keys(props.searchQuery).length > 0) {
@@ -191,7 +190,7 @@ function SearchAutocomplete(props: Props) {
props.exitSearchMode();
}
}
- }, [props.currentLocation]);
+ }, [props.currentLocation]);*/
useEffect(() => {
if (!firstRender) {
@@ -474,6 +473,7 @@ function SearchAutocomplete(props: Props) {
const clearSearch = () => {
resetValues([]);
props.setSearchQuery({});
+ props.exitSearchMode();
// props.setSearchResults([]);
props.watchForChanges();
// props.openCurrentDirectory();
@@ -664,6 +664,29 @@ function SearchAutocomplete(props: Props) {
}
return parsed;
}*/
+
+ function getHistoryOptions(
+ items: TS.HistoryItem[],
+ group: string
+ ): SearchOptionType[] {
+ if (!items) {
+ return [];
+ }
+ return items.map((item: TS.HistoryItem) => ({
+ id: item.lid,
+ descr: formatDistanceToNow(item.creationTimeStamp, {
+ includeSeconds: true,
+ addSuffix: true
+ // locale: https://date-fns.org/v2.29.3/docs/formatDistanceToNow#usage
+ }),
+ action: ExecActions.OPEN_HISTORY,
+ fullName: item.url,
+ label: item.path,
+ group: group,
+ ...(item.searchQuery && { searchQuery: item.searchQuery })
+ }));
+ }
+
function changeOptions(action: string) {
let optionsChanged = false;
if (isAction(action, SearchActions.LOCATION)) {
@@ -683,7 +706,6 @@ function SearchAutocomplete(props: Props) {
if (currentOptions.current !== action) {
currentOptions.current = action;
- const historyKeys = Pro && Pro.history ? Pro.history.historyKeys : {};
const fileOpenHistoryItems: Array<TS.HistoryItem> = Pro
? Pro.history.getHistory(historyKeys.fileOpenKey)
: [];
@@ -693,31 +715,20 @@ function SearchAutocomplete(props: Props) {
const fileEditHistoryItems: Array<TS.HistoryItem> = Pro
? Pro.history.getHistory(historyKeys.fileEditKey)
: [];
- function getOptions(
- items: TS.HistoryItem[],
- group: string
- ): SearchOptionType[] {
- if (!items) {
- return [];
- }
- return items.map((item: TS.HistoryItem) => ({
- id: item.lid,
- descr:
- '(' + format(item.creationTimeStamp, 'yyyy-MM-dd HH:mm') + ')',
- action: ExecActions.OPEN_HISTORY,
- fullName: item.url,
- label: item.path,
- group: group
- }));
- }
searchOptions.current = [
- ...getOptions(fileOpenHistoryItems, i18n.t('core:fileOpenHistory')),
- ...getOptions(
+ ...getHistoryOptions(
+ fileOpenHistoryItems,
+ i18n.t('core:fileOpenHistory')
+ ),
+ ...getHistoryOptions(
folderOpenHistoryItems,
i18n.t('core:folderOpenHistory')
),
- ...getOptions(fileEditHistoryItems, i18n.t('core:fileEditHistory'))
+ ...getHistoryOptions(
+ fileEditHistoryItems,
+ i18n.t('core:fileEditHistory')
+ )
];
}
} else if (isAction(action, SearchActions.BOOK)) {
@@ -749,6 +760,14 @@ function SearchAutocomplete(props: Props) {
i18n.t('core:searchBookmarks')
);
}
+ } else if (isAction(action, SearchActions.SEARCH_HISTORY)) {
+ const searchHistoryItems: Array<TS.HistoryItem> = Pro
+ ? Pro.history.getHistory(historyKeys.searchHistoryKey)
+ : [];
+ searchOptions.current = getHistoryOptions(
+ searchHistoryItems,
+ i18n.t('core:searchHistory')
+ );
} else if (isAction(action, SearchActions.SEARCH)) {
if (currentOptions.current !== action) {
currentOptions.current = action;
@@ -990,20 +1009,36 @@ function SearchAutocomplete(props: Props) {
isOpen.current = true;
}
actions.push(option);
+ } else if (isAction(option.action, SearchActions.SEARCH_HISTORY)) {
+ if (hasOptionsChanged) {
+ changeOptions(option.action);
+ isOpen.current = true;
+ }
+ actions.push(option);
} else if (option.action === ExecActions.OPEN_HISTORY) {
- const item: TS.HistoryItem = {
- path: option.label,
- url: option.fullName,
- lid: option.id,
- creationTimeStamp: 0
- };
- Pro.history.openItem(
- item,
- props.currentLocationId,
- props.openLink,
- props.openLocationById,
- props.openFsEntry
- );
+ if (option.searchQuery) {
+ if (option.id) {
+ props.switchLocationTypeByID(option.id).then(() => {
+ props.changeLocationByID(option.id);
+ props.setSearchQuery(option.searchQuery);
+ });
+ } else {
+ }
+ } else {
+ const item: TS.HistoryItem = {
+ path: option.label,
+ url: option.fullName,
+ lid: option.id,
+ creationTimeStamp: 0
+ };
+ Pro.history.openItem(
+ item,
+ props.currentLocation.uuid,
+ props.openLink,
+ props.openLocationById,
+ props.openFsEntry
+ );
+ }
searchOptions.current = SearchOptions;
currentOptions.current = undefined;
isOpen.current = false;
@@ -1017,7 +1052,7 @@ function SearchAutocomplete(props: Props) {
};
Pro.history.openItem(
item,
- props.currentLocationId,
+ props.currentLocation.uuid,
props.openLink,
props.openLocationById,
props.openFsEntry
@@ -1620,7 +1655,7 @@ function mapStateToProps(state) {
locations: getLocations(state),
currentLocation: getCurrentLocation(state),
isSearchMode: isSearchMode(state),
- currentLocationId: getCurrentLocationId(state),
+ //currentLocationId: getCurrentLocationId(state),
searches: getSearches(state)
// editedEntryPaths: getEditedEntryPaths(state)
};
@@ -1639,9 +1674,11 @@ function mapDispatchToProps(dispatch) {
// openCurrentDirectory: AppActions.openCurrentDirectory,
watchForChanges: AppActions.watchForChanges,
openLocationById: AppActions.openLocationById,
+ changeLocationByID: AppActions.changeLocationByID,
exitSearchMode: AppActions.exitSearchMode,
setSearchFilter: AppActions.setSearchFilter,
- openFsEntry: AppActions.openFsEntry
+ openFsEntry: AppActions.openFsEntry,
+ switchLocationTypeByID: AppActions.switchLocationTypeByID
},
dispatch
);
@@ -1655,4 +1692,5 @@ function mapDispatchToProps(dispatch) {
nextProp.currentDirectory === prevProp.currentDirectory &&
nextProp.indexedEntriesCount === prevProp.indexedEntriesCount;*/
+// @ts-ignore
export default connect(mapStateToProps, mapDispatchToProps)(SearchAutocomplete); // (React.memo(SearchAutocomplete, areEqual));
diff --git a/app/components/SearchBox.tsx b/app/components/SearchBox.tsx
index bc45860b1..ef2205e5c 100644
--- a/app/components/SearchBox.tsx
+++ b/app/components/SearchBox.tsx
@@ -16,7 +16,7 @@
*
*/
-import React, { useEffect, useState } from 'react';
+import React, { useState } from 'react';
import Tooltip from '-/components/Tooltip';
import IconButton from '@mui/material/IconButton';
import i18n from '-/services/i18n';
@@ -25,9 +25,6 @@ import DropDownIcon from '@mui/icons-material/ArrowDropDownOutlined';
import Popover from '@mui/material/Popover';
import SearchPopover from '-/components/SearchPopover';
import SearchAutocomplete from '-/components/SearchAutocomplete';
-import Autocomplete from '@mui/material/Autocomplete';
-import { TextField } from '@mui/material';
-import { SearchOptions } from '-/components/SearchOptions';
interface Props {
open: boolean;
diff --git a/app/components/SearchOptions.ts b/app/components/SearchOptions.ts
index 245d6fc52..9f2d50102 100644
--- a/app/components/SearchOptions.ts
+++ b/app/components/SearchOptions.ts
@@ -1,3 +1,5 @@
+import { TS } from '-/tagspaces.namespace';
+
export interface SearchOptionType {
id?: string;
label: string;
@@ -7,6 +9,7 @@ export interface SearchOptionType {
textcolor?: string;
action: string;
group?: string;
+ searchQuery?: TS.SearchQuery;
}
export type ActionType = { shortName: string; fullName?: string };
@@ -26,7 +29,7 @@ export const SearchQueryComposition = {
fullName: 'type:'
},
SIZE: {
- shortName: 's:',
+ shortName: 'si:',
fullName: 'size:'
},
LAST_MODIFIED: {
@@ -69,6 +72,10 @@ export const SearchActions = {
SEARCH: {
shortName: 'q:',
fullName: 'search-queries:'
+ },
+ SEARCH_HISTORY: {
+ shortName: 's:',
+ fullName: 'search-history:'
}
};
@@ -212,6 +219,14 @@ export const SearchOptions: Array<SearchOptionType> = [
descr: 'find and execute saved search queries',
group: 'actions'
},
+ {
+ id: SearchActions.SEARCH_HISTORY.shortName,
+ label: SearchActions.SEARCH_HISTORY.shortName,
+ fullName: SearchActions.SEARCH_HISTORY.fullName,
+ action: SearchActions.SEARCH_HISTORY.shortName,
+ descr: 'Execute search queries from history',
+ group: 'actions'
+ },
{
id: SearchQueryComposition.TAG_AND.shortName,
label: SearchQueryComposition.TAG_AND.shortName,
diff --git a/app/components/SearchPopover.tsx b/app/components/SearchPopover.tsx
index 3805911d8..89bc23c28 100644
--- a/app/components/SearchPopover.tsx
+++ b/app/components/SearchPopover.tsx
@@ -408,6 +408,7 @@ function SearchPopover(props: Props) {
forceIndexing.current = false;
fileSize.current = '';
props.setSearchQuery({});
+ props.exitSearchMode();
openCurrentDirectory();
props.onClose();
};
diff --git a/app/components/dialogs/settings/SettingsAdvanced.tsx b/app/components/dialogs/settings/SettingsAdvanced.tsx
index a436ca892..f51932385 100644
--- a/app/components/dialogs/settings/SettingsAdvanced.tsx
+++ b/app/components/dialogs/settings/SettingsAdvanced.tsx
@@ -206,6 +206,23 @@ function SettingsAdvanced(props: Props) {
<MenuItem value={100}>100</MenuItem>
</Select>
</ListItem>
+ <ListItem className={classes.listItem}>
+ <ListItemText primary={i18n.t('core:searchHistory')} />
+ <Select
+ data-tid="searchHistoryTID"
+ title={i18n.t('core:searchHistoryTitle')}
+ value={props.settings[historyKeys.searchHistoryKey]}
+ onChange={(event: any) =>
+ props.setHistory(historyKeys.searchHistoryKey, event.target.value)
+ }
+ input={<Input id="searchHistorySelector" />}
+ >
+ <MenuItem value={0}>{i18n.t('core:disabled')}</MenuItem>
+ <MenuItem value={10}>10</MenuItem>
+ <MenuItem value={50}>50</MenuItem>
+ <MenuItem value={100}>100</MenuItem>
+ </Select>
+ </ListItem>
</>
)}
<ListItem className={classes.listItem}>
diff --git a/app/reducers/app.ts b/app/reducers/app.ts
index fa8440928..c4236592a 100644
--- a/app/reducers/app.ts
+++ b/app/reducers/app.ts
@@ -1793,17 +1793,27 @@ export const actions = {
type: types.SET_CURRENLOCATIONID,
locationId
}),
- changeLocation: (location: TS.Location) => (
+ changeLocation: (location: TS.Location) => (dispatch: (action) => void) => {
+ dispatch(actions.changeLocationByID(location.uuid));
+ },
+ changeLocationByID: (locationId: string) => (
dispatch: (action) => void,
getState: () => any
) => {
const { currentLocationId } = getState().app;
- if (location.uuid !== currentLocationId) {
+ if (locationId !== currentLocationId) {
// dispatch(actions.exitSearchMode());
dispatch(LocationIndexActions.clearDirectoryIndex());
- dispatch(actions.setCurrentLocationId(location.uuid));
+ dispatch(actions.setCurrentLocationId(locationId));
}
},
+ switchLocationTypeByID: (locationID: string) => (
+ dispatch: (action) => Promise<string | null>,
+ getState: () => any
+ ): Promise<string | null> => {
+ const location: TS.Location = getLocation(getState(), locationID);
+ return dispatch(actions.switchLocationType(location));
+ },
/**
* @param location
* return Promise<currentLocationId> if location is changed or null if location and type is changed
@@ -2205,21 +2215,25 @@ export const actions = {
if (fsEntry.isFile) {
Pro.history.saveHistory(
historyKeys.fileOpenKey,
- fsEntry.path,
- generateSharingLink(currentLocation.uuid, relEntryPath),
- currentLocation.uuid,
+ {
+ path: fsEntry.path,
+ url: generateSharingLink(currentLocation.uuid, relEntryPath),
+ lid: currentLocation.uuid
+ },
getState().settings[historyKeys.fileOpenKey]
);
} else {
Pro.history.saveHistory(
historyKeys.folderOpenKey,
- fsEntry.path,
- generateSharingLink(
- currentLocation.uuid,
- relEntryPath,
- relEntryPath
- ),
- currentLocation.uuid,
+ {
+ path: fsEntry.path,
+ url: generateSharingLink(
+ currentLocation.uuid,
+ relEntryPath,
+ relEntryPath
+ ),
+ lid: currentLocation.uuid
+ },
getState().settings[historyKeys.folderOpenKey]
);
}
diff --git a/app/reducers/location-index.ts b/app/reducers/location-index.ts
index 6d8a01f5a..970bb339a 100644
--- a/app/reducers/location-index.ts
+++ b/app/reducers/location-index.ts
@@ -22,17 +22,31 @@ import {
extractFileExtension,
extractFileName,
extractTagsAsObjects,
- getThumbFileLocationForFile
+ getThumbFileLocationForFile,
+ generateSharingLink
} from '@tagspaces/tagspaces-common/paths';
import AppConfig from '-/AppConfig';
-import { getLocation, getLocationByPath, getLocations } from './locations';
-import { createDirectoryIndex } from '-/services/utils-io';
-import Search from '../services/search';
-import { actions as AppActions } from './app';
+import {
+ getCurrentLocation,
+ getLocation,
+ getLocationByPath,
+ getLocations
+} from './locations';
+import {
+ createDirectoryIndex,
+ getRelativeEntryPath
+} from '-/services/utils-io';
+import Search, { defaultTitle } from '../services/search';
+import {
+ actions as AppActions,
+ getCurrentLocationId,
+ getCurrentLocationPath
+} from './app';
import i18n from '../services/i18n';
import PlatformIO from '../services/platform-facade';
import GlobalSearch from '../services/search-index';
import { TS } from '-/tagspaces.namespace';
+import { Pro } from '-/pro';
export const types = {
SET_SEARCH_QUERY: 'SET_SEARCH_QUERY',
@@ -180,13 +194,34 @@ export default (state: any = initialState, action: any) => {
export const actions = {
setSearchQuery: (searchQuery: TS.SearchQuery) => (
- dispatch: (action) => void
+ dispatch: (action) => void,
+ getState: () => any
) => {
dispatch(actions.setSearchQueryInt(searchQuery));
// TODO rethink right place for this -to switch search mode based on searchQuery
if (Object.keys(searchQuery).length === 0) {
dispatch(AppActions.exitSearchMode());
} else {
+ const searchTitle = defaultTitle(searchQuery);
+ if (searchTitle.length > 0) {
+ const historyKeys = Pro.history.historyKeys;
+ const currentLocation = getCurrentLocation(getState());
+ Pro.history.saveHistory(
+ historyKeys.searchHistoryKey,
+ {
+ path:
+ searchTitle +
+ ' ' +
+ (currentLocation.path
+ ? currentLocation.path
+ : currentLocation.name),
+ url: '/',
+ lid: currentLocation.uuid,
+ searchQuery: searchQuery
+ },
+ getState().settings[historyKeys.searchHistoryKey]
+ );
+ }
dispatch(AppActions.enterSearchMode());
}
},
diff --git a/app/reducers/settings-default.ts b/app/reducers/settings-default.ts
index 5b5abdc95..23497157e 100644
--- a/app/reducers/settings-default.ts
+++ b/app/reducers/settings-default.ts
@@ -205,6 +205,7 @@ export default {
tsLastOpenedFilesHistory: 10,
tsLastOpenedFoldersHistory: 10,
tsLastEditedFilesHistory: 10,
+ tsSearchHistory: 10,
storedSearchesVisible: true,
showBookmarks: true,
fileOpenHistory: false,
diff --git a/app/services/search.ts b/app/services/search.ts
index acd539ecd..144071d07 100644
--- a/app/services/search.ts
+++ b/app/services/search.ts
@@ -92,6 +92,23 @@ export function haveSearchFilters(searchQuery: TS.SearchQuery) {
);
}
+export function defaultTitle(searchQuery: TS.SearchQuery) {
+ let title = '';
+ if (searchQuery.textQuery) {
+ title += searchQuery.textQuery;
+ }
+ if (searchQuery.tagsAND && searchQuery.tagsAND.length > 0) {
+ title += searchQuery.tagsAND.map(tag => ' +' + tag.title);
+ }
+ if (searchQuery.tagsNOT && searchQuery.tagsNOT.length > 0) {
+ title += searchQuery.tagsNOT.map(tag => ' -' + tag.title);
+ }
+ if (searchQuery.tagsOR && searchQuery.tagsOR.length > 0) {
+ title += searchQuery.tagsOR.map(tag => ' |' + tag.title);
+ }
+ return title.trim();
+}
+
// id, name, isFile, tags, extension, size, lmdt, path
// index[?size > '9000' && contains(path, 'Apple')]
diff --git a/app/tagspaces.namespace.d.ts b/app/tagspaces.namespace.d.ts
index a12d1c21e..124a58419 100644
--- a/app/tagspaces.namespace.d.ts
+++ b/app/tagspaces.namespace.d.ts
@@ -158,11 +158,16 @@ export namespace TS {
path: string;
uuid?: Uuid;
}
+
+ /**
+ * path: path or title to be shown
+ */
interface HistoryItem {
path: string;
url: string;
lid: string;
creationTimeStamp: number;
+ searchQuery?: TS.SearchQuery
}
interface BookmarkItem {