import { ActionIcon, Anchor, Button, Group, Indicator, Popover, Switch, Text, TextInput, Tooltip, } from "@mantine/core"; import { IconExternalLink, IconWorld } from "@tabler/icons-react"; import React, { useEffect, useMemo, useState } from "react"; import { useCreateShareMutation, useDeleteShareMutation, useShareForPageQuery, useUpdateShareMutation, } from "@/features/share/queries/share-query.ts"; import { Link, useParams } from "react-router-dom"; import { extractPageSlugId, getPageIcon } from "@/lib"; import { useTranslation } from "react-i18next"; import CopyTextButton from "@/components/common/copy.tsx"; import { getAppUrl } from "@/lib/config.ts"; import { buildPageUrl } from "@/features/page/page.utils.ts"; import classes from "@/features/share/components/share.module.css"; interface ShareModalProps { readOnly: boolean; } export default function ShareModal({ readOnly }: ShareModalProps) { const { t } = useTranslation(); const { pageSlug } = useParams(); const pageId = extractPageSlugId(pageSlug); const { data: share } = useShareForPageQuery(pageId); const { spaceSlug } = useParams(); const createShareMutation = useCreateShareMutation(); const updateShareMutation = useUpdateShareMutation(); const deleteShareMutation = useDeleteShareMutation(); // pageIsShared means that the share exists and its level equals zero. const pageIsShared = share && share.level === 0; // if level is greater than zero, then it is a descendant page from a shared page const isDescendantShared = share && share.level > 0; const publicLink = `${getAppUrl()}/share/${share?.key}/p/${pageSlug}`; const [isPagePublic, setIsPagePublic] = useState(false); useEffect(() => { if (share) { setIsPagePublic(true); } else { setIsPagePublic(false); } }, [share, pageId]); const handleChange = async (event: React.ChangeEvent) => { const value = event.currentTarget.checked; if (value) { createShareMutation.mutateAsync({ pageId: pageId, includeSubPages: true, searchIndexing: true, }); 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]); return ( {isDescendantShared ? ( <> {t("Inherits public sharing from")} {getPageIcon(share.sharedPage.icon)}
{share.sharedPage.title || t("untitled")}
{shareLink} ) : ( <>
{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")}
)} )}
); }