Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 48 additions & 17 deletions pages/Transaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Nip47Transaction, Nip47TransactionMetadata } from "@getalby/sdk";
import { hexToBytes } from "@noble/hashes/utils";
import dayjs from "dayjs";
import * as Clipboard from "expo-clipboard";
import { Link, useLocalSearchParams } from "expo-router";
import { Link, router, useLocalSearchParams } from "expo-router";
import { nip19 } from "nostr-tools";
import React from "react";
import {
Expand Down Expand Up @@ -51,19 +51,23 @@ export function Transaction() {
transactionJSON: string;
appPubkey?: string; // only specified when opening from push notification
};
const transaction: Nip47Transaction = JSON.parse(transactionJSON);

let transaction: Nip47Transaction | null = null;
try {
transaction = JSON.parse(decodeURIComponent(transactionJSON));
} catch (e) {
console.error("Failed to parse transaction JSON:", e);
}

const getFiatAmount = useGetFiatAmount();
const bitcoinDisplayFormat = useAppStore(
(store) => store.bitcoinDisplayFormat,
);

React.useEffect(() => {
if (appPubkey) {
useAppStore.getState().setSelectedWallet(appPubkey);
}
}, [appPubkey]);

const TransactionIcon = React.useMemo(() => {
if (!transaction) {
return FailedTransactionIcon;
}
if (transaction.type === "incoming") {
return ReceivedTransactionIcon;
}
Expand All @@ -77,9 +81,12 @@ export function Transaction() {
return AcceptedTransactionIcon;
}
return FailedTransactionIcon;
}, [transaction.state, transaction.type]);
}, [transaction]);

const boostagram = React.useMemo(() => {
if (!transaction) {
return undefined;
}
let parsedBoostagram;
try {
const tlvRecord = (
Expand All @@ -94,7 +101,38 @@ export function Transaction() {
console.error(e);
}
return parsedBoostagram;
}, [transaction.metadata]);
}, [transaction]);

const displayCharacterCount = React.useMemo(() => {
if (!transaction) {
return 0;
}
return (
new Intl.NumberFormat().format(Math.floor(transaction.amount / 1000))
.length + (bitcoinDisplayFormat === "bip177" ? 1 : 4)
);
}, [transaction, bitcoinDisplayFormat]);

React.useEffect(() => {
if (!transaction) {
Toast.show({
type: "error",
text1: "Failed to load transaction",
text2: "Could not parse transaction details.",
});
router.back();
}
}, [transaction]);
Comment on lines +116 to +125
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Effect may fire the toast and router.back() more than once.

Because transaction is a new reference every render (see previous comment), this effect re-runs on each re-render. If there's any re-render between the toast and the navigation completing, the user could see duplicate toasts. Even after memoizing transaction, consider adding a guard ref or removing transaction from the dependency array entirely (run only on mount) to be safe:

  React.useEffect(() => {
    if (!transaction) {
      Toast.show({
        type: "error",
        text1: "Failed to load transaction",
        text2: "Could not parse transaction details.",
      });
      router.back();
    }
-  }, [transaction]);
+  }, [transaction]); // safe once `transaction` is memoized; otherwise use []

If you adopt the useMemo fix from the previous comment, this dependency becomes stable and the issue goes away.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
React.useEffect(() => {
if (!transaction) {
Toast.show({
type: "error",
text1: "Failed to load transaction",
text2: "Could not parse transaction details.",
});
router.back();
}
}, [transaction]);
React.useEffect(() => {
if (!transaction) {
Toast.show({
type: "error",
text1: "Failed to load transaction",
text2: "Could not parse transaction details.",
});
router.back();
}
}, [transaction]); // safe once `transaction` is memoized; otherwise use []
🤖 Prompt for AI Agents
In `@pages/Transaction.tsx` around lines 116 - 125, The effect using
React.useEffect that calls Toast.show and router.back can run multiple times
because transaction is unstable; make the effect run only once or guard it:
either remove transaction from the dependency array and run the check on mount,
or stabilize transaction (e.g., memoize the parsed transaction) and add a
one-time guard using a ref (e.g., didHandleMissingTransaction) so the block that
calls Toast.show and router.back executes only once; update the effect around
React.useEffect, Toast.show, and router.back accordingly.


React.useEffect(() => {
if (appPubkey) {
useAppStore.getState().setSelectedWallet(appPubkey);
}
}, [appPubkey]);

if (!transaction) {
return null;
}

const eventId = transaction.metadata?.nostr?.tags?.find(
(t) => t[0] === "e",
Expand All @@ -105,13 +143,6 @@ export function Transaction() {

const metadata = transaction.metadata as Nip47TransactionMetadata;

const displayCharacterCount = React.useMemo(
() =>
new Intl.NumberFormat().format(Math.floor(transaction.amount / 1000))
.length + (bitcoinDisplayFormat === "bip177" ? 1 : 4),
[transaction.amount, bitcoinDisplayFormat],
);

return (
<>
<Screen title="Transaction" />
Expand Down
Loading