Skip to content

Commit 4a43d34

Browse files
authored
Improved operators and defaults for filters (#26890)
Closes https://linear.app/ghost/issue/BER-3452/use-sensible-operators-and-defaults-for-each-filter-type Makes sure we map to Ember for operators and improves Name and Email filters to use contains instead of is as a default for fuzzy searching.
1 parent db035ec commit 4a43d34

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

apps/posts/src/views/members/member-fields.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ describe('memberFields', () => {
5353
expect(memberFields.email_count.operators).toEqual([
5454
'is',
5555
'is-greater',
56-
'is-or-greater',
57-
'is-less',
58-
'is-or-less'
56+
'is-less'
5957
]);
6058
expect(memberFields.created_at.operators).toEqual([
6159
'is-less',

apps/posts/src/views/members/member-fields.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function getDayBoundsInUtc(date: string, timezone: string): {start: string; end:
1313

1414
const TEXT_OPERATORS = ['is', 'contains', 'does-not-contain', 'starts-with', 'ends-with'] as const;
1515
const DATE_OPERATORS = ['is-less', 'is-or-less', 'is-greater', 'is-or-greater'] as const;
16-
const NUMBER_OPERATORS = ['is', 'is-greater', 'is-or-greater', 'is-less', 'is-or-less'] as const;
16+
const NUMBER_OPERATORS = ['is', 'is-greater', 'is-less'] as const;
1717
const SCALAR_OPERATORS = ['is', 'is-not'] as const;
1818
const SET_OPERATORS = ['is-any', 'is-not-any'] as const;
1919
const SUBSCRIPTION_STATUS_OPTIONS: Array<{value: string; label: string}> = [
@@ -177,7 +177,7 @@ export const memberFields = defineFields({
177177
label: 'Name',
178178
type: 'text',
179179
placeholder: 'Enter name...',
180-
defaultOperator: 'is',
180+
defaultOperator: 'contains',
181181
className: 'w-48'
182182
},
183183
codec: textCodec()
@@ -188,7 +188,7 @@ export const memberFields = defineFields({
188188
label: 'Email',
189189
type: 'text',
190190
placeholder: 'Enter email...',
191-
defaultOperator: 'is',
191+
defaultOperator: 'contains',
192192
className: 'w-64'
193193
},
194194
codec: textCodec()

apps/posts/src/views/members/use-member-filter-fields.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ function createOperatorOptions(
4949
}));
5050
}
5151

52-
const MEMBER_OPERATOR_LABELS = {
52+
const MEMBER_OPERATOR_LABELS: Record<string, string> = {
53+
'is-any': 'is any of',
5354
'is-not-any': 'is none of',
5455
'does-not-contain': 'does not contain',
5556
'is-less': 'before',
@@ -60,6 +61,11 @@ const MEMBER_OPERATOR_LABELS = {
6061
0: 'Less like this'
6162
};
6263

64+
const NUMBER_OPERATOR_LABELS: Record<string, string> = {
65+
'is-greater': 'is greater than',
66+
'is-less': 'is less than'
67+
};
68+
6369
function getFieldIcon(key: string) {
6470
switch (key) {
6571
case 'name':
@@ -113,7 +119,8 @@ function getFieldIcon(key: string) {
113119

114120
function createFieldConfig(
115121
key: string,
116-
overrides: Partial<FilterFieldConfig> = {}
122+
overrides: Partial<FilterFieldConfig> = {},
123+
operatorLabels: Record<string, string> = MEMBER_OPERATOR_LABELS
117124
): FilterFieldConfig {
118125
const field = key.startsWith('newsletters.')
119126
? memberFields['newsletters.:slug']
@@ -123,7 +130,7 @@ function createFieldConfig(
123130
key,
124131
...field.ui,
125132
icon: getFieldIcon(key),
126-
operators: createOperatorOptions(field.operators, {labels: MEMBER_OPERATOR_LABELS}),
133+
operators: createOperatorOptions(field.operators, {labels: operatorLabels}),
127134
...('options' in field && field.options ? {options: field.options} : {}),
128135
...overrides
129136
};
@@ -425,12 +432,12 @@ export function useMemberFilterFields({
425432

426433
if (emailAnalyticsEnabled) {
427434
const emailFields: FilterFieldConfig[] = [
428-
createFieldConfig('email_count'),
429-
createFieldConfig('email_opened_count')
435+
createFieldConfig('email_count', {}, NUMBER_OPERATOR_LABELS),
436+
createFieldConfig('email_opened_count', {}, NUMBER_OPERATOR_LABELS)
430437
];
431438

432439
if (emailTrackOpens) {
433-
emailFields.push(createFieldConfig('email_open_rate'));
440+
emailFields.push(createFieldConfig('email_open_rate', {}, NUMBER_OPERATOR_LABELS));
434441
}
435442

436443
emailFields.push(createFieldConfig('emails.post_id', createSearchableFieldOverrides(

0 commit comments

Comments
 (0)