feat: insights filter refactoring (#18167)

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

This reverts commit 49070b96ae.

* support SINGLE_SELECT

* fix type error

* put back ts-expect-error

* remove playground

---------

Co-authored-by: sean-brydon <[email protected]>
This commit is contained in:
Eunjae Leeandsean-brydon authored and GitHub committed 2024-12-16 13:47:28 +00:00
1 parent da2c57c302
commit 79d488faca
38 files changed
+1383 -512

No files matched your search

@@ -1,9 +1,8 @@
import type { Table } from "@tanstack/react-table";
import type { ColumnFiltersState } from "@tanstack/react-table";
import { createContext, useContext, useState, useMemo, type PropsWithChildren } from "react";
import type { Dispatch, SetStateAction } from "react";
import { DataTableSelectionBar } from "@calcom/features/data-table";
import { DataTableSelectionBar, type ColumnFilter } from "@calcom/features/data-table";
import classNames from "@calcom/lib/classNames";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import slugify from "@calcom/lib/slugify";
@@ -30,7 +29,7 @@ import type { UserTableUser } from "../types";
interface Props {
table: Table<UserTableUser>;
filters: ColumnFiltersState;
filters: ColumnFilter[];
}
type Attribute = _Attribute & { options: AttributeOption[] };
@@ -250,10 +249,7 @@ function MassAssignAttributesBulkActionComponent({ table, filters }: Props) {
limit: 10,
searchTerm: "",
expand: ["attributes"],
filters: filters.map((filter) => ({
id: filter.id,
value: filter.value as string[],
})),
filters,
},
// @ts-expect-error i really dont know how to type this
(oldData) => {
@@ -7,6 +7,7 @@ import { useQueryState, parseAsBoolean } from "nuqs";
import { useMemo, useReducer, useRef, useState } from "react";
import {
DataTableProvider,
DataTable,
DataTableToolbar,
DataTableFilters,
@@ -16,6 +17,10 @@ import {
useFetchMoreOnBottomReached,
textFilter,
isTextFilterValue,
isSingleSelectFilterValue,
isMultiSelectFilterValue,
singleSelectFilter,
multiSelectFilter,
} from "@calcom/features/data-table";
import { useOrgBranding } from "@calcom/features/ee/organizations/context/provider";
import classNames from "@calcom/lib/classNames";
@@ -97,6 +102,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 +177,88 @@ 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 isSingleSelect = attribute.type === "SINGLE_SELECT";
const isMultiSelect = attribute.type === "MULTI_SELECT";
const filterType = isNumber
? "number"
: isText
? "text"
: isSingleSelect
? "single_select"
: "multi_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 (isSingleSelectFilterValue(filterValue)) {
return singleSelectFilter(
attributeValues.map((attr) => attr.value),
filterValue
);
} else if (isMultiSelectFilterValue(filterValue)) {
return multiSelectFilter(
attributeValues.map((attr) => attr.value),
filterValue
);
}
return false;
},
};
}) as ColumnDef<UserTableUser>[]) ?? []
);
};
const cols: ColumnDef<UserTableUser>[] = [
@@ -512,9 +551,9 @@ 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">
<DataTableToolbar.Root>
<div className="flex w-full flex-col gap-2 sm:flex-row">
<div className="w-full sm:w-auto sm:min-w-[200px] sm:flex-1">
<DataTableToolbar.SearchBar
@@ -534,7 +573,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