expiry selection in shareSettingsPopover.tsx

This commit is contained in:
Salihu committed 2026-09-09 14:09:44 +01:00
1 parent 36eb3837e0
commit bb5d548afe
4 files changed
+203 -272

No files matched your search

@@ -5,8 +5,7 @@ import { CopyIcon, GlobeIcon } from "outline-icons";
import * as React from "react";
import { useTranslation } from "react-i18next";
import { toast } from "sonner";
import styled, { useTheme } from "styled-components";
import { differenceInCalendarDays, endOfDay } from "date-fns";
import { useTheme } from "styled-components";
import { errToString } from "@shared/utils/error";
import Squircle from "@shared/components/Squircle";
import { UrlHelper } from "@shared/utils/UrlHelper";
@@ -15,7 +14,6 @@ import type Share from "~/models/Share";
import { AvatarSize } from "~/components/Avatar";
import CopyToClipboard from "~/components/CopyToClipboard";
import Flex from "~/components/Flex";
import { InputSelect, type Option } from "~/components/InputSelect";
import NudeButton from "~/components/NudeButton";
import { ResizingHeightContainer } from "~/components/ResizingHeightContainer";
import Switch from "~/components/Switch";
@@ -24,17 +22,9 @@ import Tooltip from "~/components/Tooltip";
import env from "~/env";
import usePolicy from "~/hooks/usePolicy";
import useStores from "~/hooks/useStores";
import useUserLocale from "~/hooks/useUserLocale";
import { dateToExpiry } from "~/utils/date";
import { ListItem } from "../components/ListItem";
import ShareSettingsPopover from "../components/ShareSettingsPopover";
import { DomainPrefix, ShareLinkInput, StyledInfoIcon } from "../components";
import ExpiryDatePicker from "~/scenes/ApiKeyNew/components/ExpiryDatePicker";
import {
ExpiryType,
ExpiryValues,
calculateExpiryDate,
} from "~/scenes/ApiKeyNew/utils";
type Props = {
/** The collection to share. */
@@ -57,53 +47,11 @@ function InnerPublicAccess(
const collectionAbilities = usePolicy(collection);
const canPublish = share ? can.update : collectionAbilities.share;
const [creating, setCreating] = React.useState(false);
const userLocale = useUserLocale();
const currentDate = React.useRef<Date>(new Date());
const [expiryType, setExpiryType] = React.useState<ExpiryType>(
ExpiryType.NoExpiration
);
const [expiresAt, setExpiresAt] = React.useState<Date | undefined>(
new Date()
);
const expiryOptions = React.useMemo<Option[]>(
() =>
[...ExpiryValues.entries()].map(([expType, { label }]) => ({
type: "item",
label,
value: expType,
})),
[]
);
React.useEffect(() => {
setUrlId(share?.urlId);
}, [share?.urlId]);
React.useEffect(() => {
if (!share?.expiresAt) {
setExpiryType(ExpiryType.NoExpiration);
setExpiresAt(undefined);
return;
}
const parsedExpiresAt = new Date(share.expiresAt);
const daysUntilExpiry = differenceInCalendarDays(
parsedExpiresAt,
currentDate.current
);
const matchedType = [
ExpiryType.Week,
ExpiryType.Month,
ExpiryType.TwoMonths,
ExpiryType.ThreeMonths,
].find((type) => ExpiryValues.get(type)?.value === daysUntilExpiry);
setExpiresAt(parsedExpiresAt);
setExpiryType(matchedType ?? ExpiryType.Custom);
}, []);
const handlePublishedChange = React.useCallback(
async (checked: boolean) => {
try {
@@ -113,15 +61,11 @@ function InnerPublicAccess(
type: "collection",
collectionId: collection.id,
published: true,
expiresAt: expiresAt?.toISOString(),
});
copy(newShare.url);
toast.success(t("Public link copied to clipboard"));
} else if (share) {
await share.save({
published: checked,
expiresAt: expiresAt?.toISOString() ?? null,
});
await share.save({ published: checked });
if (checked) {
copy(share.url);
toast.success(t("Public link copied to clipboard"));
@@ -133,46 +77,7 @@ function InnerPublicAccess(
setCreating(false);
}
},
[t, share, shares, collection, expiresAt]
);
const updateShareExpiry = React.useCallback(
async (date?: Date) => {
if (!share?.published) {
return;
}
try {
await share.save({ expiresAt: date?.toISOString() ?? null });
} catch (err) {
toast.error(errToString(err));
}
},
[share]
);
const handleExpiryTypeChange = React.useCallback(
async (value: string) => {
const nextExpiryType = value as ExpiryType;
const nextExpiresAt = calculateExpiryDate(
currentDate.current,
nextExpiryType
);
setExpiryType(nextExpiryType);
setExpiresAt(nextExpiresAt);
await updateShareExpiry(nextExpiresAt);
},
[updateShareExpiry]
);
const handleSelectCustomDate = React.useCallback(
async (date: Date) => {
const nextExpiresAt = endOfDay(date);
setExpiresAt(nextExpiresAt);
await updateShareExpiry(nextExpiresAt);
},
[updateShareExpiry]
[t, share, shares, collection]
);
const handleUrlChange = React.useMemo(
@@ -264,26 +169,6 @@ function InnerPublicAccess(
</ShareLinkInput>
<ShareSettingsPopover share={share} />
</Flex>
<Flex align="center" gap={8}>
<StyledExpirySelect
options={expiryOptions}
value={expiryType}
onChange={handleExpiryTypeChange}
label={t("Link Expiration")}
/>
{expiryType === ExpiryType.Custom ? (
<ExpiryDatePicker
selectedDate={expiresAt}
onSelect={handleSelectCustomDate}
/>
) : (
<Text type="secondary" size="small">
{expiresAt
? `${dateToExpiry(expiresAt.toISOString(), t, userLocale)}.`
: `${t("Never expires")}.`}
</Text>
)}
</Flex>
<Flex align="flex-start" gap={4}>
<StyledInfoIcon color={theme.textTertiary} />
<Text type="tertiary" size="xsmall">
@@ -300,9 +185,4 @@ function InnerPublicAccess(
);
}
const StyledExpirySelect = styled(InputSelect)`
width: 150px !important;
margin-bottom: 0;
`;
export const PublicAccess = observer(React.forwardRef(InnerPublicAccess));
+21 -147
View File
@@ -5,21 +5,17 @@ import { CopyIcon, GlobeIcon } from "outline-icons";
import * as React from "react";
import { Trans, useTranslation } from "react-i18next";
import { toast } from "sonner";
import styled, { useTheme } from "styled-components";
import { differenceInCalendarDays, endOfDay } from "date-fns";
import { useTheme } from "styled-components";
import { errToString } from "@shared/utils/error";
import Flex from "@shared/components/Flex";
import Squircle from "@shared/components/Squircle";
import { UrlHelper } from "@shared/utils/UrlHelper";
import type Document from "~/models/Document";
import type Share from "~/models/Share";
import { InputSelect, type Option } from "~/components/InputSelect";
import Switch from "~/components/Switch";
import env from "~/env";
import usePolicy from "~/hooks/usePolicy";
import useStores from "~/hooks/useStores";
import useUserLocale from "~/hooks/useUserLocale";
import { dateToExpiry } from "~/utils/date";
import { AvatarSize } from "../../Avatar";
import CopyToClipboard from "../../CopyToClipboard";
import NudeButton from "../../NudeButton";
@@ -34,13 +30,6 @@ import {
StyledInfoIcon,
UnderlinedLink,
} from "../components";
import ExpiryDatePicker from "~/scenes/ApiKeyNew/components/ExpiryDatePicker";
import {
ExpiryType,
ExpiryValues,
calculateExpiryDate,
} from "~/scenes/ApiKeyNew/utils";
import Container from "~/scenes/Document/components/Container";
type Props = {
/** The document to share. */
@@ -68,53 +57,11 @@ function PublicAccess(
const documentAbilities = usePolicy(document);
const canPublish = share ? can.update : documentAbilities.share;
const [creating, setCreating] = React.useState(false);
const userLocale = useUserLocale();
const currentDate = React.useRef<Date>(new Date());
const [expiryType, setExpiryType] = React.useState<ExpiryType>(
ExpiryType.NoExpiration
);
const [expiresAt, setExpiresAt] = React.useState<Date | undefined>(
new Date()
);
const expiryOptions = React.useMemo<Option[]>(
() =>
[...ExpiryValues.entries()].map(([expType, { label }]) => ({
type: "item",
label,
value: expType,
})),
[]
);
React.useEffect(() => {
setUrlId(share?.urlId);
}, [share?.urlId]);
React.useEffect(() => {
if (!share?.expiresAt) {
setExpiryType(ExpiryType.NoExpiration);
setExpiresAt(undefined);
return;
}
const parsedExpiresAt = new Date(share.expiresAt);
const daysUntilExpiry = differenceInCalendarDays(
parsedExpiresAt,
currentDate.current
);
const matchedType = [
ExpiryType.Week,
ExpiryType.Month,
ExpiryType.TwoMonths,
ExpiryType.ThreeMonths,
].find((type) => ExpiryValues.get(type)?.value === daysUntilExpiry);
setExpiresAt(parsedExpiresAt);
setExpiryType(matchedType ?? ExpiryType.Custom);
}, []);
const handlePublishedChange = React.useCallback(
async (checked: boolean) => {
try {
@@ -124,15 +71,11 @@ function PublicAccess(
type: "document",
documentId: document.id,
published: true,
expiresAt: expiresAt?.toISOString(),
});
copy(newShare.url);
toast.success(t("Public link copied to clipboard"));
} else if (share) {
await share.save({
published: checked,
expiresAt: expiresAt?.toISOString() ?? null,
});
await share.save({ published: checked });
if (checked) {
copy(share.url);
toast.success(t("Public link copied to clipboard"));
@@ -144,46 +87,7 @@ function PublicAccess(
setCreating(false);
}
},
[t, share, shares, document, expiresAt]
);
const updateShareExpiry = React.useCallback(
async (date?: Date) => {
if (!share?.published) {
return;
}
try {
await share.save({ expiresAt: date?.toISOString() ?? null });
} catch (err) {
toast.error(errToString(err));
}
},
[share]
);
const handleExpiryTypeChange = React.useCallback(
async (value: string) => {
const nextExpiryType = value as ExpiryType;
const nextExpiresAt = calculateExpiryDate(
currentDate.current,
nextExpiryType
);
setExpiryType(nextExpiryType);
setExpiresAt(nextExpiresAt);
await updateShareExpiry(nextExpiresAt);
},
[updateShareExpiry]
);
const handleSelectCustomDate = React.useCallback(
async (date: Date) => {
const nextExpiresAt = endOfDay(date);
setExpiresAt(nextExpiresAt);
await updateShareExpiry(nextExpiresAt);
},
[updateShareExpiry]
[t, share, shares, document]
);
const handleUrlChange = React.useMemo(
@@ -294,48 +198,24 @@ function PublicAccess(
{copyButton}
</ShareLinkInput>
) : share?.published ? (
<>
<Flex align="center" gap={2}>
<ShareLinkInput
type="text"
ref={inputRef}
placeholder={share?.id}
onChange={handleUrlChange}
error={validationError}
defaultValue={urlId}
prefix={
<DomainPrefix onClick={() => inputRef.current?.focus()}>
{env.URL.replace(/https?:\/\//, "") + "/s/"}
</DomainPrefix>
}
>
{copyButton}
</ShareLinkInput>
<ShareSettingsPopover share={share} />
</Flex>
<Flex align="center" gap={8}>
<StyledExpirySelect
options={expiryOptions}
value={expiryType}
onChange={handleExpiryTypeChange}
label={t("Link Expiration")}
/>
{expiryType === ExpiryType.Custom ? (
<Container style={{ marginTop: 16 }}>
<ExpiryDatePicker
selectedDate={expiresAt}
onSelect={handleSelectCustomDate}
/>
</Container>
) : (
<Text type="secondary" size="small" style={{ marginTop: 25 }}>
{expiresAt
? `${dateToExpiry(expiresAt.toISOString(), t, userLocale)}.`
: `${t("Never expires")}.`}
</Text>
)}
</Flex>
</>
<Flex align="center" gap={2}>
<ShareLinkInput
type="text"
ref={inputRef}
placeholder={share?.id}
onChange={handleUrlChange}
error={validationError}
defaultValue={urlId}
prefix={
<DomainPrefix onClick={() => inputRef.current?.focus()}>
{env.URL.replace(/https?:\/\//, "") + "/s/"}
</DomainPrefix>
}
>
{copyButton}
</ShareLinkInput>
<ShareSettingsPopover share={share} />
</Flex>
) : null}
{share?.published && !share.includeChildDocuments ? (
@@ -354,10 +234,4 @@ function PublicAccess(
);
}
const StyledExpirySelect = styled(InputSelect)`
width: 150px !important;
margin-bottom: 0;
margin-top: 0;
`;
export default observer(React.forwardRef(PublicAccess));
@@ -37,6 +37,17 @@ import {
PopoverContent,
} from "~/components/primitives/Popover";
import { ListItem } from "./ListItem";
import ExpiryDatePicker from "~/scenes/ApiKeyNew/components/ExpiryDatePicker";
import { InputSelect, type Option } from "~/components/InputSelect";
import useUserLocale from "~/hooks/useUserLocale";
import {
calculateExpiryDate,
ExpiryType,
ExpiryValues,
} from "~/scenes/ApiKeyNew/utils";
import { dateToExpiry } from "~/utils/date";
import { differenceInCalendarDays, endOfDay } from "date-fns";
import Container from "~/scenes/Document/components/Container";
type Props = {
/** The share model to configure settings for. */
@@ -391,11 +402,176 @@ function ShareSettingsPopover({ share, children }: Props) {
}
/>
)}
<LinkExpiration share={share} />
</PopoverContent>
</Popover>
);
}
function LinkExpiration({ share }: Props) {
const { t } = useTranslation();
const idPrefix = React.useMemo(() => uniqueId("share-settings-"), []);
const expirationId = `${idPrefix}-expiration`;
const userLocale = useUserLocale();
const currentDate = React.useRef<Date>(new Date());
const [expiryType, setExpiryType] = React.useState<ExpiryType>(
ExpiryType.Month
);
const [expiresAt, setExpiresAt] = React.useState<Date | undefined>(undefined);
const [showExpiration, setShowExpiration] = React.useState(
share.expiresAt ? true : false
);
React.useEffect(() => {
if (!share?.expiresAt) {
setExpiryType(ExpiryType.NoExpiration);
setExpiresAt(undefined);
return;
}
const parsedExpiresAt = new Date(share.expiresAt);
const daysUntilExpiry = differenceInCalendarDays(
parsedExpiresAt,
currentDate.current
);
const matchedType = [
ExpiryType.Week,
ExpiryType.Month,
ExpiryType.TwoMonths,
ExpiryType.ThreeMonths,
].find((type) => ExpiryValues.get(type)?.value === daysUntilExpiry);
setExpiresAt(parsedExpiresAt);
setExpiryType(matchedType ?? ExpiryType.Custom);
}, [
// we don't want to trigger when we update the share's expiresAt
]);
const expiryOptions = React.useMemo<Option[]>(
() =>
[...ExpiryValues.entries()].flatMap(([expType, { label }]) =>
expType === ExpiryType.NoExpiration
? []
: [
{
type: "item",
label,
value: expType,
},
]
),
[]
);
const updateShareExpiry = React.useCallback(
async (date?: Date) => {
if (!share?.published) {
return;
}
try {
await share.save({ expiresAt: date?.toISOString() ?? null });
} catch (err) {
toast.error(errToString(err));
}
},
[share]
);
const handleExpiryTypeChange = React.useCallback(
async (value: string) => {
const nextExpiryType = value as ExpiryType;
const nextExpiresAt = calculateExpiryDate(
currentDate.current,
nextExpiryType
);
setExpiryType(nextExpiryType);
setExpiresAt(nextExpiresAt);
await updateShareExpiry(nextExpiresAt);
},
[updateShareExpiry]
);
const handleSelectCustomDate = React.useCallback(
async (date: Date) => {
const nextExpiresAt = endOfDay(date);
setExpiresAt(nextExpiresAt);
await updateShareExpiry(nextExpiresAt);
},
[updateShareExpiry]
);
const handleShowExpirationOptions = React.useCallback(
async (val: boolean) => {
setShowExpiration(val);
if (val === true) {
await handleExpiryTypeChange(expiresAt ? expiryType : ExpiryType.Month);
}
},
[handleExpiryTypeChange, setShowExpiration, expiryType, expiresAt]
);
return (
<>
<ListItem
title={
<SwitchLabel htmlFor={expirationId}>
{t("Link Expiration")}&nbsp;
<Tooltip content={t("Set an expiration date for the link")}>
<NudeButton size={18} aria-label={t("More information")}>
<QuestionMarkIcon size={18} />
</NudeButton>
</Tooltip>
</SwitchLabel>
}
actions={
<Switch
id={expirationId}
checked={showExpiration}
onChange={handleShowExpirationOptions}
width={26}
height={14}
/>
}
/>
{showExpiration && (
<HStack align="center" justify="space-between" style={{ marginTop: 5 }}>
<StyledExpirySelect
label=""
options={expiryOptions}
value={expiryType}
onChange={handleExpiryTypeChange}
/>
{expiryType === ExpiryType.Custom ? (
<Container style={{ marginTop: -8 }}>
<ExpiryDatePicker
selectedDate={expiresAt}
onSelect={handleSelectCustomDate}
/>
</Container>
) : (
<Text type="secondary" size="small">
{expiresAt
? `${dateToExpiry(expiresAt.toISOString(), t, userLocale)}.`
: `${t("Never expires")}.`}
</Text>
)}
</HStack>
)}
</>
);
}
const StyledExpirySelect = styled(InputSelect)`
width: 150px !important;
margin-bottom: 0;
margin-top: 0;
`;
const SwitchLabel = styled.label`
display: flex;
align-items: center;
+3 -2
View File
@@ -541,8 +541,6 @@
"Publish to web": "Publish to web",
"Allow anyone with the link to access": "Allow anyone with the link to access",
"Publish to internet": "Publish to internet",
"Link Expiration": "Link Expiration",
"Never expires": "Never expires",
"All documents in this collection will be shared on the web, including any new documents added later": "All documents in this collection will be shared on the web, including any new documents added later",
"Invite": "Invite",
"{{ userName }} was added to the collection": "{{ userName }} was added to the collection",
@@ -573,6 +571,9 @@
"Disable this setting to discourage search engines from indexing the page": "Disable this setting to discourage search engines from indexing the page",
"Email subscriptions": "Email subscriptions",
"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",
"Never expires": "Never expires",
"Something went wrong": "Something went wrong",
"Check your email to confirm your subscription": "Check your email to confirm your subscription",
"Get notified when this document is updated": "Get notified when this document is updated",