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.

fix expiration modal

Steven Teyb423728f91e1
diff --git a/apps/web/ui/modals/link-builder/expiration-modal.tsx b/apps/web/ui/modals/link-builder/expiration-modal.tsx
index 9ecaadeded..35c4b45848 100644
--- a/apps/web/ui/modals/link-builder/expiration-modal.tsx
+++ b/apps/web/ui/modals/link-builder/expiration-modal.tsx
@@ -11,7 +11,6 @@ import {
import { CircleHalfDottedClock } from "@dub/ui/src/icons";
import {
cn,
- formatDate,
formatDateTime,
getDateTimeLocal,
parseDateTime,
@@ -74,201 +73,196 @@ function ExpirationModal({
}, []);
return (
- <>
- <Modal
- showModal={showExpirationModal}
- setShowModal={setShowExpirationModal}
- className="sm:max-w-md"
+ <Modal
+ showModal={showExpirationModal}
+ setShowModal={setShowExpirationModal}
+ className="sm:max-w-md"
+ >
+ <form
+ className="px-5 py-4"
+ onSubmit={(e) => {
+ e.stopPropagation();
+ handleSubmit((data) => {
+ setValueParent("expiresAt", data.expiresAt, {
+ shouldDirty: true,
+ });
+ setValueParent("expiredUrl", data.expiredUrl, {
+ shouldDirty: true,
+ });
+ setShowExpirationModal(false);
+ })(e);
+ }}
>
- <form
- className="px-5 py-4"
- onSubmit={(e) => {
- e.stopPropagation();
- handleSubmit((data) => {
- setValueParent("expiresAt", data.expiresAt, {
- shouldDirty: true,
- });
- setValueParent("expiredUrl", data.expiredUrl, {
- shouldDirty: true,
- });
- setShowExpirationModal(false);
- })(e);
- }}
- >
- <div className="flex items-center justify-between">
- <div className="flex items-center gap-2">
- <h3 className="text-lg font-medium">Link Expiration</h3>
- <ProBadgeTooltip
- content={
- <SimpleTooltipContent
- title="Set an expiration date for your links – after which it won't be accessible."
- cta="Learn more."
- href="https://dub.co/help/article/link-expiration"
- />
- }
- />
- </div>
- <div className="max-md:hidden">
- <Tooltip
- content={
- <div className="px-2 py-1 text-xs text-gray-700">
- Press{" "}
- <strong className="font-medium text-gray-950">E</strong> to
- open this quickly
- </div>
- }
- side="right"
- >
- <kbd className="flex size-6 cursor-default items-center justify-center rounded-md border border-gray-200 font-sans text-xs text-gray-950">
- E
- </kbd>
- </Tooltip>
- </div>
+ <div className="flex items-center justify-between">
+ <div className="flex items-center gap-2">
+ <h3 className="text-lg font-medium">Link Expiration</h3>
+ <ProBadgeTooltip
+ content={
+ <SimpleTooltipContent
+ title="Set an expiration date for your links – after which it won't be accessible."
+ cta="Learn more."
+ href="https://dub.co/help/article/link-expiration"
+ />
+ }
+ />
+ </div>
+ <div className="max-md:hidden">
+ <Tooltip
+ content={
+ <div className="px-2 py-1 text-xs text-gray-700">
+ Press <strong className="font-medium text-gray-950">E</strong>{" "}
+ to open this quickly
+ </div>
+ }
+ side="right"
+ >
+ <kbd className="flex size-6 cursor-default items-center justify-center rounded-md border border-gray-200 font-sans text-xs text-gray-950">
+ E
+ </kbd>
+ </Tooltip>
</div>
+ </div>
- {/* Expiration Date */}
- <div className="mt-6">
- <div className="flex items-center gap-2">
- <label
- htmlFor={`${id}-expiresAt`}
- className="block text-sm font-medium text-gray-700"
- >
- Date and Time
- </label>
- </div>
- <div className="mt-2 flex w-full items-center justify-between rounded-md border border-gray-300 bg-white shadow-sm transition-all focus-within:border-gray-800 focus-within:outline-none focus-within:ring-1 focus-within:ring-gray-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);
- }
+ {/* Expiration Date */}
+ <div className="mt-6">
+ <div className="flex items-center gap-2">
+ <label
+ htmlFor={`${id}-expiresAt`}
+ className="block text-sm font-medium text-gray-700"
+ >
+ Date and Time
+ </label>
+ </div>
+ <div className="mt-2 flex w-full items-center justify-between rounded-md border border-gray-300 bg-white shadow-sm transition-all focus-within:border-gray-800 focus-within:outline-none focus-within:ring-1 focus-within:ring-gray-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);
}
- }}
- className="flex-1 border-none bg-transparent text-gray-900 placeholder-gray-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);
+ }
+ }}
+ 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="w-[40px] border-none bg-transparent text-gray-500 focus:outline-none focus:ring-0 sm:text-sm"
- />
- </div>
+ }
+ }}
+ className="flex-1 border-none bg-transparent text-gray-900 placeholder-gray-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-gray-500 focus:outline-none focus:ring-0 sm:text-sm"
+ />
</div>
+ </div>
- {/* Expiration URL */}
- <div className="mt-6">
- <div className="flex items-center gap-2">
- <label
- htmlFor={`${id}-expiredUrl`}
- className="block text-sm font-medium text-gray-700"
- >
- Expiration URL
- </label>
- <InfoTooltip
- content={
- <SimpleTooltipContent
- title="Redirect users to a specific URL when the link has expired."
- cta="Learn more."
- href="https://dub.co/help/article/link-expiration#setting-a-custom-expiration-url"
- />
- }
- />
- </div>
- <div className="mt-2 rounded-md shadow-sm">
- <input
- id={`${id}-expiredUrl`}
- type="text"
- autoFocus={!isMobile}
- placeholder="https://example.com"
- className={`${
- errors.expiredUrl
- ? "border-red-300 pr-10 text-red-900 placeholder-red-300 focus:border-red-500 focus:ring-red-500"
- : "border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:ring-gray-500"
- } block w-full rounded-md focus:outline-none sm:text-sm`}
- {...register("expiredUrl")}
- />
- </div>
+ {/* Expiration URL */}
+ <div className="mt-6">
+ <div className="flex items-center gap-2">
+ <label
+ htmlFor={`${id}-expiredUrl`}
+ className="block text-sm font-medium text-gray-700"
+ >
+ Expiration URL
+ </label>
+ <InfoTooltip
+ content={
+ <SimpleTooltipContent
+ title="Redirect users to a specific URL when the link has expired."
+ cta="Learn more."
+ href="https://dub.co/help/article/link-expiration#setting-a-custom-expiration-url"
+ />
+ }
+ />
+ </div>
+ <div className="mt-2 rounded-md shadow-sm">
+ <input
+ id={`${id}-expiredUrl`}
+ type="text"
+ autoFocus={!isMobile}
+ placeholder="https://example.com"
+ className={`${
+ errors.expiredUrl
+ ? "border-red-300 pr-10 text-red-900 placeholder-red-300 focus:border-red-500 focus:ring-red-500"
+ : "border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:ring-gray-500"
+ } block w-full rounded-md focus:outline-none sm:text-sm`}
+ {...register("expiredUrl")}
+ />
</div>
+ </div>
- <a
- href="https://dub.co/help/article/link-expiration#setting-a-default-expiration-url-for-all-links-under-a-domain"
- target="_blank"
- className="group mt-2 flex items-center text-xs text-gray-500 hover:text-gray-700"
- >
- Set a default expiration URL for your domain
- </a>
+ <a
+ href="https://dub.co/help/article/link-expiration#setting-a-default-expiration-url-for-all-links-under-a-domain"
+ target="_blank"
+ className="group mt-2 flex items-center text-xs text-gray-500 hover:text-gray-700"
+ >
+ Set a default expiration URL for your domain
+ </a>
- <div className="mt-6 flex items-center justify-between">
- <div>
- {Boolean(expiresAtParent) && (
- <button
- type="button"
- className="text-xs font-medium text-gray-700 transition-colors hover:text-gray-950"
- onClick={() => {
- setValueParent("expiresAt", null, { shouldDirty: true });
- setValueParent("expiredUrl", null, { shouldDirty: true });
- setShowExpirationModal(false);
- }}
- >
- Remove expiration
- </button>
- )}
- </div>
- <div className="flex items-center gap-2">
- <Button
+ <div className="mt-6 flex items-center justify-between">
+ <div>
+ {Boolean(expiresAtParent) && (
+ <button
type="button"
- variant="secondary"
- text="Cancel"
- className="h-9 w-fit"
+ className="text-xs font-medium text-gray-700 transition-colors hover:text-gray-950"
onClick={() => {
- reset();
+ setValueParent("expiresAt", null, { shouldDirty: true });
+ setValueParent("expiredUrl", null, { shouldDirty: true });
setShowExpirationModal(false);
}}
- />
- <Button
- type="submit"
- variant="primary"
- text={expiresAtParent ? "Save" : "Add expiration"}
- className="h-9 w-fit"
- disabled={!isDirty}
- />
- </div>
+ >
+ Remove expiration
+ </button>
+ )}
+ </div>
+ <div className="flex items-center gap-2">
+ <Button
+ type="button"
+ variant="secondary"
+ text="Cancel"
+ className="h-9 w-fit"
+ onClick={() => {
+ reset();
+ setShowExpirationModal(false);
+ }}
+ />
+ <Button
+ type="submit"
+ variant="primary"
+ text={expiresAtParent ? "Save" : "Add expiration"}
+ className="h-9 w-fit"
+ disabled={!isDirty}
+ />
</div>
- </form>
- </Modal>
- </>
+ </div>
+ </form>
+ </Modal>
);
}
@@ -276,11 +270,7 @@ export function getExpirationLabel({
expiresAt,
}: Pick<LinkFormData, "expiresAt">) {
return expiresAt
- ? formatDate(expiresAt, {
- month: "short",
- year: undefined,
- timeZone: undefined,
- }) || "Expiration"
+ ? formatDateTime(expiresAt, { year: undefined })
: "Expiration";
}
diff --git a/packages/utils/src/functions/datetime.ts b/packages/utils/src/functions/datetime.ts
index 461831625d..46630d2cc1 100644
--- a/packages/utils/src/functions/datetime.ts
+++ b/packages/utils/src/functions/datetime.ts
@@ -30,7 +30,10 @@ export const formatDate = (
});
};
-export const formatDateTime = (datetime: Date | string) => {
+export const formatDateTime = (
+ datetime: Date | string,
+ options?: Intl.DateTimeFormatOptions,
+) => {
if (datetime.toString() === "Invalid Date") return "";
return new Date(datetime).toLocaleTimeString("en-US", {
month: "short",
@@ -39,6 +42,7 @@ export const formatDateTime = (datetime: Date | string) => {
hour: "numeric",
minute: "numeric",
hour12: true,
+ ...options,
});
};