Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

feat(audit-logs): redesign the date range picker (#6033)

Matheus Nogueirae92daac357d4
diff --git a/frontend/src/components/v3/generic/DateRangeFilter/DateRangeFilter.tsx b/frontend/src/components/v3/generic/DateRangeFilter/DateRangeFilter.tsx
new file mode 100644
index 0000000000..0335798b80
--- /dev/null
+++ b/frontend/src/components/v3/generic/DateRangeFilter/DateRangeFilter.tsx
@@ -0,0 +1,547 @@
+import { useCallback, useEffect, useMemo, useState } from "react";
+import { type DateRange } from "react-day-picker";
+import { addDays, addMonths, format, subMonths } from "date-fns";
+import { ArrowRight, CalendarIcon } from "lucide-react";
+import ms from "ms";
+
+import { cn } from "@app/components/v3/utils";
+
+import { Button, type ButtonProps } from "../Button";
+import { Calendar, CalendarDayButton } from "../Calendar";
+import { UnstableInput } from "../Input";
+import { Popover, PopoverContent, PopoverTrigger } from "../Popover";
+import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "../Select";
+import { Switch } from "../Switch";
+
+const MAX_RANGE_MONTHS = 3;
+
+const SECONDARY_PRESETS = [
+ { value: "1d", label: "1 day" },
+ { value: "2d", label: "2 days" },
+ { value: "1w", label: "1 week" },
+ { value: "2w", label: "2 weeks" },
+ { value: "1M", label: "1 month" },
+ { value: "3M", label: "3 months" }
+] as const;
+
+const RELATIVE_OPTIONS = [
+ { label: "Minutes", unit: "m", values: [5, 10, 15, 30, 45] },
+ { label: "Hours", unit: "h", values: [1, 2, 3, 6, 8, 12] },
+ { label: "Days", unit: "d", values: [1, 2, 3, 4, 5, 6] },
+ { label: "Weeks", unit: "w", values: [1, 2, 3, 4] },
+ { label: "Months", unit: "M", values: [1, 2, 3] }
+] as const;
+
+export enum DateRangeFilterType {
+ Last = "last",
+ Fixed = "fixed"
+}
+
+export type DateRangeFilterValue =
+ | { type: DateRangeFilterType.Last; value: string }
+ | { type: DateRangeFilterType.Fixed; startDate: Date; endDate: Date };
+
+export type DateRangeFilterResult = {
+ startDate: Date;
+ endDate: Date;
+ isUtc: boolean;
+};
+
+export type DateRangeFilterAccent = "primary" | "secondary";
+
+type Props = {
+ defaultValue?: DateRangeFilterValue;
+ defaultIsUtc?: boolean;
+ onChange: (value: DateRangeFilterResult) => void;
+ accent?: DateRangeFilterAccent;
+ isActive?: boolean;
+ className?: string;
+};
+
+export const ACCENT_STYLES: Record<
+ DateRangeFilterAccent,
+ {
+ selectedBorderBg: string;
+ selectedCard: string;
+ selectedBadge: string;
+ selectedChip: string;
+ switchChecked: string;
+ applyButton: string;
+ calendarMiddle: string;
+ activeVariant: ButtonProps["variant"];
+ }
+> = {
+ primary: {
+ selectedBorderBg: "border-primary bg-primary/5",
+ selectedCard: "border-primary/40 bg-primary/5 shadow-xs",
+ selectedBadge: "rounded-sm bg-primary/5 px-1.5 py-0.5 text-[11px] font-medium text-primary",
+ selectedChip: "border-primary/40 bg-primary/5 text-foreground",
+ switchChecked:
+ "data-[state=checked]:border-primary/25 data-[state=checked]:bg-primary/10 data-[state=checked]:hover:border-primary/30 data-[state=checked]:hover:bg-primary/15",
+ applyButton: "border-primary/25 bg-primary/10 hover:bg-primary/15 hover:border-primary/30",
+ calendarMiddle: "data-[range-middle=true]:!bg-muted/[12%]",
+ activeVariant: "project"
+ },
+ secondary: {
+ selectedBorderBg: "border-org bg-org/5",
+ selectedCard: "border-org/40 bg-org/5 shadow-xs",
+ selectedBadge: "rounded-sm bg-org/5 px-1.5 py-0.5 text-[11px] font-medium text-org",
+ selectedChip: "border-org/40 bg-org/5 text-foreground",
+ switchChecked:
+ "data-[state=checked]:border-org/25 data-[state=checked]:bg-org/10 data-[state=checked]:hover:border-org/30 data-[state=checked]:hover:bg-org/15",
+ applyButton: "border-org/25 bg-org/10 hover:bg-org/15 hover:border-org/30",
+ calendarMiddle: "data-[range-middle=true]:!bg-muted/[12%]",
+ activeVariant: "org"
+ }
+};
+
+function formatDateForDisplay(date: Date, isUtc: boolean): string {
+ if (isUtc) {
+ const utcDate = new Date(date.getTime() + date.getTimezoneOffset() * 60000);
+ return format(utcDate, "MMM d, yyyy HH:mm");
+ }
+ return format(date, "MMM d, yyyy HH:mm");
+}
+
+function formatTriggerLabel(value: DateRangeFilterValue, isUtc: boolean): string {
+ if (value.type === DateRangeFilterType.Last) return `Last ${value.value}`;
+ const start = formatDateForDisplay(value.startDate, isUtc);
+ const end = formatDateForDisplay(value.endDate, isUtc);
+ return `${start} — ${end}`;
+}
+
+function combineDateAndTime(date: Date, timeStr: string): Date {
+ const [hoursStr, minutesStr] = timeStr.split(":");
+ const result = new Date(date);
+ result.setHours(Number(hoursStr) || 0, Number(minutesStr) || 0, 0, 0);
+ return result;
+}
+
+function toTimeString(date: Date): string {
+ const h = date.getHours().toString().padStart(2, "0");
+ const m = date.getMinutes().toString().padStart(2, "0");
+ return `${h}:${m}`;
+}
+
+function isValidLastValue(val: string): boolean {
+ if (!val) return false;
+ if (/^(\d+)M$/.test(val)) return parseInt(val, 10) > 0;
+ const parsed = ms(val);
+ return typeof parsed === "number" && parsed > 0;
+}
+
+function parseLastValue(val: string): { duration: string; unit: string } | null {
+ const match = val.match(/^(\d+)([mhdwM])$/);
+ if (!match) return null;
+ return { duration: match[1], unit: match[2] };
+}
+
+function resolveDateRange(value: DateRangeFilterValue, isUtc: boolean): DateRangeFilterResult {
+ if (value.type === DateRangeFilterType.Last) {
+ const now = new Date();
+ const monthMatch = value.value.match(/^(\d+)M$/);
+ const startDate = monthMatch
+ ? subMonths(now, parseInt(monthMatch[1], 10))
+ : new Date(now.getTime() - ms(value.value));
+ return { startDate, endDate: now, isUtc };
+ }
+
+ return {
+ startDate: value.startDate,
+ endDate: value.endDate,
+ isUtc
+ };
+}
+
+export function DateRangeFilter({
+ defaultValue,
+ defaultIsUtc = false,
+ onChange,
+ accent = "primary",
+ isActive = true,
+ className
+}: Props) {
+ const initialValue = defaultValue ?? { type: DateRangeFilterType.Last, value: "1h" };
+ const [appliedValue, setAppliedValue] = useState<DateRangeFilterValue>(initialValue);
+ const [appliedIsUtc, setAppliedIsUtc] = useState(defaultIsUtc);
+ const [isOpen, setIsOpen] = useState(false);
+ const [mode, setMode] = useState<DateRangeFilterType>(
+ initialValue.type === DateRangeFilterType.Fixed
+ ? DateRangeFilterType.Fixed
+ : DateRangeFilterType.Last
+ );
+
+ // Last mode state — seed from the initial value if it's a Last type
+ const initialLastParsed =
+ initialValue.type === DateRangeFilterType.Last
+ ? (parseLastValue(initialValue.value) ?? { duration: "1", unit: "h" })
+ : { duration: "1", unit: "h" };
+ const [customDuration, setCustomDuration] = useState<string>(initialLastParsed.duration);
+ const [customUnit, setCustomUnit] = useState<string>(initialLastParsed.unit);
+
+ // Fixed mode state
+ const [pendingRange, setPendingRange] = useState<DateRange | undefined>(
+ initialValue.type === DateRangeFilterType.Fixed
+ ? { from: initialValue.startDate, to: initialValue.endDate }
+ : undefined
+ );
+ const [startTime, setStartTime] = useState<string>(
+ initialValue.type === DateRangeFilterType.Fixed ? toTimeString(initialValue.startDate) : "00:00"
+ );
+ const [endTime, setEndTime] = useState<string>(
+ initialValue.type === DateRangeFilterType.Fixed ? toTimeString(initialValue.endDate) : "23:59"
+ );
+
+ // Timezone state
+ const [pendingIsUtc, setPendingIsUtc] = useState(defaultIsUtc);
+
+ // Sync pending state when popover opens
+ useEffect(() => {
+ if (isOpen) {
+ setMode(
+ appliedValue.type === DateRangeFilterType.Fixed
+ ? DateRangeFilterType.Fixed
+ : DateRangeFilterType.Last
+ );
+ setPendingIsUtc(appliedIsUtc);
+ if (appliedValue.type === DateRangeFilterType.Last) {
+ const parsed = parseLastValue(appliedValue.value) ?? { duration: "1", unit: "h" };
+ setCustomDuration(parsed.duration);
+ setCustomUnit(parsed.unit);
+ } else {
+ setPendingRange({ from: appliedValue.startDate, to: appliedValue.endDate });
+ setStartTime(toTimeString(appliedValue.startDate));
+ setEndTime(toTimeString(appliedValue.endDate));
+ }
+ }
+ }, [isOpen, appliedValue, appliedIsUtc]);
+
+ const handleApply = () => {
+ let nextValue: DateRangeFilterValue;
+ if (mode === DateRangeFilterType.Last) {
+ const lastVal = `${customDuration}${customUnit}`;
+ if (!isValidLastValue(lastVal)) return;
+ nextValue = { type: DateRangeFilterType.Last, value: lastVal };
+ } else {
+ if (!pendingRange?.from || !pendingRange?.to) return;
+ const startDate = combineDateAndTime(pendingRange.from, startTime);
+ const endDate = combineDateAndTime(pendingRange.to, endTime);
+ nextValue = { type: DateRangeFilterType.Fixed, startDate, endDate };
+ }
+
+ setAppliedValue(nextValue);
+ setAppliedIsUtc(pendingIsUtc);
+ onChange(resolveDateRange(nextValue, pendingIsUtc));
+ setIsOpen(false);
+ };
+
+ const today = new Date();
+
+ const calendarDefaultMonth = pendingRange?.from ? pendingRange.from : addMonths(today, -1);
+
+ const isInvalidFixedRange = useMemo(() => {
+ if (mode !== DateRangeFilterType.Fixed || !pendingRange?.from || !pendingRange?.to)
+ return false;
+ const start = combineDateAndTime(pendingRange.from, startTime);
+ const end = combineDateAndTime(pendingRange.to, endTime);
+ return start >= end;
+ }, [mode, pendingRange, startTime, endTime]);
+
+ const isApplyDisabled = useMemo(() => {
+ if (mode === DateRangeFilterType.Fixed) {
+ if (!pendingRange?.from || !pendingRange?.to) return true;
+ return isInvalidFixedRange;
+ }
+ return !isValidLastValue(`${customDuration}${customUnit}`);
+ }, [mode, pendingRange, isInvalidFixedRange, customDuration, customUnit]);
+
+ const accentStyles = ACCENT_STYLES[accent];
+
+ // eslint-disable-next-line react/no-unstable-nested-components
+ const DayButton = useCallback(
+ (props: React.ComponentProps<typeof CalendarDayButton>) => (
+ <CalendarDayButton {...props} className={accentStyles.calendarMiddle} />
+ ),
+ [accentStyles.calendarMiddle]
+ );
+
+ const selectedCustomUnit = useMemo(
+ () => RELATIVE_OPTIONS.find((opt) => opt.unit === customUnit),
+ [customUnit]
+ );
+
+ const renderTrigger = () => {
+ if (!isActive) {
+ return (
+ <Button variant="outline" size="sm" className={cn("gap-1.5 font-normal", className)}>
+ <CalendarIcon className="text-muted-foreground size-3.5 shrink-0" />
+ Custom
+ </Button>
+ );
+ }
+
+ if (appliedValue.type === DateRangeFilterType.Fixed) {
+ return (
+ <Button
+ variant="outline"
+ size="sm"
+ className={cn("gap-1.5 font-normal", accentStyles.selectedChip, className)}
+ >
+ <span className="text-xs">{format(appliedValue.startDate, "MMM d, yyyy")}</span>
+ <ArrowRight className="text-muted-foreground size-3 shrink-0" />
+ <span className="text-xs">{format(appliedValue.endDate, "MMM d, yyyy")}</span>
+ <CalendarIcon className="text-muted-foreground size-3.5 shrink-0" />
+ </Button>
+ );
+ }
+
+ return (
+ <Button
+ variant="outline"
+ size="sm"
+ className={cn("gap-1.5 font-normal", accentStyles.selectedChip, className)}
+ >
+ <CalendarIcon className="text-muted-foreground size-3.5 shrink-0" />
+ <span className="max-w-72 truncate">{formatTriggerLabel(appliedValue, appliedIsUtc)}</span>
+ </Button>
+ );
+ };
+
+ return (
+ <Popover open={isOpen} onOpenChange={setIsOpen}>
+ <PopoverTrigger asChild>{renderTrigger()}</PopoverTrigger>
+
+ <PopoverContent
+ align="end"
+ sideOffset={8}
+ className="w-auto min-w-[30rem] overflow-hidden p-0"
+ >
+ <div className="flex flex-col">
+ {/* Body */}
+ <div className="flex">
+ {/* Left sidebar */}
+ <div className="flex w-24 shrink-0 flex-col border-r border-border py-2">
+ {[DateRangeFilterType.Last, DateRangeFilterType.Fixed].map((m) => (
+ <button
+ key={m}
+ type="button"
+ onClick={() => setMode(m)}
+ className={cn(
+ "border-l-2 px-3 py-2 text-left text-sm transition-colors",
+ mode === m
+ ? `${accentStyles.selectedBorderBg} font-medium text-foreground`
+ : "text-muted-foreground border-transparent hover:bg-foreground/5 hover:text-foreground"
+ )}
+ >
+ {m === DateRangeFilterType.Last ? "Last" : "Fixed"}
+ </button>
+ ))}
+ </div>
+
+ {/* Right content */}
+ <div className="flex-1 p-4">
+ {mode === DateRangeFilterType.Last && (
+ <div className="flex flex-col gap-3">
+ <span className="text-muted-foreground text-xs font-medium tracking-wide uppercase">
+ Quick ranges
+ </span>
+ <div className="grid grid-cols-2 gap-2">
+ {SECONDARY_PRESETS.map(({ value: val, label }) => {
+ const parsed = parseLastValue(val);
+ const isPresetActive =
+ parsed && customDuration === parsed.duration && customUnit === parsed.unit;
+ return (
+ <Button
+ key={val}
+ size="xs"
+ variant={isPresetActive ? accentStyles.activeVariant : "outline"}
+ isFullWidth
+ className="justify-start"
+ onClick={() => {
+ if (parsed) {
+ setCustomDuration(parsed.duration);
+ setCustomUnit(parsed.unit);
+ }
+ }}
+ >
+ {label}
+ </Button>
+ );
+ })}
+ </div>
+
+ <span className="text-muted-foreground text-xs font-medium tracking-wide uppercase">
+ Custom
+ </span>
+ <div className="flex items-center gap-2">
+ <UnstableInput
+ type="text"
+ inputMode="numeric"
+ placeholder="Amount"
+ className="h-8 flex-1 text-xs"
+ value={customDuration}
+ onChange={(e) => {
+ const val = e.target.value;
+ if (val === "" || /^[1-9]\d*$/.test(val)) {
+ setCustomDuration(val);
+ }
+ }}
+ />
+ <Select value={customUnit} onValueChange={setCustomUnit}>
+ <SelectTrigger size="sm" className="!h-8 flex-1 text-xs">
+ <SelectValue />
+ </SelectTrigger>
+ <SelectContent>
+ {RELATIVE_OPTIONS.map((opt) => (
+ <SelectItem key={opt.unit} value={opt.unit}>
+ {opt.label}
+ </SelectItem>
+ ))}
+ </SelectContent>
+ </Select>
+ </div>
+
+ <div className="flex flex-wrap gap-2">
+ {selectedCustomUnit?.values.map((val) => {
+ const isChipActive = customDuration === String(val);
+ return (
+ <Button
+ key={`${selectedCustomUnit.unit}-${val}`}
+ size="xs"
+ variant={isChipActive ? accentStyles.activeVariant : "outline"}
+ onClick={() => setCustomDuration(String(val))}
+ >
+ {val}
+ </Button>
+ );
+ })}
+ </div>
+ </div>
+ )}
+
+ {mode === DateRangeFilterType.Fixed && (
+ <div className="flex flex-col gap-3">
+ {/* Calendar */}
+ <Calendar
+ mode="range"
+ selected={pendingRange}
+ onSelect={(range, selectedDay) => {
+ // If a complete range (with distinct start/end) is already selected, restart from the clicked date
+ if (
+ pendingRange?.from &&
+ pendingRange?.to &&
+ pendingRange.from.getTime() !== pendingRange.to.getTime()
+ ) {
+ setPendingRange({ from: selectedDay, to: undefined });
+ return;
+ }
+ setPendingRange(range);
+ }}
+ numberOfMonths={2}
+ showOutsideDays={false}
+ captionLayout="dropdown"
+ disabled={[
+ { after: today },
+ { before: addDays(subMonths(today, MAX_RANGE_MONTHS), 1) }
+ ]}
+ defaultMonth={calendarDefaultMonth}
+ startMonth={addDays(subMonths(today, MAX_RANGE_MONTHS), 1)}
+ endMonth={today}
+ className="p-0"
+ classNames={{
+ range_start:
+ "rounded-l-(--cell-radius) bg-muted/[12%] relative after:bg-muted/[12%] after:absolute after:inset-y-0 after:w-4 after:right-0 z-0 isolate",
+ range_end:
+ "rounded-r-(--cell-radius) bg-muted/[12%] relative after:bg-muted/[12%] after:absolute after:inset-y-0 after:w-4 after:left-0 z-0 isolate",
+ range_middle: "rounded-none"
+ }}
+ components={{
+ DayButton
+ }}
+ />
+
+ {/* Time inputs */}
+ <div className="flex flex-col gap-2 border-t border-border pt-2">
+ <div className="flex items-end gap-2">
+ <div className="flex flex-1 flex-col gap-1">
+ <span className="text-muted-foreground text-xs">Start Time</span>
+ <UnstableInput
+ type="time"
+ value={startTime}
+ onChange={(e) => setStartTime(e.target.value)}
+ className="h-9 w-full text-sm scheme-dark [&::-webkit-calendar-picker-indicator]:hidden"
+ />
+ </div>
+ <span className="text-muted-foreground mb-2">→</span>
+ <div className="flex flex-1 flex-col gap-1">
+ <span className="text-muted-foreground text-xs">End Time</span>
+ <UnstableInput
+ type="time"
+ value={endTime}
+ onChange={(e) => setEndTime(e.target.value)}
+ className="h-9 w-full text-sm scheme-dark [&::-webkit-calendar-picker-indicator]:hidden"
+ />
+ </div>
+ </div>
+ {isInvalidFixedRange && (
+ <p className="text-xs text-red-400">Start date must be before end date.</p>
+ )}
+ </div>
+ </div>
+ )}
+ </div>
+ </div>
+
+ {/* Footer */}
+ <div className="flex items-center justify-between gap-2 border-t border-border px-4 py-3">
+ {mode === DateRangeFilterType.Fixed ? (
+ <div className="flex shrink-0 cursor-pointer items-center gap-2 select-none">
+ <span
+ className={cn(
+ "text-sm transition-colors",
+ !pendingIsUtc ? "text-foreground" : "text-muted-foreground"
+ )}
+ >
+ Local
+ </span>
+ <Switch
+ id="date-range-utc-toggle"
+ checked={pendingIsUtc}
+ onCheckedChange={setPendingIsUtc}
+ size="sm"
+ className={accentStyles.switchChecked}
+ />
+ {/* eslint-disable-next-line jsx-a11y/label-has-associated-control */}
+ <label
+ htmlFor="date-range-utc-toggle"
+ className={cn(
+ "cursor-pointer text-sm transition-colors",
+ pendingIsUtc ? "text-foreground" : "text-muted-foreground"
+ )}
+ >
+ UTC
+ </label>
+ </div>
+ ) : (
+ <div />
+ )}
+ <div className="flex items-center gap-2">
+ <Button variant="ghost" size="sm" onClick={() => setIsOpen(false)}>
+ Cancel
+ </Button>
+ <Button
+ variant="outline"
+ size="sm"
+ isDisabled={isApplyDisabled}
+ onClick={handleApply}
+ className={!isApplyDisabled ? accentStyles.applyButton : undefined}
+ >
+ Apply
+ </Button>
+ </div>
+ </div>
+ </div>
+ </PopoverContent>
+ </Popover>
+ );
+}
diff --git a/frontend/src/components/v3/generic/DateRangeFilter/DateRangeQuickPresets.tsx b/frontend/src/components/v3/generic/DateRangeFilter/DateRangeQuickPresets.tsx
new file mode 100644
index 0000000000..e676da3543
--- /dev/null
+++ b/frontend/src/components/v3/generic/DateRangeFilter/DateRangeQuickPresets.tsx
@@ -0,0 +1,84 @@
+import { cn } from "@app/components/v3/utils";
+
+import { Button } from "../Button";
+import type { DateRangeFilterAccent, DateRangeFilterResult } from "./DateRangeFilter";
+import { ACCENT_STYLES } from "./DateRangeFilter";
+
+export type QuickPreset = {
+ label: string;
+ value: string;
+ resolve: () => { startDate: Date; endDate: Date };
+};
+
+export const DEFAULT_QUICK_PRESETS: QuickPreset[] = [
+ {
+ label: "5m",
+ value: "5m",
+ resolve: () => ({ startDate: new Date(Date.now() - 5 * 60 * 1000), endDate: new Date() })
+ },
+ {
+ label: "30m",
+ value: "30m",
+ resolve: () => ({ startDate: new Date(Date.now() - 30 * 60 * 1000), endDate: new Date() })
+ },
+ {
+ label: "1h",
+ value: "1h",
+ resolve: () => ({ startDate: new Date(Date.now() - 60 * 60 * 1000), endDate: new Date() })
+ },
+ {
+ label: "3h",
+ value: "3h",
+ resolve: () => ({ startDate: new Date(Date.now() - 3 * 60 * 60 * 1000), endDate: new Date() })
+ },
+ {
+ label: "12h",
+ value: "12h",
+ resolve: () => ({
+ startDate: new Date(Date.now() - 12 * 60 * 60 * 1000),
+ endDate: new Date()
+ })
+ }
+];
+
+type Props = {
+ value?: string;
+ onChange: (presetValue: string, result: DateRangeFilterResult) => void;
+ presets?: QuickPreset[];
+ isUtc?: boolean;
+ accent?: DateRangeFilterAccent;
+ className?: string;
+};
+
+export function DateRangeQuickPresets({
+ value,
+ onChange,
+ presets = DEFAULT_QUICK_PRESETS,
+ isUtc = false,
+ accent = "primary",
+ className
+}: Props) {
+ const accentStyles = ACCENT_STYLES[accent];
+
+ return (
+ <>
+ {presets.map((preset) => {
+ const isActive = value === preset.value;
+ return (
+ <Button
+ key={preset.value}
+ size="sm"
+ variant={isActive ? accentStyles.activeVariant : "outline"}
+ onClick={() => {
+ const { startDate, endDate } = preset.resolve();
+ onChange(preset.value, { startDate, endDate, isUtc });
+ }}
+ className={cn(isActive ? "relative z-10" : "hover:relative hover:z-10", className)}
+ >
+ {preset.label}
+ </Button>
+ );
+ })}
+ </>
+ );
+}
diff --git a/frontend/src/components/v3/generic/DateRangeFilter/index.ts b/frontend/src/components/v3/generic/DateRangeFilter/index.ts
new file mode 100644
index 0000000000..a3c9bd147a
--- /dev/null
+++ b/frontend/src/components/v3/generic/DateRangeFilter/index.ts
@@ -0,0 +1,2 @@
+export * from "./DateRangeFilter";
+export * from "./DateRangeQuickPresets";
diff --git a/frontend/src/components/v3/generic/index.ts b/frontend/src/components/v3/generic/index.ts
index 180111b383..34e5e445cb 100644
--- a/frontend/src/components/v3/generic/index.ts
+++ b/frontend/src/components/v3/generic/index.ts
@@ -10,6 +10,7 @@ export * from "./Card";
export * from "./Checkbox";
export * from "./Command";
export * from "./DataGrid";
+export * from "./DateRangeFilter";
export * from "./Detail";
export * from "./Dialog";
export * from "./Dropdown";
diff --git a/frontend/src/pages/organization/AuditLogsPage/components/LogsDateFilter.tsx b/frontend/src/pages/organization/AuditLogsPage/components/LogsDateFilter.tsx
deleted file mode 100644
index 3407043a2f..0000000000
--- a/frontend/src/pages/organization/AuditLogsPage/components/LogsDateFilter.tsx
+++ /dev/null
@@ -1,342 +0,0 @@
-import { useState } from "react";
-import { Controller, useForm } from "react-hook-form";
-import { faCalendar, faChevronRight } from "@fortawesome/free-solid-svg-icons";
-import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
-import { zodResolver } from "@hookform/resolvers/zod";
-import ms from "ms";
-import { twMerge } from "tailwind-merge";
-
-import {
- Button,
- DatePicker,
- DropdownMenu,
- DropdownMenuContent,
- DropdownMenuTrigger,
- FormControl,
- Input,
- Select,
- SelectItem
-} from "@app/components/v2";
-import { formatDateTime, Timezone } from "@app/helpers/datetime";
-
-import {
- auditLogDateFilterFormSchema,
- AuditLogDateFilterType,
- TAuditLogDateFilterFormData
-} from "./types";
-
-type Props = {
- setFilter: (data: TAuditLogDateFilterFormData) => void;
- filter: TAuditLogDateFilterFormData;
- setTimezone: (timezone: Timezone) => void;
- timezone: Timezone;
-};
-const RELATIVE_VALUES = ["5m", "30m", "1h", "3h", "12h"];
-
-const RELATIVE_OPTIONS = [
- { label: "Minutes", unit: "m", values: [5, 10, 15, 30, 45] },
- { label: "Hours", unit: "h", values: [1, 2, 3, 6, 8, 12] },
- { label: "Days", unit: "d", values: [1, 2, 3, 4, 5, 6] },
- { label: "Weeks", unit: "w", values: [1, 2, 3, 4] }
-];
-
-export const LogsDateFilter = ({ setFilter, filter, timezone, setTimezone }: Props) => {
- const [isStartDatePickerOpen, setIsStartDatePickerOpen] = useState(false);
- const [isEndDatePickerOpen, setIsEndDatePickerOpen] = useState(false);
- const [isPopupOpen, setIsPopOpen] = useState(false);
-
- const { control, watch, handleSubmit, formState } = useForm<TAuditLogDateFilterFormData>({
- resolver: zodResolver(auditLogDateFilterFormSchema),
- values: filter
- });
- const selectType = watch("type");
- const isCustomRelative =
- filter.type === AuditLogDateFilterType.Relative &&
- !RELATIVE_VALUES.includes(filter.relativeModeValue || "");
-
- const onSubmit = (data: TAuditLogDateFilterFormData) => {
- const endDate = data.type === AuditLogDateFilterType.Relative ? new Date() : data.endDate;
- const startDate =
- data.type === AuditLogDateFilterType.Relative && data.relativeModeValue
- ? new Date(Number(new Date()) - ms(data.relativeModeValue))
- : data.startDate;
- setFilter({
- ...data,
- startDate,
- endDate
- });
- setIsPopOpen(false);
- };
-
- return (
- <>
- <DropdownMenu open={isPopupOpen} onOpenChange={(el) => setIsPopOpen(el)}>
- <div className="flex items-center">
- {filter.type === AuditLogDateFilterType.Relative ? (
- <>
- {RELATIVE_VALUES.map((el) => (
- <Button
- variant="outline_bg"
- className={twMerge(
- "w-[3.82rem] rounded-none px-3 py-2 font-normal first:rounded-l-md",
- filter.type === AuditLogDateFilterType.Relative &&
- filter.relativeModeValue === el &&
- "border-primary/40 bg-primary/10"
- )}
- key={`${el}-relative`}
- onClick={() =>
- setFilter({
- relativeModeValue: el,
- type: AuditLogDateFilterType.Relative,
- endDate: new Date(),
- startDate: new Date(Number(new Date()) - ms(el))
- })
- }
- >
- {el}
- </Button>
- ))}
- </>
- ) : (
- <div className="flex w-[19.1rem] items-center justify-between rounded-l-md border border-transparent bg-mineshaft-600 px-5 py-2 text-sm text-bunker-200">
- <div>
- {formatDateTime({
- timezone,
- timestamp: filter.startDate,
- dateFormat: "yyyy/MM/dd HH:mm"
- })}
- </div>
- <div>
- <FontAwesomeIcon className="text-bunker-300" size="sm" icon={faChevronRight} />
- </div>
- <div>
- {formatDateTime({
- timezone,
- timestamp: filter.endDate,
- dateFormat: "yyyy/MM/dd HH:mm"
- })}
- </div>
- </div>
- )}
- <DropdownMenuTrigger asChild>
- <Button
- variant="outline_bg"
- className={twMerge(
- "w-32 rounded-none rounded-r-md px-3 py-2 font-normal",
- (filter.type === AuditLogDateFilterType.Absolute || isCustomRelative) &&
- "border-primary/40 bg-primary/10"
- )}
- >
- <span>Custom</span> <FontAwesomeIcon className="ml-1" icon={faCalendar} />
- {filter.type === AuditLogDateFilterType.Relative && isCustomRelative && (
- <span className="ml-1">({filter.relativeModeValue})</span>
- )}
- </Button>
- </DropdownMenuTrigger>
- </div>
- <DropdownMenuContent
- className="min-w-[434px]! bg-mineshaft-800 p-4"
- align="end"
- sideOffset={8}
- >
- <form onSubmit={handleSubmit(onSubmit)}>
- <Controller
- control={control}
- name="type"
- render={({ field }) => (
- <div className="mb-7">
- <Button
- onClick={() => field.onChange(AuditLogDateFilterType.Absolute)}
- variant="outline_bg"
- className={twMerge(
- "h-8 rounded-r-none font-normal",
- field.value === AuditLogDateFilterType.Absolute &&
- "border-primary/40 bg-primary/10"
- )}
- >
- Absolute
- </Button>
- <Button
- onClick={() => field.onChange(AuditLogDateFilterType.Relative)}
- variant="outline_bg"
- className={twMerge(
- "h-8 rounded-l-none font-normal",
- field.value === AuditLogDateFilterType.Relative &&
- "border-primary/40 bg-primary/10"
- )}
- >
- Relative
- </Button>
- </div>
- )}
- />
- {selectType === AuditLogDateFilterType.Relative && (
- <Controller
- control={control}
- name="relativeModeValue"
- render={({ field, fieldState: { error } }) => {
- const duration = field.value?.substring(0, field.value.length - 1);
- const unitOfTime = field.value?.at(-1);
- return (
- <div className="flex flex-col gap-4">
- {RELATIVE_OPTIONS.map(({ label, unit, values }) => (
- <div key={unit} className="flex items-center gap-2">
- <div className="w-16">{label}</div>
- {values.map((v) => {
- const value = `${v}${unit}`;
- return (
- <Button
- key={value}
- variant="outline_bg"
- onClick={() => field.onChange(value)}
- className={twMerge(
- "h-8 w-12",
- field.value === value && "border-primary/40 bg-primary/10"
- )}
- >
- {v}
- </Button>
- );
- })}
- </div>
- ))}
- <div className="flex flex-col gap-1">
- <div className="flex items-center gap-2">
- <FormControl
- className="mb-0 w-28"
- label="Duration"
- isError={Boolean(error)}
- >
- <Input
- type="number"
- value={duration}
- onChange={(val) => {
- const durationVal = val.target.value
- ? Number(val.target.value)
- : undefined;
- field.onChange(`${durationVal}${unitOfTime}`);
- }}
- max={60}
- min={1}
- />
- </FormControl>
- <FormControl className="mb-0 w-36" label="Unit of Time">
- <Select
- value={unitOfTime}
- onValueChange={(val) => field.onChange(`${duration}${val}`)}
- className="w-full"
- position="popper"
- >
- {RELATIVE_OPTIONS.map((opt) => (
- <SelectItem value={opt.unit}>{opt.label}</SelectItem>
- ))}
- </Select>
- </FormControl>
- </div>
- {error && (
- <span className="text-opacity-90 text-xs text-red-600">
- {error.message}
- </span>
- )}
- </div>
- </div>
- );
- }}
- />
- )}
- {selectType === AuditLogDateFilterType.Absolute && (
- <div className="flex h-10 w-full items-center justify-between gap-2">
- <Controller
- name="startDate"
- control={control}
- render={({ field: { onChange, ...field }, fieldState: { error } }) => {
- return (
- <FormControl
- className="relative top-2"
- errorText={error?.message}
- isError={Boolean(error)}
- label="Start Date"
- >
- <DatePicker
- value={field.value || undefined}
- onChange={onChange}
- timezone={timezone}
- dateFormat="P"
- buttonClassName="w-44 h-8 font-normal"
- popUpProps={{
- open: isStartDatePickerOpen,
- onOpenChange: setIsStartDatePickerOpen
- }}
- popUpContentProps={{}}
- />
- </FormControl>
- );
- }}
- />
- <FontAwesomeIcon
- icon={faChevronRight}
- size="xs"
- className="mt-6 text-mineshaft-400"
- />
- <Controller
- name="endDate"
- control={control}
- render={({ field: { onChange, ...field }, fieldState: { error } }) => {
- return (
- <FormControl
- className="relative top-2"
- errorText={error?.message}
- isError={Boolean(error)}
- label="End Date"
- >
- <DatePicker
- value={field.value || undefined}
- onChange={onChange}
- dateFormat="P"
- buttonClassName="w-44 h-8 font-normal"
- timezone={timezone}
- popUpProps={{
- open: isEndDatePickerOpen,
- onOpenChange: setIsEndDatePickerOpen
- }}
- popUpContentProps={{}}
- />
- </FormControl>
- );
- }}
- />
- </div>
- )}
- <div className="mt-8 w-full justify-end">
- <Button
- size="sm"
- type="submit"
- className="h-9 w-24 font-normal"
- variant="outline_bg"
- isDisabled={!formState.isDirty}
- >
- Apply
- </Button>
- </div>
- </form>
- </DropdownMenuContent>
- </DropdownMenu>
- <Select
- value={timezone}
- onValueChange={(val) => setTimezone(val as Timezone)}
- className="w-[10.6rem] border border-mineshaft-500! bg-mineshaft-600! capitalize"
- dropdownContainerClassName="max-w-none"
- position="popper"
- dropdownContainerStyle={{
- width: "100%"
- }}
- >
- {Object.values(Timezone).map((tz) => (
- <SelectItem value={tz} className="capitalize" key={tz}>
- {tz} Timezone
- </SelectItem>
- ))}
- </Select>
- </>
- );
-};
diff --git a/frontend/src/pages/organization/AuditLogsPage/components/LogsSection.tsx b/frontend/src/pages/organization/AuditLogsPage/components/LogsSection.tsx
index 533f9776be..bc2a6f5d5c 100644
--- a/frontend/src/pages/organization/AuditLogsPage/components/LogsSection.tsx
+++ b/frontend/src/pages/organization/AuditLogsPage/components/LogsSection.tsx
@@ -1,11 +1,15 @@
import { useEffect, useMemo, useState } from "react";
-import ms from "ms";
import { UpgradePlanModal } from "@app/components/license/UpgradePlanModal";
import {
+ DateRangeFilter,
+ type DateRangeFilterResult,
+ DateRangeFilterType,
+ DateRangeQuickPresets,
DocumentationLinkBadge,
UnstableAlert,
- UnstableAlertDescription
+ UnstableAlertDescription,
+ UnstableButtonGroup
} from "@app/components/v3";
import {
OrgPermissionAuditLogsActions,
@@ -27,15 +31,9 @@ import {
AuditSearchFilter,
logFilterToAppliedFilters
} from "./AuditSearchFilter";
-import { LogsDateFilter } from "./LogsDateFilter";
import { LogsFilter } from "./LogsFilter";
import { LogsTable } from "./LogsTable";
-import {
- AuditLogDateFilterType,
- Presets,
- TAuditLogDateFilterFormData,
- TAuditLogFilterFormData
-} from "./types";
+import { Presets, TAuditLogFilterFormData } from "./types";
type Props = {
presets?: Presets;
@@ -70,8 +68,6 @@ const LogsSectionComponent = ({
actor: presets?.actorId,
eventMetadata: presets?.eventMetadata
});
- const [timezone, setTimezone] = useState<Timezone>(Timezone.Local);
-
const [searchFilters, setSearchFilters] = useState<AppliedFilter[]>(() =>
logFilterToAppliedFilters({
eventType: presets?.eventType,
@@ -81,20 +77,23 @@ const LogsSectionComponent = ({
const searchDerived = useMemo(() => appliedFiltersToLogFilter(searchFilters), [searchFilters]);
- const [dateFilter, setDateFilter] = useState<TAuditLogDateFilterFormData>(
- presets?.endDate || presets?.startDate
- ? {
- type: AuditLogDateFilterType.Absolute,
- startDate: presets?.startDate || new Date(Number(new Date()) - ms("1h")),
- endDate: presets?.endDate || new Date()
- }
- : {
- startDate: new Date(Number(new Date()) - ms("1h")),
- endDate: new Date(),
- type: AuditLogDateFilterType.Relative,
- relativeModeValue: "1h"
- }
- );
+ const hasPresetDates = Boolean(presets?.startDate || presets?.endDate);
+ const defaultCustomValue = hasPresetDates
+ ? {
+ type: DateRangeFilterType.Fixed as const,
+ startDate: presets?.startDate ?? new Date(Date.now() - 60 * 60 * 1000),
+ endDate: presets?.endDate ?? new Date()
+ }
+ : undefined;
+ const [activePreset, setActivePreset] = useState<string>(hasPresetDates ? "" : "1h");
+ const [dateRange, setDateRange] = useState<DateRangeFilterResult>({
+ startDate: presets?.startDate ?? new Date(Date.now() - 60 * 60 * 1000),
+ endDate: presets?.endDate ?? new Date(),
+ isUtc: false
+ });
+
+ const timezone = dateRange.isUtc ? Timezone.UTC : Timezone.Local;
+ const dateRangeAccent = project ? "primary" : "secondary";
useEffect(() => {
if (subscription && !subscription.auditLogs) {
@@ -142,12 +141,25 @@ const LogsSectionComponent = ({
</div>
<div className="flex flex-wrap items-center gap-2 lg:justify-end">
{showFilters && (
- <LogsDateFilter
- filter={dateFilter}
- setFilter={setDateFilter}
- timezone={timezone}
- setTimezone={setTimezone}
- />
+ <UnstableButtonGroup>
+ <DateRangeQuickPresets
+ value={activePreset}
+ onChange={(preset, result) => {
+ setActivePreset(preset);
+ setDateRange(result);
+ }}
+ accent={dateRangeAccent}
+ />
+ <DateRangeFilter
+ defaultValue={defaultCustomValue}
+ isActive={!activePreset}
+ onChange={(result) => {
+ setActivePreset("");
+ setDateRange(result);
+ }}
+ accent={dateRangeAccent}
+ />
+ </UnstableButtonGroup>
)}
</div>
</div>
@@ -180,8 +192,8 @@ const LogsSectionComponent = ({
environment: searchDerived.environment || logFilter?.environment?.slug,
secretPath: searchDerived.secretPath,
secretKey: searchDerived.secretKey,
- startDate: dateFilter?.startDate,
- endDate: dateFilter?.endDate,
+ startDate: dateRange.startDate,
+ endDate: dateRange.endDate,
limit: 15
}}
timezone={timezone}
@@ -229,12 +241,25 @@ const LogsSectionComponent = ({
)}
<div className="flex flex-wrap items-center gap-2 lg:justify-end">
{showFilters && (
- <LogsDateFilter
- filter={dateFilter}
- setFilter={setDateFilter}
- timezone={timezone}
- setTimezone={setTimezone}
- />
+ <UnstableButtonGroup>
+ <DateRangeQuickPresets
+ value={activePreset}
+ onChange={(preset, result) => {
+ setActivePreset(preset);
+ setDateRange(result);
+ }}
+ accent={dateRangeAccent}
+ />
+ <DateRangeFilter
+ defaultValue={defaultCustomValue}
+ isActive={!activePreset}
+ onChange={(result) => {
+ setActivePreset("");
+ setDateRange(result);
+ }}
+ accent={dateRangeAccent}
+ />
+ </UnstableButtonGroup>
)}
{showFilters && (
<LogsFilter presets={presets} setFilter={setLogFilter} filter={logFilter} />
@@ -251,8 +276,8 @@ const LogsSectionComponent = ({
limit: 15,
eventType: logFilter?.eventType,
userAgentType: logFilter?.userAgentType,
- startDate: dateFilter?.startDate,
- endDate: dateFilter?.endDate,
+ startDate: dateRange.startDate,
+ endDate: dateRange.endDate,
environment: logFilter?.environment?.slug,
actor: logFilter?.actor
}}