From 5f3cc0d50b540ff25b2bc3dd3635e9cab98aa2fb Mon Sep 17 00:00:00 2001 From: Salihu Date: Fri, 24 Jul 2026 16:31:22 +0100 Subject: [PATCH] fix bugs --- .../Sharing/Collection/PublicAccess.tsx | 4 +-- .../Sharing/Document/PublicAccess.tsx | 6 ++--- .../components/ShareSettingsPopover.tsx | 12 ++++++++- .../ApiKeyNew/components/ExpiryDatePicker.tsx | 13 +++++++--- .../Collection/components/ShareButton.tsx | 4 +-- .../Document/components/ShareButton.tsx | 6 ++--- server/routes/api/apiKeys/schema.ts | 4 +-- server/routes/api/shares/schema.ts | 6 ++--- shared/utils/date.test.ts | 26 +++++++++++++++++++ shared/utils/date.ts | 15 ++++++----- 10 files changed, 71 insertions(+), 25 deletions(-) diff --git a/app/components/Sharing/Collection/PublicAccess.tsx b/app/components/Sharing/Collection/PublicAccess.tsx index bbaae134ac..9621ef4397 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 { isTodayOrAfter } from "@shared/utils/date"; +import { isAfterToday } from "@shared/utils/date"; 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 && !isTodayOrAfter(share.expiresAt); + const isShareExpired = share?.expiresAt && !isAfterToday(share.expiresAt); React.useEffect(() => { setUrlId(share?.urlId); diff --git a/app/components/Sharing/Document/PublicAccess.tsx b/app/components/Sharing/Document/PublicAccess.tsx index da5f094f41..5ba9a1f72b 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 { isTodayOrAfter } from "@shared/utils/date"; +import { isAfterToday } from "@shared/utils/date"; 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 && !isTodayOrAfter(share.expiresAt); + const isShareExpired = share?.expiresAt && !isAfterToday(share.expiresAt); const isParentShareExpired = - sharedParent?.expiresAt && !isTodayOrAfter(sharedParent.expiresAt); + sharedParent?.expiresAt && !isAfterToday(sharedParent.expiresAt); const hasExpiredShare = (!!share?.published && isShareExpired) || diff --git a/app/components/Sharing/components/ShareSettingsPopover.tsx b/app/components/Sharing/components/ShareSettingsPopover.tsx index 69654143dc..e4a1a9548c 100644 --- a/app/components/Sharing/components/ShareSettingsPopover.tsx +++ b/app/components/Sharing/components/ShareSettingsPopover.tsx @@ -511,8 +511,17 @@ function LinkExpiration({ share }: Props) { if (val === true) { await handleExpiryTypeChange(expiresAt ? expiryType : ExpiryType.Month); } + if (val === false) { + await updateShareExpiry(undefined); + } }, - [handleExpiryTypeChange, setShowExpiration, expiryType, expiresAt] + [ + handleExpiryTypeChange, + setShowExpiration, + expiryType, + expiresAt, + updateShareExpiry, + ] ); return ( @@ -549,6 +558,7 @@ function LinkExpiration({ share }: Props) { {expiryType === ExpiryType.Custom ? ( diff --git a/app/scenes/ApiKeyNew/components/ExpiryDatePicker.tsx b/app/scenes/ApiKeyNew/components/ExpiryDatePicker.tsx index 516e9985da..c1c75bc0e4 100644 --- a/app/scenes/ApiKeyNew/components/ExpiryDatePicker.tsx +++ b/app/scenes/ApiKeyNew/components/ExpiryDatePicker.tsx @@ -1,4 +1,4 @@ -import { format as formatDate } from "date-fns"; +import { addDays, format as formatDate } from "date-fns"; import * as React from "react"; import { useTranslation } from "react-i18next"; import styled from "styled-components"; @@ -15,9 +15,14 @@ import useUserLocale from "~/hooks/useUserLocale"; type Props = { selectedDate?: Date; onSelect: (date: Date) => void; + allowToday?: boolean; }; -const ExpiryDatePicker = ({ selectedDate, onSelect }: Props) => { +const ExpiryDatePicker = ({ + selectedDate, + onSelect, + allowToday = true, +}: Props) => { const { t } = useTranslation(); const [open, setOpen] = React.useState(false); @@ -53,7 +58,9 @@ const ExpiryDatePicker = ({ selectedDate, onSelect }: Props) => { selected={selectedDate} onSelect={handleSelect} locale={locale} - disabled={{ before: new Date() }} + disabled={{ + before: allowToday ? new Date() : addDays(new Date(), 1), + }} /> diff --git a/app/scenes/Collection/components/ShareButton.tsx b/app/scenes/Collection/components/ShareButton.tsx index b2b8f56dfd..bb02118865 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 { isTodayOrAfter } from "@shared/utils/date"; +import { isAfterToday } from "@shared/utils/date"; 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 && !isTodayOrAfter(share.expiresAt); + const isShareExpired = share?.expiresAt && !isAfterToday(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 21c6c524db..4160a44dc9 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 { isTodayOrAfter } from "@shared/utils/date"; +import { isAfterToday } from "@shared/utils/date"; 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 && !isTodayOrAfter(share.expiresAt); + const isShareExpired = share?.expiresAt && !isAfterToday(share.expiresAt); const isParentShareExpired = - sharedParent?.expiresAt && !isTodayOrAfter(sharedParent.expiresAt); + sharedParent?.expiresAt && !isAfterToday(sharedParent.expiresAt); const hasActiveShare = !!share?.published && !isShareExpired; const hasActiveParentShare = !!sharedParent?.published && !isParentShareExpired; diff --git a/server/routes/api/apiKeys/schema.ts b/server/routes/api/apiKeys/schema.ts index 4c2a81f750..d61b530f62 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 { isTodayOrAfter } from "@shared/utils/date"; +import { isAfterToday } from "@shared/utils/date"; const globalScopes = new Set([...Object.values(Scope), "*"]); @@ -29,7 +29,7 @@ export const APIKeysCreateSchema = BaseSchema.extend({ expiresAt: z.coerce .date() .optional() - .refine((value) => !value || isTodayOrAfter(value), { + .refine((value) => !value || isAfterToday(value), { error: "must be today or later", }), /** 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 749e09a774..7f1cb37b77 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 { isTodayOrAfter } from "@shared/utils/date"; +import { isAfterToday } from "@shared/utils/date"; export const SharesInfoSchema = BaseSchema.extend({ body: z @@ -57,7 +57,7 @@ export const SharesUpdateSchema = BaseSchema.extend({ expiresAt: z.coerce .date() .nullish() - .refine((value) => !value || isTodayOrAfter(value), { + .refine((value) => !value || isAfterToday(value), { error: "must be today or later", }), includeChildDocuments: z.boolean().optional(), @@ -94,7 +94,7 @@ export const SharesCreateSchema = BaseSchema.extend({ expiresAt: z.coerce .date() .optional() - .refine((value) => !value || isTodayOrAfter(value), { + .refine((value) => !value || isAfterToday(value), { error: "must be today or later", }), published: z.boolean().prefault(false), diff --git a/shared/utils/date.test.ts b/shared/utils/date.test.ts index 597b7f1e90..5432302bc4 100644 --- a/shared/utils/date.test.ts +++ b/shared/utils/date.test.ts @@ -2,6 +2,7 @@ import { dateToReadable, dateToRelativeReadable, hasTimeComponent, + isAfterToday, parseISODate, toISODate, toISODateTime, @@ -125,3 +126,28 @@ 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 d7277984f3..7a7620a219 100644 --- a/shared/utils/date.ts +++ b/shared/utils/date.ts @@ -534,12 +534,15 @@ export function dateToRelativeReadable( return joinDateAndTime(dateString, format(date, "p", { locale }), language); } -export function isTodayOrAfter(value: Date | string) { - const today = new Date(); - today.setHours(0, 0, 0, 0); - +/** + * Returns true when the given date is a future day. + * + * @param value - the date to test, either a Date object or an ISO string. + * @returns true if the date is in the future, false if it is in not. + */ +export function isAfterToday(value: Date | string): boolean { + const now = new Date(); const candidate = new Date(value); - candidate.setHours(0, 0, 0, 0); - return candidate >= today; + return candidate > now; }