fix: replace filter implementations on /insights (#19033)

* fix: replace filter implementations on /insights

* add authorization for teamIds

* replace all the implementations

* provide eventTypeId

* remove log

* fix teamId for isAll

* fix Download button

* remove legacy implementations

* clean up

* nullish check

* remove unusable filters

* revert style

* fix type error

* fix e2e test

* fix type error

* fix type error

* fix e2e

* extract util method

* fix type error

* fix type error

* fix type error

---------

Co-authored-by: Benny Joo <[email protected]>
This commit is contained in:
Eunjae LeeandBenny Joo authored and GitHub committed 2025-02-10 20:28:48 +01:00
1 parent 9bf3732717
commit 720136fcf9
55 files changed
+1037 -1829

No files matched your search

@@ -63,7 +63,8 @@ function AddFilterButtonComponent<TData>(
<CommandItem
key={column.id}
onSelect={() => handleAddFilter(column.id)}
className="px-4 py-2">
className="px-4 py-2"
data-testid={`add-filter-item-${column.id}`}>
{startCase(column.title)}
</CommandItem>
);
@@ -13,6 +13,7 @@ export const ClearFiltersButton = ({ exclude }: { exclude?: string[] }) => {
return (
<Tooltip content={t("clear_filters")}>
<Button
data-testid="clear-filters-button"
variant="icon"
color="secondary"
target="_blank"
@@ -18,37 +18,22 @@ import {
} from "@calcom/ui";
import { useDataTable, useFilterValue } from "../../hooks";
import {
CUSTOM_PRESET,
CUSTOM_PRESET_VALUE,
DEFAULT_PRESET,
PRESET_OPTIONS,
getDefaultStartDate,
getDefaultEndDate,
type PresetOption,
} from "../../lib/dateRange";
import type { FilterableColumn } from "../../lib/types";
import { ZDateRangeFilterValue, ColumnFilterType } from "../../lib/types";
type PresetOption = {
labelKey: string;
i18nOptions?: Record<string, string | number>;
value: string;
};
type DateRangeFilterProps = {
column: Extract<FilterableColumn, { type: ColumnFilterType.DATE_RANGE }>;
};
const CUSTOM_PRESET_VALUE = "c" as const;
const DEFAULT_PRESET: PresetOption = {
labelKey: "last_number_of_days",
i18nOptions: { count: 7 },
value: "w",
};
const CUSTOM_PRESET: PresetOption = { labelKey: "custom_range", value: CUSTOM_PRESET_VALUE };
const PRESET_OPTIONS: PresetOption[] = [
{ labelKey: "today", value: "tdy" },
DEFAULT_PRESET,
{ labelKey: "last_number_of_days", i18nOptions: { count: 30 }, value: "t" },
{ labelKey: "month_to_date", value: "m" },
{ labelKey: "year_to_date", value: "y" },
CUSTOM_PRESET,
];
const getDateRangeFromPreset = (val: string | null) => {
let startDate;
let endDate;
@@ -87,10 +72,6 @@ const getDateRangeFromPreset = (val: string | null) => {
return { startDate, endDate, preset };
};
const getDefaultStartDate = () => dayjs().subtract(1, "week").startOf("day");
const getDefaultEndDate = () => dayjs().endOf("day");
export const DateRangeFilter = ({ column }: DateRangeFilterProps) => {
const filterValue = useFilterValue(column.id, ZDateRangeFilterValue);
const { updateFilter } = useDataTable();
@@ -134,7 +134,10 @@ export function FilterPopover({ column }: FilterPopoverProps) {
return (
<Popover>
<PopoverTrigger asChild>
<Button color="secondary" className="items-center">
<Button
color="secondary"
className="items-center"
data-testid={`filter-popover-trigger-${column.id}`}>
<Icon name={icon} className="mr-2 h-4 w-4" />
<span>{startCase(column.title)}</span>
<AppliedFilterValue column={column} filterValue={filterValue} />
@@ -28,7 +28,7 @@ export function MultiSelectFilterOptions({ column }: MultiSelectFilterOptionsPro
const { updateFilter, removeFilter } = useDataTable();
return (
<Command>
<Command data-testid={`multi-select-options-${column.id}`}>
<CommandInput placeholder={t("search")} />
<CommandList>
<CommandEmpty>{t("no_options_found")}</CommandEmpty>
@@ -28,7 +28,7 @@ export function SingleSelectFilterOptions({ column }: SingleSelectFilterOptionsP
const { updateFilter, removeFilter } = useDataTable();
return (
<Command>
<Command data-testid={`single-select-options-${column.id}`}>
<CommandInput placeholder={t("search")} />
<CommandList>
<CommandEmpty>{t("no_options_found")}</CommandEmpty>
@@ -4,12 +4,14 @@ import { type Table } from "@tanstack/react-table";
// eslint-disable-next-line no-restricted-imports
import { useMemo } from "react";
import type { FilterableColumn } from "../lib/types";
import type { FilterableColumn, FacetedValue } from "../lib/types";
import { ColumnFilterType } from "../lib/types";
import { convertMapToFacetedValues } from "../lib/utils";
export function useFilterableColumns<TData>(table: Table<TData>) {
const columns = useMemo(
() => table.getAllColumns().filter((column) => column.getCanFilter()),
[table.getAllColumns()]
);
@@ -25,20 +27,18 @@ export function useFilterableColumns<TData>(table: Table<TData>) {
type,
};
if (type === ColumnFilterType.MULTI_SELECT || type === ColumnFilterType.SINGLE_SELECT) {
const values = column.getFacetedUniqueValues();
const options = Array.from(values.keys()).map((option) => {
if (typeof option === "string") {
return {
label: option,
value: option,
};
} else {
return {
label: option.label as string,
value: option.value as string | number,
};
}
});
// `column.getFacetedUniqueValues` gets out of sync
// when we pass a new `getFacetedUniqueValues` to
// `useReactTable({ ... })`.
//
// So we use `table.options.getFacetedUniqueValues` instead.
let values: Map<FacetedValue, number> | (() => Map<FacetedValue, number>) | undefined =
table.options?.getFacetedUniqueValues?.(table, column.id);
if (typeof values === "function") {
values = values();
}
const options = convertMapToFacetedValues(values);
return {
...base,
options,
@@ -50,7 +50,10 @@ export function useFilterableColumns<TData>(table: Table<TData>) {
}
})
.filter((column): column is FilterableColumn => Boolean(column)),
[columns]
// re-calculate this when the `getFacetedUniqueValues` changes
// eslint-disable-next-line react-hooks/exhaustive-deps
[columns, table.options.getFacetedUniqueValues]
);
return filterableColumns;
@@ -0,0 +1,31 @@
import dayjs from "@calcom/dayjs";
export type PresetOptionValue = "c" | "w" | "m" | "y" | "t" | "tdy";
export type PresetOption = {
labelKey: string;
i18nOptions?: Record<string, string | number>;
value: PresetOptionValue;
};
export const CUSTOM_PRESET_VALUE = "c" as const;
export const DEFAULT_PRESET: PresetOption = {
labelKey: "last_number_of_days",
i18nOptions: { count: 7 },
value: "w",
};
export const CUSTOM_PRESET: PresetOption = { labelKey: "custom_range", value: CUSTOM_PRESET_VALUE };
export const PRESET_OPTIONS: PresetOption[] = [
{ labelKey: "today", value: "tdy" },
DEFAULT_PRESET,
{ labelKey: "last_number_of_days", i18nOptions: { count: 30 }, value: "t" },
{ labelKey: "month_to_date", value: "m" },
{ labelKey: "year_to_date", value: "y" },
CUSTOM_PRESET,
];
export const getDefaultStartDate = () => dayjs().subtract(1, "week").startOf("day");
export const getDefaultEndDate = () => dayjs().endOf("day");
@@ -148,3 +148,8 @@ export const ZSorting = z.object({
export type SortingState = Array<z.infer<typeof ZSorting>>;
export const ZColumnVisibility = z.record(z.string(), z.boolean());
export type FacetedValue = {
label: string;
value: string | number;
};
+19
View File
@@ -7,6 +7,7 @@ import type {
FilterValue,
NumberFilterValue,
DateRangeFilterValue,
FacetedValue,
} from "./types";
import {
ZNumberFilterValue,
@@ -147,3 +148,21 @@ export const dataTableFilter = (cellValue: unknown, filterValue: FilterValue) =>
}
return false;
};
export const convertFacetedValuesToMap = (array: FacetedValue[]) => {
return new Map<FacetedValue, number>(
array.map((option) => [{ label: option.label, value: option.value }, 1])
);
};
export const convertMapToFacetedValues = (map: Map<FacetedValue, number> | undefined) => {
if (!map || !(map instanceof Map)) {
return [];
}
return Array.from(map.keys()).map((option) => {
if (typeof option === "string") {
return { label: option, value: option };
}
return { label: option.label as string, value: option.value as string | number };
});
};