fix: replace filter implementations on bookings (#19445)

* fix: replace filter implementation on /bookings

* add eventTypeId column

* add eventType filter

* add team filter

* add people filter

* fix type error

* support date range filter

* fix empty view

* fix date range filter

* add comment

* fix type error

* fix type error

* fix type error

* fix type error

* fix type error

* address feedback

* remove reverted functionality
This commit is contained in:
Eunjae Lee authored and GitHub committed 2025-02-24 17:11:40 +01:00
1 parent 2d372746d3
commit 641193bd61
16 files changed
+478 -147

No files matched your search

@@ -265,7 +265,6 @@ function DataTableBody<TData>({
className={classNames(onRowMouseclick && "hover:cursor-pointer", "group")}>
{row.getVisibleCells().map((cell) => {
const column = cell.column;
const meta = column?.columnDef.meta;
return (
<TableCell
key={cell.id}
@@ -5,12 +5,12 @@ import { useEffect, useRef } from "react";
import {
DataTable,
DataTableToolbar,
DataTablePagination,
useFetchMoreOnBottomReached,
useDataTable,
useColumnFilters,
} from "@calcom/features/data-table";
import { classNames } from "@calcom/lib";
export type DataTableWrapperProps<TData, TValue> = {
testId?: string;
@@ -25,6 +25,7 @@ export type DataTableWrapperProps<TData, TValue> = {
totalDBRowCount?: number;
ToolbarLeft?: React.ReactNode;
ToolbarRight?: React.ReactNode;
EmptyView?: React.ReactNode;
className?: string;
containerClassName?: string;
children?: React.ReactNode;
@@ -44,6 +45,7 @@ export function DataTableWrapper<TData, TValue>({
hideHeader,
ToolbarLeft,
ToolbarRight,
EmptyView,
className,
containerClassName,
children,
@@ -79,21 +81,12 @@ export function DataTableWrapper<TData, TValue>({
}));
}, [table, sorting, columnFilters, columnVisibility]);
const showEmptyView = table.getRowCount() === 0 && EmptyView;
return (
<DataTable
testId={testId}
bodyTestId={bodyTestId}
table={table}
tableContainerRef={tableContainerRef}
isPending={isPending}
enableColumnResizing={true}
hideHeader={hideHeader}
variant={variant}
className={className}
containerClassName={containerClassName}
onScroll={(e) => fetchMoreOnBottomReached(e.target as HTMLDivElement)}>
<>
{(ToolbarLeft || ToolbarRight || children) && (
<DataTableToolbar.Root>
<div className={classNames("grid w-full items-center gap-2 py-4", className)}>
<div className="flex w-full flex-col gap-2">
<div className="flex w-full flex-wrap justify-between gap-2">
<div className="flex flex-wrap items-center gap-2">{ToolbarLeft}</div>
@@ -102,14 +95,29 @@ export function DataTableWrapper<TData, TValue>({
</div>
{children}
</DataTableToolbar.Root>
)}
{totalDBRowCount && (
<div style={{ gridArea: "footer", marginTop: "1rem" }}>
<DataTablePagination table={table} totalDbDataCount={totalDBRowCount} />
</div>
)}
</DataTable>
{showEmptyView && EmptyView}
{!showEmptyView && (
<DataTable
testId={testId}
bodyTestId={bodyTestId}
table={table}
tableContainerRef={tableContainerRef}
isPending={isPending}
enableColumnResizing={true}
hideHeader={hideHeader}
variant={variant}
className={className}
containerClassName={containerClassName}
onScroll={(e) => fetchMoreOnBottomReached(e.target as HTMLDivElement)}>
{totalDBRowCount && (
<div style={{ gridArea: "footer", marginTop: "1rem" }}>
<DataTablePagination table={table} totalDbDataCount={totalDBRowCount} />
</div>
)}
</DataTable>
)}
</>
);
}
@@ -5,6 +5,8 @@ import { type Table } from "@tanstack/react-table";
import { Fragment } from "react";
import { useDataTable, useFilterableColumns } from "../../hooks";
import { ColumnFilterType } from "../../lib/types";
import { DateRangeFilter } from "./DateRangeFilter";
import { FilterPopover } from "./FilterPopover";
// Add the new ActiveFilters component
@@ -21,7 +23,12 @@ export function ActiveFilters<TData>({ table }: ActiveFiltersProps<TData>) {
{activeFilters.map((filter) => {
const column = filterableColumns.find((col) => col.id === filter.f);
if (!column) return null;
return <FilterPopover key={column.id} column={column} />;
if (column.type === ColumnFilterType.DATE_RANGE) {
return <DateRangeFilter key={column.id} column={column} showClearButton />;
} else {
return <FilterPopover key={column.id} column={column} />;
}
})}
</>
);
@@ -62,7 +62,10 @@ function AddFilterButtonComponent<TData>(
<CommandList>
<CommandEmpty>{t("no_columns_found")}</CommandEmpty>
{filterableColumns.map((column) => {
const isVisible = table.getColumn(column.id)?.getIsVisible();
const showHiddenIndicator =
!table.getColumn(column.id)?.getIsVisible() &&
table.initialState.columnVisibility?.[column.id] !== false;
if (activeFilters?.some((filter) => filter.f === column.id)) return null;
return (
<CommandItem
@@ -71,7 +74,7 @@ function AddFilterButtonComponent<TData>(
className="flex items-center justify-between px-4 py-2"
data-testid={`add-filter-item-${column.id}`}>
<span>{startCase(column.title)}</span>
{!isVisible && <Icon name="eye-off" className="h-4 w-4 opacity-50" />}
{showHiddenIndicator && <Icon name="eye-off" className="h-4 w-4 opacity-50" />}
</CommandItem>
);
})}
@@ -15,6 +15,9 @@ import {
Command,
CommandList,
CommandItem,
CommandGroup,
CommandSeparator,
buttonClasses,
} from "@calcom/ui";
import { useDataTable, useFilterValue } from "../../hooks";
@@ -32,6 +35,7 @@ import { ZDateRangeFilterValue, ColumnFilterType } from "../../lib/types";
type DateRangeFilterProps = {
column: Extract<FilterableColumn, { type: ColumnFilterType.DATE_RANGE }>;
showClearButton?: boolean;
};
const getDateRangeFromPreset = (val: string | null) => {
@@ -72,9 +76,9 @@ const getDateRangeFromPreset = (val: string | null) => {
return { startDate, endDate, preset };
};
export const DateRangeFilter = ({ column }: DateRangeFilterProps) => {
export const DateRangeFilter = ({ column, showClearButton = false }: DateRangeFilterProps) => {
const filterValue = useFilterValue(column.id, ZDateRangeFilterValue);
const { updateFilter } = useDataTable();
const { updateFilter, removeFilter } = useDataTable();
const { t } = useLocale();
const currentDate = dayjs();
@@ -84,7 +88,11 @@ export const DateRangeFilter = ({ column }: DateRangeFilterProps) => {
const [endDate, setEndDate] = useState<Dayjs | undefined>(
filterValue?.data.endDate ? dayjs(filterValue.data.endDate) : getDefaultEndDate()
);
const [selectedPreset, setSelectedPreset] = useState<PresetOption>(DEFAULT_PRESET);
const [selectedPreset, setSelectedPreset] = useState<PresetOption>(
filterValue?.data.preset
? PRESET_OPTIONS.find((o) => o.value === filterValue.data.preset) ?? DEFAULT_PRESET
: DEFAULT_PRESET
);
const updateValues = ({
preset,
@@ -192,6 +200,23 @@ export const DateRangeFilter = ({ column }: DateRangeFilterProps) => {
</CommandItem>
))}
</CommandList>
{showClearButton && (
<>
<CommandSeparator />
<CommandGroup>
<CommandItem
onSelect={() => {
removeFilter(column.id);
}}
className={classNames(
"w-full justify-center text-center",
buttonClasses({ color: "secondary" })
)}>
{t("clear")}
</CommandItem>
</CommandGroup>
</>
)}
</Command>
</PopoverContent>
</Popover>
@@ -2,7 +2,6 @@
import type { FilterableColumn } from "../../lib/types";
import { ColumnFilterType } from "../../lib/types";
import { DateRangeFilter } from "./DateRangeFilter";
import { MultiSelectFilterOptions } from "./MultiSelectFilterOptions";
import { NumberFilterOptions } from "./NumberFilterOptions";
import { SingleSelectFilterOptions } from "./SingleSelectFilterOptions";
@@ -21,8 +20,6 @@ export function FilterOptions({ column }: FilterOptionsProps) {
return <SingleSelectFilterOptions column={column} />;
} else if (column.type === ColumnFilterType.NUMBER) {
return <NumberFilterOptions column={column} />;
} else if (column.type === ColumnFilterType.DATE_RANGE) {
return <DateRangeFilter column={column} />;
} else {
return null;
}
@@ -32,31 +32,42 @@ export function MultiSelectFilterOptions({ column }: MultiSelectFilterOptionsPro
<CommandInput placeholder={t("search")} />
<CommandList>
<CommandEmpty>{t("no_options_found")}</CommandEmpty>
{column.options.map((option) => {
{column.options.map((option, index) => {
if (!option) return null;
const { label: optionLabel, value: optionValue } =
typeof option === "string" ? { label: option, value: option } : option;
const {
label: optionLabel,
value: optionValue,
section,
} = typeof option === "string" ? { label: option, value: option, section: undefined } : option;
return (
<CommandItem
key={optionValue}
onSelect={() => {
const newFilterValue = filterValue?.data.includes(optionValue)
? filterValue?.data.filter((value) => value !== optionValue)
: [...(filterValue?.data || []), optionValue];
updateFilter(column.id, { type: ColumnFilterType.MULTI_SELECT, data: newFilterValue });
}}>
<div
className={classNames(
"border-subtle mr-2 flex h-4 w-4 items-center justify-center rounded-sm border",
filterValue?.data.includes(optionValue) ? "bg-primary" : "opacity-50"
)}>
{filterValue?.data.includes(optionValue) && (
<Icon name="check" className="text-primary-foreground h-4 w-4" />
)}
</div>
{optionLabel}
</CommandItem>
<>
{section && index !== 0 && <hr className="border-subtle my-1" />}
{section && (
<div className="text-subtle px-4 py-2 text-xs font-medium uppercase leading-none">
{section}
</div>
)}
<CommandItem
key={optionValue}
onSelect={() => {
const newFilterValue = filterValue?.data.includes(optionValue)
? filterValue?.data.filter((value) => value !== optionValue)
: [...(filterValue?.data || []), optionValue];
updateFilter(column.id, { type: ColumnFilterType.MULTI_SELECT, data: newFilterValue });
}}>
<div
className={classNames(
"border-subtle mr-2 flex h-4 w-4 items-center justify-center rounded-sm border",
filterValue?.data.includes(optionValue) ? "bg-primary" : "opacity-50"
)}>
{filterValue?.data.includes(optionValue) && (
<Icon name="check" className="text-primary-foreground h-4 w-4" />
)}
</div>
{optionLabel}
</CommandItem>
</>
);
})}
</CommandList>
@@ -32,28 +32,39 @@ export function SingleSelectFilterOptions({ column }: SingleSelectFilterOptionsP
<CommandInput placeholder={t("search")} />
<CommandList>
<CommandEmpty>{t("no_options_found")}</CommandEmpty>
{column.options.map((option) => {
{column.options.map((option, index) => {
if (!option) return null;
const { label: optionLabel, value: optionValue } =
typeof option === "string" ? { label: option, value: option } : option;
const {
label: optionLabel,
value: optionValue,
section,
} = typeof option === "string" ? { label: option, value: option, section: undefined } : option;
return (
<CommandItem
key={optionValue}
onSelect={() => {
updateFilter(column.id, { type: ColumnFilterType.SINGLE_SELECT, data: optionValue });
}}>
<div
className={classNames(
"border-subtle mr-2 flex h-4 w-4 items-center justify-center rounded-sm border",
filterValue?.data === optionValue ? "bg-primary" : "opacity-50"
)}>
{filterValue?.data === optionValue && (
<Icon name="check" className="text-primary-foreground h-4 w-4" />
)}
</div>
{optionLabel}
</CommandItem>
<>
{section && index !== 0 && <hr className="border-subtle my-1" />}
{section && (
<div className="text-subtle px-4 py-2 text-xs font-medium uppercase leading-none">
{section}
</div>
)}
<CommandItem
key={optionValue}
onSelect={() => {
updateFilter(column.id, { type: ColumnFilterType.SINGLE_SELECT, data: optionValue });
}}>
<div
className={classNames(
"border-subtle mr-2 flex h-4 w-4 items-center justify-center rounded-sm border",
filterValue?.data === optionValue ? "bg-primary" : "opacity-50"
)}>
{filterValue?.data === optionValue && (
<Icon name="check" className="text-primary-foreground h-4 w-4" />
)}
</div>
{optionLabel}
</CommandItem>
</>
);
})}
</CommandList>
+3 -2
View File
@@ -128,11 +128,11 @@ export type FilterableColumn = {
} & (
| {
type: ColumnFilterType.SINGLE_SELECT;
options: Array<{ label: string; value: string | number }>;
options: FacetedValue[];
}
| {
type: ColumnFilterType.MULTI_SELECT;
options: Array<{ label: string; value: string | number }>;
options: FacetedValue[];
}
| {
type: ColumnFilterType.TEXT;
@@ -186,4 +186,5 @@ export const ZColumnVisibility = z.record(z.string(), z.boolean());
export type FacetedValue = {
label: string;
value: string | number;
section?: string;
};
+3 -3
View File
@@ -151,7 +151,7 @@ export const dataTableFilter = (cellValue: unknown, filterValue: FilterValue) =>
export const convertFacetedValuesToMap = (array: FacetedValue[]) => {
return new Map<FacetedValue, number>(
array.map((option) => [{ label: option.label, value: option.value }, 1])
array.map((option) => [{ label: option.label, value: option.value, section: option.section }, 1])
);
};
@@ -161,8 +161,8 @@ export const convertMapToFacetedValues = (map: Map<FacetedValue, number> | undef
}
return Array.from(map.keys()).map((option) => {
if (typeof option === "string") {
return { label: option, value: option };
return { label: option, value: option, section: undefined };
}
return { label: option.label as string, value: option.value as string | number };
return { label: option.label as string, value: option.value as string | number, section: option.section };
});
};
@@ -123,6 +123,13 @@ export const viewerTeamsRouter = router({
const handler = await importHandler(namespaced("listMembers"), () => import("./listMembers.handler"));
return handler(opts);
}),
listSimpleMembers: authedProcedure.query(async (opts) => {
const handler = await importHandler(
namespaced("listSimpleMembers"),
() => import("./listSimpleMembers.handler")
);
return handler(opts);
}),
legacyListMembers: authedProcedure.input(ZLegacyListMembersInputSchema).query(async (opts) => {
const handler = await importHandler(
namespaced("legacyListMembers"),
@@ -0,0 +1,70 @@
/**
* Simplified version of legacyListMembers.handler.ts that returns basic member info.
* Used for filtering people on /bookings.
*/
import type { PrismaClient } from "@calcom/prisma";
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
type ListSimpleMembersOptions = {
ctx: {
user: NonNullable<TrpcSessionUser>;
prisma: PrismaClient;
};
};
export const listSimpleMembers = async ({ ctx }: ListSimpleMembersOptions) => {
const { prisma } = ctx;
const { isOrgAdmin } = ctx.user.organization;
const hasPermsToView = !ctx.user.organization.isPrivate || isOrgAdmin;
if (!hasPermsToView) {
return [];
}
// query all teams the user is a member of
const teamsToQuery = (
await prisma.membership.findMany({
where: {
userId: ctx.user.id,
accepted: true,
},
select: { teamId: true },
})
).map((membership) => membership.teamId);
if (!teamsToQuery.length) {
return [];
}
// Fetch unique users through memberships
const members = (
await prisma.membership.findMany({
where: {
accepted: true,
teamId: { in: teamsToQuery },
},
select: {
id: true,
accepted: true,
user: {
select: {
id: true,
name: true,
username: true,
avatarUrl: true,
email: true,
},
},
},
distinct: ["userId"],
orderBy: [
{ userId: "asc" }, // First order by userId to ensure consistent ordering
{ id: "asc" }, // Then by id as secondary sort
],
})
).map((membership) => membership.user);
return members;
};
export default listSimpleMembers;