Files
Jonathan Adelineandpeter_baserow a26c5ce6b0 feat: filter dashboard widgets by date or date range (#6954)
* fix: handle calendar boundaries in date comparisons

* feat: add a single-date filter to the dashboard API

* feat: send the viewer timezone with dashboard data requests

* refactor: share dashboard filter controls and target settings

* feat: configure dashboard filter conditions on date fields

* feat: add the Date filter to the dashboard widget picker

Implement the single-date mode of #6886.

* fix: restore dragging dashboard filters by their titles

* feat: persist the dashboard date filter display format

* feat: choose how dashboard filter dates are displayed

* test: align numeric filter checks with shared dashboard controls

* style: format the dashboard date filter component test

* fix: accept fixed UTC offsets in dashboard date filters

* fix: expose dashboard filter dates as ISO strings in formulas

* fix: seed dashboard conditions with compatible operators

* feat: expose date ranges in dashboard formulas and viewer selections

* feat: configure inclusive date range conditions for dashboard targets

* feat: persist and dispatch inclusive dashboard date ranges

* docs: describe date range selections in the dashboard dispatch API

* feat: select and configure dashboard date ranges

* fix: let viewers clear each dashboard date range endpoint

* fix: preserve unfinished dashboard date range bounds on refresh

* fix: refresh public dashboards after a date filter mode change

* test: group dashboard date filter mode transitions

* fix: warn about incompatible date range formula operands

* fix: convert dashboard date defaults from the saved widget

* fix: keep dashboard filter controls aligned when a condition has a warning

* fix: clear incompatible dashboard conditions when switching date modes

* fix: show cleared date conditions without warnings or redundant saves

* fix: ignore empty dashboard date conditions after switching modes

* fix: ignore dashboard date conditions without a single date and simplify mode switch clearing

---------

Co-authored-by: peter_baserow <peter@baserow.io>
2026-12-03 20:40:18 +04:00

432 lines
14 KiB
Vue

<template>
<div v-if="modelValue">
<div
v-if="
showEmptyState && modelValue.length === 0 && filterGroups.length === 0
"
>
<div class="filters__none">
<div class="filters__none-title">
{{ $t('localBaserowTableServiceConditionalForm.noFilterTitle') }}
</div>
<div class="filters__none-description">
{{ $t('localBaserowTableServiceConditionalForm.noFilterText') }}
</div>
</div>
</div>
<ViewFieldConditionsForm
:filters="getDataSourceFilters()"
:filter-groups="filterGroups"
:disable-filter="false"
:filter-read-only="readOnly"
:filter-type="filterType"
:fields="fields"
:read-only="readOnly"
full-width
sorted
class="filters__items"
:class="{ 'filters__items--frozen': frozen }"
:inert="frozen ? '' : null"
:prepare-value="prepareValue"
:add-condition-string="
$t('localBaserowTableServiceConditionalForm.addFilter')
"
:add-condition-group-string="
$t('localBaserowTableServiceConditionalForm.addFilterGroup')
"
:placeholder="
$t('localBaserowTableServiceConditionalForm.textFilterInputPlaceholder')
"
@add-filter="addFilter($event)"
@add-filter-group="addFilterGroup($event)"
@delete-filter="deleteFilter($event)"
@delete-filter-group="deleteFilterGroup($event)"
@update-filter="updateFilter($event)"
@update-filter-type="updateFilterType($event)"
>
<template
#filterInputComponent="{
slotProps: { filter, field, filterType: propFilterType },
}"
>
<InjectedFormulaInput
v-if="
propFilterType.hasEditableValue && !propFilterType.isDeprecated()
"
:model-value="getFormulaObject(filter)"
class="filters__value--formula-input"
allow-raw-values
:read-only="readOnly"
:disabled="readOnly"
:placeholder="
$t(
'localBaserowTableServiceConditionalForm.expressionFilterInputPlaceholder'
)
"
@input="
updateFilter({
filter,
values: { value: $event.formula, mode: $event.mode },
})
"
>
<template #raw-input="{ value, input }">
<component
:is="propFilterType.getInputComponent(field)"
:filter="{ ...filter, value }"
:fields="fields"
:disabled="readOnly"
:read-only="readOnly"
:placeholder="
$t(
'localBaserowTableServiceConditionalForm.textFilterInputPlaceholder'
)
"
@input="input"
@migrate="updateFilter({ filter, values: $event })"
/>
</template>
</InjectedFormulaInput>
</template>
</ViewFieldConditionsForm>
<!-- Shown under the conditions, above the add buttons. -->
<slot name="before-footer" />
<div v-if="!readOnly" class="filters_footer">
<slot name="actions" v-bind="{ addFilter, addFilterGroup }">
<ButtonText
type="secondary"
size="small"
icon="iconoir-plus"
class="filters__add"
:disabled="frozen"
@click.prevent="addFilter()"
>
{{ $t('localBaserowTableServiceConditionalForm.addFilter') }}
</ButtonText>
<ButtonText
type="secondary"
size="small"
icon="iconoir-plus"
class="filters__add"
:disabled="frozen"
@click.prevent="addFilterGroup()"
>
{{ $t('localBaserowTableServiceConditionalForm.addFilterGroup') }}
</ButtonText>
</slot>
<component
:is="component"
v-for="(component, index) in footerComponents"
:key="index"
/>
</div>
</div>
</template>
<script>
import ViewFieldConditionsForm from '@baserow/modules/database/components/view/ViewFieldConditionsForm.vue'
import {
getFirstCompatibleFilterType,
hasCompatibleFilterTypes,
} from '@baserow/modules/database/utils/field'
import { notifyIf } from '@baserow/modules/core/utils/error'
import { ulid } from 'ulid'
import InjectedFormulaInput from '@baserow/modules/core/components/formula/InjectedFormulaInput'
export default {
name: 'LocalBaserowTableServiceConditionalForm',
components: {
InjectedFormulaInput,
ViewFieldConditionsForm,
},
inject: {
dataProvidersAllowed: { default: null },
/** What filter values may read, when a host allows less than elsewhere. */
filterDataProvidersAllowed: { default: null },
},
provide() {
return {
availableViewFilterTypes: computed(() => this.filterTypes),
dataProvidersAllowed:
this.filterDataProvidersAllowed ?? this.dataProvidersAllowed,
// Lets a footer component, like Kuma's prompt, work with the filters.
serviceFilterForm: {
// Freezes the filters, e.g. while Kuma is asked to change them, so
// they can't change underneath the request.
setFrozen: (frozen) => {
this.frozen = frozen
},
},
}
},
props: {
allowedFilterTypes: { type: Object, default: null },
defaultFilterValue: {
type: Object,
default: () => ({ formula: '', mode: 'raw' }),
},
showEmptyState: { type: Boolean, default: true },
readOnly: { type: Boolean, default: false },
modelValue: {
type: Array,
required: true,
},
filterGroups: {
type: Array,
required: false,
default: () => [],
},
fields: {
type: Array,
required: true,
},
filterType: {
type: String,
required: true,
},
},
emits: ['update:modelValue', 'update:filterGroups', 'update:filterType'],
data() {
return {
frozen: false,
}
},
computed: {
filterTypes() {
return this.allowedFilterTypes ?? this.$registry.getAll('viewFilter')
},
footerComponents() {
return Object.values(this.$registry.getAll('plugin'))
.flatMap((plugin) => plugin.getServiceFilterFooterComponents())
.filter((component) => component)
},
databaseSelected() {
return this.databases.find(
(database) => database.id === this.databaseSelectedId
)
},
tables() {
return this.databaseSelected?.tables || []
},
},
methods: {
/*
* Responsible for returning the first compatible field we have in
* our schema fields. Used by `addFilter` to decide what the newly
* added filter's field should be.
*/
getFirstCompatibleField(fields) {
return fields
.slice()
.sort((a, b) => b.primary - a.primary)
.find((field) => hasCompatibleFilterTypes(field, this.filterTypes))
},
/*
* Responsible for returning all current data source filters in their
* existing array order. Because we pass `sorted` to
* `ViewFieldConditionsForm`, it renders filters and groups in insertion
* order rather than re-sorting them by id. This keeps newly added filters
* and groups appended (matching the database grid view), which id-based
* sorting cannot guarantee for our client-side `ulid` ids.
*/
getDataSourceFilters() {
// The `value` prop is an array of filters with a formula object value.
// The `ViewFieldConditionsForm` expects the filter value itself to be a
// string, so we keep the mode alongside the flattened formula string.
return this.modelValue.map((filterConf) => {
const formulaObject = this.normalizeFormulaObject(filterConf)
return {
...filterConf,
value: formulaObject.formula,
mode: formulaObject.mode,
}
})
},
/*
* Responsible for asynchronously adding a new data source filter.
* Use the first allowed operator compatible with the field, and the provided
* default value. If a `filterGroupId` is provided, it is added
* to that group, otherwise it applies directly to the service.
*/
async addFilter({
filterGroupId = null,
field = this.getFirstCompatibleField(this.fields),
conditions,
} = {}) {
try {
const filterType =
field && getFirstCompatibleFilterType(field, this.filterTypes)
if (!filterType) {
await this.$store.dispatch('toast/error', {
title: this.$t(
'localBaserowTableServiceConditionalForm.noCompatibleFilterTypesErrorTitle'
),
message: this.$t(
'localBaserowTableServiceConditionalForm.noCompatibleFilterTypesErrorMessage'
),
})
} else {
const seeds = conditions ?? [
{ type: filterType.type, value: this.defaultFilterValue },
]
// Setting an `id` of `ulid` is necessary for two reasons:
// 1) So that we can distinguish between filters locally
// 2) It has to match what is sorted against `sortNumbersAndUuid1Asc`.
this.$emit('update:modelValue', [
...this.modelValue,
...seeds.map(({ type, value }) => ({
id: ulid(),
field: field.id,
type,
value: { ...value },
group: filterGroupId,
})),
])
}
} catch (error) {
notifyIf(error, 'dataSource')
}
},
/*
* Responsible for adding a new filter group. A group is created with a
* default AND operator, and a first filter is seeded inside it (mirroring
* the database grid view's behaviour). If a `parentGroupId` is provided,
* the group is nested inside that parent group.
*/
addFilterGroup({ filterGroupId = null, parentGroupId = null } = {}) {
const groupId = filterGroupId || ulid()
const newFilterGroups = [
...this.filterGroups,
{
id: groupId,
filter_type: 'AND',
parent_group: parentGroupId,
},
]
this.$emit('update:filterGroups', newFilterGroups)
// Seed the new group with a first filter, just like the grid view does.
this.addFilter({ filterGroupId: groupId })
},
/*
* Removes a filter group along with all of its descendant groups and any
* filters belonging to them.
*/
deleteFilterGroup({ group }) {
const groupIdsToRemove = new Set([group.id])
// Collect nested groups iteratively (UI allows a few levels of nesting).
let changed = true
while (changed) {
changed = false
for (const filterGroup of this.filterGroups) {
if (
filterGroup.parent_group != null &&
groupIdsToRemove.has(filterGroup.parent_group) &&
!groupIdsToRemove.has(filterGroup.id)
) {
groupIdsToRemove.add(filterGroup.id)
changed = true
}
}
}
const newFilterGroups = this.filterGroups.filter(
({ id }) => !groupIdsToRemove.has(id)
)
const newFilters = this.modelValue.filter(
({ group: filterGroupId }) => !groupIdsToRemove.has(filterGroupId)
)
this.$emit('update:filterGroups', newFilterGroups)
this.$emit('update:modelValue', newFilters)
},
/*
* Updates the filter type (AND/OR). When a `filterGroup` is provided, the
* group's own operator is updated, otherwise the service's top-level
* operator is updated.
*/
updateFilterType({ value, filterGroup } = {}) {
if (filterGroup === undefined) {
this.$emit('update:filterType', value)
return
}
const newFilterGroups = this.filterGroups.map((group) =>
group.id === filterGroup.id ? { ...group, filter_type: value } : group
)
this.$emit('update:filterGroups', newFilterGroups)
},
/*
* Responsible for removing the chosen filter from the data source's filters.
*/
deleteFilter(filter) {
const newFilters = this.modelValue.filter(({ id }) => {
return id !== filter.id
})
this.$emit('update:modelValue', newFilters)
},
/*
* Responsible for updating the chosen filter in the data source's filters.
*/
updateFilter({ filter, values }) {
const newFilters = this.modelValue.map((filterConf) => {
if (filterConf.id === filter.id) {
const mode = values.mode || filter.mode || 'raw'
const filterValues = { ...filterConf }
delete filterValues.value
delete filterValues.value_is_formula
delete filterValues.mode
const updatedValues = { ...values }
delete updatedValues.value
delete updatedValues.value_is_formula
delete updatedValues.mode
return {
...filterValues,
...updatedValues,
value: {
formula: values.value,
mode,
},
}
}
return filterConf
})
this.$emit('update:modelValue', newFilters)
},
/*
* Responsible for bypassing the `ViewFieldConditionsForm` component's
* `updateFilter` method behaviour if the field is a formula. By default,
* when a filter is updated, `filterType.prepareValue` is called. This is
* problematic because when formulas are introduced to filters, we don't
* want any additional processing to happen when the value is reset. For example,
* when a filter on a date is added, `LocalizedDateViewFilterType.prepareValue`
* will always add the timezone and `DATE_FILTER_TIMEZONE_VALUE_SEPARATOR`.
*/
prepareValue(value, filter, field, filterType) {
// When a filter is not editable (e.g. empty/not empty), ordinarily the
// value is reset to a blank string by prepareValue. As we want to skip
// this function, we have to reset manually here.
return filterType.hasEditableValue ? value : ''
},
/**
* Baserow formula (i.e. non-database formulas) are objects, and within
* them is the actual formula string. This method is responsible for
* returning that formula string so that it can be passed to the
* `InjectedFormulaInput` component.
* @param {Object} filter - The filter object.
* @returns {Object} The formula object with the formula string.
*/
getFormulaObject(filter) {
return {
formula: filter.value,
mode: filter.mode || 'raw',
}
},
normalizeFormulaObject(filter) {
return {
...filter.value,
mode: filter.value_is_formula === false ? 'raw' : filter.value.mode,
}
},
},
}
</script>