Added relative date filter to dashboards (#16292)

This PR adds relative date filter operand to dashboard filters : 

<img width="1685" height="558" alt="image"
src="https://github.com/user-attachments/assets/a1f927e7-8c99-4171-b487-4b6a28779547"
/>

It also refactors the logic to add timezone and first day of the week
taking users preferences.

It has been tested on workflows and regular advanced filters also.


For step filters, which use a JSON format to store relative date
filters, I kept the current way to handle it.

There are a few workspaces that use a relative date filter in step
filter, so we want to avoid a migration, and instead handle both code
paths, and refactor everything to JSON later.

---------

Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
Lucas Bordeau
2025-12-15 18:51:57 +01:00
committed by GitHub
parent 31467f2173
commit e8fd5fa3ed
9 changed files with 167 additions and 36 deletions
@@ -1,12 +1,14 @@
import { DEFAULT_ADVANCED_FILTER_DROPDOWN_OFFSET } from '@/object-record/advanced-filter/constants/DefaultAdvancedFilterDropdownOffset';
import { getOperandLabel } from '@/object-record/object-filter-dropdown/utils/getOperandLabel';
import { useGetRelativeDateFilterWithUserTimezone } from '@/object-record/record-filter/hooks/useGetRelativeDateFilterWithUserTimezone';
import { Select } from '@/ui/input/components/Select';
import { GenericDropdownContentWidth } from '@/ui/layout/dropdown/constants/GenericDropdownContentWidth';
import { useUpsertStepFilterSettings } from '@/workflow/workflow-steps/workflow-actions/filter-action/hooks/useUpsertStepFilterSettings';
import { WorkflowStepFilterContext } from '@/workflow/workflow-steps/workflow-actions/filter-action/states/context/WorkflowStepFilterContext';
import { getStepFilterOperands } from '@/workflow/workflow-steps/workflow-actions/filter-action/utils/getStepFilterOperands';
import { useContext } from 'react';
import { type StepFilter, type ViewFilterOperand } from 'twenty-shared/types';
import { DEFAULT_RELATIVE_DATE_FILTER_VALUE } from 'twenty-shared/constants';
import { ViewFilterOperand, type StepFilter } from 'twenty-shared/types';
type WorkflowStepFilterOperandSelectProps = {
stepFilter: StepFilter;
@@ -28,7 +30,25 @@ export const WorkflowStepFilterOperandSelect = ({
label: getOperandLabel(operand),
}));
const { getRelativeDateFilterWithUserTimezone } =
useGetRelativeDateFilterWithUserTimezone();
const handleChange = (operand: ViewFilterOperand) => {
if (operand === ViewFilterOperand.IS_RELATIVE) {
const newRelativeDateFilter = getRelativeDateFilterWithUserTimezone(
DEFAULT_RELATIVE_DATE_FILTER_VALUE,
);
upsertStepFilterSettings({
stepFilterToUpsert: {
...stepFilter,
operand,
value: JSON.stringify(newRelativeDateFilter),
},
});
return;
}
upsertStepFilterSettings({
stepFilterToUpsert: {
...stepFilter,
@@ -9,6 +9,7 @@ import { FormRelativeDatePicker } from '@/object-record/record-field/ui/form-typ
import { FormSingleRecordPicker } from '@/object-record/record-field/ui/form-types/components/FormSingleRecordPicker';
import { FormTextFieldInput } from '@/object-record/record-field/ui/form-types/components/FormTextFieldInput';
import { type FieldMetadata } from '@/object-record/record-field/ui/types/FieldMetadata';
import { stringifyRelativeDateFilter } from '@/views/view-filter-value/utils/stringifyRelativeDateFilter';
import { WorkflowStepFilterValueCompositeInput } from '@/workflow/workflow-steps/workflow-actions/filter-action/components/WorkflowStepFilterValueCompositeInput';
import { useUpsertStepFilterSettings } from '@/workflow/workflow-steps/workflow-actions/filter-action/hooks/useUpsertStepFilterSettings';
@@ -22,7 +23,12 @@ import {
ViewFilterOperand,
type StepFilter,
} from 'twenty-shared/types';
import { isDefined, parseJson } from 'twenty-shared/utils';
import {
isDefined,
parseJson,
safeParseRelativeDateFilterJSONStringified,
type RelativeDateFilter,
} from 'twenty-shared/utils';
import { parseBooleanFromStringValue } from 'twenty-shared/workflow';
import { type JsonValue } from 'type-fest';
@@ -85,6 +91,17 @@ export const WorkflowStepFilterValueInput = ({
});
};
const handleRelativeDateFilterChange = (
newRelativeDateFilter: RelativeDateFilter,
) => {
upsertStepFilterSettings({
stepFilterToUpsert: {
...stepFilter,
value: JSON.stringify(newRelativeDateFilter),
},
});
};
const isDisabled = !stepFilter.operand;
const operandHasNoInput =
@@ -117,6 +134,18 @@ export const WorkflowStepFilterValueInput = ({
variableType === FieldMetadataType.DATE_TIME ||
variableType === FieldMetadataType.DATE;
const isRelativeDateFilter =
isDateField && stepFilter.operand === ViewFilterOperand.IS_RELATIVE;
const relativeDateFilter = safeParseRelativeDateFilterJSONStringified(
stepFilter.value,
);
const relativeDateFilterValue =
isRelativeDateFilter && isDefined(relativeDateFilter)
? stringifyRelativeDateFilter(relativeDateFilter)
: '';
if (isFullRecord) {
return (
<FormSingleRecordPicker
@@ -166,11 +195,11 @@ export const WorkflowStepFilterValueInput = ({
);
}
if (isDateField && stepFilter.operand === ViewFilterOperand.IS_RELATIVE) {
if (isRelativeDateFilter) {
return (
<FormRelativeDatePicker
defaultValue={stepFilter.value}
onChange={handleValueChange}
defaultValue={relativeDateFilterValue}
onChange={handleRelativeDateFilterChange}
readonly={readonly}
/>
);