feat: add text & numeric filters to insights routing responses (#18016)

* 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

* feat: support DataTable filters for Insights Routing WIP

* remove unused filters

* remove additionalFilters and fix types

* clean up filter components

* support icons for ActiveFilters

* support filters on json

* fix filter ui

* fix type error and clean up

* revert changes

* revert change

* clean up

* revert change

* fix compatibility with insights booking page

* remove unused params

* fix type errors

* update yarn.lock

* fix field filter and adjust ui

* chore: update yarn.lock

* fix text filter

* add Clear Filters button

* add more test data

* feat: support numeric filter WIP

* feat: support numeric filter

* feat: move People to an external filter

* fixing query states

* rename state to activeFilters

* add useDataTable

* fix ResponseCellValue bug and enable resizing for RoutingFormResponsesTable

* fix response values

* truncate attribute text

* seed booking attendees

* fix accessor (no actual change though)

* add a gap

* rename

* use pathname as default identifier for DataTable

* fix type error

* support text filter for assignment reason

* Apply suggestions from code review

Co-authored-by: Alex van Andel <[email protected]>

* rename fields to headers

* use safeParse

* fix type error

* push yarn.lock with faker

* fix MemberList

---------

Co-authored-by: Udit Takkar <[email protected]>
Co-authored-by: Alex van Andel <[email protected]>
Co-authored-by: sean-brydon <[email protected]>
This commit is contained in:
authored and GitHub committed 2024-12-12 16:06:30 +00:00
1 parent e366c10521
commit a7e7561efa
36 files changed
+1162 -482

No files matched your search

@@ -7,6 +7,7 @@ import { useQueryState, parseAsBoolean } from "nuqs";
import { useMemo, useReducer, useRef, useState } from "react";
import {
DataTableProvider,
DataTable,
DataTableToolbar,
DataTableFilters,
@@ -16,6 +17,8 @@ import {
useFetchMoreOnBottomReached,
textFilter,
isTextFilterValue,
isSelectFilterValue,
selectFilter,
} from "@calcom/features/data-table";
import { useOrgBranding } from "@calcom/features/ee/organizations/context/provider";
import classNames from "@calcom/lib/classNames";
@@ -97,6 +100,14 @@ function reducer(state: UserTableState, action: UserTableAction): UserTableState
}
export function UserListTable() {
return (
<DataTableProvider>
<UserListTableContent />
</DataTableProvider>
);
}
function UserListTableContent() {
const [dynamicLinkVisible, setDynamicLinkVisible] = useQueryState("dynamicLink", parseAsBoolean);
const orgBranding = useOrgBranding();
const domain = orgBranding?.fullDomain ?? WEBAPP_URL;
@@ -164,62 +175,75 @@ export function UserListTable() {
return [];
}
return (
(attributes?.map((attribute) => ({
id: attribute.id,
header: attribute.name,
meta: {
filterType: attribute.type.toLowerCase() === "text" ? "text" : "select",
},
size: 120,
accessorFn: (data) => data.attributes.find((attr) => attr.attributeId === attribute.id)?.value,
cell: ({ row }) => {
const attributeValues = row.original.attributes.filter(
(attr) => attr.attributeId === attribute.id
);
if (attributeValues.length === 0) return null;
return (
<div
className={classNames(
attribute.type === "NUMBER" ? "flex w-full justify-center" : "flex flex-wrap"
)}>
{attributeValues.map((attributeValue, index) => {
const isAGroupOption = attributeValue.contains?.length > 0;
const suffix = attribute.isWeightsEnabled ? `${attributeValue.weight || 100}%` : undefined;
return (
<div className="mr-1 inline-flex shrink-0" key={attributeValue.id}>
<Badge
variant={isAGroupOption ? "orange" : "gray"}
className={classNames(suffix && "rounded-r-none")}>
{attributeValue.value}
</Badge>
(attributes?.map((attribute) => {
// TODO: We need to normalize AttributeOption table first
// so that we can have `number_value` column for numeric operations.
// Currently, `value` column is used for both text and number attributes.
//
// const isNumber = attribute.type === "NUMBER";
const isNumber = false;
const isText = attribute.type === "TEXT";
const filterType = isNumber ? "number" : isText ? "text" : "select";
{suffix ? (
return {
id: attribute.id,
header: attribute.name,
meta: {
filter: { type: filterType },
},
size: 120,
accessorFn: (data) => data.attributes.find((attr) => attr.attributeId === attribute.id)?.value,
cell: ({ row }) => {
const attributeValues = row.original.attributes.filter(
(attr) => attr.attributeId === attribute.id
);
if (attributeValues.length === 0) return null;
return (
<div className={classNames(isNumber ? "flex w-full justify-center" : "flex flex-wrap")}>
{attributeValues.map((attributeValue) => {
const isAGroupOption = attributeValue.contains?.length > 0;
const suffix = attribute.isWeightsEnabled
? `${attributeValue.weight || 100}%`
: undefined;
return (
<div className="mr-1 inline-flex shrink-0" key={attributeValue.id}>
<Badge
variant={isAGroupOption ? "orange" : "gray"}
style={{
backgroundColor: "color-mix(in hsl, var(--cal-bg-emphasis), black 5%)",
}}
className="rounded-l-none">
{suffix}
className={classNames(suffix && "rounded-r-none")}>
{attributeValue.value}
</Badge>
) : null}
</div>
);
})}
</div>
);
},
filterFn: (row, id, filterValue) => {
const attributeValues = row.original.attributes.filter((attr) => attr.attributeId === id);
if (isTextFilterValue(filterValue)) {
return attributeValues.some((attr) => textFilter(attr.value, filterValue));
}
{suffix ? (
<Badge
variant={isAGroupOption ? "orange" : "gray"}
style={{
backgroundColor: "color-mix(in hsl, var(--cal-bg-emphasis), black 5%)",
}}
className="rounded-l-none">
{suffix}
</Badge>
) : null}
</div>
);
})}
</div>
);
},
filterFn: (row, id, filterValue) => {
const attributeValues = row.original.attributes.filter((attr) => attr.attributeId === id);
if (attributeValues.length === 0) return false;
return attributeValues.some((attr) => filterValue.includes(attr.value));
},
})) as ColumnDef<UserTableUser>[]) ?? []
if (isTextFilterValue(filterValue)) {
return attributeValues.some((attr) => textFilter(attr.value, filterValue));
} else if (isSelectFilterValue(filterValue)) {
return selectFilter(
attributeValues.map((attr) => attr.value),
filterValue
);
}
return false;
},
};
}) as ColumnDef<UserTableUser>[]) ?? []
);
};
const cols: ColumnDef<UserTableUser>[] = [
@@ -512,7 +536,7 @@ export function UserListTable() {
table={table}
tableContainerRef={tableContainerRef}
isPending={isPending}
enableColumnResizing={{ name: "UserListTable" }}
enableColumnResizing={true}
onScroll={(e) => fetchMoreOnBottomReached(e.target as HTMLDivElement)}>
<DataTableToolbar.Root className="lg:max-w-screen-2xl">
<div className="flex w-full flex-col gap-2 sm:flex-row">
@@ -534,7 +558,7 @@ export function UserListTable() {
{t("download")}
</DataTableToolbar.CTA>
{/* We have to omit member because we don't want the filter to show but we can't disable filtering as we need that for the search bar */}
<DataTableFilters.FilterButton table={table} omit={["member"]} />
<DataTableFilters.AddFilterButton table={table} omit={["member"]} />
<DataTableFilters.ColumnVisibilityButton table={table} />
{adminOrOwner && (
<DataTableToolbar.CTA