A click proves little, so the first visit of a shared link now earns a question (once per user) instead of a natal analysis. A natal analysis is earned by bringing a new user: the share page remembers the link, the Google sign-up sends it, and when that user makes their own chart the sharer gets one, at most three (referrals table, referral_reward). This is off until sign-up opens (REFERRAL_REWARDS, default false). The share button and its small print follow; /v1/me tells what was earned. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
168 lines
5.2 KiB
TypeScript
168 lines
5.2 KiB
TypeScript
"use client";
|
|
|
|
import { useState } from "react";
|
|
|
|
import { api, errorText } from "@/lib/api";
|
|
|
|
interface Share {
|
|
id: string;
|
|
url: string;
|
|
}
|
|
|
|
function ShareIcon() {
|
|
return (
|
|
<svg viewBox="0 0 24 24" width="20" height="20" aria-hidden="true" fill="none" stroke="currentColor" strokeWidth="1.8">
|
|
<circle cx="18" cy="5" r="2.6" />
|
|
<circle cx="6" cy="12" r="2.6" />
|
|
<circle cx="18" cy="19" r="2.6" />
|
|
<path d="M8.3 10.8l7.4-4.4M8.3 13.2l7.4 4.4" strokeLinecap="round" />
|
|
</svg>
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Share link of a chart with its short summary: copy, Facebook, the phone's
|
|
* share menu, revoke. The first visit by someone else earns a free question,
|
|
* once per user (`rewardGranted`: already received); someone who signs up
|
|
* from the link and makes their chart earns an analysis (`referralsLeft`,
|
|
* while `referralOpen`).
|
|
*/
|
|
export function ShareChart({
|
|
chartId,
|
|
rewardGranted = false,
|
|
referralOpen = false,
|
|
referralsLeft = 0,
|
|
}: {
|
|
chartId: string;
|
|
rewardGranted?: boolean;
|
|
referralOpen?: boolean;
|
|
referralsLeft?: number;
|
|
}) {
|
|
const referral = referralOpen && referralsLeft > 0;
|
|
const perks = [
|
|
!rewardGranted && "+1 ajándék kérdés, ha valaki megnézi",
|
|
referral && "+1 elemzés, ha valaki a linkedről csatlakozik",
|
|
].filter(Boolean);
|
|
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="share-button" onClick={open} disabled={busy} aria-busy={busy}>
|
|
<span className="share-icon">
|
|
<ShareIcon />
|
|
</span>
|
|
<span className="share-button-text">
|
|
<strong>{busy ? "Link készül…" : "Oszd meg a képleted"}</strong>
|
|
<small>{perks.length > 0 ? perks.join(" · ") : "Küldd el a barátaidnak, vagy tedd ki Facebookra"}</small>
|
|
</span>
|
|
</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>
|
|
<p className="share-fine">
|
|
{rewardGranted
|
|
? "Az ajándék kérdést már megkaptad. Köszönjük, hogy megosztottad!"
|
|
: "Ajándék kérdés egyszer jár: amikor valaki más először megnyitja a linkedet. A saját megnyitásod nem számít."}
|
|
{referral &&
|
|
` Ha valaki a linkedről regisztrál és elkészíti a képletét, egy képletelemzést is kapsz (még ${referralsLeft} szerezhető).`}
|
|
</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>
|
|
);
|
|
}
|