|
1 | 1 | import * as Ariakit from "@ariakit/react"; |
2 | 2 | import type { RuntimeEnvironment } from "@trigger.dev/database"; |
| 3 | +import { |
| 4 | + startOfDay, |
| 5 | + endOfDay, |
| 6 | + startOfWeek, |
| 7 | + endOfWeek, |
| 8 | + startOfMonth, |
| 9 | + endOfMonth, |
| 10 | + startOfYear, |
| 11 | + subDays, |
| 12 | + subWeeks, |
| 13 | + subMonths, |
| 14 | + previousSaturday, |
| 15 | + isSaturday, |
| 16 | + isSunday, |
| 17 | +} from "date-fns"; |
3 | 18 | import parse from "parse-duration"; |
4 | 19 | import type { ReactNode } from "react"; |
5 | 20 | import { startTransition, useCallback, useEffect, useState } from "react"; |
6 | 21 | import { AppliedFilter } from "~/components/primitives/AppliedFilter"; |
7 | | -import { DateField } from "~/components/primitives/DateField"; |
| 22 | +import { DateTimePicker } from "~/components/primitives/DateTimePicker"; |
8 | 23 | import { DateTime } from "~/components/primitives/DateTime"; |
9 | 24 | import { Label } from "~/components/primitives/Label"; |
10 | 25 | import { RadioButtonCircle } from "~/components/primitives/RadioButton"; |
@@ -528,36 +543,126 @@ export function TimeDropdown({ |
528 | 543 | <Label variant="small"> |
529 | 544 | From <span className="text-text-dimmed">(local time)</span> |
530 | 545 | </Label> |
531 | | - <DateField |
| 546 | + <DateTimePicker |
532 | 547 | label="From time" |
533 | | - defaultValue={fromValue} |
534 | | - onValueChange={(value) => { |
| 548 | + value={fromValue} |
| 549 | + onChange={(value) => { |
535 | 550 | setFromValue(value); |
536 | 551 | setActiveSection("dateRange"); |
537 | 552 | setValidationError(null); |
538 | 553 | }} |
539 | | - granularity="second" |
| 554 | + showSeconds |
540 | 555 | showNowButton |
541 | 556 | showClearButton |
542 | | - variant="small" |
543 | 557 | /> |
544 | 558 | </div> |
545 | 559 | <div className="flex flex-col gap-1" onClick={(e) => e.stopPropagation()}> |
546 | 560 | <Label variant="small"> |
547 | 561 | To <span className="text-text-dimmed">(local time)</span> |
548 | 562 | </Label> |
549 | | - <DateField |
| 563 | + <DateTimePicker |
550 | 564 | label="To time" |
551 | | - defaultValue={toValue} |
552 | | - onValueChange={(value) => { |
| 565 | + value={toValue} |
| 566 | + onChange={(value) => { |
553 | 567 | setToValue(value); |
554 | 568 | setActiveSection("dateRange"); |
555 | 569 | setValidationError(null); |
556 | 570 | }} |
557 | | - granularity="second" |
| 571 | + showSeconds |
558 | 572 | showNowButton |
559 | 573 | showClearButton |
560 | | - variant="small" |
| 574 | + /> |
| 575 | + </div> |
| 576 | + {/* Quick select date ranges */} |
| 577 | + <div className="mt-3 grid grid-cols-3 gap-2" onClick={(e) => e.stopPropagation()}> |
| 578 | + <QuickDateButton |
| 579 | + label="Yesterday" |
| 580 | + onClick={() => { |
| 581 | + const yesterday = subDays(new Date(), 1); |
| 582 | + setFromValue(startOfDay(yesterday)); |
| 583 | + setToValue(endOfDay(yesterday)); |
| 584 | + setActiveSection("dateRange"); |
| 585 | + setValidationError(null); |
| 586 | + }} |
| 587 | + /> |
| 588 | + <QuickDateButton |
| 589 | + label="Today" |
| 590 | + onClick={() => { |
| 591 | + const today = new Date(); |
| 592 | + setFromValue(startOfDay(today)); |
| 593 | + setToValue(today); |
| 594 | + setActiveSection("dateRange"); |
| 595 | + setValidationError(null); |
| 596 | + }} |
| 597 | + /> |
| 598 | + <QuickDateButton |
| 599 | + label="This week" |
| 600 | + onClick={() => { |
| 601 | + const now = new Date(); |
| 602 | + setFromValue(startOfWeek(now, { weekStartsOn: 1 })); |
| 603 | + setToValue(now); |
| 604 | + setActiveSection("dateRange"); |
| 605 | + setValidationError(null); |
| 606 | + }} |
| 607 | + /> |
| 608 | + <QuickDateButton |
| 609 | + label="Last week" |
| 610 | + onClick={() => { |
| 611 | + const lastWeek = subWeeks(new Date(), 1); |
| 612 | + setFromValue(startOfWeek(lastWeek, { weekStartsOn: 1 })); |
| 613 | + setToValue(endOfWeek(lastWeek, { weekStartsOn: 1 })); |
| 614 | + setActiveSection("dateRange"); |
| 615 | + setValidationError(null); |
| 616 | + }} |
| 617 | + /> |
| 618 | + <QuickDateButton |
| 619 | + label="Last weekend" |
| 620 | + onClick={() => { |
| 621 | + const now = new Date(); |
| 622 | + let saturday: Date; |
| 623 | + if (isSaturday(now)) { |
| 624 | + saturday = subDays(now, 7); |
| 625 | + } else if (isSunday(now)) { |
| 626 | + saturday = subDays(now, 8); |
| 627 | + } else { |
| 628 | + saturday = previousSaturday(now); |
| 629 | + } |
| 630 | + const sunday = endOfDay(subDays(saturday, -1)); |
| 631 | + setFromValue(startOfDay(saturday)); |
| 632 | + setToValue(sunday); |
| 633 | + setActiveSection("dateRange"); |
| 634 | + setValidationError(null); |
| 635 | + }} |
| 636 | + /> |
| 637 | + <QuickDateButton |
| 638 | + label="This month" |
| 639 | + onClick={() => { |
| 640 | + const now = new Date(); |
| 641 | + setFromValue(startOfMonth(now)); |
| 642 | + setToValue(now); |
| 643 | + setActiveSection("dateRange"); |
| 644 | + setValidationError(null); |
| 645 | + }} |
| 646 | + /> |
| 647 | + <QuickDateButton |
| 648 | + label="Last month" |
| 649 | + onClick={() => { |
| 650 | + const lastMonth = subMonths(new Date(), 1); |
| 651 | + setFromValue(startOfMonth(lastMonth)); |
| 652 | + setToValue(endOfMonth(lastMonth)); |
| 653 | + setActiveSection("dateRange"); |
| 654 | + setValidationError(null); |
| 655 | + }} |
| 656 | + /> |
| 657 | + <QuickDateButton |
| 658 | + label="Year to date" |
| 659 | + onClick={() => { |
| 660 | + const now = new Date(); |
| 661 | + setFromValue(startOfYear(now)); |
| 662 | + setToValue(now); |
| 663 | + setActiveSection("dateRange"); |
| 664 | + setValidationError(null); |
| 665 | + }} |
561 | 666 | /> |
562 | 667 | </div> |
563 | 668 | {validationError && activeSection === "dateRange" && ( |
@@ -628,3 +733,15 @@ export function dateFromString(value: string | undefined | null): Date | undefin |
628 | 733 |
|
629 | 734 | return new Date(value); |
630 | 735 | } |
| 736 | + |
| 737 | +function QuickDateButton({ label, onClick }: { label: string; onClick: () => void }) { |
| 738 | + return ( |
| 739 | + <button |
| 740 | + type="button" |
| 741 | + onClick={onClick} |
| 742 | + className="rounded border border-charcoal-650 bg-charcoal-750 px-2 py-0.5 text-xs text-text-dimmed transition hover:border-charcoal-600 hover:text-text-bright" |
| 743 | + > |
| 744 | + {label} |
| 745 | + </button> |
| 746 | + ); |
| 747 | +} |
0 commit comments