diff --git a/pipeline-ui/package-lock.json b/pipeline-ui/package-lock.json index bd2327ba..86a7d9b0 100644 --- a/pipeline-ui/package-lock.json +++ b/pipeline-ui/package-lock.json @@ -14,9 +14,11 @@ "axios": "^1.20.0", "class-variance-authority": "^0.7.1", "clsx": "^2.1.1", + "date-fns": "^4.4.0", "keycloak-js": "^26.2.4", "lucide-react": "^0.577.0", "react": "^19.2.0", + "react-day-picker": "^10.0.2", "react-dom": "^19.2.0", "react-router-dom": "^7.13.1", "tailwind-merge": "^3.5.0", @@ -523,6 +525,12 @@ "node": ">=20.19.0" } }, + "node_modules/@date-fns/tz": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/@date-fns/tz/-/tz-1.5.0.tgz", + "integrity": "sha512-lwYN/vDPeNRULcepoE/LO2Pgx+7/RV+S9ARfbc9lr2DtGkOD7pAiruHvbR1RX3Qyf6ja47EWJDMsNK5vK08DJg==", + "license": "MIT" + }, "node_modules/@esbuild/aix-ppc64": { "version": "0.25.12", "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.25.12.tgz", @@ -4645,6 +4653,16 @@ "node": "^20.19.0 || ^22.12.0 || >=24.0.0" } }, + "node_modules/date-fns": { + "version": "4.4.0", + "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-4.4.0.tgz", + "integrity": "sha512-+1UMbeh68lH1SegH83CGWwpb6OHHbpSgr3+s5Eww5M4CAgswBpoWS0AjTOfEJ33HiYKz1hdj/KTFprzXHmq/6w==", + "license": "MIT", + "funding": { + "type": "github", + "url": "https://github.com/sponsors/kossnocorp" + } + }, "node_modules/debug": { "version": "4.4.3", "resolved": "https://registry.npmjs.org/debug/-/debug-4.4.3.tgz", @@ -7542,6 +7560,32 @@ "node": ">=0.10.0" } }, + "node_modules/react-day-picker": { + "version": "10.0.2", + "resolved": "https://registry.npmjs.org/react-day-picker/-/react-day-picker-10.0.2.tgz", + "integrity": "sha512-QetiKtZWE9Uaw/pM/SYNv6WPYOTRnSJ2puL0GgwkmUFMIwiY0dtjH1W4B7DOVXVNQM0ZOClGpH/heOfBSzWeiA==", + "license": "MIT", + "dependencies": { + "@date-fns/tz": "^1.4.1", + "date-fns": "^4.1.0" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "type": "individual", + "url": "https://github.com/sponsors/gpbl" + }, + "peerDependencies": { + "@types/react": ">=16.8.0", + "react": ">=16.8.0" + }, + "peerDependenciesMeta": { + "@types/react": { + "optional": true + } + } + }, "node_modules/react-dom": { "version": "19.2.0", "resolved": "https://registry.npmjs.org/react-dom/-/react-dom-19.2.0.tgz", @@ -9445,6 +9489,11 @@ "integrity": "sha512-QxULHAm7cNu72w97JUNCBFODFaXpbDg+dP8b/oWFAZ2MTRppA3U00Y2L1HqaS4J6yBqxwa/Y3nMBaxVKbB/NsA==", "dev": true }, + "@date-fns/tz": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/@date-fns/tz/-/tz-1.5.0.tgz", + "integrity": "sha512-lwYN/vDPeNRULcepoE/LO2Pgx+7/RV+S9ARfbc9lr2DtGkOD7pAiruHvbR1RX3Qyf6ja47EWJDMsNK5vK08DJg==" + }, "@esbuild/aix-ppc64": { "version": "0.25.12", "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.25.12.tgz", @@ -11952,6 +12001,11 @@ "whatwg-url": "^16.0.0" } }, + "date-fns": { + "version": "4.4.0", + "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-4.4.0.tgz", + "integrity": "sha512-+1UMbeh68lH1SegH83CGWwpb6OHHbpSgr3+s5Eww5M4CAgswBpoWS0AjTOfEJ33HiYKz1hdj/KTFprzXHmq/6w==" + }, "debug": { "version": "4.4.3", "resolved": "https://registry.npmjs.org/debug/-/debug-4.4.3.tgz", @@ -13909,6 +13963,15 @@ "resolved": "https://registry.npmjs.org/react/-/react-19.2.0.tgz", "integrity": "sha512-tmbWg6W31tQLeB5cdIBOicJDJRR2KzXsV7uSK9iNfLWQ5bIZfxuPEHp7M8wiHyHnn0DD1i7w3Zmin0FtkrwoCQ==" }, + "react-day-picker": { + "version": "10.0.2", + "resolved": "https://registry.npmjs.org/react-day-picker/-/react-day-picker-10.0.2.tgz", + "integrity": "sha512-QetiKtZWE9Uaw/pM/SYNv6WPYOTRnSJ2puL0GgwkmUFMIwiY0dtjH1W4B7DOVXVNQM0ZOClGpH/heOfBSzWeiA==", + "requires": { + "@date-fns/tz": "^1.4.1", + "date-fns": "^4.1.0" + } + }, "react-dom": { "version": "19.2.0", "resolved": "https://registry.npmjs.org/react-dom/-/react-dom-19.2.0.tgz", diff --git a/pipeline-ui/package.json b/pipeline-ui/package.json index 448f5f95..7f9f3454 100644 --- a/pipeline-ui/package.json +++ b/pipeline-ui/package.json @@ -25,9 +25,11 @@ "axios": "^1.20.0", "class-variance-authority": "^0.7.1", "clsx": "^2.1.1", + "date-fns": "^4.4.0", "keycloak-js": "^26.2.4", "lucide-react": "^0.577.0", "react": "^19.2.0", + "react-day-picker": "^10.0.2", "react-dom": "^19.2.0", "react-router-dom": "^7.13.1", "tailwind-merge": "^3.5.0", @@ -60,4 +62,4 @@ "vite": "^7.2.4", "vitest": "^4.0.18" } -} \ No newline at end of file +} diff --git a/pipeline-ui/src/components/common/calendarFilter.tsx b/pipeline-ui/src/components/common/calendarFilter.tsx new file mode 100644 index 00000000..48032d96 --- /dev/null +++ b/pipeline-ui/src/components/common/calendarFilter.tsx @@ -0,0 +1,179 @@ +import { useEffect, useRef, useState } from "react" +import type { DateRange } from "react-day-picker" +import { Calendar } from "../ui/calendar" +import { Button } from "../ui/button" +import { CalendarDays } from "lucide-react" +import { cn } from "../../lib/utils" + +/** + * A filter for builds based on their start date. + * - "today" matches builds that started today. + * - "yesterday" matches builds that started yesterday. + * - "range" matches builds that started between two dates (inclusive). + * - "specific" matches builds that started on a specific date. + */ + +// TODO: Refactor this to use a more generic date filter type that can be used in other places (e.g. for filtering pipelines by creation date). +// It's very extensive now, let's change later + +export type DateFilter = + | { mode: "today" } + | { mode: "yesterday" } + | { mode: "range"; from: Date; to: Date } + | { mode: "specific"; date: Date } + +/** Returns true when `date` satisfies the given filter. */ +export function matchesDateFilter(date: Date, filter: DateFilter | null): boolean { + if (!filter) return true + + const startOfDay = (d: Date) => new Date(d.getFullYear(), d.getMonth(), d.getDate()) + const target = startOfDay(date) + const today = startOfDay(new Date()) + + const matchers: Record boolean> = { + today: () => target.getTime() === today.getTime(), + yesterday: () => target.getTime() === today.getTime() - 864e5, + range: () => filter.mode === "range" && date >= filter.from && date <= filter.to, + specific: () => filter.mode === "specific" && target.getTime() === startOfDay(filter.date).getTime(), + } + + return matchers[filter.mode]() +} + +export function dateFilterLabel(filter: DateFilter | null): string { + if (!filter) return "Filter by Date" + const fmt = (d: Date) => d.toLocaleDateString() + const labels: Record string> = { + today: () => "Today", + yesterday: () => "Yesterday", + range: () => filter.mode === "range" ? `${fmt(filter.from)} – ${fmt(filter.to)}` : "", + specific: () => filter.mode === "specific" ? fmt(filter.date) : "", + } + return labels[filter.mode]() +} + + +type FilterMode = DateFilter["mode"] + +const TABS: { key: FilterMode; label: string }[] = [ + { key: "today", label: "Today" }, + { key: "yesterday", label: "Yesterday" }, + { key: "range", label: "Date Range" }, + { key: "specific", label: "Specific Date" }, +] + +interface CalendarFilterProps { + onFilter: (filter: DateFilter | null) => void + activeFilter?: DateFilter | null +} + +export default function CalendarFilter({ onFilter, activeFilter = null }: CalendarFilterProps) { + const [open, setOpen] = useState(false) + const [mode, setMode] = useState("today") + const [range, setRange] = useState(undefined) + const [specific, setSpecific] = useState(undefined) + + const buttonRef = useRef(null) + const popoverRef = useRef(null) + + // Close the popover when clicking outside of it + useEffect(() => { + if (!open) return + const handler = (e: MouseEvent) => { + const outside = (ref: React.RefObject) => + ref.current && !ref.current.contains(e.target as Node) + if (outside(popoverRef) && outside(buttonRef)) setOpen(false) + } + document.addEventListener("mousedown", handler) + return () => document.removeEventListener("mousedown", handler) + }, [open]) + + // Build a DateFilter object based on the current state of the component + function buildFilter(): DateFilter | null { + if (mode === "today") return { mode: "today" } + if (mode === "yesterday") return { mode: "yesterday" } + if (mode === "range" && range?.from && range?.to) return { mode: "range", from: range.from, to: range.to } + if (mode === "specific" && specific) return { mode: "specific", date: specific } + return null + } + + const canApply = + mode === "today" || + mode === "yesterday" || + (mode === "range" && !!(range?.from && range?.to)) || + (mode === "specific" && !!specific) + + // Apply the current filter and close the popover + function apply() { + const filter = buildFilter() + if (filter) { onFilter(filter); setOpen(false) } + } + + function clear() { + setRange(undefined) + setSpecific(undefined) + onFilter(null) + setOpen(false) + } + + return ( +
+ + + {open && ( +
+ {/* Mode tabs */} +
+ {TABS.map(({ key, label }) => ( + + ))} +
+ + {/* Calendar (only for modes that need it) */} +
+ {mode === "range" && ( + + )} + {mode === "specific" && ( + + )} + {(mode === "today" || mode === "yesterday") && ( +

+ Click Apply to filter builds from {mode === "today" ? "today" : "yesterday"}. +

+ )} +
+ + {/* Actions */} +
+ + +
+
+ )} +
+ ) +} diff --git a/pipeline-ui/src/components/ui/calendar.tsx b/pipeline-ui/src/components/ui/calendar.tsx new file mode 100644 index 00000000..63b6fcef --- /dev/null +++ b/pipeline-ui/src/components/ui/calendar.tsx @@ -0,0 +1,211 @@ +import * as React from "react" +import { + ChevronDownIcon, + ChevronLeftIcon, + ChevronRightIcon, +} from "lucide-react" +import { DayButton, DayPicker, getDefaultClassNames } from "react-day-picker" + +import { cn } from "../../lib/utils" +import { Button, buttonVariants } from "../../components/ui/button" + +function Calendar({ + className, + classNames, + showOutsideDays = true, + captionLayout = "label", + buttonVariant = "ghost", + formatters, + components, + ...props +}: React.ComponentProps & { + buttonVariant?: React.ComponentProps["variant"] +}) { + const defaultClassNames = getDefaultClassNames() + + return ( + svg]:rotate-180`, + String.raw`rtl:**:[.rdp-button\_previous>svg]:rotate-180`, + className + )} + captionLayout={captionLayout} + formatters={{ + formatMonthDropdown: (date) => + date.toLocaleString("default", { month: "short" }), + ...formatters, + }} + classNames={{ + root: cn("w-fit", defaultClassNames.root), + months: cn( + "relative flex flex-col gap-4 md:flex-row", + defaultClassNames.months + ), + month: cn("flex w-full flex-col gap-4", defaultClassNames.month), + nav: cn( + "absolute inset-x-0 top-0 flex w-full items-center justify-between gap-1", + defaultClassNames.nav + ), + button_previous: cn( + buttonVariants({ variant: buttonVariant }), + "h-[--cell-size] w-[--cell-size] select-none p-0 aria-disabled:opacity-50", + defaultClassNames.button_previous + ), + button_next: cn( + buttonVariants({ variant: buttonVariant }), + "h-[--cell-size] w-[--cell-size] select-none p-0 aria-disabled:opacity-50", + defaultClassNames.button_next + ), + month_caption: cn( + "flex h-[--cell-size] w-full items-center justify-center px-[--cell-size]", + defaultClassNames.month_caption + ), + dropdowns: cn( + "flex h-[--cell-size] w-full items-center justify-center gap-1.5 text-sm font-medium", + defaultClassNames.dropdowns + ), + dropdown_root: cn( + "has-focus:border-ring border-input shadow-xs has-focus:ring-ring/50 has-focus:ring-[3px] relative rounded-md border", + defaultClassNames.dropdown_root + ), + dropdown: cn( + "bg-popover absolute inset-0 opacity-0", + defaultClassNames.dropdown + ), + caption_label: cn( + "select-none font-medium", + captionLayout === "label" + ? "text-sm" + : "[&>svg]:text-muted-foreground flex h-8 items-center gap-1 rounded-md pl-2 pr-1 text-sm [&>svg]:size-3.5", + defaultClassNames.caption_label + ), + month_grid: cn("w-full border-collapse", defaultClassNames.month_grid), + weekdays: cn("flex", defaultClassNames.weekdays), + weekday: cn( + "text-muted-foreground flex-1 select-none rounded-md text-[0.8rem] font-normal", + defaultClassNames.weekday + ), + week: cn("mt-2 flex w-full", defaultClassNames.week), + week_number_header: cn( + "w-[--cell-size] select-none", + defaultClassNames.week_number_header + ), + week_number: cn( + "text-muted-foreground select-none text-[0.8rem]", + defaultClassNames.week_number + ), + day: cn( + "group/day relative aspect-square h-full w-full select-none p-0 text-center [&:first-child[data-selected=true]_button]:rounded-l-md [&:last-child[data-selected=true]_button]:rounded-r-md", + defaultClassNames.day + ), + range_start: cn( + "bg-accent rounded-l-md", + defaultClassNames.range_start + ), + range_middle: cn("rounded-none", defaultClassNames.range_middle), + range_end: cn("bg-accent rounded-r-md", defaultClassNames.range_end), + today: cn( + "bg-accent text-accent-foreground rounded-md data-[selected=true]:rounded-none", + defaultClassNames.today + ), + outside: cn( + "text-muted-foreground aria-selected:text-muted-foreground", + defaultClassNames.outside + ), + disabled: cn( + "text-muted-foreground opacity-50", + defaultClassNames.disabled + ), + hidden: cn("invisible", defaultClassNames.hidden), + ...classNames, + }} + components={{ + Root: ({ className, rootRef, ...props }) => { + return ( +
+ ) + }, + Chevron: ({ className, orientation, ...props }) => { + if (orientation === "left") { + return ( + + ) + } + + if (orientation === "right") { + return ( + + ) + } + + return ( + + ) + }, + DayButton: CalendarDayButton, + WeekNumber: ({ children, ...props }) => { + return ( + +
+ {children} +
+ + ) + }, + ...components, + }} + {...props} + /> + ) +} + +function CalendarDayButton({ + className, + day, + modifiers, + ...props +}: React.ComponentProps) { + const defaultClassNames = getDefaultClassNames() + + const ref = React.useRef(null) + React.useEffect(() => { + if (modifiers.focused) ref.current?.focus() + }, [modifiers.focused]) + + return ( +