From 97e2972ad727c770d0f6eb4abf45b4cf6c47ca1e Mon Sep 17 00:00:00 2001 From: Innei Date: Thu, 25 Sep 2025 00:51:31 +0800 Subject: [PATCH] feat(ai-chat): add custom date range picker to context menu - Introduced a new `CustomRangePicker` component within the `ContextMenuContent` to allow users to select custom date ranges. - Enhanced the `DateTimePicker` to support range selection mode, enabling users to pick start and end dates. - Integrated relative date options into the context menu, improving the flexibility of date mentions in the AI chat interface. - Updated localization to ensure new date range options are translatable. These changes aim to enhance user experience by providing more options for date selection in the AI chat context menu. Signed-off-by: Innei --- .../context-bar/menus/ContextMenuContent.tsx | 86 +++++++++++ .../src/ui/input/DateTimePicker.tsx | 141 +++++++++++++++--- 2 files changed, 209 insertions(+), 18 deletions(-) diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/components/context-bar/menus/ContextMenuContent.tsx b/apps/desktop/layer/renderer/src/modules/ai-chat/components/context-bar/menus/ContextMenuContent.tsx index 26e84e174..8631ce5b7 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/components/context-bar/menus/ContextMenuContent.tsx +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/components/context-bar/menus/ContextMenuContent.tsx @@ -1,18 +1,27 @@ +import { DateTimePicker } from "@follow/components/ui/input/index.js" +import dayjs from "dayjs" import type { FC } from "react" +import { useState } from "react" +import { useTranslation } from "react-i18next" import { DropdownMenuContent, + DropdownMenuItem, DropdownMenuSeparator, DropdownMenuSub, DropdownMenuSubContent, DropdownMenuSubTrigger, } from "~/components/ui/dropdown-menu/dropdown-menu" +import { RELATIVE_DATE_DEFINITIONS } from "~/modules/ai-chat/editor/plugins/mention/hooks/dateMentionConfig" +import { formatRangeValue } from "~/modules/ai-chat/editor/plugins/mention/hooks/dateMentionUtils" import { useChatBlockActions } from "~/modules/ai-chat/store/hooks" import { CurrentFeedEntriesPickerList, FeedPickerList, RecentEntriesPickerList } from "../pickers" export const ContextMenuContent: FC = () => { const blockActions = useChatBlockActions() + const { t } = useTranslation("ai") + const today = dayjs().startOf("day") return ( @@ -66,6 +75,83 @@ export const ContextMenuContent: FC = () => { /> + + {/* Reference Date */} + + + + Reference Date + + + {/* Presets based on RELATIVE_DATE_DEFINITIONS */} + {RELATIVE_DATE_DEFINITIONS.map((def) => { + const range = def.range(today) + if (!range) return null + const label = t(def.labelKey) + const value = formatRangeValue(range) + return ( + blockActions.addBlock({ type: "referDate", value })} + > + {String(label)} + + ) + })} + + + + {/* Custom range (opens a small inline range picker) */} + blockActions.addBlock({ type: "referDate", value })} + /> + + ) } + +function CustomRangePicker({ onConfirm }: { onConfirm: (value: string) => void }) { + const [range, setRange] = useState<{ start?: string; end?: string }>({}) + const buildValue = (): string | null => { + if (!range.start || !range.end) return null + return formatRangeValue({ + start: dayjs(range.start).startOf("day"), + end: dayjs(range.end).startOf("day"), + }) + } + + return ( +
+
Custom range
+ setRange(next)} + rangePlaceholder="Select range" + contentClassName="z-[999]" + /> +
+ + +
+
+ ) +} diff --git a/packages/internal/components/src/ui/input/DateTimePicker.tsx b/packages/internal/components/src/ui/input/DateTimePicker.tsx index 3e337e115..5444356f6 100644 --- a/packages/internal/components/src/ui/input/DateTimePicker.tsx +++ b/packages/internal/components/src/ui/input/DateTimePicker.tsx @@ -1,7 +1,7 @@ -import { cn } from "@follow/utils/utils" +import { clsx, cn } from "@follow/utils/utils" import { useDatePicker } from "@rehookify/datepicker" import dayjs from "dayjs" -import { memo, useMemo, useState } from "react" +import { memo, useEffect, useMemo, useState } from "react" import * as React from "react" import { Button } from "../button" @@ -19,6 +19,16 @@ export interface DateTimePickerProps { placeholder?: string className?: string disabled?: boolean + /** Picker mode. Default is single */ + mode?: "single" | "range" + /** Range value when mode is range */ + rangeValue?: { start?: string; end?: string } + /** Called with new range when mode is range */ + onRangeChange?: (value: { start?: string; end?: string }) => void + /** Placeholder for range mode */ + rangePlaceholder?: string + /** Class name for the content */ + contentClassName?: string } /** @@ -26,7 +36,19 @@ export interface DateTimePickerProps { * and custom time selection components. */ export const DateTimePicker = memo( - ({ value, onChange, minDate, placeholder = "Select date & time", className, disabled }) => { + ({ + value, + onChange, + minDate, + placeholder = "Select date & time", + className, + disabled, + mode = "single", + rangeValue, + onRangeChange, + rangePlaceholder = "Select date range", + contentClassName, + }) => { const [isOpen, setIsOpen] = useState(false) const [viewMode, setViewMode] = useState<"days" | "months" | "years">("days") @@ -35,6 +57,21 @@ export const DateTimePicker = memo( return dayjs(value) }, [value]) + const isRangeMode = mode === "range" + + // Local state for range mode + const [rangeStart, setRangeStart] = useState(() => + rangeValue?.start ? dayjs(rangeValue.start) : null, + ) + const [rangeEnd, setRangeEnd] = useState(() => + rangeValue?.end ? dayjs(rangeValue.end) : null, + ) + + useEffect(() => { + setRangeStart(rangeValue?.start ? dayjs(rangeValue.start) : null) + setRangeEnd(rangeValue?.end ? dayjs(rangeValue.end) : null) + }, [rangeValue?.start, rangeValue?.end]) + const minDateObj = useMemo(() => { return minDate ? dayjs(minDate) : dayjs() }, [minDate]) @@ -43,8 +80,16 @@ export const DateTimePicker = memo( data: { weekDays, calendars, months, years }, propGetters: { dayButton, addOffset, subtractOffset, monthButton, yearButton }, } = useDatePicker({ - selectedDates: value ? [new Date(value)] : [], + selectedDates: isRangeMode + ? ([rangeStart?.toDate(), rangeEnd?.toDate()].filter(Boolean) as Date[]) + : value + ? [new Date(value)] + : [], onDatesChange: (dates) => { + if (isRangeMode) { + // Ignore internal range selection; we handle it per-click + return + } if (dates.length > 0 && dates[0]) { const selectedDate = dayjs(dates[0]) const newDateTime = selectedDate @@ -79,6 +124,40 @@ export const DateTimePicker = memo( onChange?.(newDateTime.toISOString()) } + const formatRangeButtonLabel = (): string => { + if (!rangeStart && !rangeEnd) return rangePlaceholder + if (rangeStart && !rangeEnd) return `${rangeStart.format("MMM DD, YYYY")} – ...` + if (!rangeStart && rangeEnd) return `... – ${rangeEnd.format("MMM DD, YYYY")}` + return `${rangeStart!.format("MMM DD, YYYY")} – ${rangeEnd!.format("MMM DD, YYYY")}` + } + + const handleDayClickRange = (clickedDate: Date) => { + const clicked = dayjs(clickedDate).startOf("day") + if (clicked.isBefore(minDateObj)) { + return + } + + if (!rangeStart || (rangeStart && rangeEnd)) { + setRangeStart(clicked) + setRangeEnd(null) + return + } + + if (rangeStart && !rangeEnd) { + if (clicked.isBefore(rangeStart)) { + setRangeEnd(rangeStart) + setRangeStart(clicked) + onRangeChange?.({ start: clicked.toISOString(), end: rangeStart.toISOString() }) + setIsOpen(false) + return + } + + setRangeEnd(clicked) + onRangeChange?.({ start: rangeStart.toISOString(), end: clicked.toISOString() }) + setIsOpen(false) + } + } + return ( @@ -87,15 +166,25 @@ export const DateTimePicker = memo( disabled={disabled} buttonClassName={cn( "w-full justify-start text-left font-normal px-2.5", - !value && "text-text-tertiary", + isRangeMode + ? !rangeStart && !rangeEnd && "text-text-tertiary" + : !value && "text-text-tertiary", className, )} > - {value ? currentDateTime.format("MMM DD, YYYY HH:mm") : placeholder} + {isRangeMode + ? formatRangeButtonLabel() + : value + ? currentDateTime.format("MMM DD, YYYY HH:mm") + : placeholder} - + +
{/* Calendar Header */}
@@ -153,7 +242,12 @@ export const DateTimePicker = memo(
{calendar.days.map((day) => { const dayProps = dayButton(day) - const isSelected = day.selected + const isSelected = isRangeMode + ? !!( + (rangeStart && dayjs(day.$date).isSame(rangeStart, "day")) || + (rangeEnd && dayjs(day.$date).isSame(rangeEnd, "day")) + ) + : day.selected const isToday = day.now const isOtherMonth = !day.inCurrentMonth const isDisabled = day.disabled @@ -164,6 +258,15 @@ export const DateTimePicker = memo( variant="ghost" size="sm" {...dayProps} + onClick={(ev) => { + if (isRangeMode) { + ev.preventDefault() + ev.stopPropagation() + handleDayClickRange(day.$date) + } else { + dayProps.onClick?.(ev) + } + }} buttonClassName={cn( "size-7 p-0 font-normal text-xs rounded-[4px]", isSelected && "bg-accent hover:bg-accent/90 text-white", @@ -252,17 +355,19 @@ export const DateTimePicker = memo(
)} - {/* Time Selection */} -
-
- Time - + {/* Time Selection (only single mode) */} + {!isRangeMode && ( +
+
+ Time + +
-
+ )}