From f3873b55bae51536969c9372899911ec538fcfba Mon Sep 17 00:00:00 2001 From: danidfra Date: Wed, 26 Mar 2025 17:59:36 -0300 Subject: [PATCH] Update fetch wallet data --- src/components/pure-status-action-bar.tsx | 7 +++--- src/components/status-action-bar.tsx | 7 +++--- src/features/zap/hooks/useHooks.ts | 27 ++++++++++++++++++----- src/pages/search-page.tsx | 2 +- 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/components/pure-status-action-bar.tsx b/src/components/pure-status-action-bar.tsx index 075fc00cf..2c47abfd9 100644 --- a/src/components/pure-status-action-bar.tsx +++ b/src/components/pure-status-action-bar.tsx @@ -48,7 +48,7 @@ import DropdownMenu from 'soapbox/components/dropdown-menu/index.ts'; import PureStatusReactionWrapper from 'soapbox/components/pure-status-reaction-wrapper.tsx'; import StatusActionButton from 'soapbox/components/status-action-button.tsx'; import HStack from 'soapbox/components/ui/hstack.tsx'; -import { useZapCashuRequest, useWallet } from 'soapbox/features/zap/hooks/useHooks.ts'; +import { useZapCashuRequest, useWalletStore } from 'soapbox/features/zap/hooks/useHooks.ts'; import { useAppDispatch } from 'soapbox/hooks/useAppDispatch.ts'; import { useAppSelector } from 'soapbox/hooks/useAppSelector.ts'; import { useDislike } from 'soapbox/hooks/useDislike.ts'; @@ -180,7 +180,7 @@ const PureStatusActionBar: React.FC = ({ const features = useFeatures(); const { boostModal, deleteModal } = useSettings(); - const { wallet } = useWallet(); + const { acceptsZapsCashu } = useWalletStore(); const { zapCashuList } = useZapCashuRequest(); const isZappedCashu = zapCashuList.some((zapCashu)=> zapCashu === status.id); @@ -764,7 +764,6 @@ const PureStatusActionBar: React.FC = ({ const canShare = ('share' in navigator) && (status.visibility === 'public' || status.visibility === 'group'); const acceptsZaps = status.account.ditto.accepts_zaps === true; - const hasWallet = wallet !== null; const spacing: { [key: string]: React.ComponentProps['space']; @@ -848,7 +847,7 @@ const PureStatusActionBar: React.FC = ({ /> )} - {(acceptsZaps || hasWallet) && ( + {(acceptsZaps || acceptsZapsCashu) && ( = ({ const features = useFeatures(); const { boostModal, deleteModal } = useSettings(); - const { wallet } = useWallet(); + const { acceptsZapsCashu } = useWalletStore(); const { zapCashuList } = useZapCashuRequest(); const isZappedCashu = zapCashuList.some((zapCashu)=> zapCashu === status.id); @@ -754,7 +754,6 @@ const StatusActionBar: React.FC = ({ const canShare = ('share' in navigator) && (status.visibility === 'public' || status.visibility === 'group'); const acceptsZaps = status.account.ditto.accepts_zaps === true; - const hasWallet = wallet !== null; const spacing: { [key: string]: React.ComponentProps['space']; @@ -838,7 +837,7 @@ const StatusActionBar: React.FC = ({ /> )} - {(acceptsZaps || hasWallet) && ( + {(acceptsZaps || acceptsZapsCashu) && ( void; setWallet: (wallet: WalletData | null) => void; + setHasFetchedWallet: (hasFetchedWallet: boolean) => void; setTransactions: (transactions: Transactions | null, prevTransaction?: string | null, nextTransaction?: string | null) => void; + setHasFetchedTransactions: (hasFetched: boolean) => void; addZapCashu: (statusId: string) => void; } @@ -26,13 +32,19 @@ interface IWalletInfo { const useWalletStore = create((set) => ({ wallet: null, + acceptsZapsCashu: false, transactions: null, prevTransaction: null, nextTransaction: null, zapCashuList: [], + hasFetchedWallet: false, + hasFetchedTransactions: false, + setAcceptsZapsCashu: (acceptsZapsCashu) => set({ acceptsZapsCashu }), setWallet: (wallet) => set({ wallet }), + setHasFetchedWallet: (hasFetchedWallet) => set({ hasFetchedWallet }), setTransactions: (transactions, prevTransaction, nextTransaction) => set({ transactions, prevTransaction, nextTransaction }), + setHasFetchedTransactions: (hasFetched) => set({ hasFetchedTransactions: hasFetched }), addZapCashu: (statusId) => set((state) => ({ zapCashuList: [ @@ -44,7 +56,7 @@ const useWalletStore = create((set) => ({ const useWallet = () => { const api = useApi(); - const { wallet, setWallet } = useWalletStore(); + const { wallet, setWallet, setAcceptsZapsCashu, hasFetchedWallet, setHasFetchedWallet } = useWalletStore(); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); @@ -74,6 +86,8 @@ const useWallet = () => { const data = await response.json(); if (data) { const normalizedData = baseWalletSchema.parse(data); + setAcceptsZapsCashu(true); + setWallet(normalizedData); } } catch (err) { @@ -82,11 +96,13 @@ const useWallet = () => { setError(messageError); } finally { setIsLoading(false); + setHasFetchedWallet(true); } }; useEffect(() => { - if (!wallet) { + if (!hasFetchedWallet) { + setHasFetchedWallet(true); getWallet(false); } }, []); @@ -96,7 +112,7 @@ const useWallet = () => { const useTransactions = () => { const api = useApi(); - const { transactions, nextTransaction, setTransactions } = useWalletStore(); + const { transactions, nextTransaction, setTransactions, hasFetchedTransactions, setHasFetchedTransactions } = useWalletStore(); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); @@ -144,7 +160,8 @@ const useTransactions = () => { }; useEffect(() => { - if (!transactions) { + if (!hasFetchedTransactions) { + setHasFetchedTransactions(true); getTransactions(); } }, []); @@ -192,4 +209,4 @@ const useZapCashuRequest = () => { return { zapCashuList, isLoading, error, zapCashuRequest }; }; -export { useWallet, useTransactions, useZapCashuRequest }; \ No newline at end of file +export { useWalletStore, useWallet, useTransactions, useZapCashuRequest }; \ No newline at end of file diff --git a/src/pages/search-page.tsx b/src/pages/search-page.tsx index 6b48177cc..e05df5441 100644 --- a/src/pages/search-page.tsx +++ b/src/pages/search-page.tsx @@ -23,7 +23,7 @@ const ExplorePage: React.FC = ({ children }) => { const me = useAppSelector(state => state.me); const features = useFeatures(); const accountsPath = useLocation().pathname === '/explore/accounts'; - const wallet = useWallet(); + const { wallet } = useWallet(); return ( <>