import { useReplicache } from "src/replicache"; import React, { useEffect, useState } from "react"; import { getShareLink } from "./getShareLink"; import { useEntitySetContext } from "components/EntitySetProvider"; import { useSmoker } from "components/Toast"; import { Menu, MenuItem } from "components/Layout"; import { ActionButton } from "components/ActionBar/ActionButton"; import useSWR from "swr"; import LoginForm from "app/login/LoginForm"; import { CustomDomainMenu } from "./DomainOptions"; import { useIdentityData } from "components/IdentityProvider"; import { useLeafletDomains, useLeafletPublicationData, } from "components/PageSWRDataProvider"; import { ShareSmall } from "components/Icons/ShareSmall"; import { PubLeafletDocument } from "lexicons/api"; import { getPublicationURL } from "app/lish/createPub/getPublicationURL"; import { AtUri } from "@atproto/syntax"; import { useIsMobile } from "src/hooks/isMobile"; export type ShareMenuStates = "default" | "login" | "domain"; export let usePublishLink = () => { let { permission_token, rootEntity } = useReplicache(); let entity_set = useEntitySetContext(); let { data: publishLink } = useSWR( "publishLink-" + permission_token.id, async () => { if ( !permission_token.permission_token_rights.find( (s) => s.entity_set === entity_set.set && s.create_token, ) ) return; let shareLink = await getShareLink( { id: permission_token.id, entity_set: entity_set.set }, rootEntity, ); return shareLink?.id; }, ); return publishLink; }; export function ShareOptions() { let [menuState, setMenuState] = useState("default"); let { data: pub } = useLeafletPublicationData(); let isMobile = useIsMobile(); return ( { setMenuState("default"); }} trigger={ primary={!!!pub} secondary={!!pub} label={`Share ${pub ? "Draft" : ""}`} /> } > {menuState === "login" ? (
) : menuState === "domain" ? ( ) : ( )}
); } const ShareMenu = (props: { setMenuState: (state: ShareMenuStates) => void; domainConnected: boolean; isPub?: boolean; }) => { let { permission_token } = useReplicache(); let { data: pub } = useLeafletPublicationData(); let record = pub?.documents?.data as PubLeafletDocument.Record | null; let postLink = pub?.publications && pub.documents ? `${getPublicationURL(pub.publications)}/${new AtUri(pub?.documents.uri).rkey}` : null; let publishLink = usePublishLink(); let [collabLink, setCollabLink] = useState(null); useEffect(() => { // strip leading '/' character from pathname setCollabLink(window.location.pathname.slice(1)); }, []); let { data: domains } = useLeafletDomains(); return ( <> {domains?.[0] ? ( <> This Leaflet is published on{" "} {domains[0].domain} {domains[0].route} ) : ( "" )} smokerText="View link copied!" id="get-view-link" fullLink={ domains?.[0] ? `https://${domains[0].domain}${domains[0].route}` : undefined } link={publishLink || ""} /> {postLink && ( <>
)} {!props.isPub && ( <>
)} ); }; export const ShareButton = (props: { text: React.ReactNode; subtext?: React.ReactNode; smokerText: string; id: string; link: null | string; fullLink?: string; className?: string; }) => { let smoker = useSmoker(); return ( { e.preventDefault(); let rect = document.getElementById(props.id)?.getBoundingClientRect(); if (props.link || props.fullLink) { navigator.clipboard.writeText( props.fullLink ? props.fullLink : `${location.protocol}//${location.host}/${props.link}`, ); smoker({ position: { x: rect ? rect.left + (rect.right - rect.left) / 2 : 0, y: rect ? rect.top + 26 : 0, }, text: props.smokerText, }); } }} >
{props.text} {props.subtext && (
{props.subtext}
)}
); }; const DomainMenuItem = (props: { setMenuState: (state: ShareMenuStates) => void; }) => { let { identity } = useIdentityData(); let { data: domains } = useLeafletDomains(); if (identity === null) return (
{" "} to publish on a custom domain!
); else return ( <> {domains?.[0] ? ( ) : ( { e.preventDefault(); props.setMenuState("domain"); }} > Publish on a custom domain )} ); };