-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
75 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { useQuery } from '@tanstack/react-query' | ||
import { MANGO_DATA_API_URL } from 'utils/constants' | ||
import { ActivityFeed, EmptyObject } from 'types' | ||
import dayjs from 'dayjs' | ||
import useMangoAccount from './useMangoAccount' | ||
|
||
const fetchHistory = async ( | ||
mangoAccountPk: string, | ||
): Promise<Array<ActivityFeed> | EmptyObject | null> => { | ||
const response = await fetch( | ||
`${MANGO_DATA_API_URL}/stats/activity-feed?mango-account=${mangoAccountPk}&offset=0&limit=1000`, | ||
) | ||
const parsedResponse: Array<ActivityFeed> = await response.json() | ||
|
||
if (Array.isArray(parsedResponse)) { | ||
const activity = parsedResponse | ||
.map((i) => { | ||
return { | ||
...i, | ||
} | ||
}) | ||
.sort( | ||
(a, b) => | ||
dayjs(b.block_datetime).unix() - dayjs(a.block_datetime).unix(), | ||
) | ||
|
||
// only add to current feed if data request is offset and the mango account hasn't changed | ||
// const combinedFeed = | ||
// offset !== 0 ? loadedFeed.concat(latestFeed) : latestFeed | ||
|
||
return activity | ||
} else return null | ||
} | ||
|
||
export default function useAccountHistory() { | ||
const { mangoAccount } = useMangoAccount() | ||
|
||
const response = useQuery<Array<ActivityFeed> | EmptyObject | null>( | ||
['history'], | ||
() => | ||
mangoAccount?.publicKey | ||
? fetchHistory(mangoAccount.publicKey.toString()) | ||
: null, | ||
{ | ||
cacheTime: 1000 * 60 * 5, | ||
staleTime: 1000 * 60, | ||
retry: 3, | ||
refetchOnWindowFocus: true, | ||
enabled: !!mangoAccount, | ||
}, | ||
) | ||
|
||
return response | ||
} |