123 lines
3.4 KiB
TypeScript
123 lines
3.4 KiB
TypeScript
"use client";
|
|||
|
|
|
||
|
|
import { useState } from "react";
|
||
|
|
|
||
|
|
import { api, errorText } from "@/lib/api";
|
||
|
|
|
||
|
|
interface Share {
|
||
|
|
id: string;
|
||
|
|
url: string;
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Share link of a chart with its short summary: copy, Facebook, the phone's share menu, revoke. */
|
||
|
|
export function ShareChart({ chartId }: { chartId: string }) {
|
||
|
|
const [share, setShare] = useState<Share | null>(null);
|
||
|
|
const [busy, setBusy] = useState(false);
|
||
|
|
const [copied, setCopied] = useState(false);
|
||
|
|
const [error, setError] = useState<string | null>(null);
|
||
|
|
const [revoked, setRevoked] = useState(false);
|
||
|
|
|
||
|
|
async function open() {
|
||
|
|
setBusy(true);
|
||
|
|
setError(null);
|
||
|
|
try {
|
||
|
|
setShare(await api<Share>("/shares", { body: { kind: "chart", target_id: chartId } }));
|
||
|
|
setRevoked(false);
|
||
|
|
} catch (e) {
|
||
|
|
setError(errorText(e));
|
||
|
|
} finally {
|
||
|
|
setBusy(false);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async function copy() {
|
||
|
|
if (!share) return;
|
||
|
|
try {
|
||
|
|
await navigator.clipboard.writeText(share.url);
|
||
|
|
setCopied(true);
|
||
|
|
setTimeout(() => setCopied(false), 2000);
|
||
|
|
} catch {
|
||
|
|
setError("Nem sikerült másolni; jelöld ki a linket, és másold kézzel.");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async function nativeShare() {
|
||
|
|
if (!share) return;
|
||
|
|
try {
|
||
|
|
await navigator.share({ url: share.url });
|
||
|
|
} catch {
|
||
|
|
// closed by the user
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async function revoke() {
|
||
|
|
if (!share || !window.confirm("Visszavonod a linket? Aki megkapta, többé nem tudja megnyitni.")) return;
|
||
|
|
setBusy(true);
|
||
|
|
try {
|
||
|
|
await api(`/shares/${share.id}`, { method: "DELETE" });
|
||
|
|
setShare(null);
|
||
|
|
setRevoked(true);
|
||
|
|
} catch (e) {
|
||
|
|
setError(errorText(e));
|
||
|
|
} finally {
|
||
|
|
setBusy(false);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!share) {
|
||
|
|
return (
|
||
|
|
<div className="share">
|
||
|
|
<button type="button" className="btn btn-sm" onClick={open} disabled={busy}>
|
||
|
|
{busy ? "Link készül…" : "Megosztás"}
|
||
|
|
</button>
|
||
|
|
{revoked && <p className="hint">A linket visszavontad.</p>}
|
||
|
|
{error && (
|
||
|
|
<p className="msg msg-error" role="alert">
|
||
|
|
{error}
|
||
|
|
</p>
|
||
|
|
)}
|
||
|
|
</div>
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
const facebook = `https://www.facebook.com/sharer/sharer.php?u=${encodeURIComponent(share.url)}`;
|
||
|
|
return (
|
||
|
|
<div className="share share-open">
|
||
|
|
<p className="hint">
|
||
|
|
A linken a képleted rajza és ez az összefoglaló látszik, név és születési adatok nélkül. Bárki megnyithatja,
|
||
|
|
akinek elküldöd.
|
||
|
|
</p>
|
||
|
|
<div className="share-link">
|
||
|
|
<input
|
||
|
|
className="inp"
|
||
|
|
readOnly
|
||
|
|
value={share.url}
|
||
|
|
aria-label="Megosztási link"
|
||
|
|
onFocus={(e) => e.currentTarget.select()}
|
||
|
|
/>
|
||
|
|
<button type="button" className="btn btn-sm" onClick={copy}>
|
||
|
|
{copied ? "Kimásolva" : "Másolás"}
|
||
|
|
</button>
|
||
|
|
</div>
|
||
|
|
<div className="form-actions">
|
||
|
|
<a className="btn btn-sm btn-primary" href={facebook} target="_blank" rel="noopener noreferrer">
|
||
|
|
Megosztás Facebookon
|
||
|
|
</a>
|
||
|
|
{typeof navigator !== "undefined" && "share" in navigator && (
|
||
|
|
<button type="button" className="btn btn-sm" onClick={nativeShare}>
|
||
|
|
Küldés…
|
||
|
|
</button>
|
||
|
|
)}
|
||
|
|
<button type="button" className="btn btn-sm btn-ghost" onClick={revoke} disabled={busy}>
|
||
|
|
Link visszavonása
|
||
|
|
</button>
|
||
|
|
</div>
|
||
|
|
{error && (
|
||
|
|
<p className="msg msg-error" role="alert">
|
||
|
|
{error}
|
||
|
|
</p>
|
||
|
|
)}
|
||
|
|
</div>
|
||
|
|
);
|
||
|
|
}
|