Skip to content

Commit

Permalink
refactor params
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenhoaidanh committed Aug 1, 2023
1 parent 60d70ca commit 9e96cfa
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
29 changes: 25 additions & 4 deletions src/components/Announcement/Popups/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
}
Expand All @@ -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
}
})
Expand Down
28 changes: 17 additions & 11 deletions src/state/application/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
)
Expand All @@ -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],
)
Expand All @@ -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],
)
Expand Down

0 comments on commit 9e96cfa

Please sign in to comment.