nsarrazin's picture
nsarrazin HF Staff
fix(share): use copy link on desktop (#1477)
15615aa unverified
raw
history blame
739 Bytes
import { browser } from "$app/environment";
import { isDesktop } from "./isDesktop";
export async function share(url: string, title: string, appendLeafId: boolean = false) {
if (!browser) return;
// Retrieve the leafId from localStorage
const leafId = localStorage.getItem("leafId");
if (appendLeafId && leafId) {
// Use URL and URLSearchParams to add the leafId parameter
const shareUrl = new URL(url);
shareUrl.searchParams.append("leafId", leafId);
url = shareUrl.toString();
}
if (navigator.share && !isDesktop(window)) {
navigator.share({ url, title });
} else {
if (document.hasFocus()) {
await navigator.clipboard.writeText(url);
} else {
alert("Document is not focused. Please try again.");
}
}
}