Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

feat: add custom days input to time window picker (#339)

Ege Öztaşa4e3396065f7
diff --git a/apps/start/src/components/time-window-picker.tsx b/apps/start/src/components/time-window-picker.tsx
index e611e3e07..a8bb0035f 100644
--- a/apps/start/src/components/time-window-picker.tsx
+++ b/apps/start/src/components/time-window-picker.tsx
@@ -1,9 +1,9 @@
-import { timeWindows } from '@openpanel/constants';
+import { getDefaultIntervalByDates, timeWindows } from '@openpanel/constants';
import type { IChartRange, IInterval } from '@openpanel/validation';
import { bind } from 'bind-event-listener';
-import { endOfDay, format, startOfDay } from 'date-fns';
+import { addDays, endOfDay, format, startOfDay, subDays } from 'date-fns';
import { CalendarIcon } from 'lucide-react';
-import { useCallback, useEffect, useRef } from 'react';
+import { useCallback, useEffect, useRef, useState } from 'react';
import { Button } from '@/components/ui/button';
import {
DropdownMenu,
@@ -15,6 +15,7 @@ import {
DropdownMenuShortcut,
DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu';
+import { InputEnter } from '@/components/ui/input-enter';
import { pushModal, useOnPushModal } from '@/modals';
import { cn } from '@/utils/cn';
import { shouldIgnoreKeypress } from '@/utils/should-ignore-keypress';
@@ -44,6 +45,33 @@ export function TimeWindowPicker({
isDateRangerPickerOpen.current = open;
});
const timeWindow = timeWindows[value ?? '30d'];
+ const [open, setOpen] = useState(false);
+ const [customDaysKey, setCustomDaysKey] = useState(0);
+
+ const handleCustomDays = useCallback(
+ (raw: string) => {
+ const days = Number(raw);
+ if (!Number.isInteger(days) || days < 1 || days > 365) {
+ return;
+ }
+ const now = new Date();
+ const start = startOfDay(subDays(now, days - 1));
+ const end = startOfDay(addDays(now, 1));
+ onStartDateChange(format(start, 'yyyy-MM-dd HH:mm:ss'));
+ onEndDateChange(format(end, 'yyyy-MM-dd HH:mm:ss'));
+ onChange('custom');
+ const interval = getDefaultIntervalByDates(
+ start.toISOString(),
+ end.toISOString()
+ );
+ if (interval) {
+ onIntervalChange(interval);
+ }
+ setCustomDaysKey((k) => k + 1);
+ setOpen(false);
+ },
+ [onChange, onStartDateChange, onEndDateChange, onIntervalChange]
+ );
const handleCustom = useCallback(() => {
pushModal('DateRangerPicker', {
@@ -83,7 +111,7 @@ export function TimeWindowPicker({
}, [handleCustom]);
return (
- <DropdownMenu>
+ <DropdownMenu onOpenChange={setOpen} open={open}>
<DropdownMenuTrigger asChild>
<Button
className={cn('justify-start', className)}
@@ -193,6 +221,31 @@ export function TimeWindowPicker({
<DropdownMenuSeparator />
+ <DropdownMenuGroup>
+ {/** biome-ignore lint/a11y/noNoninteractiveElementInteractions: none */}
+ {/** biome-ignore lint/a11y/noStaticElementInteractions: none */}
+ <div
+ className="flex items-center gap-4 rounded-sm px-2 py-1.5"
+ onClick={(e) => e.stopPropagation()}
+ onKeyDown={(e) => e.stopPropagation()}
+ >
+ <span className="whitespace-nowrap">Last days</span>
+ <InputEnter
+ aria-label="Number of days for custom date filter"
+ className="h-7 [appearance:textfield] [&::-webkit-inner-spin-button]:appearance-none [&::-webkit-outer-spin-button]:appearance-none"
+ inputMode="numeric"
+ key={customDaysKey}
+ max={365}
+ min={1}
+ onChangeValue={handleCustomDays}
+ placeholder="X days"
+ step={1}
+ type="number"
+ value=""
+ />
+ </div>
+ </DropdownMenuGroup>
+ <DropdownMenuSeparator />
<DropdownMenuGroup>
<DropdownMenuItem onClick={() => handleCustom()}>
{timeWindows.custom.label}
diff --git a/apps/start/src/components/ui/input-enter.tsx b/apps/start/src/components/ui/input-enter.tsx
index b0bf4c05d..16054e378 100644
--- a/apps/start/src/components/ui/input-enter.tsx
+++ b/apps/start/src/components/ui/input-enter.tsx
@@ -1,7 +1,7 @@
import { motion } from 'framer-motion';
import { AnimatePresence } from 'framer-motion';
-import { RefreshCcwIcon } from 'lucide-react';
+import { CornerDownLeftIcon } from 'lucide-react';
import { type InputHTMLAttributes, useEffect, useState } from 'react';
import { Badge } from './badge';
import { Input, type InputProps } from './input';
@@ -51,9 +51,12 @@ export function InputEnter({
exit={{ opacity: 0, scale: 0.8 }}
onClick={() => onChangeValue(internalValue)}
>
- <Badge variant="secondary">
- Press enter
- <RefreshCcwIcon className="ml-1 h-3 w-3" />
+ <Badge
+ variant="secondary"
+ className="gap-1 px-1.5 py-0 text-xs"
+ >
+ Press
+ <CornerDownLeftIcon className="h-3 w-3" />
</Badge>
</motion.button>
)}