-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate activities from transactions
- Loading branch information
1 parent
cfa43b8
commit 1ce5f97
Showing
8 changed files
with
68 additions
and
35 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 was deleted.
Oops, something went wrong.
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,6 @@ | ||
import { selectTransactions } from "#/store/wallet" | ||
import { useAppSelector } from "./useAppSelector" | ||
|
||
export function useTransactions() { | ||
return useAppSelector(selectTransactions) | ||
} |
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 |
---|---|---|
@@ -1,24 +1,42 @@ | ||
import { useCallback } from "react" | ||
import { subgraphAPI } from "#/utils" | ||
import { setActivities } from "#/store/wallet" | ||
import { useCallback, useEffect } from "react" | ||
import { logPromiseFailure, subgraphAPI } from "#/utils" | ||
import { setActivities, setTransactions } from "#/store/wallet" | ||
import { useInterval } from "@chakra-ui/react" | ||
import { ONE_MINUTE_IN_SECONDS, ONE_SEC_IN_MILLISECONDS } from "#/constants" | ||
import { useAppDispatch } from "../store/useAppDispatch" | ||
import { useWalletContext } from "../useWalletContext" | ||
|
||
const INTERVAL_TIME = ONE_SEC_IN_MILLISECONDS * ONE_MINUTE_IN_SECONDS * 30 | ||
// TODO: Use the correct function from SDK | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const calculateEthAddress = (btcAddress: string) => | ||
"0x4c9e39e5ff458a811708c03aea21b8327118cf13" | ||
"0x45e2e415989477ea5450e440405f6ac858019e6b" | ||
|
||
export function useFetchActivities() { | ||
const dispatch = useAppDispatch() | ||
const { btcAccount } = useWalletContext() | ||
|
||
return useCallback(async () => { | ||
const fetchActivities = useCallback(async () => { | ||
if (!btcAccount) return | ||
|
||
const ethAddress = calculateEthAddress(btcAccount.address) | ||
const result = await subgraphAPI.fetchActivityData(ethAddress) | ||
const activities = result.filter(({ status }) => status !== "completed") | ||
|
||
dispatch(setActivities(result)) | ||
dispatch(setTransactions(result)) | ||
dispatch(setActivities(activities)) | ||
}, [btcAccount, dispatch]) | ||
|
||
const handleFetchActivities = useCallback( | ||
() => logPromiseFailure(fetchActivities()), | ||
[fetchActivities], | ||
) | ||
|
||
useEffect(() => { | ||
if (!btcAccount) return | ||
|
||
handleFetchActivities() | ||
}, [btcAccount, dispatch, handleFetchActivities]) | ||
|
||
useInterval(handleFetchActivities, INTERVAL_TIME) | ||
} |
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 |
---|---|---|
@@ -1,15 +1,5 @@ | ||
import { useEffect } from "react" | ||
import { logPromiseFailure } from "#/utils" | ||
import { useFetchActivities } from "./useFetchActivities" | ||
import { useWalletContext } from "../useWalletContext" | ||
|
||
export function useInitDataFromSubgraph() { | ||
const { btcAccount } = useWalletContext() | ||
const fetchActivities = useFetchActivities() | ||
|
||
useEffect(() => { | ||
if (btcAccount) { | ||
logPromiseFailure(fetchActivities()) | ||
} | ||
}, [btcAccount, fetchActivities]) | ||
useFetchActivities() | ||
} |
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 |
---|---|---|
@@ -1,11 +1,8 @@ | ||
import { Activity } from "#/types" | ||
import { createSelector } from "@reduxjs/toolkit" | ||
import { RootState } from ".." | ||
|
||
export const selectActivities = (state: RootState): Activity[] => | ||
state.wallet.activities | ||
Object.values(state.wallet.activities) | ||
|
||
export const selectCompletedActivities = createSelector( | ||
[selectActivities], | ||
(activities) => activities.filter(({ status }) => status === "completed"), | ||
) | ||
export const selectTransactions = (state: RootState): Activity[] => | ||
state.wallet.transactions |
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 |
---|---|---|
@@ -1,22 +1,46 @@ | ||
import { Activity } from "#/types" | ||
import { ActivitiesByIds, Activity } from "#/types" | ||
import { PayloadAction, createSlice } from "@reduxjs/toolkit" | ||
|
||
type WalletState = { | ||
activities: Activity[] | ||
activities: ActivitiesByIds | ||
transactions: Activity[] | ||
} | ||
|
||
const initialState: WalletState = { | ||
activities: [], | ||
activities: {}, | ||
transactions: [], | ||
} | ||
|
||
export const walletSlice = createSlice({ | ||
name: "wallet", | ||
initialState, | ||
reducers: { | ||
setTransactions(state, action: PayloadAction<Activity[]>) { | ||
state.transactions = action.payload | ||
}, | ||
setActivities(state, action: PayloadAction<Activity[]>) { | ||
state.activities = action.payload | ||
const newActivitiesByIds = Object.fromEntries( | ||
action.payload.map((activity) => [activity.id, activity]), | ||
) | ||
const newActivitiesIds = new Set(Object.keys(newActivitiesByIds)) | ||
|
||
const updatedActivities = Object.values( | ||
state.activities, | ||
).reduce<ActivitiesByIds>((acc, activity) => { | ||
if (!newActivitiesIds.has(activity.id)) { | ||
acc[activity.id] = { ...activity, status: "completed" } | ||
} | ||
return acc | ||
}, {}) | ||
|
||
state.activities = Object.assign(updatedActivities, newActivitiesByIds) | ||
}, | ||
deleteActivity(state, action: PayloadAction<string>) { | ||
const activityId = action.payload | ||
delete state.activities[activityId] | ||
}, | ||
}, | ||
}) | ||
|
||
export const { setActivities } = walletSlice.actions | ||
export const { setTransactions, setActivities, deleteActivity } = | ||
walletSlice.actions |
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