Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
"use client";
import { useState } from "react";
import {
toDateOnly,
dateOnlyToLocalDate,
formatDateOnly,
isPastDateOnly,
} from "@multica/core/issues/date";
import { Check } from "@multica/ui/icons";
import { LazyCalendar } from "@multica/ui/components/ui/lazy-calendar";
import {
Popover,
PopoverTrigger,
PopoverContent,
} from "@multica/ui/components/ui/popover";
import { DeferredTooltip } from "@multica/ui/components/common/deferred-tooltip";
import { DeferredPopup } from "./deferred-popup";
import { useLocale } from "../i18n";
/**
* Default class of the date pill trigger — shared with the deferred lookalike
* trigger so the swap on first interaction is pixel-identical.
*/
const DATE_TRIGGER_CLASS =
"flex items-center gap-1.5 cursor-pointer rounded-xs px-1 -mx-1 hover:bg-accent/30 transition-colors";
interface DateOnlyPickerProps {
/** Selected calendar day ("YYYY-MM-DD") or null. */
value: string | null;
/** Emits the new calendar day, or null when cleared. */
onChange: (value: string | null) => void;
/** Trigger icon — the entity picks CalendarClock (start) vs CalendarDays (due). */
icon: React.ReactNode;
/** Placeholder label shown when no date is set. */
placeholder: string;
/** Label of the empty-value row at the top of the popover ("No due date"). */
emptyLabel: string;
/** Paint the value with `text-destructive` when it is in the past (due dates). */
highlightOverdue?: boolean;
/** Fully custom trigger contents (replaces the icon + date/placeholder). */
trigger?: React.ReactNode;
/** Custom trigger element (e.g. a pill button). */
triggerRender?: React.ReactElement<Record<string, unknown>>;
/** Hover tooltip naming the field; hidden while the popover is open. */
tooltip?: React.ReactNode;
/** Controlled open state — lets a ⋯ overflow menu reveal + open the pill. */
open?: boolean;
onOpenChange?: (v: boolean) => void;
align?: "start" | "center" | "end";
/** Open the popover on first mount (progressive-disclosure sidebars). */
defaultOpen?: boolean;
}
/**
* Entity-agnostic calendar-day picker: the shared behaviour behind every
* start/due-date pill (issues, projects, …). It owns the Popover + Calendar +
* clear wiring and the calendar-day transport ("YYYY-MM-DD", no timezone shift,
* via @multica/core/issues/date); each entity wraps it to supply only the field
* name (through `onChange`), the icon, and the localized copy. Keeping this in
* one place stops the per-entity pills from drifting in behaviour or display
* formatting.
*
* Uncontrolled usages render a deferred lookalike trigger and mount the
* Popover + Calendar machinery on first interaction — the default trigger is
* computable from props alone, so every uncontrolled pill defers. See
* `DeferredPopup` for why.
*/
export function DateOnlyPicker(props: DateOnlyPickerProps) {
// A one-shot defaultOpen must not replace the mounted calendar with a cold trigger.
const [initialDefaultOpen] = useState(props.defaultOpen ?? false);
const canDefer =
props.open === undefined &&
props.onOpenChange === undefined &&
!props.defaultOpen && !initialDefaultOpen;
if (!canDefer) {
return <DateOnlyPickerImpl {...props} />;
}
return (
<DeferredPopup
trigger={props.trigger ?? <DateTriggerContent {...props} />}
triggerRender={props.triggerRender}
triggerClassName={DATE_TRIGGER_CLASS}
tooltip={props.tooltip}
>
{(open, onOpenChange) => (
<DateOnlyPickerImpl {...props} open={open} onOpenChange={onOpenChange} />
)}
</DeferredPopup>
);
}
/** Default trigger content, kept identical to what the Impl renders. */
function DateTriggerContent({
value,
icon,
placeholder,
highlightOverdue = false,
}: Pick<DateOnlyPickerProps, "value" | "icon" | "placeholder" | "highlightOverdue">) {
const locale = useLocale();
const date = dateOnlyToLocalDate(value);
const overdue = highlightOverdue && isPastDateOnly(value);
return (
<>
{icon}
{date ? (
<span className={overdue ? "text-destructive" : ""}>
{formatDateOnly(value, { month: "short", day: "numeric" }, locale)}
</span>
) : (
<span className="text-muted-foreground">{placeholder}</span>
)}
</>
);
}
function DateOnlyPickerImpl({
value,
onChange,
icon,
placeholder,
emptyLabel,
highlightOverdue = false,
trigger: customTrigger,
triggerRender,
tooltip,
open: controlledOpen,
onOpenChange: controlledOnOpenChange,
align = "start",
defaultOpen = false,
}: DateOnlyPickerProps) {
const [internalOpen, setInternalOpen] = useState(defaultOpen);
const open = controlledOpen ?? internalOpen;
const setOpen = controlledOnOpenChange ?? setInternalOpen;
const date = dateOnlyToLocalDate(value);
const popoverTrigger = (
<PopoverTrigger
className={triggerRender ? undefined : DATE_TRIGGER_CLASS}
render={triggerRender}
>
{customTrigger ?? (
<DateTriggerContent
value={value}
icon={icon}
placeholder={placeholder}
highlightOverdue={highlightOverdue}
/>
)}
</PopoverTrigger>
);
return (
<Popover open={open} onOpenChange={setOpen}>
{tooltip == null ? (
popoverTrigger
) : (
<DeferredTooltip trigger={popoverTrigger} content={tooltip} />
)}
<PopoverContent className="w-auto p-0" align={align}>
{/* Empty value first, above the calendar — the same position every
list-backed picker puts it in, so clearing a field is always the
top row no matter which pill was opened. Unlike a footer button it
is also a selectable value: it carries the checkmark when the
field is empty. */}
<button
type="button"
onClick={() => {
onChange(null);
setOpen(false);
}}
className="flex w-full items-center gap-3 border-b px-3 py-2 text-left text-body transition-colors hover:bg-accent"
>
<span className="flex min-w-0 flex-1 items-center gap-2 text-muted-foreground">
{emptyLabel}
</span>
<Check className={`h-3.5 w-3.5 shrink-0 text-muted-foreground ${date ? "invisible" : ""}`} />
</button>
<LazyCalendar
mode="single"
selected={date}
onSelect={(d: Date | undefined) => {
onChange(d ? toDateOnly(d) : null);
setOpen(false);
}}
/>
</PopoverContent>
</Popover>
);
}