Skip to content

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

Tsn224 extract index in a separate reducer (#780)

Stanimir869d74d7952e
diff --git a/app/components/LocationManager.js b/app/components/LocationManager.js
index 55e9c2a7b..d7fedb073 100644
--- a/app/components/LocationManager.js
+++ b/app/components/LocationManager.js
@@ -61,6 +61,7 @@ import {
type Location
} from '../reducers/locations';
import { actions as AppActions, getCurrentLocationId } from '../reducers/app';
+import { actions as LocationIndexActions } from '../reducers/location-index';
import { getPerspectives } from '../reducers/settings';
import i18n from '../services/i18n';
import { isObj } from '../utils/misc';
@@ -73,7 +74,7 @@ type Props = {
perspectives: Array<Object>,
currentLocationId: string,
loadDirectoryContent: (path: string) => void,
- openLocation: (path: string) => void,
+ openLocation: (location: Location) => void,
openFileNatively: (path: string) => void,
openDirectory: (path: string) => void,
createDirectoryIndex: (path: string) => void,
@@ -426,7 +427,7 @@ class LocationManager extends React.Component<Props, State> {
this.props.loadDirectoryContent(location.paths[0]);
} else {
this.loadSubDirectories(location, 1);
- this.props.openLocation(location.uuid);
+ this.props.openLocation(location);
this.state.locationRootPath = location.paths[0];
}
const grid = document.querySelector('[data-tid="perspectiveGridFileTable"]');
@@ -780,7 +781,7 @@ function mapDispatchToProps(dispatch) {
return bindActionCreators({
...LocationActions,
openLocation: AppActions.openLocation,
- createDirectoryIndex: AppActions.createDirectoryIndex,
+ createDirectoryIndex: LocationIndexActions.createDirectoryIndex,
closeLocation: AppActions.closeLocation,
loadDirectoryContent: AppActions.loadDirectoryContent,
reflectCreateEntry: AppActions.reflectCreateEntry,
diff --git a/app/components/LocationMenu.js b/app/components/LocationMenu.js
index a1a381067..0641ecf81 100644
--- a/app/components/LocationMenu.js
+++ b/app/components/LocationMenu.js
@@ -38,7 +38,7 @@ type Props = {
currentLocationId: string | null,
locations: Array<Location>,
menuAnchorEl: null | Object,
- openLocation: (locationId: string) => void
+ openLocation: (location: Location) => void
};
type State = {
@@ -71,8 +71,8 @@ class LocationMenu extends React.Component { // <Props, State>
}
};
- openLocation = locationId => {
- this.props.openLocation(locationId);
+ openLocation = location => {
+ this.props.openLocation(location);
this.toggleLocationChooser();
};
@@ -113,7 +113,7 @@ class LocationMenu extends React.Component { // <Props, State>
<MenuItem
data-tid="folderContainerMenuOpenLocation"
key={location.uuid}
- onClick={() => this.openLocation(location.uuid)}
+ onClick={() => this.openLocation(location)}
>
<ListItemIcon>
<FolderIcon />
diff --git a/app/components/Search.js b/app/components/Search.js
index 6842837ce..f9b30fd44 100644
--- a/app/components/Search.js
+++ b/app/components/Search.js
@@ -44,7 +44,8 @@ import FormControl from '@material-ui/core/FormControl';
import FormHelperText from '@material-ui/core/FormHelperText';
import TagsSelect from './TagsSelect';
import CustomLogo from './CustomLogo';
-import { actions as AppActions, getIndexedEntriesCount, isIndexing, getDirectoryPath } from '../reducers/app';
+import { actions as AppActions, getDirectoryPath } from '../reducers/app';
+import { actions as LocationIndexActions, getIndexedEntriesCount, isIndexing } from '../reducers/location-index';
import { getMaxSearchResults } from '../reducers/settings';
import styles from './SidePanels.css';
import i18n from '../services/i18n';
@@ -390,7 +391,7 @@ function mapStateToProps(state) {
function mapDispatchToProps(dispatch) {
return bindActionCreators({
- searchLocationIndex: AppActions.searchLocationIndex,
+ searchLocationIndex: LocationIndexActions.searchLocationIndex,
loadDirectoryContent: AppActions.loadDirectoryContent
}, dispatch);
}
diff --git a/app/components/dialogs/ColorPickerDialog.js b/app/components/dialogs/ColorPickerDialog.js
index 27d653a0d..1a21c868f 100644
--- a/app/components/dialogs/ColorPickerDialog.js
+++ b/app/components/dialogs/ColorPickerDialog.js
@@ -117,11 +117,14 @@ class ColorPickerDialog extends React.Component<Props, State> {
);
render() {
+ const {
+ open = false,
+ onClose
+ } = this.props;
return (
<GenericDialog
- open={this.props.open}
- onClose={this.props.onClose}
- onEnterKey={(event) => onEnterKeyHandler(event, this.onConfirm)}
+ open={open}
+ onClose={onClose}
renderTitle={this.renderTitle}
renderContent={this.renderContent}
renderActions={this.renderActions}
diff --git a/app/components/menus/TagMenu.js b/app/components/menus/TagMenu.js
index 9b8822b3f..969befabf 100644
--- a/app/components/menus/TagMenu.js
+++ b/app/components/menus/TagMenu.js
@@ -31,7 +31,7 @@ import MenuItem from '@material-ui/core/MenuItem';
import ConfirmDialog from '../dialogs/ConfirmDialog';
import EditTagDialog from '../dialogs/EditTagDialog';
import { type Tag, type TagGroup } from '../../reducers/taglibrary';
-import { actions as AppActions } from '../../reducers/app';
+import { actions as LocationIndexActions } from '../../reducers/location-index';
import i18n from '../../services/i18n';
import { type SearchQuery } from '../../services/search';
import { getMaxSearchResults } from '../../reducers/settings';
@@ -175,7 +175,7 @@ function mapStateToProps(state) {
function mapDispatchToProps(dispatch) {
return bindActionCreators({
- searchLocationIndex: AppActions.searchLocationIndex,
+ searchLocationIndex: LocationIndexActions.searchLocationIndex,
}, dispatch);
}
diff --git a/app/containers/MainPage.js b/app/containers/MainPage.js
index a67a2970a..73f4ee522 100644
--- a/app/containers/MainPage.js
+++ b/app/containers/MainPage.js
@@ -53,8 +53,9 @@ import {
// isFileDragged,
isEntryInFullWidth,
isUpdateAvailable,
- isIndexing, getDirectoryPath
+ getDirectoryPath
} from '../reducers/app';
+import { actions as LocationIndexActions, isIndexing } from '../reducers/location-index';
import { buffer } from '../utils/misc';
import { normalizePath } from '../utils/paths';
import TargetFileBox from '../components/TargetFileBox';
@@ -538,7 +539,7 @@ function mapDispatchToProps(dispatch) {
return bindActionCreators({
// setFileDragged: AppActions.setFileDragged,
hideNotifications: AppActions.hideNotifications,
- cancelDirectoryIndexing: AppActions.cancelDirectoryIndexing,
+ cancelDirectoryIndexing: LocationIndexActions.cancelDirectoryIndexing,
saveFile: AppActions.saveFile,
setZoomResetApp: SettingsActions.setZoomResetApp,
setZoomInApp: SettingsActions.setZoomInApp,
diff --git a/app/index.js b/app/index.js
index 1da61980e..8aaf9a8d1 100644
--- a/app/index.js
+++ b/app/index.js
@@ -71,7 +71,7 @@ document.addEventListener('storeLoaded', () => {
const state = store.getState();
const defaultLocationId = getDefaultLocationId(state);
if (defaultLocationId && defaultLocationId.length > 0) {
- store.dispatch(AppActions.openLocation(defaultLocationId));
+ store.dispatch(AppActions.openLocationById(defaultLocationId));
}
if (getCheckForUpdateOnStartup(state)) {
store.dispatch(SettingsActions.checkForUpdate());
diff --git a/app/reducers/app.js b/app/reducers/app.js
index 0aa13d3fc..5609d0a66 100644
--- a/app/reducers/app.js
+++ b/app/reducers/app.js
@@ -18,13 +18,10 @@
*/
import uuidv1 from 'uuid';
-import { type Location, getLocation, locationType } from './locations';
+import { type Location, locationType } from './locations';
import PlatformIO from '../services/platform-io';
-import Search, { type SearchQuery } from '../services/search';
import AppConfig from '../config';
import {
- type FileSystemEntry,
- createDirectoryIndex,
enhanceEntry,
deleteFilesPromise,
renameFilesPromise
@@ -43,7 +40,7 @@ import { sortByCriteria, formatDateTime4Tag } from '../utils/misc';
import i18n from '../services/i18n';
import { Pro } from '../pro';
import { getThumbnailURLPromise } from '../services/thumbsgenerator';
-// import { getTagDelimiter } from './settings';
+import { actions as LocationIndexActions } from './location-index';
export const types = {
DEVICE_ONLINE: 'APP/DEVICE_ONLINE',
@@ -55,12 +52,6 @@ export const types = {
LOAD_DIRECTORY_SUCCESS: 'APP/LOAD_DIRECTORY_SUCCESS',
LOAD_DIRECTORY_FAILURE: 'APP/LOAD_DIRECTORY_FAILURE',
CLEAR_DIRECTORY_CONTENT: 'APP/CLEAR_DIRECTORY_CONTENT',
- INDEX_DIRECTORY: 'APP/INDEX_DIRECTORY',
- INDEX_DIRECTORY_CLEAR: 'APP/INDEX_DIRECTORY_CLEAR',
- INDEX_DIRECTORY_START: 'APP/INDEX_DIRECTORY_START',
- INDEX_DIRECTORY_CANCEL: 'APP/INDEX_DIRECTORY_CANCEL',
- INDEX_DIRECTORY_SUCCESS: 'APP/INDEX_DIRECTORY_SUCCESS',
- INDEX_DIRECTORY_FAILURE: 'APP/INDEX_DIRECTORY_FAILURE',
INDEX_DIRECTORY_SEARCH: 'APP/INDEX_DIRECTORY_SEARCH',
OPEN_FILE: 'APP/OPEN_FILE',
TOGGLE_ENTRY_FULLWIDTH: 'APP/TOGGLE_ENTRY_FULLWIDTH',
@@ -121,8 +112,6 @@ export const initialState = {
currentLocationId: null,
currentDirectoryPath: '',
currentDirectoryEntries: [],
- currentDirectoryIndex: [],
- isIndexing: false,
isReadOnlyMode: false,
searchResults: [],
notificationStatus: {
@@ -177,24 +166,6 @@ export default (state: Object = initialState, action: Object) => {
currentDirectoryPath: ''
};
}
- case types.INDEX_DIRECTORY_START: {
- return {
- ...state,
- currentDirectoryIndex: [],
- isIndexing: true
- };
- }
- case types.INDEX_DIRECTORY_CLEAR: {
- return {
- ...state,
- currentDirectoryIndex: [],
- isIndexing: false
- };
- }
- case types.INDEX_DIRECTORY_CANCEL: {
- window.walkCanceled = true;
- return { ...state, isIndexing: false };
- }
case types.SET_CURRENLOCATIONID: {
return {
...state,
@@ -208,22 +179,6 @@ export default (state: Object = initialState, action: Object) => {
return result; */
return { ...state, lastSelectedEntry: action.entryPath };
}
- case types.INDEX_DIRECTORY_SUCCESS: {
- // return state; // uncomment to disable index in redux
- return {
- ...state,
- currentDirectoryIndex: action.directoryIndex,
- isIndexing: false
- };
- }
- case types.INDEX_DIRECTORY_FAILURE: {
- return {
- ...state,
- lastError: action.error,
- currentDirectoryIndex: [],
- isIndexing: false
- };
- }
case types.TOGGLE_ABOUT_DIALOG: {
return { ...state, aboutDialogOpened: !state.aboutDialogOpened };
}
@@ -327,7 +282,6 @@ export default (state: Object = initialState, action: Object) => {
}
case types.REFLECT_DELETE_ENTRY: {
let indexForRemoving = -1;
- let indexForRemovingInIndex = -1;
let indexForRenamingInOpenedFiles = -1;
// Updating entries in the current directory
let openedFiles = state.openedFiles;
@@ -337,86 +291,57 @@ export default (state: Object = initialState, action: Object) => {
openedFiles = []; // TODO extend for multiple files
}
});
- state.currentDirectoryIndex.forEach((entry, index) => {
- if (entry.path === action.path) { // TODO handle directory case, delete contained files and dirs
- indexForRemovingInIndex = index;
- }
- });
state.openedFiles.forEach((entry, index) => {
if (entry.path === action.path) { // TODO handle directory case, update contained files and dirs too
indexForRenamingInOpenedFiles = index;
}
});
let directoryEntries = state.currentDirectoryEntries;
- let directoryIndex = state.currentDirectoryIndex;
if (indexForRemoving >= 0) {
directoryEntries = [
...state.currentDirectoryEntries.slice(0, indexForRemoving),
...state.currentDirectoryEntries.slice(indexForRemoving + 1)
];
}
- if (indexForRemovingInIndex >= 0) {
- directoryIndex = [
- ...state.currentDirectoryIndex.slice(0, indexForRemovingInIndex),
- ...state.currentDirectoryIndex.slice(indexForRemovingInIndex + 1)
- ];
- }
- if (indexForRemovingInIndex >= 0 || indexForRemoving >= 0 || indexForRenamingInOpenedFiles >= 0) {
+ if (indexForRemoving >= 0 || indexForRenamingInOpenedFiles >= 0) {
return {
...state,
currentDirectoryEntries: directoryEntries,
- currentDirectoryIndex: directoryIndex,
openedFiles
};
}
return state;
}
case types.REFLECT_CREATE_ENTRY: {
- const newEntry: FileSystemEntry = {
- uuid: uuidv1(),
- name: extractFileName(action.path),
- isFile: action.isFile,
- extension: extractFileExtension(action.path),
- description: '',
- tags: [],
- size: 0,
- lmdt: (new Date()).getTime(),
- path: action.path
- };
let currentDirectoryEntries = state.currentDirectoryEntries;
// TODO Case changed current directory
- if (state.currentDirectoryPath === action.path) {
+ if (state.currentDirectoryPath === action.newEntry.path) {
// Reindex ??
}
// Workarround for double update caused by the watcher
let entryAlreadyAdded = false;
state.currentDirectoryEntries.forEach((entry) => {
- if (entry.path === action.path) {
+ if (entry.path === action.newEntry.path) {
entryAlreadyAdded = true;
}
});
if (
!entryAlreadyAdded &&
- extractParentDirectoryPath(action.path) === state.currentDirectoryPath
+ extractParentDirectoryPath(action.newEntry.path) === state.currentDirectoryPath
) {
currentDirectoryEntries = [ // TODO evtl. apply sorting
...state.currentDirectoryEntries,
- newEntry
+ action.newEntry
];
}
return {
...state,
- currentDirectoryEntries,
- currentDirectoryIndex: [
- newEntry,
- ...state.currentDirectoryIndex
- ],
+ currentDirectoryEntries
};
}
case types.REFLECT_RENAME_ENTRY: {
let indexForRenaming = -1;
- let indexForRenamingInIndex = -1;
let indexForRenamingInOpenedFiles = -1;
// Updating entries in the current directory
state.currentDirectoryEntries.forEach((entry, index) => {
@@ -424,18 +349,12 @@ export default (state: Object = initialState, action: Object) => {
indexForRenaming = index;
}
});
- state.currentDirectoryIndex.forEach((entry, index) => {
- if (entry.path === action.path) { // TODO handle directory case, update contained files and dirs too
- indexForRenamingInIndex = index;
- }
- });
state.openedFiles.forEach((entry, index) => {
if (entry.path === action.path) { // TODO handle directory case, update contained files and dirs too
indexForRenamingInOpenedFiles = index;
}
});
let directoryEntries = state.currentDirectoryEntries;
- let directoryIndex = state.currentDirectoryIndex;
let openedFiles = state.openedFiles;
if (indexForRenaming >= 0) {
const updateEntry = {
@@ -455,24 +374,6 @@ export default (state: Object = initialState, action: Object) => {
...state.currentDirectoryEntries.slice(indexForRenaming + 1)
];
}
- if (indexForRenamingInIndex >= 0) {
- const updateEntry = {
- ...state.currentDirectoryIndex[indexForRenamingInIndex],
- path: action.newPath, // TODO handle thumbPath
- thumbPath: getThumbFileLocationForFile(action.newPath),
- name: extractFileName(action.newPath),
- extension: extractFileExtension(action.newPath),
- tags: [
- ...state.currentDirectoryEntries[indexForRenaming].tags.filter(tag => tag.type === 'sidecar'), // add only sidecar tags
- ...extractTagsAsObjects(action.newPath) // , getTagDelimiter(state))
- ]
- };
- directoryIndex = [
- ...state.currentDirectoryIndex.slice(0, indexForRenamingInIndex),
- updateEntry,
- ...state.currentDirectoryIndex.slice(indexForRenamingInIndex + 1)
- ];
- }
if (indexForRenamingInOpenedFiles >= 0) {
const updateEntry = {
...state.openedFiles[indexForRenamingInOpenedFiles],
@@ -484,11 +385,10 @@ export default (state: Object = initialState, action: Object) => {
...state.openedFiles.slice(indexForRenamingInOpenedFiles + 1)
];
}
- if (indexForRenamingInIndex >= 0 || indexForRenaming >= 0 || indexForRenamingInOpenedFiles >= 0) {
+ if (indexForRenaming >= 0 || indexForRenamingInOpenedFiles >= 0) {
return {
...state,
currentDirectoryEntries: directoryEntries,
- currentDirectoryIndex: directoryIndex,
openedFiles
};
}
@@ -496,7 +396,6 @@ export default (state: Object = initialState, action: Object) => {
}
case types.REFLECT_UPDATE_SIDECARTAGS: {
let indexForUpdating = -1;
- let indexForUpdatingInIndex = -1;
let indexForUpdatingInOpenedFiles = -1;
// Updating entries in the current directory
state.currentDirectoryEntries.forEach((entry, index) => {
@@ -504,18 +403,12 @@ export default (state: Object = initialState, action: Object) => {
indexForUpdating = index;
}
});
- state.currentDirectoryIndex.forEach((entry, index) => {
- if (entry.path === action.path) {
- indexForUpdatingInIndex = index;
- }
- });
state.openedFiles.forEach((entry, index) => {
if (entry.path === action.path) {
indexForUpdatingInOpenedFiles = index;
}
});
let directoryEntries = state.currentDirectoryEntries;
- let directoryIndex = state.currentDirectoryIndex;
let openedFiles = state.openedFiles;
if (indexForUpdating >= 0) {
const updateEntry = {
@@ -531,20 +424,6 @@ export default (state: Object = initialState, action: Object) => {
...state.currentDirectoryEntries.slice(indexForUpdating + 1)
];
}
- if (indexForUpdatingInIndex >= 0) {
- const updateEntry = {
- ...state.currentDirectoryIndex[indexForUpdatingInIndex],
- tags: [
- ...state.currentDirectoryIndex[indexForUpdatingInIndex].tags.filter(tag => tag.type === 'plain'),
- ...action.tags
- ]
- };
- directoryIndex = [
- ...state.currentDirectoryIndex.slice(0, indexForUpdatingInIndex),
- updateEntry,
- ...state.currentDirectoryIndex.slice(indexForUpdatingInIndex + 1)
- ];
- }
if (indexForUpdatingInOpenedFiles >= 0) {
const updateEntry = {
...state.openedFiles[indexForUpdatingInOpenedFiles],
@@ -556,11 +435,10 @@ export default (state: Object = initialState, action: Object) => {
...state.openedFiles.slice(indexForUpdatingInOpenedFiles + 1)
];
}
- if (indexForUpdatingInIndex >= 0 || indexForUpdating >= 0 || indexForUpdatingInOpenedFiles >= 0) {
+ if (indexForUpdating >= 0 || indexForUpdatingInOpenedFiles >= 0) {
return {
...state,
currentDirectoryEntries: directoryEntries,
- currentDirectoryIndex: directoryIndex,
openedFiles
};
}
@@ -900,78 +778,61 @@ export const actions = {
);
});
},
- startDirectoryIndexing: () => ({ type: types.INDEX_DIRECTORY_START }),
- cancelDirectoryIndexing: () => ({ type: types.INDEX_DIRECTORY_CANCEL }),
- searchLocationIndexFinished: (searchResults: Array<Object> | []) => ({
+ updateSearchResults: (searchResults: Array<Object> | []) => ({
type: types.INDEX_DIRECTORY_SEARCH,
searchResults
}),
- searchLocationIndex: (searchQuery: SearchQuery) => (
- dispatch: (actions: Object) => void,
- getState: () => Object
- ) => {
- dispatch(actions.showNotification('Searching...', 'default', false));
- setTimeout(() => { // Workarround used to show the start search notication
- Search.searchLocationIndex(
- getState().app.currentDirectoryIndex,
- searchQuery
- ).then((searchResults) => {
- dispatch(actions.searchLocationIndexFinished(searchResults));
- dispatch(actions.hideNotifications());
- return true;
- }).catch(() => {
- dispatch(actions.searchLocationIndexFinished([]));
- dispatch(actions.hideNotifications());
- dispatch(actions.showNotification('Search failed.', 'warning', true));
- });
- }, 50);
- },
setCurrentLocationId: (locationId: string | null) => ({
type: types.SET_CURRENLOCATIONID,
locationId
}),
- openLocation: (locationId: string) => (
+ openLocationById: (locationId: string) => (
dispatch: (actions: Object) => void,
getState: () => Object
) => {
const locations: Array<Location> = getState().locations;
- const currentLocationId = getState().app;
- if (Pro && Pro.Watcher) {
- Pro.Watcher.stopWatching();
- }
-
locations.map(location => {
if (location.uuid === locationId) {
- if (location.type === locationType.TYPE_CLOUD) {
- PlatformIO.enableObjectStoreSupport(location).then(() => {
- dispatch(actions.showNotification('Connected to object store', 'default', true));
- dispatch(actions.setReadOnlyMode(location.isReadOnly || false));
- dispatch(actions.setCurrentLocationId(location.uuid));
- dispatch(actions.loadDirectoryContent(location.paths[0]));
- if (locationId !== currentLocationId) {
- dispatch(actions.createDirectoryIndex(location.paths[0]));
- }
- return true;
- }).catch(() => {
- dispatch(actions.showNotification('Connection to object store failed!', 'warning', true));
- PlatformIO.disableObjectStoreSupport();
- });
- } else { // if (location.type === locationType.TYPE_LOCAL) {
- PlatformIO.disableObjectStoreSupport();
- dispatch(actions.setReadOnlyMode(location.isReadOnly || false));
- dispatch(actions.setCurrentLocationId(location.uuid));
- dispatch(actions.loadDirectoryContent(location.paths[0]));
- if (locationId !== currentLocationId) {
- dispatch(actions.createDirectoryIndex(location.paths[0]));
- }
- if (Pro && Pro.Watcher && location.watchForChanges) {
- Pro.Watcher.watchFolder(location.paths[0], dispatch, actions);
- }
- }
+ dispatch(actions.openLocation(location));
}
return true;
});
},
+ openLocation: (location: Location) => (
+ dispatch: (actions: Object) => void,
+ getState: () => Object
+ ) => {
+ if (Pro && Pro.Watcher) {
+ Pro.Watcher.stopWatching();
+ }
+ const currentLocationId = getState().app;
+ if (location.type === locationType.TYPE_CLOUD) {
+ PlatformIO.enableObjectStoreSupport(location).then(() => {
+ dispatch(actions.showNotification('Connected to object store', 'default', true));
+ dispatch(actions.setReadOnlyMode(location.isReadOnly || false));
+ dispatch(actions.setCurrentLocationId(location.uuid));
+ dispatch(actions.loadDirectoryContent(location.paths[0]));
+ if (location.uuid !== currentLocationId) {
+ dispatch(LocationIndexActions.createDirectoryIndex(location.paths[0]));
+ }
+ return true;
+ }).catch(() => {
+ dispatch(actions.showNotification('Connection to object store failed!', 'warning', true));
+ PlatformIO.disableObjectStoreSupport();
+ });
+ } else { // if (location.type === locationType.TYPE_LOCAL) {
+ PlatformIO.disableObjectStoreSupport();
+ dispatch(actions.setReadOnlyMode(location.isReadOnly || false));
+ dispatch(actions.setCurrentLocationId(location.uuid));
+ dispatch(actions.loadDirectoryContent(location.paths[0]));
+ if (location.uuid !== currentLocationId) {
+ dispatch(LocationIndexActions.createDirectoryIndex(location.paths[0]));
+ }
+ if (Pro && Pro.Watcher && location.watchForChanges) {
+ Pro.Watcher.watchFolder(location.paths[0], dispatch, actions);
+ }
+ }
+ },
closeLocation: (locationId: string) => (
dispatch: (actions: Object) => void,
getState: () => Object
@@ -984,7 +845,7 @@ export const actions = {
// location needed evtl. to unwatch many loc. root folders if available
dispatch(actions.setCurrentLocationId(null));
dispatch(actions.clearDirectoryContent());
- dispatch(actions.clearDirectoryIndex());
+ dispatch(LocationIndexActions.clearDirectoryIndex());
if (Pro && Pro.Watcher) {
Pro.Watcher.stopWatching();
}
@@ -993,40 +854,9 @@ export const actions = {
});
}
},
- createDirectoryIndex: (directoryPath: string) => (
- dispatch: (actions: Object) => void,
- getState: () => Object
- ) => {
- const state = getState();
- const currentLocation: Location = getLocation(state, state.app.currentLocationId);
- dispatch(actions.startDirectoryIndexing());
- createDirectoryIndex(directoryPath)
- .then(directoryIndex => {
- dispatch(actions.indexDirectorySuccess(directoryIndex));
- if (Pro && currentLocation.persistIndex) {
- Pro.Indexer.persistIndex(directoryPath, directoryIndex);
- }
- return true;
- })
- .catch(err => {
- dispatch(actions.indexDirectoryFailure(err));
- // dispatch(actions.startDirectoryIndexing());
- });
- },
- clearDirectoryIndex: () => ({
- type: types.INDEX_DIRECTORY_CLEAR
- }),
clearDirectoryContent: () => ({
type: types.CLEAR_DIRECTORY_CONTENT
}),
- indexDirectorySuccess: (directoryIndex: Array<Object>) => ({
- type: types.INDEX_DIRECTORY_SUCCESS,
- directoryIndex
- }),
- indexDirectoryFailure: (error: string) => ({
- type: types.INDEX_DIRECTORY_FAILURE,
- error
- }),
showNotification: (
text: string,
notificationType?: string = 'default',
@@ -1138,25 +968,59 @@ export const actions = {
order
}),
closeAllFiles: () => ({ type: types.CLOSE_ALL_FILES }),
- reflectDeleteEntry: (path: string) => ({
+ reflectDeleteEntryInt: (path: string) => ({
type: types.REFLECT_DELETE_ENTRY,
path
}),
- reflectCreateEntry: (path: string, isFile: boolean) => ({
+ reflectDeleteEntry: (path: string) => (
+ dispatch: (actions: Object) => void
+ ) => {
+ dispatch(actions.reflectDeleteEntryInt(path));
+ dispatch(LocationIndexActions.reflectDeleteEntry(path));
+ },
+ reflectCreateEntryInt: (newEntry) => ({
type: types.REFLECT_CREATE_ENTRY,
- path,
- isFile
+ newEntry
}),
- reflectRenameEntry: (path: string, newPath: string) => ({
+ reflectCreateEntry: (path: string, isFile: boolean) => (
+ dispatch: (actions: Object) => void
+ ) => {
+ const newEntry = {
+ uuid: uuidv1(),
+ name: extractFileName(path),
+ isFile,
+ extension: extractFileExtension(path),
+ description: '',
+ tags: [],
+ size: 0,
+ lmdt: (new Date()).getTime(),
+ path
+ };
+ dispatch(actions.reflectCreateEntryInt(newEntry));
+ dispatch(LocationIndexActions.reflectCreateEntry(newEntry));
+ },
+ reflectRenameEntryInt: (path: string, newPath: string) => ({
type: types.REFLECT_RENAME_ENTRY,
path,
newPath
}),
- reflectUpdateSidecarTags: (path: string, tags: Array<Tags>) => ({
+ reflectRenameEntry: (path: string, newPath: string) => (
+ dispatch: (actions: Object) => void
+ ) => {
+ dispatch(actions.reflectRenameEntryInt(path, newPath));
+ dispatch(LocationIndexActions.reflectRenameEntry(path, newPath));
+ },
+ reflectUpdateSidecarTagsInt: (path: string, tags: Array<Tags>) => ({
type: types.REFLECT_UPDATE_SIDECARTAGS,
path,
tags
}),
+ reflectUpdateSidecarTags: (path: string, tags: Array<Tags>) => (
+ dispatch: (actions: Object) => void
+ ) => {
+ dispatch(actions.reflectUpdateSidecarTagsInt(path, tags));
+ dispatch(LocationIndexActions.reflectUpdateSidecarTags(path, tags));
+ },
deleteFile: (filePath: string) => (
dispatch: (actions: Object) => void,
getState: () => Object
@@ -1428,6 +1292,4 @@ export const getNotificationStatus = (state: Object) => state.app.notificationSt
export const getSearchResults = (state: Object) => state.app.currentDirectoryEntries;
export const getSearchResultCount = (state: Object) => state.app.currentDirectoryEntries.length;
export const getCurrentLocationId = (state: Object) => state.app.currentLocationId;
-export const getIndexedEntriesCount = (state: Object) => state.app.currentDirectoryIndex.length;
export const isEntryInFullWidth = (state: Object) => state.app.isEntryInFullWidth;
-export const isIndexing = (state: Object) => state.app.isIndexing;
diff --git a/app/reducers/index.js b/app/reducers/index.js
index 776342051..e8fd68417 100644
--- a/app/reducers/index.js
+++ b/app/reducers/index.js
@@ -23,6 +23,7 @@ import settings from './settings';
import app from './app';
import locations from './locations';
import taglibrary from './taglibrary';
+import locationIndex from './location-index';
const rootReducer = combineReducers({
settings,
@@ -30,6 +31,7 @@ const rootReducer = combineReducers({
locations,
taglibrary,
router,
+ locationIndex
});
export default rootReducer;
diff --git a/app/reducers/location-index.js b/app/reducers/location-index.js
new file mode 100644
index 000000000..9fd870d48
--- /dev/null
+++ b/app/reducers/location-index.js
@@ -0,0 +1,266 @@
+/**
+ * TagSpaces - universal file and folder organizer
+ * Copyright (C) 2017-present TagSpaces UG (haftungsbeschraenkt)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License (version 3) as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * @flow
+ */
+
+import { type Location, getLocation } from './locations';
+import { createDirectoryIndex } from '../services/utils-io';
+import { Pro } from '../pro';
+import {
+ extractFileExtension,
+ extractFileName,
+ extractTagsAsObjects,
+ getThumbFileLocationForFile
+} from '../utils/paths';
+import type { SearchQuery } from '../services/search';
+import Search from '../services/search';
+import { actions as AppActions } from './app';
+
+export const types = {
+ INDEX_DIRECTORY: 'INDEX_DIRECTORY',
+ INDEX_DIRECTORY_CLEAR: 'INDEX_DIRECTORY_CLEAR',
+ INDEX_DIRECTORY_START: 'INDEX_DIRECTORY_START',
+ INDEX_DIRECTORY_CANCEL: 'INDEX_DIRECTORY_CANCEL',
+ INDEX_DIRECTORY_SUCCESS: 'INDEX_DIRECTORY_SUCCESS',
+ INDEX_DIRECTORY_FAILURE: 'INDEX_DIRECTORY_FAILURE',
+ INDEX_DIRECTORY_SEARCH: 'INDEX_DIRECTORY_SEARCH',
+ REFLECT_DELETE_ENTRY: 'REFLECT_DELETE_ENTRY',
+ REFLECT_CREATE_ENTRY: 'REFLECT_CREATE_ENTRY',
+ REFLECT_RENAME_ENTRY: 'REFLECT_RENAME_ENTRY',
+ REFLECT_UPDATE_SIDECARTAGS: 'REFLECT_UPDATE_SIDECARTAGS',
+};
+
+
+export const initialState = {
+ currentDirectoryIndex: [],
+ isIndexing: false
+};
+
+export default (state: Object = initialState, action: Object) => {
+ switch (action.type) {
+ case types.INDEX_DIRECTORY_START: {
+ return {
+ ...state,
+ currentDirectoryIndex: [],
+ isIndexing: true
+ };
+ }
+ case types.INDEX_DIRECTORY_CLEAR: {
+ return {
+ ...state,
+ currentDirectoryIndex: [],
+ isIndexing: false
+ };
+ }
+ case types.INDEX_DIRECTORY_CANCEL: {
+ window.walkCanceled = true;
+ return { ...state, isIndexing: false };
+ }
+ case types.INDEX_DIRECTORY_SUCCESS: {
+ // return state; // uncomment to disable index in redux
+ return {
+ ...state,
+ currentDirectoryIndex: action.directoryIndex,
+ isIndexing: false
+ };
+ }
+ case types.INDEX_DIRECTORY_FAILURE: {
+ return {
+ ...state,
+ lastError: action.error,
+ currentDirectoryIndex: [],
+ isIndexing: false
+ };
+ }
+ case types.REFLECT_DELETE_ENTRY: {
+ let indexForRemovingInIndex = -1;
+ state.currentDirectoryIndex.forEach((entry, index) => {
+ if (entry.path === action.path) { // TODO handle directory case, delete contained files and dirs
+ indexForRemovingInIndex = index;
+ }
+ });
+ let directoryIndex = state.currentDirectoryIndex;
+ if (indexForRemovingInIndex >= 0) {
+ directoryIndex = [
+ ...state.currentDirectoryIndex.slice(0, indexForRemovingInIndex),
+ ...state.currentDirectoryIndex.slice(indexForRemovingInIndex + 1)
+ ];
+ }
+ if (indexForRemovingInIndex >= 0) {
+ return {
+ ...state,
+ currentDirectoryIndex: directoryIndex
+ };
+ }
+ return state;
+ }
+ case types.REFLECT_CREATE_ENTRY: {
+ return {
+ ...state,
+ currentDirectoryIndex: [
+ action.newEntry,
+ ...state.currentDirectoryIndex
+ ],
+ };
+ }
+ case types.REFLECT_RENAME_ENTRY: {
+ let indexForRenamingInIndex = -1;
+ state.currentDirectoryIndex.forEach((entry, index) => {
+ if (entry.path === action.path) { // TODO handle directory case, update contained files and dirs too
+ indexForRenamingInIndex = index;
+ }
+ });
+ let directoryIndex = state.currentDirectoryIndex;
+ if (indexForRenamingInIndex >= 0) {
+ const updateEntry = {
+ ...state.currentDirectoryIndex[indexForRenamingInIndex],
+ path: action.newPath, // TODO handle thumbPath
+ thumbPath: getThumbFileLocationForFile(action.newPath),
+ name: extractFileName(action.newPath),
+ extension: extractFileExtension(action.newPath),
+ tags: [
+ ...state.currentDirectoryIndex[indexForRenamingInIndex].tags.filter(tag => tag.type === 'sidecar'), // add only sidecar tags
+ ...extractTagsAsObjects(action.newPath) // , getTagDelimiter(state))
+ ]
+ };
+ directoryIndex = [
+ ...state.currentDirectoryIndex.slice(0, indexForRenamingInIndex),
+ updateEntry,
+ ...state.currentDirectoryIndex.slice(indexForRenamingInIndex + 1)
+ ];
+ }
+ if (indexForRenamingInIndex >= 0) {
+ return {
+ ...state,
+ currentDirectoryIndex: directoryIndex,
+ };
+ }
+ return state;
+ }
+ case types.REFLECT_UPDATE_SIDECARTAGS: {
+ let indexForUpdatingInIndex = -1;
+ state.currentDirectoryIndex.forEach((entry, index) => {
+ if (entry.path === action.path) {
+ indexForUpdatingInIndex = index;
+ }
+ });
+ let directoryIndex = state.currentDirectoryIndex;
+ if (indexForUpdatingInIndex >= 0) {
+ const updateEntry = {
+ ...state.currentDirectoryIndex[indexForUpdatingInIndex],
+ tags: [
+ ...state.currentDirectoryIndex[indexForUpdatingInIndex].tags.filter(tag => tag.type === 'plain'),
+ ...action.tags
+ ]
+ };
+ directoryIndex = [
+ ...state.currentDirectoryIndex.slice(0, indexForUpdatingInIndex),
+ updateEntry,
+ ...state.currentDirectoryIndex.slice(indexForUpdatingInIndex + 1)
+ ];
+ }
+ if (indexForUpdatingInIndex >= 0) {
+ return {
+ ...state,
+ currentDirectoryIndex: directoryIndex
+ };
+ }
+ return state;
+ }
+ default: {
+ return state;
+ }
+ }
+};
+
+export const actions = {
+ startDirectoryIndexing: () => ({ type: types.INDEX_DIRECTORY_START }),
+ cancelDirectoryIndexing: () => ({ type: types.INDEX_DIRECTORY_CANCEL }),
+ createDirectoryIndex: (directoryPath: string) => (
+ dispatch: (actions: Object) => void,
+ getState: () => Object
+ ) => {
+ const state = getState();
+ const currentLocation: Location = getLocation(state, state.app.currentLocationId);
+ dispatch(actions.startDirectoryIndexing());
+ createDirectoryIndex(directoryPath)
+ .then(directoryIndex => {
+ dispatch(actions.indexDirectorySuccess(directoryIndex));
+ if (Pro && currentLocation.persistIndex) {
+ Pro.Indexer.persistIndex(directoryPath, directoryIndex);
+ }
+ return true;
+ })
+ .catch(err => {
+ dispatch(actions.indexDirectoryFailure(err));
+ // dispatch(actions.startDirectoryIndexing());
+ });
+ },
+ clearDirectoryIndex: () => ({
+ type: types.INDEX_DIRECTORY_CLEAR
+ }),
+ searchLocationIndex: (searchQuery: SearchQuery) => (
+ dispatch: (actions: Object) => void,
+ getState: () => Object
+ ) => {
+ dispatch(AppActions.showNotification('Searching...', 'default', false));
+ setTimeout(() => { // Workarround used to show the start search notication
+ Search.searchLocationIndex(
+ getState().locationIndex.currentDirectoryIndex,
+ searchQuery
+ ).then((searchResults) => {
+ dispatch(AppActions.updateSearchResults(searchResults));
+ dispatch(AppActions.hideNotifications());
+ return true;
+ }).catch(() => {
+ dispatch(AppActions.updateSearchResults([]));
+ dispatch(AppActions.hideNotifications());
+ dispatch(AppActions.showNotification('Search failed.', 'warning', true));
+ });
+ }, 50);
+ },
+ indexDirectorySuccess: (directoryIndex: Array<Object>) => ({
+ type: types.INDEX_DIRECTORY_SUCCESS,
+ directoryIndex
+ }),
+ indexDirectoryFailure: (error: string) => ({
+ type: types.INDEX_DIRECTORY_FAILURE,
+ error
+ }),
+ reflectDeleteEntry: (path: string) => ({
+ type: types.REFLECT_DELETE_ENTRY,
+ path
+ }),
+ reflectCreateEntry: (newEntry) => ({
+ type: types.REFLECT_CREATE_ENTRY,
+ newEntry
+ }),
+ reflectRenameEntry: (path: string, newPath: string) => ({
+ type: types.REFLECT_RENAME_ENTRY,
+ path,
+ newPath
+ }),
+ reflectUpdateSidecarTags: (path: string, tags: Array<Tags>) => ({
+ type: types.REFLECT_UPDATE_SIDECARTAGS,
+ path,
+ tags
+ })
+};
+
+// Selectors
+export const getIndexedEntriesCount = (state: Object) => state.locationIndex.currentDirectoryIndex.length;
+export const isIndexing = (state: Object) => state.locationIndex.isIndexing;
diff --git a/app/reducers/locations.js b/app/reducers/locations.js
index ec8ed4664..d48888930 100644
--- a/app/reducers/locations.js
+++ b/app/reducers/locations.js
@@ -168,7 +168,7 @@ export const actions = {
dispatch: (actions: Object) => void
) => {
dispatch(actions.createLocation(location));
- dispatch(AppActions.openLocation(location.uuid));
+ dispatch(AppActions.openLocation(location));
},
createLocation: (location: Location) => ({ type: types.ADD_LOCATION, location }),
moveLocationUp: (uuid: string) => ({ type: types.MOVE_UP_LOCATION, uuid }),
diff --git a/app/store/configureStore.dev.js b/app/store/configureStore.dev.js
index a9e083b10..d8f75042e 100644
--- a/app/store/configureStore.dev.js
+++ b/app/store/configureStore.dev.js
@@ -71,7 +71,7 @@ const configureStore = (initialState) => {
onlineListener(store.dispatch);
- persistStore(store, { blacklist: ['app'] }, () => {
+ persistStore(store, { blacklist: ['app', 'locationIndex'] }, () => {
document.dispatchEvent(new Event('storeLoaded'));
// store.dispatch(push('/main'));
console.log('Store rehydrated.');
diff --git a/app/store/configureStore.prod.js b/app/store/configureStore.prod.js
index e0c17052a..3ab7b611d 100644
--- a/app/store/configureStore.prod.js
+++ b/app/store/configureStore.prod.js
@@ -35,7 +35,7 @@ const enhancer = compose(
function configureStore(initialState) {
const store = createStore(rootReducer, initialState, enhancer);
onlineListener(store.dispatch);
- persistStore(store, { blacklist: ['app'] }, () => {
+ persistStore(store, { blacklist: ['app', 'locationIndex'] }, () => {
document.dispatchEvent(new Event('storeLoaded'));
});
return store;