From a29ceacb063b79fd55c02f9af7446734a295fdc5 Mon Sep 17 00:00:00 2001 From: Danh Date: Mon, 31 Jul 2023 23:55:49 +0700 Subject: [PATCH 1/3] filter txs by account --- src/state/transactions/hooks.tsx | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/state/transactions/hooks.tsx b/src/state/transactions/hooks.tsx index add202bf21..f3953d3190 100644 --- a/src/state/transactions/hooks.tsx +++ b/src/state/transactions/hooks.tsx @@ -54,17 +54,28 @@ export function useTransactionAdder(): (tx: TransactionHistory) => void { ) } +const filterTxsMapByAccount = (obj: GroupedTxsByHash | undefined, account: string | undefined) => { + if (!obj) return + const result: GroupedTxsByHash = {} + Object.keys(obj).forEach(key => { + const arr = obj[key] ?? [] + if (isOwnTransactionGroup(arr, account)) { + result[key] = obj[key] + } + }) + return result +} // returns all the transactions for the current chain export function useAllTransactions(allChain = false): GroupedTxsByHash | undefined { - const { chainId } = useActiveWeb3React() + const { chainId, account } = useActiveWeb3React() const transactions = useSelector(state => state.transactions) return useMemo(() => { - if (!allChain) return transactions[chainId] + if (!allChain) return filterTxsMapByAccount(transactions[chainId], account) return Object.values(transactions).reduce((rs, obj) => { - return { ...rs, ...obj } + return { ...rs, ...filterTxsMapByAccount(obj, account) } }, {}) - }, [allChain, transactions, chainId]) + }, [allChain, transactions, chainId, account]) } export function useSortRecentTransactions(recentOnly = true, allChain = false) { From 60d70ca438939ae0af102ee4078a806314120458 Mon Sep 17 00:00:00 2001 From: Danh Date: Tue, 1 Aug 2023 10:32:17 +0700 Subject: [PATCH 2/3] fix noti by acc --- src/components/Announcement/type.ts | 1 + src/state/application/hooks.ts | 13 ++++++++----- src/state/transactions/updater.tsx | 2 ++ 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/components/Announcement/type.ts b/src/components/Announcement/type.ts index aadf8574c8..c1c97e6dfc 100644 --- a/src/components/Announcement/type.ts +++ b/src/components/Announcement/type.ts @@ -135,6 +135,7 @@ export enum PopupType { export type PopupContentTxn = { hash: string type: NotificationType + account: string } export type PopupContentSimple = { diff --git a/src/state/application/hooks.ts b/src/state/application/hooks.ts index bdad33a5c6..e91e081e73 100644 --- a/src/state/application/hooks.ts +++ b/src/state/application/hooks.ts @@ -151,7 +151,7 @@ export const useTransactionNotify = () => { const addPopup = useAddPopup() return useCallback( (data: PopupContentTxn) => { - addPopup(data, PopupType.TRANSACTION, data.hash) + addPopup(data, PopupType.TRANSACTION, data.hash, undefined, data.account) }, [addPopup], ) @@ -203,11 +203,14 @@ export function useActivePopups() { const { chainId, account } = useActiveWeb3React() return useMemo(() => { - const topRightPopups = popups.filter(e => { - if ([PopupType.SIMPLE, PopupType.TRANSACTION].includes(e.popupType)) return true + const topRightPopups = popups.filter(item => { + const { popupType, content } = item + if (popupType === PopupType.SIMPLE) return true + if (popupType === PopupType.TRANSACTION) return account === item.account + const announcementsAckMap = getAnnouncementsAckMap() - const isRead = announcementsAckMap[e.content.metaMessageId] - if (e.popupType === PopupType.TOP_RIGHT) return !isRead + const isRead = announcementsAckMap[content?.metaMessageId] + if (popupType === PopupType.TOP_RIGHT) return !isRead return false }) diff --git a/src/state/transactions/updater.tsx b/src/state/transactions/updater.tsx index 1335f49652..df6923298b 100644 --- a/src/state/transactions/updater.tsx +++ b/src/state/transactions/updater.tsx @@ -135,6 +135,7 @@ export default function Updater(): null { transactionNotify({ hash: receipt.transactionHash, type: receipt.status === 1 ? NotificationType.SUCCESS : NotificationType.ERROR, + account: account ?? '', }) if (receipt.status === 1) { const arbitrary = transaction.extraInfo?.arbitrary @@ -223,6 +224,7 @@ export default function Updater(): null { transactionNotify({ hash, type: tx.meta?.err ? NotificationType.ERROR : NotificationType.SUCCESS, + account: account ?? '', }) if (!tx.meta?.err && transaction) { const arbitrary = transaction.extraInfo?.arbitrary From 9e96cfab3a2e4f6b48db893fe53bf0cea5632157 Mon Sep 17 00:00:00 2001 From: Danh Date: Tue, 1 Aug 2023 10:40:21 +0700 Subject: [PATCH 3/3] refactor params --- src/components/Announcement/Popups/index.tsx | 29 +++++++++++++++++--- src/state/application/hooks.ts | 28 +++++++++++-------- 2 files changed, 42 insertions(+), 15 deletions(-) diff --git a/src/components/Announcement/Popups/index.tsx b/src/components/Announcement/Popups/index.tsx index 742f7e862d..3d597f7bfc 100644 --- a/src/components/Announcement/Popups/index.tsx +++ b/src/components/Announcement/Popups/index.tsx @@ -91,7 +91,12 @@ export default function Popups() { const { popupType } = item.templateBody if ((!isInit.current && popupType === PopupType.CENTER) || popupType !== PopupType.CENTER) { // only show PopupType.CENTER when the first visit app - addPopup(item, popupType, item.metaMessageId, null) + addPopup({ + content: item, + popupType, + key: item.metaMessageId, + removeAfterMs: null, + }) } }) isInit.current = true @@ -101,11 +106,21 @@ export default function Popups() { data.forEach(item => { switch (item.templateType) { case PrivateAnnouncementType.CROSS_CHAIN: - addPopup(item, PopupType.TOP_RIGHT, item.metaMessageId, 15_000) + addPopup({ + content: item, + popupType: PopupType.TOP_RIGHT, + key: item.metaMessageId, + removeAfterMs: 15_000, + }) break case PrivateAnnouncementType.DIRECT_MESSAGE: { const { templateBody, metaMessageId } = item - addPopup(item, templateBody.popupType, metaMessageId, undefined, account) + addPopup({ + content: item, + popupType: templateBody.popupType, + key: metaMessageId, + account, + }) break } } @@ -116,7 +131,13 @@ export default function Popups() { switch (item.templateType) { case PrivateAnnouncementType.PRICE_ALERT: const mins = (Date.now() / 1000 - item.createdAt) / TIMES_IN_SECS.ONE_MIN - if (mins <= 5) addPopup(item, PopupType.TOP_RIGHT, item.metaMessageId, 15_000) + if (mins <= 5) + addPopup({ + content: item, + popupType: PopupType.TOP_RIGHT, + key: item.metaMessageId, + removeAfterMs: 15_000, + }) break } }) diff --git a/src/state/application/hooks.ts b/src/state/application/hooks.ts index e91e081e73..ab39abe3cd 100644 --- a/src/state/application/hooks.ts +++ b/src/state/application/hooks.ts @@ -117,19 +117,20 @@ export function useRegisterCampaignSuccessModalToggle(): () => void { return useToggleModal(ApplicationModal.REGISTER_CAMPAIGN_SUCCESS) } +type AddPopupPayload = { + content: PopupContent + popupType: PopupType + key?: string + removeAfterMs?: number | null + account?: string +} // returns a function that allows adding a popup -export function useAddPopup(): ( - content: PopupContent, - popupType: PopupType, - key?: string, - removeAfterMs?: number | null, - account?: string, -) => void { +export function useAddPopup(): (data: AddPopupPayload) => void { const dispatch = useDispatch() return useCallback( - (content: PopupContent, popupType: PopupType, key?: string, removeAfterMs?: number | null, account?: string) => { - dispatch(addPopup({ content, key, popupType, removeAfterMs, account })) + (data: AddPopupPayload) => { + dispatch(addPopup(data)) }, [dispatch], ) @@ -140,7 +141,7 @@ export const useNotify = () => { const addPopup = useAddPopup() return useCallback( (data: PopupContentSimple, removeAfterMs: number | null | undefined = 4000) => { - addPopup(data, PopupType.SIMPLE, data.title + Math.random(), removeAfterMs) + addPopup({ content: data, popupType: PopupType.SIMPLE, key: data.title + Math.random(), removeAfterMs }) }, [addPopup], ) @@ -151,7 +152,12 @@ export const useTransactionNotify = () => { const addPopup = useAddPopup() return useCallback( (data: PopupContentTxn) => { - addPopup(data, PopupType.TRANSACTION, data.hash, undefined, data.account) + addPopup({ + content: data, + popupType: PopupType.TRANSACTION, + key: data.hash, + account: data.account, + }) }, [addPopup], )