2023-04-05 15:10:14 +00:00
|
|
|
import { TaggedRawEvent } from "@snort/nostr";
|
|
|
|
import { useState } from "react";
|
2023-04-10 13:00:26 +00:00
|
|
|
import { FormattedMessage, FormattedNumber, useIntl } from "react-intl";
|
2023-04-05 15:10:14 +00:00
|
|
|
|
|
|
|
import { ParsedZap } from "Element/Zap";
|
|
|
|
import Text from "Element/Text";
|
|
|
|
import useEventPublisher from "Feed/EventPublisher";
|
|
|
|
import { useWallet } from "Wallet";
|
|
|
|
import { useUserProfile } from "Hooks/useUserProfile";
|
|
|
|
import { LNURL } from "LNURL";
|
|
|
|
import { unwrap } from "Util";
|
|
|
|
import { formatShort } from "Number";
|
|
|
|
import Spinner from "Icons/Spinner";
|
|
|
|
import SendSats from "Element/SendSats";
|
2023-04-14 11:33:19 +00:00
|
|
|
import useLogin from "Hooks/useLogin";
|
2023-04-05 15:10:14 +00:00
|
|
|
|
|
|
|
interface PollProps {
|
|
|
|
ev: TaggedRawEvent;
|
|
|
|
zaps: Array<ParsedZap>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function Poll(props: PollProps) {
|
|
|
|
const { formatMessage } = useIntl();
|
|
|
|
const publisher = useEventPublisher();
|
|
|
|
const { wallet } = useWallet();
|
2023-04-14 15:02:15 +00:00
|
|
|
const { preferences: prefs, publicKey: myPubKey, relays } = useLogin();
|
2023-04-05 15:10:14 +00:00
|
|
|
const pollerProfile = useUserProfile(props.ev.pubkey);
|
|
|
|
const [error, setError] = useState("");
|
|
|
|
const [invoice, setInvoice] = useState("");
|
|
|
|
const [voting, setVoting] = useState<number>();
|
2023-04-10 15:25:41 +00:00
|
|
|
const didVote = props.zaps.some(a => a.sender === myPubKey);
|
2023-04-10 15:29:43 +00:00
|
|
|
const isMyPoll = props.ev.pubkey === myPubKey;
|
|
|
|
const showResults = didVote || isMyPoll;
|
2023-04-05 15:10:14 +00:00
|
|
|
|
|
|
|
const options = props.ev.tags.filter(a => a[0] === "poll_option").sort((a, b) => Number(a[1]) - Number(b[1]));
|
2023-04-10 15:25:41 +00:00
|
|
|
async function zapVote(ev: React.MouseEvent, opt: number) {
|
|
|
|
ev.stopPropagation();
|
2023-04-14 15:02:15 +00:00
|
|
|
if (voting || !publisher) return;
|
2023-04-10 12:53:53 +00:00
|
|
|
|
2023-04-05 15:10:14 +00:00
|
|
|
const amount = prefs.defaultZapAmount;
|
|
|
|
try {
|
2023-04-10 15:25:41 +00:00
|
|
|
if (amount <= 0) {
|
|
|
|
throw new Error(
|
|
|
|
formatMessage(
|
|
|
|
{
|
|
|
|
defaultMessage: "Can't vote with {amount} sats, please set a different default zap amount",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
amount,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-04-05 15:10:14 +00:00
|
|
|
setVoting(opt);
|
2023-04-14 15:02:15 +00:00
|
|
|
const r = Object.keys(relays.item);
|
|
|
|
const zap = await publisher.zap(amount * 1000, props.ev.pubkey, r, props.ev.id, undefined, eb =>
|
|
|
|
eb.tag(["poll_option", opt.toString()])
|
|
|
|
);
|
2023-04-10 12:53:53 +00:00
|
|
|
|
2023-04-05 15:10:14 +00:00
|
|
|
const lnurl = props.ev.tags.find(a => a[0] === "zap")?.[1] || pollerProfile?.lud16 || pollerProfile?.lud06;
|
|
|
|
if (!lnurl) return;
|
|
|
|
|
|
|
|
const svc = new LNURL(lnurl);
|
|
|
|
await svc.load();
|
|
|
|
|
|
|
|
if (!svc.canZap) {
|
2023-04-10 12:53:53 +00:00
|
|
|
throw new Error(
|
|
|
|
formatMessage({
|
|
|
|
defaultMessage: "Can't vote because LNURL service does not support zaps",
|
|
|
|
})
|
|
|
|
);
|
2023-04-05 15:10:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const invoice = await svc.getInvoice(amount, undefined, zap);
|
|
|
|
if (wallet?.isReady()) {
|
|
|
|
await wallet?.payInvoice(unwrap(invoice.pr));
|
|
|
|
} else {
|
|
|
|
setInvoice(unwrap(invoice.pr));
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof Error) {
|
|
|
|
setError(e.message);
|
2023-04-10 12:53:53 +00:00
|
|
|
} else {
|
|
|
|
setError(
|
|
|
|
formatMessage({
|
|
|
|
defaultMessage: "Failed to send vote",
|
|
|
|
})
|
|
|
|
);
|
2023-04-05 15:10:14 +00:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
setVoting(undefined);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const allTotal = props.zaps.filter(a => a.pollOption !== undefined).reduce((acc, v) => (acc += v.amount), 0);
|
|
|
|
return (
|
|
|
|
<>
|
2023-04-10 13:00:26 +00:00
|
|
|
<small>
|
|
|
|
<FormattedMessage
|
2023-04-11 14:45:57 +00:00
|
|
|
defaultMessage="You are voting with {amount} sats"
|
2023-04-10 13:00:26 +00:00
|
|
|
values={{
|
|
|
|
amount: formatShort(prefs.defaultZapAmount),
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</small>
|
2023-04-05 15:10:14 +00:00
|
|
|
<div className="poll-body">
|
|
|
|
{options.map(a => {
|
|
|
|
const opt = Number(a[1]);
|
|
|
|
const desc = a[2];
|
|
|
|
const zapsOnOption = props.zaps.filter(b => b.pollOption === opt);
|
|
|
|
const total = zapsOnOption.reduce((acc, v) => (acc += v.amount), 0);
|
2023-04-10 12:53:53 +00:00
|
|
|
const weight = allTotal === 0 ? 0 : total / allTotal;
|
2023-04-05 15:10:14 +00:00
|
|
|
return (
|
2023-04-10 15:25:41 +00:00
|
|
|
<div key={a[1]} className="flex" onClick={e => zapVote(e, opt)}>
|
2023-04-05 15:10:14 +00:00
|
|
|
<div className="f-grow">
|
|
|
|
{opt === voting ? <Spinner /> : <Text content={desc} tags={props.ev.tags} creator={props.ev.pubkey} />}
|
|
|
|
</div>
|
2023-04-10 15:29:43 +00:00
|
|
|
{showResults && (
|
2023-04-10 15:25:41 +00:00
|
|
|
<>
|
|
|
|
<div className="flex">
|
|
|
|
<FormattedNumber value={weight * 100} maximumFractionDigits={0} />%
|
|
|
|
<small>({formatShort(total)})</small>
|
|
|
|
</div>
|
|
|
|
<div style={{ width: `${weight * 100}%` }} className="progress"></div>
|
|
|
|
</>
|
|
|
|
)}
|
2023-04-05 15:10:14 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
{error && <b className="error">{error}</b>}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<SendSats show={invoice !== ""} onClose={() => setInvoice("")} invoice={invoice} />
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|