Sharing a chart with its short summary, with link previews
The summary card gets a share button: a public link (/s/<slug>) with the wheel and the newest short summary, no name or birth data. The page carries og:/twitter: tags, and /s/<slug>/kep/<version>.png renders a 1200x630 preview (wheel, title, text, big three) with next/og and bundled fonts. The version changes with the summary, so Facebook fetches a rewritten one again. Backend: a shared chart comes with its newest finished summary, and sharing the same thing again returns the open link. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
@@ -9,6 +9,7 @@ import { AnalysisLoading } from "./AnalysisLoading";
|
||||
import { RegenerateButton } from "./RegenerateButton";
|
||||
import { RetryNotice } from "./RetryNotice";
|
||||
import { Rich } from "./Rich";
|
||||
import { ShareChart } from "./ShareChart";
|
||||
import { openTab } from "./Tabs";
|
||||
|
||||
const POLL_MS = 3000;
|
||||
@@ -81,6 +82,7 @@ export function ChartSummary({
|
||||
<p className="summary-text" lang={summary?.language}>
|
||||
<Rich text={content.summary ?? ""} />
|
||||
</p>
|
||||
<ShareChart chartId={chartId} />
|
||||
<a
|
||||
href="#elemzes"
|
||||
className="summary-more"
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
"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>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user