From 241ead7a0303c36666fbed6f6623af4403c09533 Mon Sep 17 00:00:00 2001 From: Kieran Date: Tue, 9 May 2023 16:25:59 +0100 Subject: [PATCH] refactor --- packages/app/src/Element/CashuNuts.tsx | 66 ++++++++++++++++++++++++++ packages/app/src/Element/Text.tsx | 43 ++--------------- packages/app/src/Pages/Login.tsx | 2 +- packages/app/src/index.css | 4 ++ 4 files changed, 74 insertions(+), 41 deletions(-) create mode 100644 packages/app/src/Element/CashuNuts.tsx diff --git a/packages/app/src/Element/CashuNuts.tsx b/packages/app/src/Element/CashuNuts.tsx new file mode 100644 index 00000000..4fc0cc97 --- /dev/null +++ b/packages/app/src/Element/CashuNuts.tsx @@ -0,0 +1,66 @@ +import { getDecodedToken } from "@cashu/cashu-ts"; +import { useMemo } from "react"; +import { FormattedMessage } from "react-intl"; + +import useLogin from "Hooks/useLogin"; +import { useUserProfile } from "Hooks/useUserProfile"; + +export default function CashuNuts({ token }: { token: string }) { + const login = useLogin(); + const profile = useUserProfile(login.publicKey); + + async function copyToken(e: React.MouseEvent, token: string) { + e.stopPropagation(); + await navigator.clipboard.writeText(token); + } + async function redeemToken(e: React.MouseEvent, token: string) { + e.stopPropagation(); + const lnurl = profile?.lud16 ?? ""; + const url = `https://redeem.cashu.me?token=${encodeURIComponent(token)}&lightning=${encodeURIComponent(lnurl)}`; + window.open(url, "_blank"); + } + + const cashu = useMemo(() => { + try { + if (!token.startsWith("cashuA") || token.length < 10) { + return; + } + return getDecodedToken(token); + } catch { + // ignored + } + }, [token]); + + if (!cashu) return <>{token}; + + return ( +
+
+
+

+ +

+

+ acc + v.amount, 0), + }} + /> +

+ + + +
+
+ + +
+
+
+ ); +} diff --git a/packages/app/src/Element/Text.tsx b/packages/app/src/Element/Text.tsx index 8dc99288..7eb5d56c 100644 --- a/packages/app/src/Element/Text.tsx +++ b/packages/app/src/Element/Text.tsx @@ -6,15 +6,13 @@ import { visit, SKIP } from "unist-util-visit"; import * as unist from "unist"; import { HexKey, NostrPrefix } from "@snort/nostr"; -import { getDecodedToken } from "@cashu/cashu-ts"; -import { FormattedMessage } from "react-intl"; - import { MentionRegex, InvoiceRegex, HashtagRegex, CashuRegex } from "Const"; -import { eventLink, hexToBech32, splitByUrl, unwrap } from "Util"; +import { eventLink, hexToBech32, splitByUrl, unwrap, validateNostrLink } from "Util"; import Invoice from "Element/Invoice"; import Hashtag from "Element/Hashtag"; import Mention from "Element/Mention"; import HyperText from "Element/HyperText"; +import CashuNuts from "Element/CashuNuts"; export type Fragment = string | React.ReactNode; @@ -72,46 +70,11 @@ export default function Text({ content, tags, creator, disableMedia, depth }: Te } function extractCashuTokens(fragments: Fragment[]) { - async function copyToken(token: string) { - await navigator.clipboard.writeText(token); - } - async function redeemToken(token: string) { - const url = `https://redeem.cashu.me?token=${encodeURIComponent(token)}&lightning=${encodeURIComponent( - "callebtc@ln.tips" - )}`; - window.open(url, "_blank"); - } - return fragments .map(f => { if (typeof f === "string" && f.includes("cashuA")) { return f.split(CashuRegex).map(a => { - if (!a.startsWith("cashuA") || a.length < 10) { - return a; - } - const cashu = getDecodedToken(a); - if (cashu && cashu.token[0].proofs) { - return ( -
-
-
-

Cashu token

-

Amount: {cashu.token[0].proofs.reduce((acc, v) => (acc += v.amount), 0)} sats

-

Mint: {cashu.token[0].mint}

-
-
- - -
-
-
- ); - } - return a; + return ; }); } return f; diff --git a/packages/app/src/Pages/Login.tsx b/packages/app/src/Pages/Login.tsx index 1da909b9..c1e2282e 100644 --- a/packages/app/src/Pages/Login.tsx +++ b/packages/app/src/Pages/Login.tsx @@ -220,7 +220,7 @@ export default function LoginPage() { /> {error.length > 0 ? {error} : null} -

+