diff --git a/app/components/Sharing/Collection/PublicAccess.tsx b/app/components/Sharing/Collection/PublicAccess.tsx index fcf2ffad50..0c7734cece 100644 --- a/app/components/Sharing/Collection/PublicAccess.tsx +++ b/app/components/Sharing/Collection/PublicAccess.tsx @@ -5,7 +5,8 @@ import { CopyIcon, GlobeIcon } from "outline-icons"; import * as React from "react"; import { useTranslation } from "react-i18next"; import { toast } from "sonner"; -import { useTheme } from "styled-components"; +import styled, { useTheme } from "styled-components"; +import { differenceInCalendarDays, endOfDay } from "date-fns"; import { errToString } from "@shared/utils/error"; import Squircle from "@shared/components/Squircle"; import { UrlHelper } from "@shared/utils/UrlHelper"; @@ -14,6 +15,7 @@ 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"; @@ -22,9 +24,17 @@ 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. */ @@ -47,11 +57,53 @@ 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.Month + ); + const [expiresAt, setExpiresAt] = React.useState(() => + calculateExpiryDate(currentDate.current, ExpiryType.Month) + ); + + 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); + }, [share?.expiresAt]); + const handlePublishedChange = React.useCallback( async (checked: boolean) => { try { @@ -61,11 +113,15 @@ 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 }); + await share.save({ + published: checked, + expiresAt: expiresAt?.toISOString() ?? null, + }); if (checked) { copy(share.url); toast.success(t("Public link copied to clipboard")); @@ -77,7 +133,46 @@ function InnerPublicAccess( setCreating(false); } }, - [t, share, shares, collection] + [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] ); const handleUrlChange = React.useMemo( @@ -169,6 +264,26 @@ function InnerPublicAccess( + + + {expiryType === ExpiryType.Custom ? ( + + ) : ( + + {expiresAt + ? `${dateToExpiry(expiresAt.toISOString(), t, userLocale)}.` + : `${t("Never expires")}.`} + + )} + @@ -185,4 +300,9 @@ 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 e57a2fd3e9..f0dc115ff9 100644 --- a/app/components/Sharing/Document/PublicAccess.tsx +++ b/app/components/Sharing/Document/PublicAccess.tsx @@ -5,17 +5,21 @@ import { CopyIcon, GlobeIcon } from "outline-icons"; import * as React from "react"; import { Trans, useTranslation } from "react-i18next"; import { toast } from "sonner"; -import { useTheme } from "styled-components"; +import styled, { useTheme } from "styled-components"; +import { differenceInCalendarDays, endOfDay } from "date-fns"; 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"; @@ -30,6 +34,12 @@ import { StyledInfoIcon, UnderlinedLink, } from "../components"; +import ExpiryDatePicker from "~/scenes/ApiKeyNew/components/ExpiryDatePicker"; +import { + ExpiryType, + ExpiryValues, + calculateExpiryDate, +} from "~/scenes/ApiKeyNew/utils"; type Props = { /** The document to share. */ @@ -57,11 +67,53 @@ 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.Month + ); + const [expiresAt, setExpiresAt] = React.useState(() => + calculateExpiryDate(currentDate.current, ExpiryType.Month) + ); + + 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); + }, [share?.expiresAt]); + const handlePublishedChange = React.useCallback( async (checked: boolean) => { try { @@ -71,11 +123,15 @@ 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 }); + await share.save({ + published: checked, + expiresAt: expiresAt?.toISOString() ?? null, + }); if (checked) { copy(share.url); toast.success(t("Public link copied to clipboard")); @@ -87,7 +143,46 @@ function PublicAccess( setCreating(false); } }, - [t, share, shares, document] + [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] ); const handleUrlChange = React.useMemo( @@ -198,24 +293,46 @@ function PublicAccess( {copyButton} ) : share?.published ? ( - - inputRef.current?.focus()}> - {env.URL.replace(/https?:\/\//, "") + "/s/"} - - } - > - {copyButton} - - - + <> + + inputRef.current?.focus()}> + {env.URL.replace(/https?:\/\//, "") + "/s/"} + + } + > + {copyButton} + + + + + + {expiryType === ExpiryType.Custom ? ( + + ) : ( + + {expiresAt + ? `${dateToExpiry(expiresAt.toISOString(), t, userLocale)}.` + : `${t("Never expires")}.`} + + )} + + ) : null} {share?.published && !share.includeChildDocuments ? ( @@ -234,4 +351,9 @@ function PublicAccess( ); } +const StyledExpirySelect = styled(InputSelect)` + width: 150px !important; + margin-bottom: 0; +`; + export default observer(React.forwardRef(PublicAccess)); diff --git a/app/models/Share.ts b/app/models/Share.ts index 76fe11d8a3..f4695ea21f 100644 --- a/app/models/Share.ts +++ b/app/models/Share.ts @@ -27,6 +27,10 @@ class Share extends Model implements Searchable { @observable includeChildDocuments: boolean; + @Field + @observable + expiresAt: string | null; + /** The document ID that is shared. */ @Field @observable diff --git a/app/scenes/Errors/ErrorExpiredLink.tsx b/app/scenes/Errors/ErrorExpiredLink.tsx new file mode 100644 index 0000000000..741a6fb4d5 --- /dev/null +++ b/app/scenes/Errors/ErrorExpiredLink.tsx @@ -0,0 +1,39 @@ +import { Trans, useTranslation } from "react-i18next"; +import Button from "~/components/Button"; +import Empty from "~/components/Empty"; +import Heading from "~/components/Heading"; +import Scene from "~/components/Scene"; +import { + navigateToHome, + navigateToSearch, +} from "~/actions/definitions/navigation"; +import { VStack } from "~/components/primitives/VStack"; +import { HStack } from "~/components/primitives/HStack"; + +const ErrorExpiredLink = () => { + const { t } = useTranslation(); + + return ( + + {t("Expired Link")} + + + + This Link has expired or has been deactivated. Please contact a + document admin to restore access. + + + + + + + + + ); +}; + +export default ErrorExpiredLink; diff --git a/app/scenes/Settings/components/SharesTable.tsx b/app/scenes/Settings/components/SharesTable.tsx index 396a0ef16f..e36dceae65 100644 --- a/app/scenes/Settings/components/SharesTable.tsx +++ b/app/scenes/Settings/components/SharesTable.tsx @@ -114,6 +114,28 @@ export function SharesTable({ data, canManage, ...rest }: Props) { ) : null, width: "2fr", }, + { + type: "data", + id: "expiresAt", + header: t("Expires"), + accessor: (share) => share.expiresAt, + component: (share) => + share.expiresAt ? ( +