diff --git a/src/components/lists/DynamicList.tsx b/src/components/lists/DynamicList.tsx index c5c5162..38c3bb1 100644 --- a/src/components/lists/DynamicList.tsx +++ b/src/components/lists/DynamicList.tsx @@ -76,7 +76,7 @@ import { import { isPendingDelete, usePendingDeletes } from '@/lib/pendingDeletes'; import { friendlySetError } from '@/lib/jmapErrors'; import { coerceLabel } from '@/lib/objectOptions'; -import { buildJmapFilter } from '@/lib/listFilter'; +import { buildJmapFilter, staticFilterValue } from '@/lib/listFilter'; import { useResetOnChange } from '@/hooks/useBufferedValue'; import { useSchemaStore } from '@/stores/schemaStore'; @@ -973,14 +973,13 @@ export function DynamicList({ viewName }: DynamicListProps) { case 'enum': { const enumVariants = schema!.enums[filterDef.enumName] ?? []; + const fixed = staticFilterValue(resolved?.list?.filtersStatic, filterDef.field); + const allOption = fixed ? [] : [{ value: '__all__', label: t('filters.all', 'All') }]; if (enumVariants.length > ENUM_FILTER_COMBOBOX_THRESHOLD) { return wrapper( ({ value: v.name, label: v.label })), - ]} - value={value || '__all__'} + options={[...allOption, ...enumVariants.map((v) => ({ value: v.name, label: v.label }))]} + value={value || fixed || '__all__'} onValueChange={(v) => handleFilterSelectChange(filterDef.field, v)} searchPlaceholder={t('common.searchPlaceholder', 'Search...')} emptyText={t('field.noMatches', 'No matches')} @@ -988,12 +987,15 @@ export function DynamicList({ viewName }: DynamicListProps) { ); } return wrapper( - handleFilterSelectChange(filterDef.field, v)} + > - {t('filters.all', 'All')} + {!fixed && {t('filters.all', 'All')}} {enumVariants.map((v) => ( {v.label} diff --git a/src/lib/listFilter.test.ts b/src/lib/listFilter.test.ts index b0ed6e3..1d58770 100644 --- a/src/lib/listFilter.test.ts +++ b/src/lib/listFilter.test.ts @@ -7,7 +7,7 @@ import { describe, expect, it } from 'vitest'; import type { Filter } from '@/types/schema'; -import { buildJmapFilter } from './listFilter'; +import { buildJmapFilter, staticFilterValue } from './listFilter'; const dateFilter = [{ field: 'created', type: 'date' }] as Filter[]; const x = (appliedFilters: Record) => @@ -84,3 +84,19 @@ describe('buildJmapFilter', () => { }); }); }); + +describe('staticFilterValue', () => { + it('gives the string a static filter sets, and nothing otherwise', () => { + const statics = { event: 'delivery.attempt-start', kind: 'remote', limit: 5 }; + expect(staticFilterValue(statics, 'kind')).toBe('remote'); + expect(staticFilterValue(statics, 'limit')).toBeUndefined(); + expect(staticFilterValue(statics, 'text')).toBeUndefined(); + expect(staticFilterValue(undefined, 'kind')).toBeUndefined(); + }); + + it('lets the reader override it in the query', () => { + expect( + buildJmapFilter({ appliedFilters: { kind: 'any' }, filtersStatic: { kind: 'remote' }, isXPrefixed: true }), + ).toEqual({ kind: 'any' }); + }); +}); diff --git a/src/lib/listFilter.ts b/src/lib/listFilter.ts index 2d93eca..0698cdc 100644 --- a/src/lib/listFilter.ts +++ b/src/lib/listFilter.ts @@ -72,3 +72,13 @@ export function buildJmapFilter({ } return out; } + +/** + * The value a list's static filters already give a field, when it's a plain + * string (delivery history's `kind`). An enum filter on that field shows it as + * the default and offers no "All", since clearing the filter brings it back. + */ +export function staticFilterValue(filtersStatic: Record | undefined, field: string): string | undefined { + const value = filtersStatic?.[field]; + return typeof value === 'string' ? value : undefined; +}