import { useEffect, useMemo, useState } from "react"; import { ActionIcon, Anchor, Button, Group, Stack, Switch, Text, TextInput, } from "@mantine/core"; import { IconExternalLink, IconLock } from "@tabler/icons-react"; import { Link, useNavigate, useParams } from "react-router-dom"; import { useTranslation } from "react-i18next"; import { getPageIcon } from "@/lib"; import CopyTextButton from "@/components/common/copy"; import { getAppUrl, isCloud } from "@/lib/config"; import { buildPageUrl } from "@/features/page/page.utils"; import { useCreateShareMutation, useDeleteShareMutation, useShareForPageQuery, useUpdateShareMutation, } from "@/features/share/queries/share-query"; import useTrial from "@/ee/hooks/use-trial"; type PublishTabProps = { pageId: string; readOnly?: boolean; isRestricted?: boolean; workspaceSharingDisabled?: boolean; spaceSharingDisabled?: boolean; }; export function PublishTab({ pageId, readOnly, isRestricted, workspaceSharingDisabled, spaceSharingDisabled }: PublishTabProps) { const { t } = useTranslation(); const navigate = useNavigate(); const { pageSlug, spaceSlug } = useParams(); const { isTrial } = useTrial(); const { data: share } = useShareForPageQuery(pageId); const createShareMutation = useCreateShareMutation(); const updateShareMutation = useUpdateShareMutation(); const deleteShareMutation = useDeleteShareMutation(); const pageIsShared = share && share.level === 0; const isDescendantShared = share && share.level > 0; const publicLink = `${getAppUrl()}/share/${share?.key}/p/${pageSlug}`; const [isPagePublic, setIsPagePublic] = useState(false); useEffect(() => { setIsPagePublic(!!share); }, [share, pageId]); const handleChange = async (event: React.ChangeEvent) => { const value = event.currentTarget.checked; if (value) { createShareMutation.mutateAsync({ pageId: pageId, includeSubPages: true, searchIndexing: false, }); setIsPagePublic(value); } else { if (share && share.id) { deleteShareMutation.mutateAsync(share.id); setIsPagePublic(value); } } }; const handleSubPagesChange = async ( event: React.ChangeEvent, ) => { const value = event.currentTarget.checked; updateShareMutation.mutateAsync({ shareId: share.id, includeSubPages: value, }); }; const handleIndexSearchChange = async ( event: React.ChangeEvent, ) => { const value = event.currentTarget.checked; updateShareMutation.mutateAsync({ shareId: share.id, searchIndexing: value, }); }; const shareLink = useMemo( () => ( } style={{ width: "100%" }} /> ), [publicLink], ); if (isCloud() && isTrial) { return ( {t("Upgrade to share pages")} {t( "Page sharing is available on paid plans. Upgrade to share your pages publicly.", )} ); } if (workspaceSharingDisabled || spaceSharingDisabled) { return ( {t("Public sharing is disabled")} {workspaceSharingDisabled ? t("Public sharing has been disabled at the workspace level.") : t("Public sharing has been disabled for this space.")} ); } if (isRestricted) { return ( {t("Restricted page")} {t("Restricted pages cannot be shared publicly.")} ); } if (isDescendantShared) { return ( {t("Inherits public sharing from")} {getPageIcon(share.sharedPage.icon)} {share.sharedPage.title || t("untitled")} {shareLink} ); } return (
{isPagePublic ? t("Shared to web") : t("Share to web")} {isPagePublic ? t("Anyone with the link can view this page") : t("Make this page publicly accessible")}
{pageIsShared && ( <> {shareLink}
{t("Include sub-pages")} {t("Make sub-pages public too")}
{t("Search engine indexing")} {t("Allow search engines to index page")}
)}
); }