Files
calendar/apps/web/modules/bookings/hooks/useFacetedUniqueValues.ts
T
Eunjae LeeGitHubDevin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
052e99f7ba feat: allow normal users to see userId filter with only themselves as option (#26835)
* feat: allow normal users to see userId filter with only themselves as option

Co-Authored-By: eunjae@cal.com <hey@eunjae.dev>

* fix: return empty map when canReadOthersBookings is false and currentUser doesn't exist

Co-Authored-By: eunjae@cal.com <hey@eunjae.dev>

* fix: use generic type parameter for Table to fix type compatibility

Co-Authored-By: eunjae@cal.com <hey@eunjae.dev>

* perf: disable listSimpleMembers query when canReadOthersBookings is false

Co-Authored-By: eunjae@cal.com <hey@eunjae.dev>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-01-23 16:05:55 +05:30

64 lines
2.2 KiB
TypeScript

import { convertFacetedValuesToMap, type FacetedValue } from "@calcom/features/data-table";
import { trpc } from "@calcom/trpc/react";
import useMeQuery from "@calcom/trpc/react/hooks/useMeQuery";
import type { RowData, Table } from "@tanstack/react-table";
import { useCallback } from "react";
import { useEventTypes } from "./useEventTypes";
interface UseFacetedUniqueValuesOptions {
canReadOthersBookings: boolean;
}
export function useFacetedUniqueValues({
canReadOthersBookings,
}: UseFacetedUniqueValuesOptions): <TData extends RowData>(
table: Table<TData>,
columnId: string
) => () => Map<FacetedValue, number> {
const eventTypes = useEventTypes();
const { data: teams } = trpc.viewer.teams.list.useQuery();
const { data: members } = trpc.viewer.teams.listSimpleMembers.useQuery(undefined, {
enabled: canReadOthersBookings,
});
const { data: currentUser } = useMeQuery();
return useCallback(
<TData extends RowData>(_: Table<TData>, columnId: string) =>
(): Map<FacetedValue, number> => {
if (columnId === "eventTypeId") {
return convertFacetedValuesToMap(eventTypes || []);
} else if (columnId === "teamId") {
return convertFacetedValuesToMap(
(teams || []).map((team) => ({
label: team.name,
value: team.id,
}))
);
} else if (columnId === "userId") {
if (!canReadOthersBookings) {
if (!currentUser) {
return new Map<FacetedValue, number>();
}
return convertFacetedValuesToMap([
{
label: currentUser.name || currentUser.email,
value: currentUser.id,
},
]);
}
return convertFacetedValuesToMap(
(members || [])
.map((member) => ({
label: member.name,
value: member.id,
}))
.filter((option): option is { label: string; value: number } => Boolean(option.label))
);
}
return new Map<FacetedValue, number>();
},
[eventTypes, teams, members, canReadOthersBookings, currentUser]
);
}