index.community/frontend/src/searchFilters.ts

29 lines
891 B
TypeScript
Raw Permalink Normal View History

2020-05-19 13:45:27 +00:00
type SearchFilterRelation = "eq" | "gt" | "gte" | "lt" | "lte";
export interface SearchFilter {
2019-08-04 11:39:29 +00:00
// The ES field to filter on
field: string;
2020-05-19 13:45:27 +00:00
relation: SearchFilterRelation;
2019-08-04 11:39:29 +00:00
// The value we want to filter to
value: string;
// Human-meaningful text that we're showing in the UI
displayValue: string;
}
// Maps to translate this to user-friendly text
2020-05-19 13:45:27 +00:00
type SearchFilterField = "type" | "user_count";
2019-08-04 11:39:29 +00:00
const searchFilterFieldTranslations = {
type: "Instance type",
2020-05-19 13:45:27 +00:00
// eslint-disable-next-line @typescript-eslint/camelcase
user_count: "User count",
2019-08-04 11:39:29 +00:00
};
const searchFilterRelationTranslations = {
eq: "=",
gt: ">",
gte: ">=",
lt: "<",
2020-05-19 13:45:27 +00:00
lte: "<=",
2019-08-04 11:39:29 +00:00
};
2020-05-19 13:45:27 +00:00
export const getSearchFilterDisplayValue = (field: SearchFilterField, relation: SearchFilterRelation, value: string) =>
2019-08-04 11:39:29 +00:00
`${searchFilterFieldTranslations[field]} ${searchFilterRelationTranslations[relation]} ${value}`;