Skip to content
Merged
Show file tree
Hide file tree
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
22 changes: 19 additions & 3 deletions src/features/payments/flows/direct-send/useDirectSendFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,14 @@ export function useDirectSendFlow() {
const { user } = useAuth()
const { createCharge, isCreating: isCreatingCharge } = useChargeManager()
const { recordPayment, isRecording } = usePaymentRecorder()
const { isConnected, address: walletAddress, sendMoney, formattedBalance, hasSufficientBalance } = useWallet()
const {
isConnected,
address: walletAddress,
sendMoney,
formattedBalance,
hasSufficientBalance,
isFetchingBalance,
} = useWallet()

const isLoggedIn = !!user?.user?.userId

Expand Down Expand Up @@ -84,9 +91,18 @@ export function useDirectSendFlow() {
}, [amount, hasSufficientBalance])

// check if should show insufficient balance error
// gate on !isFetchingBalance to avoid flash while balance is still loading
const isInsufficientBalance = useMemo(() => {
return isLoggedIn && !!amount && !hasEnoughBalance && !isLoading && !isCreatingCharge && !isRecording
}, [isLoggedIn, amount, hasEnoughBalance, isLoading, isCreatingCharge, isRecording])
return (
isLoggedIn &&
!!amount &&
!hasEnoughBalance &&
!isFetchingBalance &&
!isLoading &&
!isCreatingCharge &&
!isRecording
)
}, [isLoggedIn, amount, hasEnoughBalance, isFetchingBalance, isLoading, isCreatingCharge, isRecording])

// execute the payment (called from input view)
const executePayment = useCallback(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export function useSemanticRequestFlow() {
sendTransactions,
formattedBalance,
hasSufficientBalance,
isFetchingBalance,
} = useWallet()

// use token selector context for ui integration
Expand Down Expand Up @@ -174,11 +175,13 @@ export function useSemanticRequestFlow() {
}, [amount, hasSufficientBalance])

// check if should show insufficient balance error
// gate on !isFetchingBalance to avoid flash while balance is still loading
const isInsufficientBalance = useMemo(() => {
return (
isLoggedIn &&
!!amount &&
!hasEnoughBalance &&
!isFetchingBalance &&
!isLoading &&
!isCreatingCharge &&
!isFetchingCharge &&
Expand All @@ -189,6 +192,7 @@ export function useSemanticRequestFlow() {
isLoggedIn,
amount,
hasEnoughBalance,
isFetchingBalance,
isLoading,
isCreatingCharge,
isFetchingCharge,
Expand Down
13 changes: 9 additions & 4 deletions src/features/payments/shared/components/SendWithPeanutCta.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,15 @@ export default function SendWithPeanutCta({
const { user, isFetchingUser } = useAuth()

const isLoggedIn = !!user?.user?.userId
// assume logged in while fetching to prevent "Join Peanut" flash
const showAsLoggedIn = isFetchingUser || isLoggedIn

const handleClick = (e: React.MouseEvent<HTMLButtonElement>) => {
// don't act while auth is still resolving
if (isFetchingUser) return

// if auth is required and user is not logged in, redirect to login
if (requiresAuth && !user?.user?.userId && !isFetchingUser) {
if (requiresAuth && !isLoggedIn) {
saveRedirectUrl()
router.push('/setup')
return
Expand All @@ -66,14 +71,14 @@ export default function SendWithPeanutCta({
}

const icon = useMemo((): IconName | undefined => {
if (!isLoggedIn) {
if (!showAsLoggedIn) {
return undefined
}
if (insufficientBalance) {
return 'arrow-down'
}
return 'arrow-up-right'
}, [isLoggedIn, insufficientBalance])
}, [showAsLoggedIn, insufficientBalance])

const peanutLogo = useMemo((): React.ReactNode => {
return (
Expand All @@ -94,7 +99,7 @@ export default function SendWithPeanutCta({
onClick={handleClick}
{...props}
>
{!isLoggedIn ? (
{!showAsLoggedIn ? (
<div className="flex items-center gap-1">
<div>Join </div>
{peanutLogo}
Expand Down
Loading