-
Notifications
You must be signed in to change notification settings - Fork 3
claude/shared-date-range-filter-IAWME #116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8efe706
feat: create shared DateRangeFilter component
claude 24c4c75
fix(date-range-filter): use from/to as single source of truth
joaopcm 959e4c5
fix(date-range-filter): require two clicks before closing calendar
joaopcm 6e73266
refactor date range filter to use daterange type and simplify state i…
joaopcm ca9fef6
refactor remove unused utils import and simplify button classname
joaopcm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,94 +1,28 @@ | ||
| 'use client' | ||
|
|
||
| import { Button } from '@/components/ui/button' | ||
| import { Calendar } from '@/components/ui/calendar' | ||
| import { | ||
| Popover, | ||
| PopoverContent, | ||
| PopoverTrigger, | ||
| } from '@/components/ui/popover' | ||
| import { cn } from '@/lib/utils' | ||
| import { format } from 'date-fns' | ||
| import { CalendarIcon } from 'lucide-react' | ||
| import { useMemo, useState } from 'react' | ||
| import { | ||
| DATE_PRESETS, | ||
| type DatePreset, | ||
| getPresetLabel, | ||
| useDashboardFilters, | ||
| } from './search-params' | ||
| DateRangeFilter, | ||
| } from '@/components/logged-in/shared/date-range-filter' | ||
| import { useDashboardFilters } from './search-params' | ||
|
|
||
| export function DashboardFilters() { | ||
| const { filters, setFilters } = useDashboardFilters() | ||
| const [isCalendarOpen, setIsCalendarOpen] = useState(false) | ||
|
|
||
| const selectedRange = useMemo( | ||
| () => ({ | ||
| from: filters.from ? new Date(filters.from) : undefined, | ||
| to: filters.to ? new Date(filters.to) : undefined, | ||
| }), | ||
| [filters.from, filters.to], | ||
| ) | ||
|
|
||
| const handlePresetClick = (preset: DatePreset) => { | ||
| if (preset === 'custom') { | ||
| setIsCalendarOpen(true) | ||
| return | ||
| } | ||
| setFilters({ preset, from: null, to: null }) | ||
| } | ||
|
|
||
| const handleDateSelect = (range: { from?: Date; to?: Date } | undefined) => { | ||
| if (range?.from) { | ||
| setFilters({ from: range.from, preset: 'custom' }) | ||
| } | ||
| if (range?.to) { | ||
| setFilters({ to: range.to, preset: 'custom' }) | ||
| setIsCalendarOpen(false) | ||
| } | ||
| const handleFilterChange = (updates: { | ||
| preset?: DatePreset | ||
| from?: Date | null | ||
| to?: Date | null | ||
| }) => { | ||
| setFilters(updates) | ||
| } | ||
|
|
||
| return ( | ||
| <div className="flex flex-wrap items-center gap-2"> | ||
| {DATE_PRESETS.filter((p) => p !== 'custom').map((preset) => ( | ||
| <Button | ||
| key={preset} | ||
| variant={filters.preset === preset ? 'default' : 'outline'} | ||
| size="sm" | ||
| onClick={() => handlePresetClick(preset)} | ||
| > | ||
| {getPresetLabel(preset)} | ||
| </Button> | ||
| ))} | ||
|
|
||
| <Popover open={isCalendarOpen} onOpenChange={setIsCalendarOpen}> | ||
| <PopoverTrigger asChild> | ||
| <Button | ||
| variant={filters.preset === 'custom' ? 'default' : 'outline'} | ||
| size="sm" | ||
| className={cn( | ||
| 'min-w-[140px] justify-start text-left font-normal', | ||
| filters.preset !== 'custom' && 'text-muted-foreground', | ||
| )} | ||
| > | ||
| <CalendarIcon className="mr-2 size-4" /> | ||
| {filters.preset === 'custom' && filters.from && filters.to ? ( | ||
| `${format(filters.from, 'MMM d')} - ${format(filters.to, 'MMM d')}` | ||
| ) : ( | ||
| <span>Pick a date range</span> | ||
| )} | ||
| </Button> | ||
| </PopoverTrigger> | ||
| <PopoverContent className="w-auto p-0" align="start"> | ||
| <Calendar | ||
| mode="range" | ||
| selected={selectedRange} | ||
| onSelect={handleDateSelect} | ||
| captionLayout="dropdown" | ||
| numberOfMonths={2} | ||
| /> | ||
| </PopoverContent> | ||
| </Popover> | ||
| </div> | ||
| <DateRangeFilter | ||
| preset={filters.preset} | ||
| from={filters.from} | ||
| to={filters.to} | ||
| onFilterChange={handleFilterChange} | ||
| /> | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,123 @@ | ||||||
| 'use client' | ||||||
|
|
||||||
| import { Button } from '@/components/ui/button' | ||||||
| import { Calendar } from '@/components/ui/calendar' | ||||||
| import { | ||||||
| Popover, | ||||||
| PopoverContent, | ||||||
| PopoverTrigger, | ||||||
| } from '@/components/ui/popover' | ||||||
| import { cn } from '@/lib/utils' | ||||||
| import { format } from 'date-fns' | ||||||
| import { CalendarIcon } from 'lucide-react' | ||||||
| import { useMemo, useState } from 'react' | ||||||
|
|
||||||
| export const DATE_PRESETS = ['7d', '30d', '3m', '6m', 'ytd', 'custom'] as const | ||||||
| export type DatePreset = (typeof DATE_PRESETS)[number] | ||||||
|
|
||||||
| export function getPresetLabel(preset: DatePreset): string { | ||||||
| switch (preset) { | ||||||
| case '7d': | ||||||
| return 'Last 7 days' | ||||||
| case '30d': | ||||||
| return 'Last 30 days' | ||||||
| case '3m': | ||||||
| return 'Last 3 months' | ||||||
| case '6m': | ||||||
| return 'Last 6 months' | ||||||
| case 'ytd': | ||||||
| return 'Year to date' | ||||||
| case 'custom': | ||||||
| return 'Custom' | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| interface DateRangeFilterProps { | ||||||
| preset: DatePreset | ||||||
| from: Date | null | ||||||
| to: Date | null | ||||||
| onFilterChange: (updates: { | ||||||
| preset?: DatePreset | ||||||
| from?: Date | null | ||||||
| to?: Date | null | ||||||
| }) => void | ||||||
| } | ||||||
|
|
||||||
| export function DateRangeFilter({ | ||||||
| preset, | ||||||
| from, | ||||||
| to, | ||||||
| onFilterChange, | ||||||
| }: DateRangeFilterProps) { | ||||||
| const [isCalendarOpen, setIsCalendarOpen] = useState(false) | ||||||
|
|
||||||
| const selectedRange = useMemo( | ||||||
| () => ({ | ||||||
| from: from ? new Date(from) : undefined, | ||||||
| to: to ? new Date(to) : undefined, | ||||||
| }), | ||||||
| [from, to], | ||||||
| ) | ||||||
|
|
||||||
| const handlePresetClick = (selectedPreset: DatePreset) => { | ||||||
| if (selectedPreset === 'custom') { | ||||||
| setIsCalendarOpen(true) | ||||||
| return | ||||||
| } | ||||||
| onFilterChange({ preset: selectedPreset, from: null, to: null }) | ||||||
| } | ||||||
|
|
||||||
| const handleDateSelect = (range: { from?: Date; to?: Date } | undefined) => { | ||||||
| if (range?.from) { | ||||||
| onFilterChange({ from: range.from, preset: 'custom' }) | ||||||
| } | ||||||
| if (range?.to) { | ||||||
| onFilterChange({ to: range.to, preset: 'custom' }) | ||||||
| setIsCalendarOpen(false) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| return ( | ||||||
| <div className="flex flex-wrap items-center gap-2"> | ||||||
| {DATE_PRESETS.filter((p) => p !== 'custom').map((presetOption) => ( | ||||||
| <Button | ||||||
| key={presetOption} | ||||||
| variant={preset === presetOption ? 'default' : 'outline'} | ||||||
| size="sm" | ||||||
| onClick={() => handlePresetClick(presetOption)} | ||||||
| > | ||||||
| {getPresetLabel(presetOption)} | ||||||
| </Button> | ||||||
| ))} | ||||||
|
|
||||||
| <Popover open={isCalendarOpen} onOpenChange={setIsCalendarOpen}> | ||||||
| <PopoverTrigger asChild> | ||||||
| <Button | ||||||
| variant={preset === 'custom' ? 'default' : 'outline'} | ||||||
| size="sm" | ||||||
| className={cn( | ||||||
| 'min-w-[140px] justify-start text-left font-normal', | ||||||
| preset !== 'custom' && 'text-muted-foreground', | ||||||
| )} | ||||||
| > | ||||||
| <CalendarIcon className="mr-2 size-4" /> | ||||||
| {preset === 'custom' && from && to ? ( | ||||||
| `${format(from, 'MMM d')} - ${format(to, 'MMM d')}` | ||||||
| ) : ( | ||||||
| <span>Pick a date range</span> | ||||||
|
||||||
| <span>Pick a date range</span> | |
| <CalendarIcon className="size-4 mr-2" /> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When selecting a custom date range, only the 'to' date closes the calendar (line 76). If a user selects a 'from' date but doesn't select a 'to' date, the calendar remains open and the preset is set to 'custom' but with incomplete date information. Consider either:
This could lead to a confusing UX where the preset shows 'custom' but only one date is set.