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

+45 -7
View File
@@ -1,5 +1,12 @@
"use client";
import {
DataTableProvider,
DataTableFilters,
DateRangeFilter,
ColumnFilterType,
type FilterableColumn,
} from "@calcom/features/data-table";
import {
AverageEventDurationChart,
BookingKPICards,
@@ -12,25 +19,56 @@ import {
HighestRatedMembersTable,
LowestRatedMembersTable,
} from "@calcom/features/insights/components";
import { FiltersProvider } from "@calcom/features/insights/context/FiltersProvider";
import { Filters } from "@calcom/features/insights/filters";
import "@calcom/features/insights/components/tremor.css";
import { InsightsOrgTeamsProvider } from "@calcom/features/insights/context/InsightsOrgTeamsProvider";
import { Download } from "@calcom/features/insights/filters/Download";
import { OrgTeamsFilter } from "@calcom/features/insights/filters/OrgTeamsFilter";
import { useInsightsBookings } from "@calcom/features/insights/hooks/useInsightsBookings";
import { useInsightsOrgTeams } from "@calcom/features/insights/hooks/useInsightsOrgTeams";
import { useLocale } from "@calcom/lib/hooks/useLocale";
export default function InsightsPage() {
return (
<DataTableProvider>
<InsightsOrgTeamsProvider>
<InsightsPageContent />
</InsightsOrgTeamsProvider>
</DataTableProvider>
);
}
const createdAtColumn: Extract<FilterableColumn, { type: ColumnFilterType.DATE_RANGE }> = {
id: "createdAt",
title: "createdAt",
type: ColumnFilterType.DATE_RANGE,
};
function InsightsPageContent() {
const { t } = useLocale();
const { table } = useInsightsBookings();
const { isAll, teamId, userId } = useInsightsOrgTeams();
return (
<FiltersProvider>
<Filters />
<>
<div
className="flex flex-wrap items-center gap-2"
data-testid={`insights-filters-${isAll}-${teamId}-${userId}`}>
<OrgTeamsFilter />
<DataTableFilters.AddFilterButton table={table} />
<DataTableFilters.ActiveFilters table={table} />
<DataTableFilters.ClearFiltersButton exclude={["createdAt"]} />
<div className="grow" />
<Download />
<DateRangeFilter column={createdAtColumn} />
</div>
<div className="mb-4 space-y-4">
<div className="my-4 space-y-4">
<BookingKPICards />
<BookingStatusLineChart />
<div className="grid grid-cols-1 gap-4 md:grid-cols-2">
<PopularEventsTable />
<AverageEventDurationChart />
</div>
<div className="grid grid-cols-1 gap-4 md:grid-cols-2">
@@ -53,6 +91,6 @@ export default function InsightsPage() {
</a>
</small>
</div>
</FiltersProvider>
</>
);
}
+55 -61
View File
@@ -75,10 +75,8 @@ test.describe("Insights", async () => {
// go to insights page
await page.goto("/insights");
await page.waitForURL(/.*\/insights\?teamId=[^&]*&isAll=false.*/);
// expect url to have isAll and TeamId in query params
await expect(page).toHaveURL(/.*\/insights\?teamId=[^&]*&isAll=false.*/);
await page.locator('[data-testid^="insights-filters-false-"]').waitFor();
});
test("should be able to go to insights as members", async ({ page, users }) => {
@@ -91,10 +89,7 @@ test.describe("Insights", async () => {
// go to insights page
await page.goto("/insights");
await page.waitForURL(/.*\/insights\?userId=[^&]*&isAll=false.*/);
// expect url to have isAll and TeamId in query params
await expect(page).toHaveURL(/.*\/insights\?userId=[^&]*&isAll=false.*/);
await page.locator('[data-testid^="insights-filters-false-"]').waitFor();
});
test("team select filter should have 2 teams and your account option only as member", async ({
@@ -107,19 +102,22 @@ test.describe("Insights", async () => {
await user.apiLogin();
await createTeamsAndMembership(user.id, userTwo.id);
// go to insights page
await page.goto("/insights");
await page.locator(`[data-testid=insights-filters-false-undefined-${user.id}]`).waitFor();
// get div from team select filter with this class flex flex-col gap-0.5 [&>*:first-child]:mt-1 [&>*:last-child]:mb-1
await page.getByTestId("dashboard-shell").getByText("Team: test-insights").click();
await page
.locator('div[class="flex flex-col gap-0.5 [&>*:first-child]:mt-1 [&>*:last-child]:mb-1"]')
.click();
const teamSelectFilter = await page.locator(
'div[class="hover:bg-muted flex items-center py-2 pl-3 pr-2.5 transition hover:cursor-pointer"]'
await page.getByTestId("dashboard-shell").getByText("Your account").click();
await expect(await page.locator('[data-testid="org-teams-filter-item"]')).toHaveCount(3);
await expect(await page.locator('[data-testid="org-teams-filter-item"]', { hasText: "All" })).toHaveCount(
0
);
await expect(teamSelectFilter).toHaveCount(3);
await expect(
await page.locator('[data-testid="org-teams-filter-item"]', { hasText: "Your account" })
).toHaveCount(1);
});
test("Insights Organization should have isAll option true", async ({ users, page }) => {
@@ -132,17 +130,20 @@ test.describe("Insights", async () => {
await owner.apiLogin();
await page.goto("/insights");
await page.locator('[data-testid^="insights-filters-true-"]').waitFor();
await page.getByTestId("dashboard-shell").getByText("All").nth(1).click();
await page.locator('[data-testid^="insights-filters-true-"]').getByText("All").click();
const teamSelectFilter = await page.locator(
'div[class="hover:bg-muted flex items-center py-2 pl-3 pr-2.5 transition hover:cursor-pointer"]'
);
await expect(await page.locator('[data-testid="org-teams-filter-item"]')).toHaveCount(3);
await expect(teamSelectFilter).toHaveCount(4);
await expect(
await page
.locator('[data-testid="org-teams-filter-item"]', { hasText: "All" })
.locator('input[type="checkbox"]')
).toBeChecked();
});
test("should have all option in team-and-self filter as admin", async ({ page, users }) => {
test("should not have all option as admin in non-org", async ({ page, users }) => {
const owner = await users.create();
const member = await users.create();
@@ -152,16 +153,13 @@ test.describe("Insights", async () => {
await page.goto("/insights");
// get div from team select filter with this class flex flex-col gap-0.5 [&>*:first-child]:mt-1 [&>*:last-child]:mb-1
await page.getByTestId("dashboard-shell").getByText("Team: test-insights").click();
await page
.locator('div[class="flex flex-col gap-0.5 [&>*:first-child]:mt-1 [&>*:last-child]:mb-1"]')
.click();
const teamSelectFilter = await page.locator(
'div[class="hover:bg-muted flex items-center py-2 pl-3 pr-2.5 transition hover:cursor-pointer"]'
);
await page.getByTestId("dashboard-shell").getByText("Your account").click();
await expect(teamSelectFilter).toHaveCount(3);
await expect(await page.locator('[data-testid="org-teams-filter-item"]')).toHaveCount(3);
await expect(await page.locator('[data-testid="org-teams-filter-item"]', { hasText: "All" })).toHaveCount(
0
);
});
test("should be able to switch between teams and self profile for insights", async ({ page, users }) => {
@@ -174,25 +172,18 @@ test.describe("Insights", async () => {
await page.goto("/insights");
// get div from team select filter with this class flex flex-col gap-0.5 [&>*:first-child]:mt-1 [&>*:last-child]:mb-1
await page.getByTestId("dashboard-shell").getByText("Team: test-insights").click();
await page
.locator('div[class="flex flex-col gap-0.5 [&>*:first-child]:mt-1 [&>*:last-child]:mb-1"]')
.click();
const teamSelectFilter = await page.locator(
'div[class="hover:bg-muted flex items-center py-2 pl-3 pr-2.5 transition hover:cursor-pointer"]'
);
await page.getByTestId("dashboard-shell").getByText("Your account").click();
await expect(teamSelectFilter).toHaveCount(3);
await expect(await page.locator('[data-testid="org-teams-filter-item"]')).toHaveCount(3);
// switch to self profile
await page.getByTestId("dashboard-shell").getByText("Your Account").click();
await page.locator('[data-testid="org-teams-filter-item"]', { hasText: "Your account" }).click();
// switch to team 1
await page.getByTestId("dashboard-shell").getByText("test-insights").nth(0).click();
await page.locator('[data-testid="org-teams-filter-item"]', { hasText: "test-insights" }).first().click();
// switch to team 2
await page.getByTestId("dashboard-shell").getByText("test-insights-2").click();
await page.locator('[data-testid="org-teams-filter-item"]', { hasText: "test-insights-2" }).click();
});
test("should be able to switch between memberUsers", async ({ page, users }) => {
@@ -205,36 +196,39 @@ test.describe("Insights", async () => {
await page.goto("/insights");
await page.getByText("Add filter").click();
// Choose a team
await page.getByTestId("dashboard-shell").getByText("Your account").click();
await page.locator('[data-testid="org-teams-filter-item"]').nth(1).click();
await page.keyboard.press("Escape");
await page.getByRole("button", { name: "User" }).click();
// <div class="flex select-none truncate font-medium" data-state="closed">People</div>
// await page.locator('div[class="flex select-none truncate font-medium"]').getByText("People").click();
await page.waitForSelector('[data-testid="people-filter-button"]');
await page.click('[data-testid="people-filter-button"]');
// Choose User filter item from dropdown
await page.getByTestId("add-filter-button").click();
await page.getByTestId("add-filter-item-bookingUserId").click();
// Wait for the URL to include bookingUserId
await page.waitForURL((url) => url.toString().includes("bookingUserId"));
// Click User filter to see a user list
await page.getByTestId("filter-popover-trigger-bookingUserId").click();
await page
// .locator(
// 'div[class="hover:bg-muted flex items-center py-2 pl-3 pr-2.5 transition hover:cursor-pointer"]'
// )
.locator('[data-testid="people-filter-group-option-0"]')
// .nth(0)
.locator('[data-testid="single-select-options-bookingUserId"]')
.getByRole("option")
.nth(0)
.click();
await page
// .locator(
// 'div[class="hover:bg-muted flex items-center py-2 pl-3 pr-2.5 transition hover:cursor-pointer"]'
// )
.locator('[data-testid="people-filter-group-option-1"]')
// .nth(1)
.locator('[data-testid="single-select-options-bookingUserId"]')
.getByRole("option")
.nth(1)
.click();
// press escape button to close the filter
await page.keyboard.press("Escape");
await page.getByRole("button", { name: "Clear" }).click();
await page.getByTestId("clear-filters-button").click();
// expect for "Team: test-insight" text in page
await expect(page.locator("text=Team: test-insights")).toBeVisible();
await expect(page.url()).not.toContain("bookingUserId");
});
test("should test download button", async ({ page, users }) => {