feat: allow to choose dateTarget for /insights (startTime by default) (#23752)
* feat: allow to choose dateTarget for /insights (startTime by default) * feat: add timestamp selector for insights date filtering - Add TimestampFilter component with Start Time/Created At options - Extend useInsightsBookingParameters hook with timestamp selection - Update all insight components to use dateTarget parameter - Add i18n translations for new UI strings - Position selector next to DateRangeFilter as requested Addresses user request to add select box next to date range filter allowing users to choose between startTime (default) and createdAt for displaying booking metrics on the Insights page. Co-Authored-By: [email protected] <[email protected]> * refactor: rename TimestampFilter to DateTargetSelector with nuqs URL state - Rename TimestampFilter component to DateTargetSelector - Implement nuqs hook in InsightsPageContent for URL state management - Update useInsightsBookingParameters to return dateTarget from URL state - Add dateTarget field to insightsRoutingServiceInputSchema and related types - Simplify individual insight components to use insightsBookingParams directly - Remove manual timestampTarget destructuring from all components - Update all tRPC routing service calls to include dateTarget parameter - All TypeScript checks now pass successfully Co-Authored-By: [email protected] <[email protected]> * update styles * fix inconsistency * feat: replace Select with Command component and rename filter ID - Replace Select with Command + Popover for compact width and wider dropdown - Add descriptive option labels with translations - Change filter ID from 'createdAt' to 'timestamp' across all components - Maintain URL state management with nuqs - Fix ESLint warning for missing dependency in useEffect Co-Authored-By: [email protected] <[email protected]> * fix: revert routing components to use createdAt filter ID - Keep timestamp filter ID change scoped only to main insights page - Routing components should continue using createdAt as filter ID - Only insights-view.tsx and related booking hooks use timestamp Co-Authored-By: [email protected] <[email protected]> * update styles * fix trpc router * refactor timestamp column for insights booking service * fix * update text * rename and clean up * fix endDate in DateRangeFilter * fix type errors * fix startTime filter and type errors * provide default date range * add completed to getMembersStatsWithCount * add unit tests * fix type error * address feedback --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
1 parent
616e4c4908
commit
bdc3cb9d6e
20 files changed
+884
-154
No files matched your search
@@ -4,6 +4,7 @@ import { useState, useEffect, useCallback } from "react";
|
||||
|
||||
import dayjs from "@calcom/dayjs";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import { CURRENT_TIMEZONE } from "@calcom/lib/timezoneConstants";
|
||||
import classNames from "@calcom/ui/classNames";
|
||||
import { Badge } from "@calcom/ui/components/badge";
|
||||
import { Button, buttonClasses } from "@calcom/ui/components/button";
|
||||
@@ -29,6 +30,7 @@ import {
|
||||
getDateRangeFromPreset,
|
||||
type PresetOption,
|
||||
} from "../../lib/dateRange";
|
||||
import { preserveLocalTime } from "../../lib/preserveLocalTime";
|
||||
import type { FilterableColumn, DateRangeFilterOptions } from "../../lib/types";
|
||||
import { ZDateRangeFilterValue, ColumnFilterType } from "../../lib/types";
|
||||
import { useFilterPopoverOpen } from "./useFilterPopoverOpen";
|
||||
@@ -48,9 +50,8 @@ export const DateRangeFilter = ({
|
||||
}: DateRangeFilterProps) => {
|
||||
const { open, onOpenChange } = useFilterPopoverOpen(column.id);
|
||||
const filterValue = useFilterValue(column.id, ZDateRangeFilterValue);
|
||||
const { updateFilter, removeFilter } = useDataTable();
|
||||
const { updateFilter, removeFilter, timeZone: givenTimeZone } = useDataTable();
|
||||
const range = options?.range ?? "past";
|
||||
const endOfDay = options?.endOfDay ?? false;
|
||||
const forceCustom = range === "custom";
|
||||
const forcePast = range === "past";
|
||||
|
||||
@@ -70,6 +71,19 @@ export const DateRangeFilter = ({
|
||||
: DEFAULT_PRESET
|
||||
);
|
||||
|
||||
const convertTimestamp = useCallback(
|
||||
(timestamp: string) => {
|
||||
if (!options?.convertToTimeZone) {
|
||||
return timestamp;
|
||||
}
|
||||
if (!givenTimeZone || CURRENT_TIMEZONE === givenTimeZone) {
|
||||
return timestamp;
|
||||
}
|
||||
return preserveLocalTime(timestamp, CURRENT_TIMEZONE, givenTimeZone);
|
||||
},
|
||||
[options?.convertToTimeZone, givenTimeZone]
|
||||
);
|
||||
|
||||
const updateValues = useCallback(
|
||||
({ preset, startDate, endDate }: { preset: PresetOption; startDate?: Dayjs; endDate?: Dayjs }) => {
|
||||
setSelectedPreset(preset);
|
||||
@@ -80,14 +94,14 @@ export const DateRangeFilter = ({
|
||||
updateFilter(column.id, {
|
||||
type: ColumnFilterType.DATE_RANGE,
|
||||
data: {
|
||||
startDate: startDate.toDate().toISOString(),
|
||||
endDate: (endOfDay ? endDate.endOf("day") : endDate).toDate().toISOString(),
|
||||
startDate: convertTimestamp(startDate.toDate().toISOString()),
|
||||
endDate: convertTimestamp(endDate.toDate().toISOString()),
|
||||
preset: preset.value,
|
||||
},
|
||||
});
|
||||
}
|
||||
},
|
||||
[column.id, endOfDay]
|
||||
[column.id, updateFilter, convertTimestamp]
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
@@ -126,10 +140,12 @@ export const DateRangeFilter = ({
|
||||
startDate?: Date | undefined;
|
||||
endDate?: Date | undefined;
|
||||
}) => {
|
||||
// DateRangePicker returns the beginning of the day,
|
||||
// so we need to update `endDate` to the end of the day.
|
||||
updateValues({
|
||||
preset: CUSTOM_PRESET,
|
||||
startDate: startDate ? dayjs(startDate) : getDefaultStartDate(),
|
||||
endDate: endDate ? dayjs(endDate) : undefined,
|
||||
endDate: endDate ? dayjs(endDate).add(1, "day").subtract(1, "millisecond") : undefined,
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@@ -1,28 +1,20 @@
|
||||
import { useMemo } from "react";
|
||||
|
||||
import dayjs from "@calcom/dayjs";
|
||||
import { CURRENT_TIMEZONE } from "@calcom/lib/timezoneConstants";
|
||||
|
||||
import { preserveLocalTime } from "../lib/preserveLocalTime";
|
||||
import { useDataTable } from "./useDataTable";
|
||||
|
||||
/**
|
||||
* Converts a timestamp to maintain the same local time in a different timezone.
|
||||
*
|
||||
* For example, if it's midnight (00:00) in Paris time:
|
||||
* - Input : "2025-05-22T22:00:00.000Z" (Midnight/00:00 in Paris)
|
||||
* - Output: "2025-05-22T15:00:00.000Z" (Midnight/00:00 in Seoul)
|
||||
*
|
||||
* This ensures that times like midnight (00:00) or end of day (23:59)
|
||||
* remain at those exact local times when converting between timezones.
|
||||
* The output timestamp is based on the timezone in the user's profile settings.
|
||||
* Fore more info, read packages/features/data-table/lib/preserveLocalTime.ts
|
||||
*/
|
||||
export function useChangeTimeZoneWithPreservedLocalTime(isoString: string) {
|
||||
const { timeZone: profileTimeZone } = useDataTable();
|
||||
return useMemo(() => {
|
||||
const currentTimeZone = dayjs.tz.guess();
|
||||
if (!profileTimeZone || currentTimeZone === profileTimeZone) {
|
||||
if (!profileTimeZone || CURRENT_TIMEZONE === profileTimeZone) {
|
||||
return isoString;
|
||||
}
|
||||
return preserveLocalTime(isoString, currentTimeZone, profileTimeZone);
|
||||
return preserveLocalTime(isoString, CURRENT_TIMEZONE, profileTimeZone);
|
||||
}, [isoString, profileTimeZone]);
|
||||
}
|
||||
@@ -10,6 +10,12 @@ import dayjs from "@calcom/dayjs";
|
||||
* This ensures that times like midnight (00:00) or end of day (23:59)
|
||||
* remain at those exact local times when converting between timezones.
|
||||
* The output timestamp is based on the timezone in the user's profile settings.
|
||||
*
|
||||
* For example, the profile timezone is Asia/Seoul,
|
||||
* but the current user is in Europe/Paris.
|
||||
* `Date` pickers will normally emit timestamps in the user's local timezone. (00:00:00 ~ 23:59:59 in Paris time)
|
||||
* but what we really want is to fetch the data based on the user's profile timezone. (00:00:00 ~ 23:59:59 in Seoul time)
|
||||
* That's why we need to convert the timestamp to the user's profile timezone.
|
||||
*/
|
||||
export const preserveLocalTime = (isoString: string, originalTimeZone: string, targetTimeZone: string) => {
|
||||
// Parse the input time
|
||||
|
||||
@@ -116,7 +116,7 @@ export const ZFilterValue = z.union([
|
||||
|
||||
export type DateRangeFilterOptions = {
|
||||
range?: "past" | "custom";
|
||||
endOfDay?: boolean;
|
||||
convertToTimeZone?: boolean;
|
||||
};
|
||||
|
||||
export type TextFilterOptions = {
|
||||
|
||||
Reference in new issue
Block a user