feat: add text filter on org member list (#17632)
* feat: improve text filters (WIP) * move function to bottom * apply some styles * fix selection of TextFilterOptions * rename value to operand * remove unused file * merge filters/filters into filters/utils * fix regression of not putting url params correctly * move makeWhereClause to filters/utils * fix negative, empty, and not empty operators * fix initial filtering from search state (url) * fix type errors * do not send an empty array to query * update yarn.lock * i18n for text filter operators * extract logic as useColumnFilters() * add missing import * fix type error * revert yarn.lock * use i18n * insensitive text match * move data-table to @calcom/features * fix type errors * fix type errors * fix type errors --------- Co-authored-by: Udit Takkar <[email protected]>
This commit is contained in:
1 parent
ff18b9a733
commit
1404581f78
28 files changed
+520
-309
No files matched your search
@@ -0,0 +1,74 @@
|
||||
import type { FilterValue } from "./types";
|
||||
import { isSelectFilterValue, isTextFilterValue } from "./utils";
|
||||
|
||||
export function makeWhereClause(columnName: string, filterValue: FilterValue) {
|
||||
if (isSelectFilterValue(filterValue)) {
|
||||
return {
|
||||
[columnName]: {
|
||||
in: filterValue,
|
||||
},
|
||||
};
|
||||
} else if (isTextFilterValue(filterValue)) {
|
||||
const { operator, operand } = filterValue.data;
|
||||
|
||||
switch (operator) {
|
||||
case "equals":
|
||||
return {
|
||||
[columnName]: {
|
||||
equals: operand,
|
||||
},
|
||||
};
|
||||
case "notEquals":
|
||||
return {
|
||||
[columnName]: {
|
||||
not: operand,
|
||||
},
|
||||
};
|
||||
case "contains":
|
||||
return {
|
||||
[columnName]: {
|
||||
contains: operand,
|
||||
mode: "insensitive",
|
||||
},
|
||||
};
|
||||
case "notContains":
|
||||
return {
|
||||
NOT: {
|
||||
[columnName]: {
|
||||
contains: operand,
|
||||
mode: "insensitive",
|
||||
},
|
||||
},
|
||||
};
|
||||
case "startsWith":
|
||||
return {
|
||||
[columnName]: {
|
||||
startsWith: operand,
|
||||
mode: "insensitive",
|
||||
},
|
||||
};
|
||||
case "endsWith":
|
||||
return {
|
||||
[columnName]: {
|
||||
endsWith: operand,
|
||||
mode: "insensitive",
|
||||
},
|
||||
};
|
||||
case "isEmpty":
|
||||
return {
|
||||
[columnName]: {
|
||||
equals: "",
|
||||
},
|
||||
};
|
||||
case "isNotEmpty":
|
||||
return {
|
||||
NOT: {
|
||||
[columnName]: {
|
||||
equals: "",
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const ZTextFilterOperator = z.enum([
|
||||
"equals",
|
||||
"notEquals",
|
||||
"contains",
|
||||
"notContains",
|
||||
"startsWith",
|
||||
"endsWith",
|
||||
"isEmpty",
|
||||
"isNotEmpty",
|
||||
]);
|
||||
|
||||
export type TextFilterOperator = z.infer<typeof ZTextFilterOperator>;
|
||||
|
||||
export const ZSelectFilterValue = z.array(z.string());
|
||||
|
||||
export type SelectFilterValue = z.infer<typeof ZSelectFilterValue>;
|
||||
|
||||
export const ZTextFilterValue = z.object({
|
||||
type: z.literal("text"),
|
||||
data: z.object({
|
||||
operator: ZTextFilterOperator,
|
||||
operand: z.string(),
|
||||
}),
|
||||
});
|
||||
|
||||
export type TextFilterValue = z.infer<typeof ZTextFilterValue>;
|
||||
|
||||
export const ZFilterValue = z.union([ZSelectFilterValue, ZTextFilterValue]);
|
||||
|
||||
export type FilterValue = z.infer<typeof ZFilterValue>;
|
||||
|
||||
export type FilterableColumn = {
|
||||
id: string;
|
||||
title: string;
|
||||
filterType: "text" | "select";
|
||||
options: Map<string, number>;
|
||||
};
|
||||
@@ -0,0 +1,82 @@
|
||||
"use client";
|
||||
|
||||
import { parseAsArrayOf, parseAsJson, useQueryStates } from "nuqs";
|
||||
import { useMemo } from "react";
|
||||
import { z } from "zod";
|
||||
|
||||
import type { SelectFilterValue, TextFilterValue } from "./types";
|
||||
import { ZSelectFilterValue, ZTextFilterValue } from "./types";
|
||||
|
||||
const filterSchema = z.object({
|
||||
f: z.string(),
|
||||
v: z.union([ZSelectFilterValue, ZTextFilterValue]),
|
||||
});
|
||||
|
||||
export const filtersSearchParams = {
|
||||
activeFilters: parseAsArrayOf(parseAsJson(filterSchema.parse)).withDefault([]),
|
||||
};
|
||||
|
||||
export function useFiltersSearchState() {
|
||||
return useQueryStates(filtersSearchParams);
|
||||
}
|
||||
|
||||
export function useColumnFilters() {
|
||||
const [filtersSearchState] = useFiltersSearchState();
|
||||
const columnFilters = useMemo(() => {
|
||||
return (filtersSearchState.activeFilters || [])
|
||||
.map((filter) => ({
|
||||
id: filter.f,
|
||||
value: filter.v,
|
||||
}))
|
||||
.filter((filter) => {
|
||||
// The empty arrays in `filtersSearchState` keep the filter UI component,
|
||||
// but we do not send them to the actual query.
|
||||
// Otherwise, { value: [] } would result in nothing being returned.
|
||||
if (Array.isArray(filter.value) && filter.value.length === 0) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}, [filtersSearchState.activeFilters]);
|
||||
return columnFilters;
|
||||
}
|
||||
|
||||
export type FiltersSearchState = ReturnType<typeof useFiltersSearchState>[0];
|
||||
export type SetFiltersSearchState = ReturnType<typeof useFiltersSearchState>[1];
|
||||
export type ActiveFilter = NonNullable<FiltersSearchState["activeFilters"]>[number];
|
||||
|
||||
export const textFilter = (cellValue: string, filterValue: TextFilterValue) => {
|
||||
switch (filterValue.data.operator) {
|
||||
case "equals":
|
||||
return cellValue.toLowerCase() === (filterValue.data.operand || "").toLowerCase();
|
||||
case "notEquals":
|
||||
return cellValue.toLowerCase() !== (filterValue.data.operand || "").toLowerCase();
|
||||
case "contains":
|
||||
return cellValue.toLowerCase().includes((filterValue.data.operand || "").toLowerCase());
|
||||
case "notContains":
|
||||
return !cellValue.toLowerCase().includes((filterValue.data.operand || "").toLowerCase());
|
||||
case "startsWith":
|
||||
return cellValue.toLowerCase().startsWith((filterValue.data.operand || "").toLowerCase());
|
||||
case "endsWith":
|
||||
return cellValue.toLowerCase().endsWith((filterValue.data.operand || "").toLowerCase());
|
||||
case "isEmpty":
|
||||
return cellValue.trim() === "";
|
||||
case "isNotEmpty":
|
||||
return cellValue.trim() !== "";
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
export const isTextFilterValue = (filterValue: unknown): filterValue is TextFilterValue => {
|
||||
return (
|
||||
typeof filterValue === "object" &&
|
||||
filterValue !== null &&
|
||||
"type" in filterValue &&
|
||||
filterValue.type === "text"
|
||||
);
|
||||
};
|
||||
|
||||
export const isSelectFilterValue = (filterValue: unknown): filterValue is SelectFilterValue => {
|
||||
return Array.isArray(filterValue) && filterValue.every((item) => typeof item === "string");
|
||||
};
|
||||
Reference in new issue
Block a user