diff --git a/app/components/Sharing/Collection/PublicAccess.tsx b/app/components/Sharing/Collection/PublicAccess.tsx index 10351d0f08..fcf2ffad50 100644 --- a/app/components/Sharing/Collection/PublicAccess.tsx +++ b/app/components/Sharing/Collection/PublicAccess.tsx @@ -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(new Date()); - const [expiryType, setExpiryType] = React.useState( - ExpiryType.NoExpiration - ); - const [expiresAt, setExpiresAt] = React.useState( - new Date() - ); - - const expiryOptions = React.useMemo( - () => - [...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( - - - {expiryType === ExpiryType.Custom ? ( - - ) : ( - - {expiresAt - ? `${dateToExpiry(expiresAt.toISOString(), t, userLocale)}.` - : `${t("Never expires")}.`} - - )} - @@ -300,9 +185,4 @@ function InnerPublicAccess( ); } -const StyledExpirySelect = styled(InputSelect)` - width: 150px !important; - margin-bottom: 0; -`; - export const PublicAccess = observer(React.forwardRef(InnerPublicAccess)); diff --git a/app/components/Sharing/Document/PublicAccess.tsx b/app/components/Sharing/Document/PublicAccess.tsx index 75ff325f4a..e57a2fd3e9 100644 --- a/app/components/Sharing/Document/PublicAccess.tsx +++ b/app/components/Sharing/Document/PublicAccess.tsx @@ -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(new Date()); - const [expiryType, setExpiryType] = React.useState( - ExpiryType.NoExpiration - ); - const [expiresAt, setExpiresAt] = React.useState( - new Date() - ); - - const expiryOptions = React.useMemo( - () => - [...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} ) : share?.published ? ( - <> - - inputRef.current?.focus()}> - {env.URL.replace(/https?:\/\//, "") + "/s/"} - - } - > - {copyButton} - - - - - - {expiryType === ExpiryType.Custom ? ( - - - - ) : ( - - {expiresAt - ? `${dateToExpiry(expiresAt.toISOString(), t, userLocale)}.` - : `${t("Never expires")}.`} - - )} - - + + inputRef.current?.focus()}> + {env.URL.replace(/https?:\/\//, "") + "/s/"} + + } + > + {copyButton} + + + ) : 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)); diff --git a/app/components/Sharing/components/ShareSettingsPopover.tsx b/app/components/Sharing/components/ShareSettingsPopover.tsx index 3f3baaabcc..d45161fd75 100644 --- a/app/components/Sharing/components/ShareSettingsPopover.tsx +++ b/app/components/Sharing/components/ShareSettingsPopover.tsx @@ -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) { } /> )} + + ); } +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(new Date()); + const [expiryType, setExpiryType] = React.useState( + ExpiryType.Month + ); + const [expiresAt, setExpiresAt] = React.useState(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( + () => + [...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 ( + <> + + {t("Link Expiration")}  + + + + + + + } + actions={ + + } + /> + {showExpiration && ( + + + {expiryType === ExpiryType.Custom ? ( + + + + ) : ( + + {expiresAt + ? `${dateToExpiry(expiresAt.toISOString(), t, userLocale)}.` + : `${t("Never expires")}.`} + + )} + + )} + + ); +} + +const StyledExpirySelect = styled(InputSelect)` + width: 150px !important; + margin-bottom: 0; + margin-top: 0; +`; + const SwitchLabel = styled.label` display: flex; align-items: center; diff --git a/shared/i18n/locales/en_US/translation.json b/shared/i18n/locales/en_US/translation.json index 8e9dd6b884..16c7bea194 100644 --- a/shared/i18n/locales/en_US/translation.json +++ b/shared/i18n/locales/en_US/translation.json @@ -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",