snort/packages/app/src/Pages/ZapPool/ZapPoolPageInner.tsx

186 lines
5.5 KiB
TypeScript
Raw Normal View History

2024-04-22 13:38:14 +00:00
import { SnortContext } from "@snort/system-react";
import { useContext, useMemo, useSyncExternalStore } from "react";
2023-05-16 21:30:52 +00:00
import { FormattedMessage, FormattedNumber } from "react-intl";
2024-01-04 17:01:18 +00:00
import AsyncButton from "@/Components/Button/AsyncButton";
2024-04-22 13:38:14 +00:00
import usePreferences from "@/Hooks/usePreferences";
2024-02-28 10:57:26 +00:00
import { ZapPoolTarget } from "@/Pages/ZapPool/ZapPoolTarget";
import { bech32ToHex, getRelayName, trackEvent, unwrap } from "@/Utils";
2024-01-04 17:01:18 +00:00
import { SnortPubKey } from "@/Utils/Const";
import { UploaderServices } from "@/Utils/Upload";
2024-02-28 10:57:26 +00:00
import { ZapPoolController, ZapPoolRecipientType } from "@/Utils/ZapPoolController";
2023-11-17 11:52:10 +00:00
import { useWallet } from "@/Wallet";
2023-05-16 21:30:52 +00:00
2023-05-18 09:29:27 +00:00
const DataProviders = [
{
name: "nostr.band",
owner: bech32ToHex("npub1sx9rnd03vs34lp39fvfv5krwlnxpl90f3dzuk8y3cuwutk2gdhdqjz6g8m"),
},
];
2024-02-28 10:57:26 +00:00
export function ZapPoolPageInner() {
2024-04-22 13:38:14 +00:00
const defaultZapAmount = usePreferences(s => s.defaultZapAmount);
const system = useContext(SnortContext);
2023-05-16 21:30:52 +00:00
const zapPool = useSyncExternalStore(
2023-10-17 09:54:34 +00:00
c => unwrap(ZapPoolController).hook(c),
() => unwrap(ZapPoolController).snapshot(),
2023-05-16 21:30:52 +00:00
);
const { wallet } = useWallet();
const relayConnections = useMemo(() => {
2024-01-25 15:21:42 +00:00
return [...system.pool]
.map(([, a]) => {
if (a.Info?.pubkey && !a.Ephemeral) {
return {
address: a.Address,
pubkey: a.Info.pubkey,
};
}
})
2023-05-16 21:30:52 +00:00
.filter(a => a !== undefined)
.map(unwrap);
2024-04-22 13:38:14 +00:00
}, []);
2023-05-16 21:30:52 +00:00
const sumPending = zapPool.reduce((acc, v) => acc + v.sum, 0);
return (
2023-08-21 13:58:57 +00:00
<div className="zap-pool main-content p">
2023-05-16 21:30:52 +00:00
<h1>
<FormattedMessage defaultMessage="Zap Pool" id="i/dBAR" />
2023-05-16 21:30:52 +00:00
</h1>
<p>
2023-11-20 19:16:47 +00:00
<FormattedMessage
defaultMessage="Fund the services that you use by splitting a portion of all your zaps into a pool of funds!"
id="x/Fx2P"
/>
2023-05-16 21:30:52 +00:00
</p>
<p>
2023-11-20 19:16:47 +00:00
<FormattedMessage
defaultMessage="Zap Pool only works if you use one of the supported wallet connections (WebLN, LNC, LNDHub or Nostr Wallet Connect)"
id="QWhotP"
/>
2023-05-16 21:30:52 +00:00
</p>
<p>
<FormattedMessage
2023-11-20 19:16:47 +00:00
defaultMessage="Your default zap amount is {number} sats, example values are calculated from this."
id="Xopqkl"
2023-05-16 21:30:52 +00:00
values={{
number: (
<b>
2024-04-22 13:38:14 +00:00
<FormattedNumber value={defaultZapAmount} />
2023-05-16 21:30:52 +00:00
</b>
),
}}
/>
</p>
<p>
<FormattedMessage
2023-11-20 19:16:47 +00:00
defaultMessage="A single zap of {nIn} sats will allocate {nOut} sats to the zap pool."
id="eSzf2G"
2023-05-16 21:30:52 +00:00
values={{
nIn: (
<b>
2024-04-22 13:38:14 +00:00
<FormattedNumber value={defaultZapAmount} />
2023-05-16 21:30:52 +00:00
</b>
),
nOut: (
<b>
2024-04-22 13:38:14 +00:00
<FormattedNumber value={ZapPoolController?.calcAllocation(defaultZapAmount) ?? 0} />
2023-05-16 21:30:52 +00:00
</b>
),
}}
/>
</p>
<p>
<FormattedMessage
2023-11-20 19:16:47 +00:00
defaultMessage="You currently have {number} sats in your zap pool."
id="Qxv0B2"
2023-05-16 21:30:52 +00:00
values={{
number: (
<b>
<FormattedNumber value={sumPending} />
</b>
),
}}
/>
</p>
<p>
{wallet && (
2023-12-10 17:41:25 +00:00
<AsyncButton
onClick={async () => {
2023-12-10 18:40:01 +00:00
trackEvent("ZapPool", { manual: true });
2023-12-10 17:41:25 +00:00
await ZapPoolController?.payout(wallet);
}}>
<FormattedMessage defaultMessage="Payout Now" id="+PzQ9Y" />
2023-05-16 21:30:52 +00:00
</AsyncButton>
)}
</p>
2023-08-21 13:58:57 +00:00
<div>
2023-12-11 10:37:03 +00:00
<ZapPoolTarget
2023-05-16 21:30:52 +00:00
target={
zapPool.find(b => b.pubkey === bech32ToHex(SnortPubKey) && b.type === ZapPoolRecipientType.Generic) ?? {
type: ZapPoolRecipientType.Generic,
pubkey: bech32ToHex(SnortPubKey),
split: 0,
sum: 0,
}
}
/>
</div>
<h3>
<FormattedMessage defaultMessage="Relays" id="RoOyAh" />
2023-05-16 21:30:52 +00:00
</h3>
{relayConnections.map(a => (
2024-01-04 09:54:58 +00:00
<div key={a.address}>
2023-05-16 21:30:52 +00:00
<h4>{getRelayName(a.address)}</h4>
2023-12-11 10:37:03 +00:00
<ZapPoolTarget
2023-05-16 21:30:52 +00:00
target={
zapPool.find(b => b.pubkey === a.pubkey && b.type === ZapPoolRecipientType.Relay) ?? {
type: ZapPoolRecipientType.Relay,
pubkey: a.pubkey,
split: 0,
sum: 0,
}
}
/>
</div>
))}
<h3>
<FormattedMessage defaultMessage="File hosts" id="XICsE8" />
2023-05-16 21:30:52 +00:00
</h3>
{UploaderServices.map(a => (
2024-01-04 09:54:58 +00:00
<div key={a.name}>
2023-05-16 21:30:52 +00:00
<h4>{a.name}</h4>
2023-12-11 10:37:03 +00:00
<ZapPoolTarget
2023-05-16 21:30:52 +00:00
target={
zapPool.find(b => b.pubkey === a.owner && b.type === ZapPoolRecipientType.FileHost) ?? {
type: ZapPoolRecipientType.FileHost,
pubkey: a.owner,
split: 0,
sum: 0,
}
}
/>
</div>
))}
2023-05-18 09:29:27 +00:00
<h3>
<FormattedMessage defaultMessage="Data Providers" id="ELbg9p" />
2023-05-18 09:29:27 +00:00
</h3>
{DataProviders.map(a => (
2024-01-04 09:54:58 +00:00
<div key={a.name}>
2023-05-18 09:29:27 +00:00
<h4>{a.name}</h4>
2023-12-11 10:37:03 +00:00
<ZapPoolTarget
2023-05-18 09:29:27 +00:00
target={
zapPool.find(b => b.pubkey === a.owner && b.type === ZapPoolRecipientType.DataProvider) ?? {
type: ZapPoolRecipientType.DataProvider,
pubkey: a.owner,
split: 0,
sum: 0,
}
}
/>
</div>
))}
2023-05-16 21:30:52 +00:00
</div>
);
}