update acct history with refresh

This commit is contained in:
tjs 2023-11-10 12:29:03 -05:00
parent 90c5810f17
commit 1df2e35169
2 changed files with 23 additions and 13 deletions

View File

@ -6,9 +6,18 @@ import { ActivityFeed, DepositWithdrawFeedItem, SwapHistoryItem } from 'types'
import SheenLoader from './shared/SheenLoader'
const TransactionHistory = () => {
const { history, isLoading } = useAccountHistory()
const { history, isLoading, refetch } = useAccountHistory()
return (
<div className="flex min-h-[380px] flex-col space-y-2 rounded-2xl border-2 border-th-fgd-1 bg-th-bkg-1 p-6">
<div className="text-center text-sm opacity-50">
History may be delayed by a few minutes.{' '}
<button
className="text-blue-400 hover:text-blue-600 active:text-purple-900"
onClick={() => refetch()}
>
Click to refresh.
</button>
</div>
{history?.length ? (
history.map((history: ActivityFeed | any) => {
const { activity_type, activity_details } = history

View File

@ -7,7 +7,7 @@ import { useMemo } from 'react'
import mangoStore from '@store/mangoStore'
import { PublicKey } from '@solana/web3.js'
import useMangoGroup from './useMangoGroup'
import { AnchorProvider } from '@project-serum/anchor'
import { useWallet } from '@solana/wallet-adapter-react'
const fetchHistory = async (
mangoAccountPk: string,
@ -41,13 +41,13 @@ const accountNums = STAKEABLE_TOKENS_DATA.map((d) => d.id)
export default function useAccountHistory() {
const { stakeAccounts } = useStakeAccounts()
const { group } = useMangoGroup()
const { wallet } = useWallet()
// const accountPks = stakeAccounts?.map((acc) => acc.publicKey.toString()) || []
const accountPks = useMemo(() => {
const client = mangoStore.getState().client
const payer = (client.program.provider as AnchorProvider).wallet.publicKey
if (!group) return []
const payer = wallet?.adapter.publicKey?.toBuffer()
if (!group || !payer) return []
const x = accountNums.map((n) => {
const acctNumBuffer = Buffer.alloc(4)
@ -56,7 +56,7 @@ export default function useAccountHistory() {
[
Buffer.from('MangoAccount'),
group.publicKey.toBuffer(),
payer.toBuffer(),
payer,
acctNumBuffer,
],
client.program.programId,
@ -64,15 +64,17 @@ export default function useAccountHistory() {
return mangoAccountPda.toString()
})
return x
}, [group])
}, [group, wallet])
const activeStakeAccts =
stakeAccounts?.map((acc) => acc.publicKey.toString()) ?? []
const uniqueAccts = [...new Set([...accountPks, ...activeStakeAccts])]
const response = useQuery<Array<ActivityFeed[] | null> | EmptyObject | null>(
['history', ...accountPks],
['history', ...activeStakeAccts],
() =>
stakeAccounts?.length
? Promise.all(
stakeAccounts.map((acc) => fetchHistory(acc.publicKey.toString())),
)
? Promise.all(uniqueAccts.map((act) => fetchHistory(act)))
: // ? fetchHistory(mangoAccount.publicKey.toString())
null,
{
@ -84,9 +86,8 @@ export default function useAccountHistory() {
},
)
console.log('response', response)
return {
refetch: response.refetch,
history:
response?.data && Array.isArray(response.data)
? response.data