TagSpaces is an offline, open source, document manager with tagging support
fix Adding tag to a file breaks search results (#1869)
Stanimir67d5237bab2e
diff --git a/app/components/SearchAutocomplete.tsx b/app/components/SearchAutocomplete.tsxindex 55152acc4..c83bf63bb 100644--- a/app/components/SearchAutocomplete.tsx+++ b/app/components/SearchAutocomplete.tsx@@ -103,7 +103,7 @@ interface Props {currentLocationId: string;openFsEntry: (fsEntry: TS.FileSystemEntry) => void;searches: Array<TS.SearchQuery>;- editedEntryPaths: Array<TS.EditedEntryPath>;+ // editedEntryPaths: Array<TS.EditedEntryPath>;}const useStyles = makeStyles(theme => ({@@ -193,15 +193,6 @@ function SearchAutocomplete(props: Props) {}}, [props.currentLocation]);- useEffect(() => {- // HANDLE CHANGE currentDirectoryEntries (ADD/REMOVE TAGS) IN SEARCH RESULTS- if (!firstRender) {- if (props.isSearchMode && Object.keys(props.searchQuery).length > 0) {- executeSearch();- }- }- }, [props.editedEntryPaths]);-useEffect(() => {if (!firstRender) {if (!props.isSearchMode) {@@ -1430,7 +1421,11 @@ function SearchAutocomplete(props: Props) {onClose={handleClose}*/filterOptions={(options: Array<SearchOptionType>, state: any) => {const filteredOptions = options.filter(option => {- return option.label.toLowerCase().indexOf(state.inputValue.toLowerCase()) > -1;+ return (+ option.label+ .toLowerCase()+ .indexOf(state.inputValue.toLowerCase()) > -1+ );});if (filteredOptions.length === 0 && !haveEmptyAction()) {isOpen.current = false;@@ -1626,8 +1621,8 @@ function mapStateToProps(state) {currentLocation: getCurrentLocation(state),isSearchMode: isSearchMode(state),currentLocationId: getCurrentLocationId(state),- searches: getSearches(state),- editedEntryPaths: getEditedEntryPaths(state)+ searches: getSearches(state)+ // editedEntryPaths: getEditedEntryPaths(state)};}diff --git a/app/components/TagsPreview.tsx b/app/components/TagsPreview.tsxindex eb8d55084..dd3ae287c 100644--- a/app/components/TagsPreview.tsx+++ b/app/components/TagsPreview.tsx@@ -62,8 +62,8 @@ function TagsPreview(props: Props) {defaultTextColor,defaultBackgroundColor);- firstTagColor = tagColors.textcolor;- firstTagTextColor = tagColors.color;+ firstTagColor = tagColors.color;+ firstTagTextColor = tagColors.textcolor;}let secondTagColor = defaultBackgroundColor;diff --git a/app/perspectives/grid-perspective/components/MainContainer.tsx b/app/perspectives/grid-perspective/components/MainContainer.tsxindex a21a98701..36f9fa855 100644--- a/app/perspectives/grid-perspective/components/MainContainer.tsx+++ b/app/perspectives/grid-perspective/components/MainContainer.tsx@@ -50,6 +50,7 @@ import IOActions from '-/reducers/io-actions';import {actions as AppActions,getDirectoryMeta,+ getEditedEntryPaths,getLastSelectedEntry,getLastSelectedEntryPath,getSearchFilter,@@ -120,6 +121,7 @@ interface Props {setDirectoryMeta: (fsEntryMeta: TS.FileSystemEntryMeta) => void;searchResultsCount: number;searchFilter: string;+ editedEntryPaths: Array<TS.EditedEntryPath>;}function getSettings(directoryMeta: TS.FileSystemEntryMeta): TS.FolderSettings {@@ -310,12 +312,21 @@ function GridPerspective(props: Props) {forceUpdate();}}, [- props.directoryContent, // open subdirs+ props.currentDirectoryPath, // open subdirs+ // props.directoryContent, // open subdirs todo rethink this (replace with useEffect for currDirPath changes only)props.searchResultsCount,sortBy.current,orderBy.current]);+ useEffect(() => {+ // HANDLE (ADD/REMOVE sidecar TAGS) IN SEARCH RESULTS+ if (!firstRender && props.searchResultsCount > -1) {+ sortedDirContent.current = GlobalSearch.results;+ forceUpdate();+ }+ }, [props.editedEntryPaths]);+useEffect(() => {if (!firstRender) {const perspectiveSettings = getSettings(props.directoryMeta);@@ -475,7 +486,11 @@ function GridPerspective(props: Props) {if (someFileSelected) {clearSelection();} else {- props.setSelectedEntries(props.directoryContent);+ if (props.searchResultsCount < 0) {+ props.setSelectedEntries(props.directoryContent);+ } else {+ props.setSelectedEntries(GlobalSearch.results);+ }}};@@ -977,7 +992,8 @@ function mapStateToProps(state) {isDeleteMultipleEntriesDialogOpened: isDeleteMultipleEntriesDialogOpened(state),- searchFilter: getSearchFilter(state)+ searchFilter: getSearchFilter(state),+ editedEntryPaths: getEditedEntryPaths(state)};}diff --git a/app/reducers/app.ts b/app/reducers/app.tsindex d04fb3b33..f3cf21e44 100644--- a/app/reducers/app.ts+++ b/app/reducers/app.ts@@ -55,7 +55,8 @@ import {setLocationType,getUuid,getRelativeEntryPath,- getCleanLocationPath+ getCleanLocationPath,+ updateFsEntries} from '-/services/utils-io';import i18n from '../services/i18n';import { Pro } from '../pro';@@ -839,34 +840,10 @@ export default (state: any = initialState, action: any) => {...state,// warning: edit action is handled in FolderContainer; to reload column in KanBan if properties is changed (dir color)// editedEntryPaths: [{ action: 'edit', path: action.path }],- currentDirectoryEntries: state.currentDirectoryEntries.map(entry => {- if (entry.path !== action.path) {- return entry;- }- if (action.entry.tags && action.entry.tags.length > 0) {- /* const tags = [...entry.tags]; // .filter(tag => tag.type === 'plain')];- action.entry.tags.map(tag => {- if (!entry.tags.some(oldTag => oldTag.title === tag.title)) {- tags.push(tag);- }- return true;- }); */-- return {- ...entry,- ...action.entry- // tags- /* tags: [- ...entry.tags.filter(tag => tag.type === 'plain'),- ...action.entry.tags- ] */- };- }- return {- ...entry,- ...action.entry- };- })+ currentDirectoryEntries: updateFsEntries(+ state.currentDirectoryEntries,+ [action.entry]+ )};}/* case types.REFLECT_UPDATE_SIDECARMETA: {@@ -1784,10 +1761,22 @@ export const actions = {type: types.SET_SEARCH_RESULTS,searchResults}),- exitSearchMode: () => ({+ exitSearchMode: () => (dispatch: (action) => void, getState: () => any) => {+ const { searchMode } = getState().app;+ if (searchMode) {+ dispatch(actions.exitSearchModeInt());+ }+ },+ exitSearchModeInt: () => ({type: types.EXIT_SEARCH_MODE}),- enterSearchMode: () => ({+ enterSearchMode: () => (dispatch: (action) => void, getState: () => any) => {+ const { searchMode } = getState().app;+ if (!searchMode) {+ dispatch(actions.enterSearchModeInt());+ }+ },+ enterSearchModeInt: () => ({type: types.ENTER_SEARCH_MODE}),appendSearchResults: (searchResults: Array<any> | []) => ({@@ -2312,16 +2301,49 @@ export const actions = {newPath}),reflectRenameEntry: (path: string, newPath: string) => (- dispatch: (action) => void+ dispatch: (action) => void,+ getState: () => any) => {+ const { searchMode } = getState().app;+ if (searchMode) {+ GlobalSearch.results = GlobalSearch.results.map(+ (fsEntry: TS.FileSystemEntry) => {+ if (fsEntry.path === path) {+ return {+ ...fsEntry,+ path: newPath,+ name: extractFileName(newPath, PlatformIO.getDirSeparator())+ };+ }+ return fsEntry;+ }+ );+ }dispatch(actions.reflectRenameEntryInt(path, newPath));dispatch(LocationIndexActions.reflectRenameEntry(path, newPath));dispatch(actions.setSelectedEntries([]));},- updateCurrentDirEntry: (path: string, entry: any) => ({+ /**+ * TODO include path in entry => only one entry parameter+ * @param path+ * @param entry+ */+ updateCurrentDirEntry: (path: string, entry: any) => (+ dispatch: (action) => void,+ getState: () => any+ ) => {+ const { searchMode } = getState().app;+ if (searchMode) {+ GlobalSearch.results = updateFsEntries(GlobalSearch.results, [+ { ...entry, path }+ ]);+ } else {+ dispatch(actions.updateCurrentDirEntryInt(path, entry));+ }+ },+ updateCurrentDirEntryInt: (path: string, entry: any) => ({type: types.UPDATE_CURRENTDIR_ENTRY,- path,- entry+ entry: { ...entry, path }}),updateCurrentDirEntries: (dirEntries: TS.FileSystemEntry[]) => ({type: types.UPDATE_CURRENTDIR_ENTRIES,diff --git a/app/reducers/location-index.ts b/app/reducers/location-index.tsindex 569f3d79f..6d8a01f5a 100644--- a/app/reducers/location-index.ts+++ b/app/reducers/location-index.ts@@ -183,6 +183,7 @@ export const actions = {dispatch: (action) => void) => {dispatch(actions.setSearchQueryInt(searchQuery));+ // TODO rethink right place for this -to switch search mode based on searchQueryif (Object.keys(searchQuery).length === 0) {dispatch(AppActions.exitSearchMode());} else {diff --git a/app/services/utils-io.ts b/app/services/utils-io.tsindex f4ce89d99..5035044cf 100644--- a/app/services/utils-io.ts+++ b/app/services/utils-io.ts@@ -1425,3 +1425,25 @@ export function getRelativeEntryPath(relEntryPath = cleanFrontDirSeparator(relEntryPath);return relEntryPath;}++/**+ * @param entries+ * @param entriesChanges Array<TS.FileSystemEntry> but partly changes from FileSystemEntry model like { tags:[] } are acceptable+ * return entries: Array<TS.FileSystemEntry> updated from+ */+export function updateFsEntries(+ entries: Array<TS.FileSystemEntry>,+ entriesChanges: Array<any>+): Array<TS.FileSystemEntry> {+ return entries.map(entry => {+ const entryUpdated = entriesChanges.find(e => e.path === entry.path);+ if (!entryUpdated) {+ return entry;+ }++ return {+ ...entry,+ ...entryUpdated+ };+ });+}