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

228 lines
6.7 KiB
TypeScript
Raw Normal View History

2023-05-16 21:30:52 +00:00
import "./ZapPool.css";
import { useMemo, useSyncExternalStore } from "react";
import { FormattedMessage, FormattedNumber } from "react-intl";
2023-06-16 19:31:33 +00:00
import { useUserProfile } from "@snort/system-react";
2023-05-16 21:30:52 +00:00
2023-11-17 11:52:10 +00:00
import { SnortPubKey } from "@/Const";
import ProfilePreview from "@/Element/User/ProfilePreview";
import useLogin from "@/Hooks/useLogin";
import { UploaderServices } from "@/Upload";
2023-12-10 17:35:18 +00:00
import { bech32ToHex, getRelayName, trackEvent, unwrap } from "@/SnortUtils";
2023-11-17 11:52:10 +00:00
import { ZapPoolController, ZapPoolRecipient, ZapPoolRecipientType } from "@/ZapPoolController";
2023-11-28 10:05:55 +00:00
import AsyncButton from "@/Element/Button/AsyncButton";
2023-11-17 11:52:10 +00:00
import { useWallet } from "@/Wallet";
import useEventPublisher from "@/Hooks/useEventPublisher";
2023-05-16 21:30:52 +00:00
2023-05-18 09:29:27 +00:00
const DataProviders = [
{
name: "nostr.band",
owner: bech32ToHex("npub1sx9rnd03vs34lp39fvfv5krwlnxpl90f3dzuk8y3cuwutk2gdhdqjz6g8m"),
},
];
2023-05-16 21:30:52 +00:00
function ZapTarget({ target }: { target: ZapPoolRecipient }) {
2023-10-17 09:54:34 +00:00
if (!ZapPoolController) return;
2023-05-16 21:30:52 +00:00
const login = useLogin();
2023-08-24 14:25:54 +00:00
const profile = useUserProfile(target.pubkey);
2023-05-16 21:30:52 +00:00
const hasAddress = profile?.lud16 || profile?.lud06;
2023-11-13 16:51:29 +00:00
const defaultZapMount = Math.ceil(login.appData.item.preferences.defaultZapAmount * (target.split / 100));
2023-05-16 21:30:52 +00:00
return (
<ProfilePreview
pubkey={target.pubkey}
actions={
hasAddress ? (
<div>
<div>
<FormattedNumber value={target.split} />% (
<FormattedMessage defaultMessage="{n} sats" id="CsCUYo" values={{ n: defaultZapMount }} />)
2023-05-16 21:30:52 +00:00
</div>
<input
type="range"
min={0}
max={100}
step={0.5}
2023-05-16 21:30:52 +00:00
value={target.split}
onChange={e =>
2023-10-17 09:54:34 +00:00
ZapPoolController?.set({
2023-05-16 21:30:52 +00:00
...target,
split: e.target.valueAsNumber,
})
}
/>
</div>
) : (
<FormattedMessage defaultMessage="No lightning address" id="JPFYIM" />
2023-05-16 21:30:52 +00:00
)
}
/>
);
}
export default function ZapPoolPage() {
2023-10-17 09:54:34 +00:00
if (!ZapPoolController) return;
2023-05-16 21:30:52 +00:00
const login = useLogin();
2023-10-13 15:34:31 +00:00
const { system } = useEventPublisher();
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(() => {
2023-10-13 15:34:31 +00:00
return system.Sockets.map(a => {
2023-06-01 08:54:25 +00:00
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);
}, [login.relays]);
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>
2023-11-13 16:51:29 +00:00
<FormattedNumber value={login.appData.item.preferences.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>
2023-11-13 16:51:29 +00:00
<FormattedNumber value={login.appData.item.preferences.defaultZapAmount} />
2023-05-16 21:30:52 +00:00
</b>
),
nOut: (
<b>
2023-11-13 16:51:29 +00:00
<FormattedNumber
value={ZapPoolController.calcAllocation(login.appData.item.preferences.defaultZapAmount)}
/>
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:35:18 +00:00
<AsyncButton onClick={async () => {
trackEvent("ZapPool:Manual")
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-05-16 21:30:52 +00:00
<ZapTarget
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 => (
2023-08-21 13:58:57 +00:00
<div>
2023-05-16 21:30:52 +00:00
<h4>{getRelayName(a.address)}</h4>
<ZapTarget
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 => (
2023-08-21 13:58:57 +00:00
<div>
2023-05-16 21:30:52 +00:00
<h4>{a.name}</h4>
<ZapTarget
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 => (
2023-08-21 13:58:57 +00:00
<div>
2023-05-18 09:29:27 +00:00
<h4>{a.name}</h4>
<ZapTarget
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>
);
}