This commit is contained in:
Salihu committed 2026-09-09 14:11:51 +01:00
1 parent 7acc46141c
commit 5f3cc0d50b
10 files changed
+71 -25

No files matched your search

@@ -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);
@@ -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) ||
@@ -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 ? (
<Container style={{ marginTop: -8 }}>
<ExpiryDatePicker
allowToday={false}
selectedDate={expiresAt}
onSelect={handleSelectCustomDate}
/>
@@ -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),
}}
/>
</PopoverContent>
</Popover>
@@ -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 &&
@@ -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;
+2 -2
View File
@@ -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<string>([...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 */
+3 -3
View File
@@ -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),
+26
View File
@@ -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);
});
});
+9 -6
View File
@@ -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;
}