diff --git a/app/components/Sharing/Collection/PublicAccess.tsx b/app/components/Sharing/Collection/PublicAccess.tsx index 9621ef4397..3bb0bb3cc8 100644 --- a/app/components/Sharing/Collection/PublicAccess.tsx +++ b/app/components/Sharing/Collection/PublicAccess.tsx @@ -27,7 +27,7 @@ import ShareSettingsPopover from "../components/ShareSettingsPopover"; import { DomainPrefix, ShareLinkInput, StyledInfoIcon } from "../components"; import styled from "styled-components"; import { s } from "@shared/styles"; -import { isAfterToday } from "@shared/utils/date"; +import { isFuture } from "date-fns"; type Props = { /** The collection to share. */ @@ -50,7 +50,7 @@ function InnerPublicAccess( const collectionAbilities = usePolicy(collection); const canPublish = share ? can.update : collectionAbilities.share; const [creating, setCreating] = React.useState(false); - const isShareExpired = share?.expiresAt && !isAfterToday(share.expiresAt); + const isShareExpired = share?.expiresAt && !isFuture(share.expiresAt); React.useEffect(() => { setUrlId(share?.urlId); diff --git a/app/components/Sharing/Document/PublicAccess.tsx b/app/components/Sharing/Document/PublicAccess.tsx index 5ba9a1f72b..4b03e89edd 100644 --- a/app/components/Sharing/Document/PublicAccess.tsx +++ b/app/components/Sharing/Document/PublicAccess.tsx @@ -31,7 +31,7 @@ import { StyledInfoIcon, UnderlinedLink, } from "../components"; -import { isAfterToday } from "@shared/utils/date"; +import { isFuture } from "date-fns"; type Props = { /** The document to share. */ @@ -59,9 +59,9 @@ function PublicAccess( const documentAbilities = usePolicy(document); const canPublish = share ? can.update : documentAbilities.share; const [creating, setCreating] = React.useState(false); - const isShareExpired = share?.expiresAt && !isAfterToday(share.expiresAt); + const isShareExpired = share?.expiresAt && !isFuture(share.expiresAt); const isParentShareExpired = - sharedParent?.expiresAt && !isAfterToday(sharedParent.expiresAt); + sharedParent?.expiresAt && !isFuture(sharedParent.expiresAt); const hasExpiredShare = (!!share?.published && isShareExpired) || diff --git a/app/scenes/Collection/components/ShareButton.tsx b/app/scenes/Collection/components/ShareButton.tsx index bb02118865..0ff8093f7b 100644 --- a/app/scenes/Collection/components/ShareButton.tsx +++ b/app/scenes/Collection/components/ShareButton.tsx @@ -15,7 +15,7 @@ import useShareDataLoader from "~/hooks/useShareDataLoader"; import useStores from "~/hooks/useStores"; import { preventDefault } from "~/utils/events"; import lazyWithRetry from "~/utils/lazyWithRetry"; -import { isAfterToday } from "@shared/utils/date"; +import { isFuture } from "date-fns"; const SharePopover = lazyWithRetry( () => import("~/components/Sharing/Collection/SharePopover") @@ -33,7 +33,7 @@ function ShareButton({ collection }: Props) { const isMobile = useMobile(); const team = useCurrentTeam(); const share = shares.getByCollectionId(collection.id); - const isShareExpired = share?.expiresAt && !isAfterToday(share.expiresAt); + const isShareExpired = share?.expiresAt && !isFuture(share.expiresAt); const isPubliclyShared = team.sharing !== false && collection?.sharing !== false && diff --git a/app/scenes/Document/components/ShareButton.tsx b/app/scenes/Document/components/ShareButton.tsx index 4160a44dc9..03eac364b4 100644 --- a/app/scenes/Document/components/ShareButton.tsx +++ b/app/scenes/Document/components/ShareButton.tsx @@ -14,7 +14,7 @@ import useShareDataLoader from "~/hooks/useShareDataLoader"; import useStores from "~/hooks/useStores"; import { preventDefault } from "~/utils/events"; import lazyWithRetry from "~/utils/lazyWithRetry"; -import { isAfterToday } from "@shared/utils/date"; +import { isFuture } from "date-fns"; const SharePopover = lazyWithRetry( () => import("~/components/Sharing/Document") @@ -33,9 +33,9 @@ function ShareButton({ document }: Props) { const share = shares.getByDocumentId(document.id); const sharedParent = shares.getByDocumentParents(document); const domain = share?.domain || sharedParent?.domain; - const isShareExpired = share?.expiresAt && !isAfterToday(share.expiresAt); + const isShareExpired = share?.expiresAt && !isFuture(share.expiresAt); const isParentShareExpired = - sharedParent?.expiresAt && !isAfterToday(sharedParent.expiresAt); + sharedParent?.expiresAt && !isFuture(sharedParent.expiresAt); const hasActiveShare = !!share?.published && !isShareExpired; const hasActiveParentShare = !!sharedParent?.published && !isParentShareExpired; diff --git a/server/routes/api/apiKeys/apiKeys.test.ts b/server/routes/api/apiKeys/apiKeys.test.ts index c0d02c162f..672b617a12 100644 --- a/server/routes/api/apiKeys/apiKeys.test.ts +++ b/server/routes/api/apiKeys/apiKeys.test.ts @@ -6,12 +6,13 @@ import { buildViewer, } from "@server/test/factories"; import { getTestServer } from "@server/test/support"; +import { addMonths } from "date-fns"; const server = getTestServer(); describe("#apiKeys.create", () => { it("should allow creating an api key with expiry", async () => { - const now = new Date(); + const now = addMonths(new Date(), 1); const user = await buildUser(); const res = await server.post("/api/apiKeys.create", user, { diff --git a/server/routes/api/apiKeys/schema.ts b/server/routes/api/apiKeys/schema.ts index d48963d110..632d129852 100644 --- a/server/routes/api/apiKeys/schema.ts +++ b/server/routes/api/apiKeys/schema.ts @@ -4,7 +4,7 @@ import { BaseSchema } from "@server/routes/api/schema"; import AuthenticationHelper from "@shared/helpers/AuthenticationHelper"; import { Scope } from "@shared/types"; import { ApiKeyValidation } from "@shared/validations"; -import { isAfterToday } from "@shared/utils/date"; +import { isFuture } from "date-fns"; const globalScopes = new Set([...Object.values(Scope), "*"]); @@ -29,7 +29,7 @@ export const APIKeysCreateSchema = BaseSchema.extend({ expiresAt: z.coerce .date() .optional() - .refine((value) => !value || isAfterToday(value), { + .refine((value) => !value || isFuture(value), { error: "must be in the future", }), /** A list of scopes that this API key has access to */ diff --git a/server/routes/api/shares/schema.ts b/server/routes/api/shares/schema.ts index 7b52adb1da..6dffed812c 100644 --- a/server/routes/api/shares/schema.ts +++ b/server/routes/api/shares/schema.ts @@ -7,7 +7,7 @@ import { Share } from "@server/models"; import { ValidateURL } from "@server/validation"; import { zodIdType } from "@server/utils/zod"; import { BaseSchema } from "../schema"; -import { isAfterToday } from "@shared/utils/date"; +import { isFuture } from "date-fns"; export const SharesInfoSchema = BaseSchema.extend({ body: z @@ -37,9 +37,7 @@ export const SharesListSchema = BaseSchema.extend({ sort: z .string() .refine((val) => Object.keys(Share.getAttributes()).includes(val), { - message: `must be one of ${Object.keys(Share.getAttributes()).join( - ", " - )}`, + message: `must be one of ${Object.keys(Share.getAttributes()).join(", ")}`, }) .prefault("updatedAt"), direction: z @@ -57,7 +55,7 @@ export const SharesUpdateSchema = BaseSchema.extend({ expiresAt: z.coerce .date() .nullish() - .refine((value) => !value || isAfterToday(value), { + .refine((value) => !value || isFuture(value), { error: "must be in the future", }), includeChildDocuments: z.boolean().optional(), @@ -94,7 +92,7 @@ export const SharesCreateSchema = BaseSchema.extend({ expiresAt: z.coerce .date() .optional() - .refine((value) => !value || isAfterToday(value), { + .refine((value) => !value || isFuture(value), { error: "must be in the future", }), published: z.boolean().prefault(false), diff --git a/shared/i18n/locales/en_US/translation.json b/shared/i18n/locales/en_US/translation.json index d388abb496..e4d4d66c76 100644 --- a/shared/i18n/locales/en_US/translation.json +++ b/shared/i18n/locales/en_US/translation.json @@ -574,6 +574,7 @@ "Allow viewers to subscribe and receive email notifications when documents are updated": "Allow viewers to subscribe and receive email notifications when documents are updated", "Link Expiration": "Link Expiration", "Set an expiration date for the link": "Set an expiration date for the link", + "Expiration": "Expiration", "Never expires": "Never expires", "Something went wrong": "Something went wrong", "Check your email to confirm your subscription": "Check your email to confirm your subscription", @@ -850,7 +851,6 @@ "API key created. Please copy the value now as it will not be shown again.": "API key created. Please copy the value now as it will not be shown again.", "Scopes": "Scopes", "Space-separated scopes restrict the access of this API key to specific parts of the API. Leave blank for full access": "Space-separated scopes restrict the access of this API key to specific parts of the API. Leave blank for full access", - "Expiration": "Expiration", "7 days": "7 days", "30 days": "30 days", "60 days": "60 days", diff --git a/shared/utils/date.test.ts b/shared/utils/date.test.ts index 5432302bc4..597b7f1e90 100644 --- a/shared/utils/date.test.ts +++ b/shared/utils/date.test.ts @@ -2,7 +2,6 @@ import { dateToReadable, dateToRelativeReadable, hasTimeComponent, - isAfterToday, parseISODate, toISODate, toISODateTime, @@ -126,28 +125,3 @@ describe("dateToRelativeReadable", () => { ); }); }); - -describe("isAfterToday", () => { - it("returns true for a future date", () => { - const future = new Date(); - future.setDate(future.getDate() + 1); - expect(isAfterToday(future)).toBe(true); - }); - - it("returns false for a past date", () => { - const past = new Date(); - past.setDate(past.getDate() - 1); - expect(isAfterToday(past)).toBe(false); - }); - - it("returns false for today", () => { - const today = new Date(); - expect(isAfterToday(today)).toBe(false); - }); - - it("accepts an ISO string", () => { - const future = new Date(); - future.setDate(future.getDate() + 7); - expect(isAfterToday(future.toISOString())).toBe(true); - }); -}); diff --git a/shared/utils/date.ts b/shared/utils/date.ts index a8ceabe9b6..79f6fbf993 100644 --- a/shared/utils/date.ts +++ b/shared/utils/date.ts @@ -533,16 +533,3 @@ export function dateToRelativeReadable( } return joinDateAndTime(dateString, format(date, "p", { locale }), language); } - -/** - * Returns true when the given date is in the future relative to now. - * - * @param value - the date to test, either a Date object or an ISO string. - * @returns true if the date is in the future, false otherwise. - */ -export function isAfterToday(value: Date | string): boolean { - const now = new Date(); - const candidate = new Date(value); - - return candidate > now; -}