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.

SmartDateTimePicker

Steven Teycf3b8340f6ae
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/commissions/create-commission-sheet.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/commissions/create-commission-sheet.tsx
index e66bd0e21b..b0c4e97180 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/commissions/create-commission-sheet.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/commissions/create-commission-sheet.tsx
@@ -13,6 +13,7 @@ import {
Button,
Combobox,
Sheet,
+ SmartDateTimePicker,
Switch,
} from "@dub/ui";
import { cn } from "@dub/utils";
@@ -199,40 +200,15 @@ function CreateCommissionSheetContent({
<div className="grid grid-cols-1 gap-6 rounded-xl border border-neutral-200 px-4 py-5">
{programDefaultReward?.event === "sale" && (
- <div>
- <label
- htmlFor="saleDate"
- className="flex items-center space-x-2"
- >
- <h2 className="text-sm font-medium text-neutral-900">
- Sale date
- </h2>
- </label>
- <div className="mt-2">
- <input
- type="datetime-local"
- id="saleDate"
- className={cn(
- "block w-full rounded-md border-neutral-300 px-3 py-2 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm",
- errors.saleDate &&
- "border-red-600 focus:border-red-500 focus:ring-red-600",
- )}
- {...register("saleDate", {
- required: programDefaultReward?.event === "sale",
- })}
- value={
- saleDate
- ? new Date(saleDate).toISOString().slice(0, 16)
- : ""
- }
- onChange={(e) => {
- if (e.target.value) {
- setValue("saleDate", new Date(e.target.value));
- }
- }}
- />
- </div>
- </div>
+ <SmartDateTimePicker
+ value={saleDate}
+ onChange={(date) => {
+ setValue("saleDate", date, { shouldDirty: true });
+ }}
+ label="Sale date"
+ required={programDefaultReward?.event === "sale"}
+ placeholder='E.g. "2024-03-01", "Last Thursday", "2 hours ago"'
+ />
)}
{programDefaultReward?.event === "sale" && (
@@ -272,38 +248,14 @@ function CreateCommissionSheetContent({
)}
{programDefaultReward?.event === "lead" && (
- <div>
- <label
- htmlFor="saleDate"
- className="flex items-center space-x-2"
- >
- <h2 className="text-sm font-medium text-neutral-900">
- Lead date
- </h2>
- </label>
- <div className="mt-2">
- <input
- type="datetime-local"
- id="leadDate"
- className={cn(
- "block w-full rounded-md border-neutral-300 px-3 py-2 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm",
- errors.leadDate &&
- "border-red-600 focus:border-red-500 focus:ring-red-600",
- )}
- {...register("leadDate")}
- value={
- leadDate
- ? new Date(leadDate).toISOString().slice(0, 16)
- : ""
- }
- onChange={(e) => {
- if (e.target.value) {
- setValue("leadDate", new Date(e.target.value));
- }
- }}
- />
- </div>
- </div>
+ <SmartDateTimePicker
+ value={leadDate}
+ onChange={(date) => {
+ setValue("leadDate", date, { shouldDirty: true });
+ }}
+ label="Lead date"
+ placeholder='E.g. "2024-03-01", "Last Thursday", "2 hours ago"'
+ />
)}
{programDefaultReward?.event !== "lead" && (
@@ -405,37 +357,15 @@ function CreateCommissionSheetContent({
</div>
{hasDifferentCreationDate && (
- <div>
- <label
- htmlFor="leadDate"
- className="flex items-center space-x-2"
- >
- <h2 className="text-sm font-medium text-neutral-900">
- Customer creation date
- </h2>
- </label>
- <div className="mt-2">
- <input
- type="datetime-local"
- id="leadDate"
- className={cn(
- "block w-full rounded-md border-neutral-300 px-3 py-2 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm",
- errors.leadDate &&
- "border-red-600 focus:border-red-500 focus:ring-red-600",
- )}
- {...register("leadDate")}
- value={
- leadDate
- ? new Date(leadDate).toISOString().slice(0, 16)
- : ""
- }
- onChange={(e) => {
- if (e.target.value) {
- setValue("leadDate", new Date(e.target.value));
- }
- }}
- />
- </div>
+ <div className="p-px">
+ <SmartDateTimePicker
+ value={leadDate}
+ onChange={(date) => {
+ setValue("leadDate", date, { shouldDirty: true });
+ }}
+ label="Customer creation date"
+ placeholder='E.g. "2024-03-01", "Last Thursday", "2 hours ago"'
+ />
</div>
)}
</div>
diff --git a/apps/web/ui/modals/link-builder/expiration-modal.tsx b/apps/web/ui/modals/link-builder/expiration-modal.tsx
index 6260dfa17f..70a8e92390 100644
--- a/apps/web/ui/modals/link-builder/expiration-modal.tsx
+++ b/apps/web/ui/modals/link-builder/expiration-modal.tsx
@@ -6,16 +6,12 @@ import {
InfoTooltip,
Modal,
SimpleTooltipContent,
+ SmartDateTimePicker,
Tooltip,
useMediaQuery,
} from "@dub/ui";
import { CircleHalfDottedClock } from "@dub/ui/icons";
-import {
- cn,
- formatDateTime,
- getDateTimeLocal,
- parseDateTime,
-} from "@dub/utils";
+import { cn, formatDateTime } from "@dub/utils";
import {
Dispatch,
SetStateAction,
@@ -126,61 +122,14 @@ function ExpirationModal({
{/* Expiration Date */}
<div className="mt-6">
- <div className="flex items-center gap-2">
- <label
- htmlFor={`${id}-expiresAt`}
- className="block text-sm font-medium text-neutral-700"
- >
- Date and Time
- </label>
- </div>
- <div className="mt-2 flex w-full items-center justify-between rounded-md border border-neutral-300 bg-white shadow-sm transition-all focus-within:border-neutral-800 focus-within:outline-none focus-within:ring-1 focus-within:ring-neutral-500">
- <input
- ref={inputRef}
- id={`${id}-expiresAt`}
- type="text"
- placeholder='E.g. "tomorrow at 5pm" or "in 2 hours"'
- defaultValue={expiresAt ? formatDateTime(expiresAt) : ""}
- onBlur={(e) => {
- if (e.target.value.length > 0) {
- const parsedDateTime = parseDateTime(e.target.value);
- if (parsedDateTime) {
- setValue("expiresAt", parsedDateTime, {
- shouldDirty: true,
- });
- e.target.value = formatDateTime(parsedDateTime);
- }
- }
- }}
- onKeyDown={(e) => {
- if (e.key === "Enter" && inputRef.current) {
- e.preventDefault();
- const parsedDateTime = parseDateTime(inputRef.current.value);
- if (parsedDateTime) {
- setValue("expiresAt", parsedDateTime, {
- shouldDirty: true,
- });
- inputRef.current.value = formatDateTime(parsedDateTime);
- }
- }
- }}
- className="flex-1 border-none bg-transparent text-neutral-900 placeholder-neutral-400 focus:outline-none focus:ring-0 sm:text-sm"
- />
- <input
- type="datetime-local"
- id="expiresAt"
- name="expiresAt"
- value={expiresAt ? getDateTimeLocal(expiresAt) : ""}
- onChange={(e) => {
- const expiryDate = new Date(e.target.value);
- setValue("expiresAt", expiryDate, { shouldDirty: true });
- if (inputRef.current) {
- inputRef.current.value = formatDateTime(expiryDate);
- }
- }}
- className="w-[40px] border-none bg-transparent text-neutral-500 focus:outline-none focus:ring-0 sm:text-sm"
- />
- </div>
+ <SmartDateTimePicker
+ value={expiresAt}
+ onChange={(date) => {
+ setValue("expiresAt", date, { shouldDirty: true });
+ }}
+ label="Date and Time"
+ autoFocus={!isMobile}
+ />
</div>
{/* Expiration URL */}
diff --git a/packages/ui/src/index.tsx b/packages/ui/src/index.tsx
index 03fc87282e..5902aee963 100644
--- a/packages/ui/src/index.tsx
+++ b/packages/ui/src/index.tsx
@@ -29,6 +29,7 @@ export * from "./popover";
export * from "./radio-group";
export * from "./sheet";
export * from "./shimmer-dots";
+export * from "./smart-datetime-picker";
export * from "./status-badge";
export * from "./switch";
export * from "./table";
diff --git a/packages/ui/src/smart-datetime-picker.tsx b/packages/ui/src/smart-datetime-picker.tsx
new file mode 100644
index 0000000000..e9f6d8b415
--- /dev/null
+++ b/packages/ui/src/smart-datetime-picker.tsx
@@ -0,0 +1,92 @@
+import { formatDateTime, getDateTimeLocal, parseDateTime } from "@dub/utils";
+import { useEffect, useId, useRef } from "react";
+
+interface SmartDateTimePickerProps {
+ value: Date | null | undefined;
+ onChange: (date: Date | null) => void;
+ label?: string;
+ placeholder?: string;
+ className?: string;
+ required?: boolean;
+ autoFocus?: boolean;
+}
+
+export function SmartDateTimePicker({
+ value,
+ onChange,
+ label,
+ placeholder = 'E.g. "tomorrow at 5pm" or "in 2 hours"',
+ className,
+ required,
+ autoFocus = false,
+}: SmartDateTimePickerProps) {
+ const id = useId();
+ const inputRef = useRef<HTMLInputElement>(null);
+
+ // Hacky fix to focus the input automatically, not sure why autoFocus doesn't work here
+ useEffect(() => {
+ if (inputRef.current && autoFocus) {
+ setTimeout(() => {
+ inputRef.current?.focus();
+ }, 10);
+ }
+ }, [autoFocus]);
+
+ return (
+ <div className={className}>
+ {label && (
+ <div className="flex items-center gap-2">
+ <label
+ htmlFor={`${id}-datetime`}
+ className="block text-sm font-medium text-neutral-700"
+ >
+ {label}
+ </label>
+ </div>
+ )}
+ <div className="mt-2 flex w-full items-center justify-between rounded-md border border-neutral-300 bg-white shadow-sm transition-all focus-within:border-neutral-800 focus-within:outline-none focus-within:ring-1 focus-within:ring-neutral-500">
+ <input
+ ref={inputRef}
+ id={`${id}-datetime`}
+ type="text"
+ placeholder={placeholder}
+ defaultValue={value ? formatDateTime(value) : ""}
+ onBlur={(e) => {
+ if (e.target.value.length > 0) {
+ const parsedDateTime = parseDateTime(e.target.value);
+ if (parsedDateTime) {
+ onChange(parsedDateTime);
+ e.target.value = formatDateTime(parsedDateTime);
+ }
+ }
+ }}
+ onKeyDown={(e) => {
+ if (e.key === "Enter" && inputRef.current) {
+ e.preventDefault();
+ const parsedDateTime = parseDateTime(inputRef.current.value);
+ if (parsedDateTime) {
+ onChange(parsedDateTime);
+ inputRef.current.value = formatDateTime(parsedDateTime);
+ }
+ }
+ }}
+ className="flex-1 border-none bg-transparent text-neutral-900 placeholder-neutral-400 focus:outline-none focus:ring-0 sm:text-sm"
+ />
+ <input
+ type="datetime-local"
+ id={`${id}-datetime-local`}
+ required={required}
+ value={value ? getDateTimeLocal(value) : ""}
+ onChange={(e) => {
+ const date = new Date(e.target.value);
+ onChange(date);
+ if (inputRef.current) {
+ inputRef.current.value = formatDateTime(date);
+ }
+ }}
+ className="w-[40px] border-none bg-transparent text-neutral-500 focus:outline-none focus:ring-0 sm:text-sm"
+ />
+ </div>
+ </div>
+ );
+}