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 <tukon479@gmail.com>
This commit is contained in:
+86
@@ -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 (
|
||||
<DropdownMenuContent align="start">
|
||||
@@ -66,6 +75,83 @@ export const ContextMenuContent: FC = () => {
|
||||
/>
|
||||
</DropdownMenuSubContent>
|
||||
</DropdownMenuSub>
|
||||
|
||||
{/* Reference Date */}
|
||||
<DropdownMenuSub>
|
||||
<DropdownMenuSubTrigger>
|
||||
<i className="i-mgc-calendar-time-add-cute-re mr-1.5 size-4" />
|
||||
Reference Date
|
||||
</DropdownMenuSubTrigger>
|
||||
<DropdownMenuSubContent className="w-64">
|
||||
{/* 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 (
|
||||
<DropdownMenuItem
|
||||
key={def.id}
|
||||
className="text-xs"
|
||||
onClick={() => blockActions.addBlock({ type: "referDate", value })}
|
||||
>
|
||||
{String(label)}
|
||||
</DropdownMenuItem>
|
||||
)
|
||||
})}
|
||||
|
||||
<DropdownMenuSeparator />
|
||||
|
||||
{/* Custom range (opens a small inline range picker) */}
|
||||
<CustomRangePicker
|
||||
onConfirm={(value) => blockActions.addBlock({ type: "referDate", value })}
|
||||
/>
|
||||
</DropdownMenuSubContent>
|
||||
</DropdownMenuSub>
|
||||
</DropdownMenuContent>
|
||||
)
|
||||
}
|
||||
|
||||
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 (
|
||||
<div className="space-y-2 p-1">
|
||||
<div className="text-text-secondary px-1 text-xs">Custom range</div>
|
||||
<DateTimePicker
|
||||
mode="range"
|
||||
rangeValue={range}
|
||||
onRangeChange={(next) => setRange(next)}
|
||||
rangePlaceholder="Select range"
|
||||
contentClassName="z-[999]"
|
||||
/>
|
||||
<div className="flex items-center justify-end gap-2">
|
||||
<button
|
||||
type="button"
|
||||
className="bg-material-opaque hover:bg-fill-tertiary text-text-secondary hover:text-text rounded px-2 py-1 text-xs"
|
||||
onClick={() => setRange({})}
|
||||
>
|
||||
Clear
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
className="bg-accent hover:bg-accent/90 rounded px-2 py-1 text-xs text-white disabled:opacity-50"
|
||||
disabled={!range.start || !range.end}
|
||||
onClick={() => {
|
||||
const value = buildValue()
|
||||
if (value) onConfirm(value)
|
||||
}}
|
||||
>
|
||||
Apply
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -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<DateTimePickerProps>(
|
||||
({ 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<DateTimePickerProps>(
|
||||
return dayjs(value)
|
||||
}, [value])
|
||||
|
||||
const isRangeMode = mode === "range"
|
||||
|
||||
// Local state for range mode
|
||||
const [rangeStart, setRangeStart] = useState<dayjs.Dayjs | null>(() =>
|
||||
rangeValue?.start ? dayjs(rangeValue.start) : null,
|
||||
)
|
||||
const [rangeEnd, setRangeEnd] = useState<dayjs.Dayjs | null>(() =>
|
||||
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<DateTimePickerProps>(
|
||||
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<DateTimePickerProps>(
|
||||
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 (
|
||||
<Popover open={isOpen} onOpenChange={setIsOpen}>
|
||||
<PopoverTrigger asChild>
|
||||
@@ -87,15 +166,25 @@ export const DateTimePicker = memo<DateTimePickerProps>(
|
||||
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,
|
||||
)}
|
||||
>
|
||||
<i className="i-mgc-calendar-time-add-cute-re mr-2 size-4" />
|
||||
{value ? currentDateTime.format("MMM DD, YYYY HH:mm") : placeholder}
|
||||
{isRangeMode
|
||||
? formatRangeButtonLabel()
|
||||
: value
|
||||
? currentDateTime.format("MMM DD, YYYY HH:mm")
|
||||
: placeholder}
|
||||
</Button>
|
||||
</PopoverTrigger>
|
||||
<PopoverContent className="w-auto min-w-[280px] rounded-[6px] border p-0" align="start">
|
||||
|
||||
<PopoverContent
|
||||
className={clsx("w-auto min-w-[280px] rounded-[6px] border p-0", contentClassName)}
|
||||
align="start"
|
||||
>
|
||||
<div className="p-2">
|
||||
{/* Calendar Header */}
|
||||
<div className="mb-2 flex items-center justify-between">
|
||||
@@ -153,7 +242,12 @@ export const DateTimePicker = memo<DateTimePickerProps>(
|
||||
<div className="mb-2 grid grid-cols-7 gap-0.5">
|
||||
{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<DateTimePickerProps>(
|
||||
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<DateTimePickerProps>(
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Time Selection */}
|
||||
<div className="border-border -mx-2 border-t px-2 pt-2">
|
||||
<div className="flex items-center justify-between">
|
||||
<span className="text-text-secondary text-xs font-medium">Time</span>
|
||||
<TimeSelect
|
||||
value={currentDateTime.format("HH:mm")}
|
||||
onChange={handleTimeChange}
|
||||
className="gap-0.5"
|
||||
/>
|
||||
{/* Time Selection (only single mode) */}
|
||||
{!isRangeMode && (
|
||||
<div className="border-border -mx-2 border-t px-2 pt-2">
|
||||
<div className="flex items-center justify-between">
|
||||
<span className="text-text-secondary text-xs font-medium">Time</span>
|
||||
<TimeSelect
|
||||
value={currentDateTime.format("HH:mm")}
|
||||
onChange={handleTimeChange}
|
||||
className="gap-0.5"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
|
||||
Reference in New Issue
Block a user