Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Swap bounty custom start/end to inline calendar popover

Kiran Ke3146af3434b
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/add-edit-bounty/bounty-duration.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/add-edit-bounty/bounty-duration.tsx
index 78e814c401..86ffdbec76 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/add-edit-bounty/bounty-duration.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/add-edit-bounty/bounty-duration.tsx
@@ -10,13 +10,14 @@ import {
} from "@/lib/bounty/bounty-period";
import {
InlineBadgePopover,
+ InlineBadgePopoverContext,
InlineBadgePopoverMenu,
} from "@/ui/shared/inline-badge-popover";
-import { CalendarIcon, DatePicker } from "@dub/ui";
+import { Calendar, CalendarIcon } from "@dub/ui";
import { formatDate } from "@dub/utils";
import { BountyStartMode } from "@prisma/client";
import { addDays, addMonths, addWeeks } from "date-fns";
-import { useEffect, useState } from "react";
+import { useContext, useEffect, useState } from "react";
type PresetOption<T extends string> = { value: T; label: string };
type BountyTimingInput = ReturnType<typeof resolveBountyTiming>;
@@ -246,45 +247,87 @@ function parsePresetsFromValue(
return isEditing ? parsePresetsForEdit(value) : parsePresets(value);
}
-function CustomDatePickerIcon({
- value,
- onChange,
+function mergeDateWithTime(date: Date, previous: Date | null | undefined) {
+ const merged = new Date(date);
+
+ if (previous) {
+ merged.setHours(
+ previous.getHours(),
+ previous.getMinutes(),
+ previous.getSeconds(),
+ previous.getMilliseconds(),
+ );
+ }
+
+ return merged;
+}
+
+function BountyDatePopoverContent<T extends string>({
+ options,
+ selectedPreset,
+ customDate,
+ onSelectPreset,
+ onSelectDate,
}: {
- value: Date | null | undefined;
- onChange: (date: Date | null) => void;
+ options: PresetOption<T>[];
+ selectedPreset: T | undefined;
+ customDate: Date | null | undefined;
+ onSelectPreset: (preset: T) => void;
+ onSelectDate: (date: Date) => void;
}) {
- return (
- <DatePicker
- value={value ?? undefined}
- onChange={(date) => {
- if (!date) {
- onChange(null);
- return;
- }
-
- const merged = new Date(date);
+ const { isOpen, setIsOpen } = useContext(InlineBadgePopoverContext);
+ const [showCustomCalendar, setShowCustomCalendar] = useState(false);
- if (value) {
- merged.setHours(
- value.getHours(),
- value.getMinutes(),
- value.getSeconds(),
- value.getMilliseconds(),
- );
- }
+ useEffect(() => {
+ if (!isOpen) return;
+ if (selectedPreset === "custom") {
+ setShowCustomCalendar(true);
+ }
+ }, [isOpen, selectedPreset]);
- onChange(merged);
- }}
- align="start"
- showYearNavigation
- trigger={() => (
+ if (showCustomCalendar) {
+ return (
+ <div className="flex flex-col gap-1.5">
<button
type="button"
- className="inline-flex items-center rounded p-0.5 text-neutral-500 hover:bg-neutral-100 hover:text-neutral-700"
+ onClick={() => setShowCustomCalendar(false)}
+ className="flex items-center gap-1 rounded px-0.5 text-xs text-neutral-500 transition-colors hover:text-neutral-700"
>
- <CalendarIcon className="size-3.5" />
+ ← Presets
</button>
- )}
+ <Calendar
+ mode="single"
+ selected={customDate ?? undefined}
+ onSelect={(date) => {
+ if (!date) return;
+ onSelectDate(mergeDateWithTime(date, customDate));
+ setIsOpen(false);
+ }}
+ showYearNavigation
+ className="p-2"
+ />
+ </div>
+ );
+ }
+
+ return (
+ <InlineBadgePopoverMenu
+ selectedValue={selectedPreset}
+ onSelect={(preset) => {
+ if (preset === "custom") {
+ setShowCustomCalendar(true);
+ onSelectPreset(preset);
+ return;
+ }
+
+ setShowCustomCalendar(false);
+ onSelectPreset(preset);
+ }}
+ items={options.map((option) => ({
+ value: option.value,
+ text: option.label,
+ ...(option.value === "custom" ? { preventClose: true } : {}),
+ }))}
/>
);
}
@@ -397,95 +440,78 @@ export function BountyDuration({
<CalendarIcon className="size-4 shrink-0 text-neutral-500" />
<span className="text-sm font-medium leading-relaxed text-neutral-800">
Starts{" "}
- <span className="inline-flex items-center gap-0.5">
- <InlineBadgePopover text={startLabel} buttonClassName="mx-0.5">
- <InlineBadgePopoverMenu
- items={START_OPTIONS.map((option) => ({
- value: option.value,
- text: option.label,
- }))}
- selectedValue={startPreset}
- onSelect={(preset) => {
- setStartPreset(preset);
-
- if (preset === "custom") {
- const nextCustomStartsAt = customStartsAt ?? value.startsAt;
- setCustomStartsAt(nextCustomStartsAt);
- applyTiming({
- nextStartPreset: "custom",
- nextCustomStartsAt,
- });
- return;
- }
-
- applyTiming({ nextStartPreset: preset });
- }}
- />
- </InlineBadgePopover>
- {startPreset === "custom" && (
- <CustomDatePickerIcon
- value={customStartsAt ?? value.startsAt}
- onChange={(date) => {
- const nextCustomStartsAt = date ?? null;
+ <InlineBadgePopover text={startLabel} buttonClassName="mx-0.5">
+ <BountyDatePopoverContent
+ options={START_OPTIONS}
+ selectedPreset={startPreset}
+ customDate={customStartsAt ?? value.startsAt}
+ onSelectPreset={(preset) => {
+ setStartPreset(preset);
+
+ if (preset === "custom") {
+ const nextCustomStartsAt = customStartsAt ?? value.startsAt;
setCustomStartsAt(nextCustomStartsAt);
applyTiming({
nextStartPreset: "custom",
nextCustomStartsAt,
});
- }}
- />
- )}
- </span>{" "}
+ return;
+ }
+
+ applyTiming({ nextStartPreset: preset });
+ }}
+ onSelectDate={(date) => {
+ setStartPreset("custom");
+ setCustomStartsAt(date);
+ applyTiming({
+ nextStartPreset: "custom",
+ nextCustomStartsAt: date,
+ });
+ }}
+ />
+ </InlineBadgePopover>{" "}
and ends{" "}
- <span className="inline-flex items-center gap-0.5">
- <InlineBadgePopover text={endLabel} buttonClassName="mx-0.5">
- <InlineBadgePopoverMenu
- items={endOptions.map((option) => ({
- value: option.value,
- text: option.label,
- }))}
- selectedValue={
- customEndsAfterDays != null ? undefined : endPreset
+ <InlineBadgePopover text={endLabel} buttonClassName="mx-0.5">
+ <BountyDatePopoverContent
+ options={endOptions}
+ selectedPreset={
+ customEndsAfterDays != null ? undefined : endPreset
+ }
+ customDate={
+ customEndsAt ?? value.endsAt ?? addWeeks(value.startsAt, 2)
+ }
+ onSelectPreset={(preset) => {
+ if (endDateLocked && preset === "never") {
+ return;
}
- onSelect={(preset) => {
- if (endDateLocked && preset === "never") {
- return;
- }
-
- setEndPreset(preset);
- setCustomEndsAfterDays(null);
-
- if (preset === "custom") {
- const nextCustomEndsAt =
- customEndsAt ??
- value.endsAt ??
- addWeeks(value.startsAt, 2);
- setCustomEndsAt(nextCustomEndsAt);
- applyTiming({
- nextEndPreset: "custom",
- nextCustomEndsAt,
- });
- return;
- }
-
- applyTiming({ nextEndPreset: preset });
- }}
- />
- </InlineBadgePopover>
- {endPreset === "custom" && (
- <CustomDatePickerIcon
- value={customEndsAt ?? value.endsAt}
- onChange={(date) => {
- const nextCustomEndsAt = date ?? null;
+
+ setEndPreset(preset);
+ setCustomEndsAfterDays(null);
+
+ if (preset === "custom") {
+ const nextCustomEndsAt =
+ customEndsAt ?? value.endsAt ?? addWeeks(value.startsAt, 2);
setCustomEndsAt(nextCustomEndsAt);
applyTiming({
nextEndPreset: "custom",
nextCustomEndsAt,
});
- }}
- />
- )}
- </span>
+ return;
+ }
+
+ applyTiming({ nextEndPreset: preset });
+ }}
+ onSelectDate={(date) => {
+ setEndPreset("custom");
+ setCustomEndsAfterDays(null);
+ setCustomEndsAt(date);
+ applyTiming({
+ nextEndPreset: "custom",
+ nextCustomEndsAt: date,
+ });
+ }}
+ />
+ </InlineBadgePopover>
{endSuffix && <span> {endSuffix}</span>}
</span>
</div>