snort/src/Element/Zap.tsx

168 lines
4.2 KiB
TypeScript
Raw Normal View History

2023-02-03 21:38:14 +00:00
import "./Zap.css";
2023-02-04 10:28:13 +00:00
import { useMemo } from "react";
2023-02-08 21:10:26 +00:00
import { FormattedMessage } from "react-intl";
2023-02-03 23:45:04 +00:00
import { useSelector } from "react-redux";
2023-02-03 21:38:14 +00:00
// @ts-expect-error
import { decode as invoiceDecode } from "light-bolt11-decoder";
import { bytesToHex } from "@noble/hashes/utils";
2023-02-04 13:30:05 +00:00
import { sha256 } from "Util";
2023-02-03 21:38:14 +00:00
2023-02-04 10:16:08 +00:00
//import { sha256 } from "Util";
2023-02-03 21:38:14 +00:00
import { formatShort } from "Number";
import { HexKey, TaggedRawEvent } from "Nostr";
import Event from "Nostr/Event";
import Text from "Element/Text";
import ProfileImage from "Element/ProfileImage";
2023-02-03 23:45:04 +00:00
import { RootState } from "State/Store";
2023-02-03 21:38:14 +00:00
2023-02-08 21:10:26 +00:00
import messages from "./messages";
2023-02-03 21:38:14 +00:00
function findTag(e: TaggedRawEvent, tag: string) {
const maybeTag = e.tags.find((evTag) => {
return evTag[0] === tag;
});
return maybeTag && maybeTag[1];
2023-02-03 21:38:14 +00:00
}
function getInvoice(zap: TaggedRawEvent) {
const bolt11 = findTag(zap, "bolt11");
const decoded = invoiceDecode(bolt11);
2023-02-03 21:38:14 +00:00
const amount = decoded.sections.find(
(section: any) => section.name === "amount"
)?.value;
const hash = decoded.sections.find(
(section: any) => section.name === "description_hash"
)?.value;
2023-02-03 21:38:14 +00:00
return { amount, hash: hash ? bytesToHex(hash) : undefined };
}
2023-02-04 13:30:05 +00:00
interface Zapper {
pubkey?: HexKey;
isValid: boolean;
2023-02-04 13:30:05 +00:00
}
function getZapper(zap: TaggedRawEvent, dhash: string): Zapper {
const zapRequest = findTag(zap, "description");
2023-02-03 21:38:14 +00:00
if (zapRequest) {
const rawEvent: TaggedRawEvent = JSON.parse(zapRequest);
if (Array.isArray(rawEvent)) {
// old format, ignored
2023-02-04 13:30:05 +00:00
return { isValid: false };
2023-02-03 21:38:14 +00:00
}
2023-02-04 13:30:05 +00:00
const metaHash = sha256(zapRequest);
const ev = new Event(rawEvent);
2023-02-04 13:30:05 +00:00
return { pubkey: ev.PubKey, isValid: dhash === metaHash };
2023-02-03 21:38:14 +00:00
}
return { isValid: false };
2023-02-03 21:38:14 +00:00
}
2023-02-08 21:10:26 +00:00
export interface ParsedZap {
id: HexKey;
e?: HexKey;
p: HexKey;
amount: number;
content: string;
zapper?: HexKey;
valid: boolean;
2023-02-03 21:38:14 +00:00
}
export function parseZap(zap: TaggedRawEvent): ParsedZap {
const { amount, hash } = getInvoice(zap);
2023-02-04 13:30:05 +00:00
const zapper = hash ? getZapper(zap, hash) : { isValid: false };
const e = findTag(zap, "e");
const p = findTag(zap, "p")!;
2023-02-03 21:38:14 +00:00
return {
id: zap.id,
e,
p,
amount: Number(amount) / 1000,
2023-02-04 13:30:05 +00:00
zapper: zapper.pubkey,
2023-02-03 21:38:14 +00:00
content: zap.content,
2023-02-04 13:30:05 +00:00
valid: zapper.isValid,
};
2023-02-03 21:38:14 +00:00
}
const Zap = ({
zap,
showZapped = true,
}: {
zap: ParsedZap;
showZapped?: boolean;
}) => {
const { amount, content, zapper, valid, p } = zap;
const pubKey = useSelector((s: RootState) => s.login.publicKey);
2023-02-03 21:38:14 +00:00
2023-02-08 21:10:26 +00:00
return valid && zapper ? (
2023-02-03 23:35:57 +00:00
<div className="zap note card">
<div className="header">
2023-02-08 21:10:26 +00:00
<ProfileImage pubkey={zapper} />
2023-02-04 10:16:08 +00:00
{p !== pubKey && showZapped && <ProfileImage pubkey={p} />}
2023-02-03 21:38:14 +00:00
<div className="amount">
2023-02-08 21:10:26 +00:00
<span className="amount-number">
<FormattedMessage
{...messages.Sats}
values={{ n: formatShort(amount) }}
/>
</span>
2023-02-03 21:38:14 +00:00
</div>
</div>
2023-02-08 21:10:26 +00:00
{content.length > 0 && zapper && (
<div className="body">
<Text
creator={zapper}
content={content}
tags={[]}
users={new Map()}
/>
</div>
)}
2023-02-03 21:38:14 +00:00
</div>
) : null;
};
2023-02-03 21:38:14 +00:00
interface ZapsSummaryProps {
zaps: ParsedZap[];
}
2023-02-03 21:38:14 +00:00
export const ZapsSummary = ({ zaps }: ZapsSummaryProps) => {
2023-02-04 10:28:13 +00:00
const sortedZaps = useMemo(() => {
2023-02-08 21:10:26 +00:00
const pub = [...zaps.filter((z) => z.zapper && z.valid)];
const priv = [...zaps.filter((z) => !z.zapper && z.valid)];
pub.sort((a, b) => b.amount - a.amount);
return pub.concat(priv);
}, [zaps]);
2023-02-04 10:28:13 +00:00
if (zaps.length === 0) {
return null;
2023-02-04 10:28:13 +00:00
}
const [topZap, ...restZaps] = sortedZaps;
2023-02-08 21:10:26 +00:00
const { zapper, amount } = topZap;
2023-02-03 21:38:14 +00:00
return (
<div className="zaps-summary">
2023-02-04 10:28:13 +00:00
{amount && (
2023-02-03 21:38:14 +00:00
<div className={`top-zap`}>
<div className="summary">
2023-02-04 15:25:37 +00:00
{zapper && <ProfileImage pubkey={zapper} />}
{restZaps.length > 0 && (
2023-02-08 21:10:26 +00:00
<FormattedMessage
{...messages.Others}
values={{ n: restZaps.length }}
/>
)}{" "}
<FormattedMessage
{...messages.OthersZapped}
values={{ n: restZaps.length }}
/>
2023-02-03 21:38:14 +00:00
</div>
</div>
)}
</div>
);
};
2023-02-03 21:38:14 +00:00
export default Zap;