Skip to content

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

add date created in search filters (#2378)

Stanimir42694b4e4de7
diff --git a/src/renderer/components/EditSearchQuery.tsx b/src/renderer/components/EditSearchQuery.tsx
index 8713fe04a..4ab63859b 100644
--- a/src/renderer/components/EditSearchQuery.tsx
+++ b/src/renderer/components/EditSearchQuery.tsx
@@ -96,6 +96,15 @@ function EditSearchQuery(props: Props) {
setTempSearchQuery({ lastModified: value });
};
+ const handleDateCreatedChange = (
+ event: React.ChangeEvent<HTMLInputElement>,
+ ) => {
+ const { target } = event;
+ const { value, name } = target;
+
+ setTempSearchQuery({ dateCreated: value });
+ };
+
function removeTags(tagsArray, removeTagsArray) {
// eslint-disable-next-line react/no-access-state-in-setstate
return tagsArray.filter((tag) =>
@@ -589,6 +598,36 @@ function EditSearchQuery(props: Props) {
</MenuItem>
</TsSelect>
</FormControl>
+ <FormControl disabled={isIndexing !== undefined}>
+ <TsSelect
+ value={tempSearchQuery.dateCreated}
+ onChange={handleDateCreatedChange}
+ label={t('core:dateCreatedSearchTitle')}
+ >
+ <MenuItem value="">{t('core:anyTime')}</MenuItem>
+ <MenuItem value={AppConfig.SearchTimePeriods.today.key}>
+ {t('core:today')}
+ </MenuItem>
+ <MenuItem value={AppConfig.SearchTimePeriods.yesterday.key}>
+ {t('core:yesterday')}
+ </MenuItem>
+ <MenuItem value={AppConfig.SearchTimePeriods.past7Days.key}>
+ {t('core:past7Days')}
+ </MenuItem>
+ <MenuItem value={AppConfig.SearchTimePeriods.past30Days.key}>
+ {t('core:past30Days')}
+ </MenuItem>
+ <MenuItem value={AppConfig.SearchTimePeriods.past6Months.key}>
+ {t('core:past6Months')}
+ </MenuItem>
+ <MenuItem value={AppConfig.SearchTimePeriods.pastYear.key}>
+ {t('core:pastYear')}
+ </MenuItem>
+ <MenuItem value={AppConfig.SearchTimePeriods.moreThanYear.key}>
+ {t('core:moreThanYear')}
+ </MenuItem>
+ </TsSelect>
+ </FormControl>
<FormControl>
<TooltipTS title={t('enterTimePeriodTooltip')}>
<LocalizationProvider dateAdapter={AdapterDateFns}>
diff --git a/src/renderer/components/SearchAutocomplete.tsx b/src/renderer/components/SearchAutocomplete.tsx
index 46520e684..f68bc4e3c 100644
--- a/src/renderer/components/SearchAutocomplete.tsx
+++ b/src/renderer/components/SearchAutocomplete.tsx
@@ -135,6 +135,9 @@ function SearchAutocomplete(props: Props) {
const lastModified = useRef<string>(
searchQuery.lastModified ? searchQuery.lastModified : '',
);
+ const dateCreated = useRef<string>(
+ searchQuery.dateCreated ? searchQuery.dateCreated : '',
+ );
const tagTimePeriod = useRef<string>('');
const tagTimePeriodHelper = useRef<string>(' ');
const tagTimePeriodFrom = useRef<number | null>(
@@ -279,6 +282,17 @@ function SearchAutocomplete(props: Props) {
t('core:' + searchQuery.lastModified),
});
}
+ if (searchQuery.dateCreated) {
+ dateCreated.current = searchQuery.dateCreated;
+ emptySearch = false;
+ actions.push({
+ action: SearchQueryComposition.DATE_CREATED.fullName,
+ label:
+ SearchQueryComposition.DATE_CREATED.fullName +
+ ' ' +
+ t('core:' + searchQuery.dateCreated),
+ });
+ }
if (searchQuery.tagTimePeriodFrom) {
tagTimePeriodFrom.current = searchQuery.tagTimePeriodFrom;
emptySearch = false;
@@ -390,6 +404,13 @@ function SearchAutocomplete(props: Props) {
) {
lastModified.current = '';
}
+ if (
+ !exceptions.some((action) =>
+ isAction(action.action, SearchQueryComposition.DATE_CREATED),
+ )
+ ) {
+ dateCreated.current = '';
+ }
tagTimePeriod.current = '';
tagTimePeriodHelper.current = ' ';
tagTimePeriodFrom.current = null;
@@ -530,6 +551,7 @@ function SearchAutocomplete(props: Props) {
searchType: searchType.current,
fileTypes: fileTypes.current,
lastModified: lastModified.current,
+ dateCreated: dateCreated.current,
fileSize: fileSize.current,
tagTimePeriodFrom: tagTimePeriodFrom.current,
tagTimePeriodTo: tagTimePeriodTo.current,
@@ -768,6 +790,24 @@ function SearchAutocomplete(props: Props) {
searchOptions.current = options;
optionsChanged = true;
}
+ } else if (isAction(action, SearchQueryComposition.DATE_CREATED)) {
+ if (
+ currentOptions.current !== SearchQueryComposition.DATE_CREATED.shortName
+ ) {
+ currentOptions.current = action;
+ const options = [];
+
+ Object.entries(AppConfig.SearchTimePeriods).forEach((period: any) => {
+ options.push({
+ id: period[1].key,
+ action: ExecActions.DATE_CREATED_SEARCH,
+ label: t('core:' + period[1].key),
+ filter,
+ });
+ });
+ searchOptions.current = options;
+ optionsChanged = true;
+ }
} else if (isAction(action, SearchQueryComposition.SCOPE)) {
if (currentOptions.current !== SearchQueryComposition.SCOPE.shortName) {
currentOptions.current = action;
@@ -1037,7 +1077,8 @@ function SearchAutocomplete(props: Props) {
}
// executeSearch();
} else if (
- isAction(option.action, SearchQueryComposition.LAST_MODIFIED)
+ isAction(option.action, SearchQueryComposition.LAST_MODIFIED) ||
+ isAction(option.action, SearchQueryComposition.DATE_CREATED)
) {
if (hasOptionsChanged) {
changeOptions(option.action);
@@ -1060,6 +1101,22 @@ function SearchAutocomplete(props: Props) {
changeOptions(option.action);
}
// executeSearch();
+ } else if (option.action === ExecActions.DATE_CREATED_SEARCH) {
+ const id = setActionLabel(
+ SearchQueryComposition.DATE_CREATED,
+ option,
+ );
+ if (id) {
+ dateCreated.current = id;
+ setSearchQuery({
+ ...searchQuery,
+ dateCreated: id,
+ executeSearch: false,
+ });
+ }
+ if (hasOptionsChanged) {
+ changeOptions(option.action);
+ }
} else if (isAction(option.action, SearchQueryComposition.SCOPE)) {
if (hasOptionsChanged) {
changeOptions(option.action);
diff --git a/src/renderer/components/SearchOptions.ts b/src/renderer/components/SearchOptions.ts
index cce1b2993..6c982ea76 100644
--- a/src/renderer/components/SearchOptions.ts
+++ b/src/renderer/components/SearchOptions.ts
@@ -38,6 +38,10 @@ export const SearchQueryComposition = {
shortName: 'lm:',
fullName: 'modified:',
},
+ DATE_CREATED: {
+ shortName: 'cd:',
+ fullName: 'created:',
+ },
SCOPE: {
shortName: 'sc:',
fullName: 'scope:',
@@ -86,6 +90,7 @@ export const ExecActions = {
TYPE_SEARCH: 'type_search',
SIZE_SEARCH: 'size_search',
LAST_MODIFIED_SEARCH: 'last_modified_search',
+ DATE_CREATED_SEARCH: 'date_created_search',
SCOPE_SEARCH: 'scope_search',
ACCURACY_SEARCH: 'accuracy_search',
};
diff --git a/src/renderer/components/SearchOptionsMenu.ts b/src/renderer/components/SearchOptionsMenu.ts
index 387e95c8e..4d86e40e7 100644
--- a/src/renderer/components/SearchOptionsMenu.ts
+++ b/src/renderer/components/SearchOptionsMenu.ts
@@ -88,38 +88,40 @@ export function getSearchOptions(): Array<SearchOptionType> {
'allows searching for entries having one tag or another (logical OR)',
group: 'query',
});
- if (Pro) {
- options.push({
- id: SearchQueryComposition.TYPE.shortName,
- label: SearchQueryComposition.TYPE.shortName,
- fullName: SearchQueryComposition.TYPE.fullName,
- action: SearchQueryComposition.TYPE.shortName,
- descr: 'filter by file type: document, video, files, folders',
- group: 'query',
- });
- }
+ options.push({
+ id: SearchQueryComposition.TYPE.shortName,
+ label: SearchQueryComposition.TYPE.shortName,
+ fullName: SearchQueryComposition.TYPE.fullName,
+ action: SearchQueryComposition.TYPE.shortName,
+ descr: 'filter by file type: document, video, files, folders',
+ group: 'query',
+ });
- if (Pro) {
- options.push({
- id: SearchQueryComposition.SIZE.shortName,
- label: SearchQueryComposition.SIZE.shortName,
- fullName: SearchQueryComposition.SIZE.fullName,
- action: SearchQueryComposition.SIZE.shortName,
- descr: 'list sizes from advanced search',
- group: 'query',
- });
- }
+ options.push({
+ id: SearchQueryComposition.SIZE.shortName,
+ label: SearchQueryComposition.SIZE.shortName,
+ fullName: SearchQueryComposition.SIZE.fullName,
+ action: SearchQueryComposition.SIZE.shortName,
+ descr: 'list sizes from advanced search',
+ group: 'query',
+ });
- if (Pro) {
- options.push({
- id: SearchQueryComposition.LAST_MODIFIED.shortName,
- label: SearchQueryComposition.LAST_MODIFIED.shortName,
- fullName: SearchQueryComposition.LAST_MODIFIED.fullName,
- action: SearchQueryComposition.LAST_MODIFIED.shortName,
- descr: 'list last modified options from advanced search',
- group: 'query',
- });
- }
+ options.push({
+ id: SearchQueryComposition.LAST_MODIFIED.shortName,
+ label: SearchQueryComposition.LAST_MODIFIED.shortName,
+ fullName: SearchQueryComposition.LAST_MODIFIED.fullName,
+ action: SearchQueryComposition.LAST_MODIFIED.shortName,
+ descr: 'list last modified options from advanced search',
+ group: 'query',
+ });
+ options.push({
+ id: SearchQueryComposition.DATE_CREATED.shortName,
+ label: SearchQueryComposition.DATE_CREATED.shortName,
+ fullName: SearchQueryComposition.DATE_CREATED.fullName,
+ action: SearchQueryComposition.DATE_CREATED.shortName,
+ descr: 'list date created options from advanced search',
+ group: 'query',
+ });
options.push({
id: SearchQueryComposition.SCOPE.shortName,
diff --git a/src/renderer/hooks/SearchQueryContextProvider.tsx b/src/renderer/hooks/SearchQueryContextProvider.tsx
index fd7fdec87..4efa733f3 100644
--- a/src/renderer/hooks/SearchQueryContextProvider.tsx
+++ b/src/renderer/hooks/SearchQueryContextProvider.tsx
@@ -129,6 +129,7 @@ export const SearchQueryContextProvider = ({
searchType: tempSearchQuery.current.searchType,
fileTypes: tempSearchQuery.current.fileTypes,
lastModified: tempSearchQuery.current.lastModified,
+ dateCreated: tempSearchQuery.current.dateCreated,
fileSize: tempSearchQuery.current.fileSize,
tagTimePeriodFrom: tempSearchQuery.current.tagTimePeriodFrom,
tagTimePeriodTo: tempSearchQuery.current.tagTimePeriodTo,
diff --git a/src/renderer/locales/en/core.json b/src/renderer/locales/en/core.json
index 952f305fb..64e99612a 100644
--- a/src/renderer/locales/en/core.json
+++ b/src/renderer/locales/en/core.json
@@ -1099,5 +1099,6 @@
"fullNameRequired": "Full name required",
"importCompleted": "Import completed",
"importCompletedWithErrors": "Import completed with errors",
+ "dateCreatedSearchTitle": "Date Created",
"-": "-"
}
diff --git a/src/renderer/services/search.ts b/src/renderer/services/search.ts
index 0ceb52e1c..5728c74cd 100644
--- a/src/renderer/services/search.ts
+++ b/src/renderer/services/search.ts
@@ -36,6 +36,7 @@ export function haveSearchFilters(searchQuery: TS.SearchQuery) {
(searchQuery.fileTypes !== undefined &&
searchQuery.fileTypes !== AppConfig.SearchTypeGroups.any) ||
searchQuery.lastModified ||
+ searchQuery.dateCreated ||
searchQuery.tagTimePeriodFrom ||
searchQuery.tagTimePeriodTo ||
searchQuery.tagPlaceLat ||
@@ -431,64 +432,76 @@ export default class Search {
});
}
-function filterIndex(data, searchQuery: TS.SearchQuery) {
- console.log('Pro filter ' + JSON.stringify(searchQuery));
- let results = data;
+function filterByDatePeriod(
+ items: TS.SearchIndex[],
+ periodKey: string | undefined,
+ getTimeValue: (item: TS.SearchIndex) => number | undefined,
+): TS.SearchIndex[] {
+ if (!periodKey) return items;
const now = new Date();
- const today = new Date(
- now.getFullYear() + '-' + (now.getMonth() + 1) + '-' + now.getUTCDate(),
- );
+ // start of today in local timezone
+ const startOfToday = new Date(
+ now.getFullYear(),
+ now.getMonth(),
+ now.getDate(),
+ ).getTime();
const msInDay = 1000 * 60 * 60 * 24;
- if (searchQuery.lastModified === AppConfig.SearchTimePeriods.today.key) {
- results = results.filter((entry) => entry.lmdt > today.getTime());
- } else if (
- searchQuery.lastModified === AppConfig.SearchTimePeriods.yesterday.key
- ) {
- results = results.filter(
- (entry) =>
- entry.lmdt > today.getTime() - msInDay && entry.lmdt < today.getTime(),
- );
- } else if (
- searchQuery.lastModified === AppConfig.SearchTimePeriods.past7Days.key
- ) {
- results = results.filter(
- (entry) =>
- entry.lmdt > today.getTime() - msInDay * 7 &&
- entry.lmdt < today.getTime(),
- );
- } else if (
- searchQuery.lastModified === AppConfig.SearchTimePeriods.past30Days.key
- ) {
- results = results.filter(
- (entry) =>
- entry.lmdt > today.getTime() - msInDay * 30 &&
- entry.lmdt < today.getTime(),
- );
- } else if (
- searchQuery.lastModified === AppConfig.SearchTimePeriods.past6Months.key
- ) {
- results = results.filter(
- (entry) =>
- entry.lmdt > today.getTime() - msInDay * 30 * 6 &&
- entry.lmdt < today.getTime(),
- );
- } else if (
- searchQuery.lastModified === AppConfig.SearchTimePeriods.pastYear.key
- ) {
- results = results.filter(
- (entry) =>
- entry.lmdt > today.getTime() - msInDay * 365 &&
- entry.lmdt < today.getTime(),
- );
- } else if (
- searchQuery.lastModified === AppConfig.SearchTimePeriods.moreThanYear.key
- ) {
- results = results.filter(
- (entry) => entry.lmdt < today.getTime() - msInDay * 365,
- );
+
+ // convenience predicate helpers
+ const after = (ts: number) => (entry: TS.SearchIndex) => {
+ const t = getTimeValue(entry) ?? 0;
+ return t > ts;
+ };
+ const between = (fromTs: number, toTs: number) => (entry: TS.SearchIndex) => {
+ const t = getTimeValue(entry) ?? 0;
+ return t > fromTs && t < toTs;
+ };
+ const before = (ts: number) => (entry: TS.SearchIndex) => {
+ const t = getTimeValue(entry) ?? 0;
+ return t < ts;
+ };
+
+ let predicate: ((entry: TS.SearchIndex) => boolean) | null = null;
+
+ if (periodKey === AppConfig.SearchTimePeriods.today.key) {
+ predicate = after(startOfToday);
+ } else if (periodKey === AppConfig.SearchTimePeriods.yesterday.key) {
+ predicate = between(startOfToday - msInDay, startOfToday);
+ } else if (periodKey === AppConfig.SearchTimePeriods.past7Days.key) {
+ predicate = between(startOfToday - msInDay * 7, startOfToday);
+ } else if (periodKey === AppConfig.SearchTimePeriods.past30Days.key) {
+ predicate = between(startOfToday - msInDay * 30, startOfToday);
+ } else if (periodKey === AppConfig.SearchTimePeriods.past6Months.key) {
+ predicate = between(startOfToday - msInDay * 30 * 6, startOfToday);
+ } else if (periodKey === AppConfig.SearchTimePeriods.pastYear.key) {
+ predicate = between(startOfToday - msInDay * 365, startOfToday);
+ } else if (periodKey === AppConfig.SearchTimePeriods.moreThanYear.key) {
+ predicate = before(startOfToday - msInDay * 365);
}
+ if (!predicate) return items;
+ return items.filter(predicate);
+}
+
+function filterIndex(
+ data: TS.SearchIndex[],
+ searchQuery: TS.SearchQuery,
+): TS.SearchIndex[] {
+ console.log('Pro filter ' + JSON.stringify(searchQuery));
+ let results: TS.SearchIndex[] = data;
+
+ results = filterByDatePeriod(
+ results,
+ searchQuery.lastModified,
+ (entry) => entry.lmdt,
+ );
+ results = filterByDatePeriod(
+ results,
+ searchQuery.dateCreated,
+ (entry) => entry.cdt,
+ );
+
if (searchQuery.fileSize === AppConfig.SearchSizes.empty.key) {
results = results.filter(
(entry) =>
diff --git a/src/renderer/tagspaces.namespace.d.ts b/src/renderer/tagspaces.namespace.d.ts
index bcce8e8f4..986f717e9 100644
--- a/src/renderer/tagspaces.namespace.d.ts
+++ b/src/renderer/tagspaces.namespace.d.ts
@@ -107,6 +107,7 @@ export namespace TS {
tagsOR?: Array<Tag>;
tagsNOT?: Array<Tag>;
lastModified?: string;
+ dateCreated?: string;
fileSize?: string;
searchBoxing?: ScopeType;
searchType?: 'fuzzy' | 'semistrict' | 'strict';
@@ -208,6 +209,13 @@ export namespace TS {
links?: Link[];
}
+ interface SearchIndex extends FileSystemEntry {
+ fromTime?: number;
+ toTime?: number;
+ lat?: number;
+ lon?: number;
+ }
+
type LinkType = 'url' | 'email' | 'tslink' | 'hashtag' | 'mention';
interface Link {