import { useState, useRef, useEffect } from "react";
import { Copy, ExternalLink, Check } from "lucide-react";
import { BlueskyIcon } from "./Icons";
const BLUESKY_COLOR = "#1185fe";
const WitchskyIcon = () => (
);
const BlackskyIcon = () => (
);
const CatskyIcon = () => (
);
const DeerIcon = () => (
);
const BLUESKY_FORKS = [
{
name: "Bluesky",
domain: "bsky.app",
Icon: () => ,
},
{ name: "Witchsky", domain: "witchsky.app", Icon: WitchskyIcon },
{ name: "Blacksky", domain: "blacksky.community", Icon: BlackskyIcon },
{ name: "Catsky", domain: "catsky.social", Icon: CatskyIcon },
{ name: "Deer", domain: "deer.social", Icon: DeerIcon },
];
export default function ShareMenu({ uri, text, customUrl, handle, type }) {
const [isOpen, setIsOpen] = useState(false);
const [copied, setCopied] = useState(false);
const menuRef = useRef(null);
const getShareUrl = () => {
if (customUrl) return customUrl;
if (!uri) return "";
const uriParts = uri.split("/");
const rkey = uriParts[uriParts.length - 1];
if (handle && type) {
return `${window.location.origin}/${handle}/${type.toLowerCase()}/${rkey}`;
}
const did = uriParts[2];
return `${window.location.origin}/at/${did}/${rkey}`;
};
const shareUrl = getShareUrl();
useEffect(() => {
const handleClickOutside = (e) => {
if (menuRef.current && !menuRef.current.contains(e.target)) {
setIsOpen(false);
}
};
const card = menuRef.current?.closest(".card");
if (card) {
if (isOpen) {
card.style.zIndex = "50";
} else {
card.style.zIndex = "";
}
}
if (isOpen) {
document.addEventListener("mousedown", handleClickOutside);
}
return () => document.removeEventListener("mousedown", handleClickOutside);
}, [isOpen]);
const handleShareToFork = (domain) => {
const composeText = text
? `${text.substring(0, 200)}...\n\n${shareUrl}`
: shareUrl;
const composeUrl = `https://${domain}/intent/compose?text=${encodeURIComponent(composeText)}`;
window.open(composeUrl, "_blank");
setIsOpen(false);
};
const handleCopy = async () => {
try {
await navigator.clipboard.writeText(shareUrl);
setCopied(true);
setTimeout(() => {
setCopied(false);
setIsOpen(false);
}, 1500);
} catch {
prompt("Copy this link:", shareUrl);
}
};
const handleSystemShare = async () => {
if (navigator.share) {
try {
await navigator.share({
title: "Margin Annotation",
text: text?.substring(0, 100),
url: shareUrl,
});
} catch {
/* ignore */
}
}
setIsOpen(false);
};
return (
{isOpen && (
Share to
{BLUESKY_FORKS.map((fork) => (
))}
{navigator.share && (
)}
)}
);
}