// Sharing a list: copy its link, or hand it to the system share sheet. import { h, icon, api, toast, openDialog, dialogHead, confirmDialog, busy } from "./common.js"; export const VISIBILITY = { public: { label: "Public", icon: "external", hint: "On your page, and anyone with the link can see it." }, unlisted: { label: "Unlisted", icon: "link", hint: "Hidden from your page. Only people you send the link to can see it." }, private: { label: "Private", icon: "lock", hint: "Only you can see it." }, }; export function visibilityOf(list) { return list.visibility || (list.visible ? "public" : "private"); } export function shareURL(list) { const v = visibilityOf(list); if (v === "public") return new URL(`/l/${list.slug}`, location.origin).href; if (v === "unlisted" && list.shareToken) return new URL(`/s/${list.shareToken}`, location.origin).href; return ""; } async function copy(url, input) { try { await navigator.clipboard.writeText(url); toast("Link copied"); } catch { // Clipboard needs HTTPS; fall back to selecting the text. input?.select(); toast("Press Ctrl+C to copy the link"); } } const canNativeShare = () => typeof navigator.share === "function" && matchMedia("(pointer: coarse)").matches; /** Visitor share: native sheet on phones, copy elsewhere. */ export async function quickShare(url, title) { if (canNativeShare()) { try { await navigator.share({ title, url }); return; } catch (e) { if (e.name === "AbortError") return; } } await copy(url); } /** Owner share dialog. onChange gets the updated list after a link reset. */ export function shareDialog(list, { onChange } = {}) { let dlg; const v = visibilityOf(list); const body = h("div", { class: "dialog-body stack" }); const draw = () => { const url = shareURL(list); if (!url) { body.replaceChildren(h("p", {}, "This list is private, so there's nothing to send yet. Set it to public or unlisted in List settings first.")); return; } const input = h("input", { type: "text", readonly: true, value: url, onfocus: (e) => e.target.select() }); const copyBtn = h("button", { type: "button", class: "btn btn--primary", onclick: () => copy(url, input) }, icon("link"), "Copy"); const parts = [ h("p", { class: "muted" }, VISIBILITY[v].hint), h("div", { class: "input-group" }, input, copyBtn), ]; if (typeof navigator.share === "function") { parts.push(h("button", { type: "button", class: "btn", onclick: () => quickShare(url, list.title) }, icon("external"), "Share…")); } if (v === "unlisted") { const reset = h("button", { type: "button", class: "btn btn--ghost" }, icon("refresh"), "Make a new link"); reset.addEventListener("click", async () => { const ok = await confirmDialog({ title: "Make a new link?", message: "The current link will stop working for anyone you've sent it to.", confirm: "Make new link", danger: true, }); if (!ok) return; busy(reset, true); try { list = await api("POST", `/api/lists/${list.id}/share-link`); onChange?.(list); draw(); toast("New link made"); } catch (err) { toast(err.message, { error: true }); } finally { busy(reset, false); } }); parts.push(h("div", {}, reset, h("p", { class: "field-hint" }, "Use this if the link got passed around further than you wanted."))); } body.replaceChildren(...parts); }; draw(); const content = h("div", {}, dialogHead(`Share “${list.title}”`, () => dlg), body, h("div", { class: "dialog-foot" }, h("button", { type: "button", class: "btn", onclick: () => dlg.close() }, "Done")), ); dlg = openDialog(content); return dlg; }