Skip to content

Commit

Permalink
move file
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenhoaidanh committed Oct 3, 2023
1 parent e266379 commit 9d3a0da
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 57 deletions.
2 changes: 1 addition & 1 deletion src/components/Announcement/Popups/CenterPopup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { useMedia } from 'react-use'
import styled from 'styled-components'

import CtaButton from 'components/Announcement/Popups/CtaButton'
import { useNavigateToUrl } from 'components/Announcement/helper'
import {
AnnouncementTemplatePopup,
PopupContentAnnouncement,
Expand All @@ -17,6 +16,7 @@ import { Z_INDEXS } from 'constants/styles'
import useMixpanel, { MIXPANEL_TYPE } from 'hooks/useMixpanel'
import useTheme from 'hooks/useTheme'
import { MEDIA_WIDTHS } from 'theme'
import { useNavigateToUrl } from 'utils/redirect'
import { escapeScriptHtml } from 'utils/string'

const Wrapper = styled.div`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import styled from 'styled-components'

import NotificationImage from 'assets/images/notification_default.png'
import CtaButton from 'components/Announcement/Popups/CtaButton'
import { useNavigateToUrl } from 'components/Announcement/helper'
import { AnnouncementTemplatePopup } from 'components/Announcement/type'
import Modal from 'components/Modal'
import Row from 'components/Row'
import { Z_INDEXS } from 'constants/styles'
import useTheme from 'hooks/useTheme'
import { useDetailAnnouncement } from 'state/application/hooks'
import { MEDIA_WIDTHS } from 'theme'
import { useNavigateToUrl } from 'utils/redirect'
import { escapeScriptHtml } from 'utils/string'

const PaginationButton = styled.div`
Expand Down
2 changes: 1 addition & 1 deletion src/components/Announcement/Popups/SnippetPopup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { Swiper, SwiperSlide } from 'swiper/react'

import NotificationImage from 'assets/images/notification_default.png'
import CtaButton from 'components/Announcement/Popups/CtaButton'
import { useNavigateToUrl } from 'components/Announcement/helper'
import {
AnnouncementTemplatePopup,
PopupContentAnnouncement,
Expand All @@ -20,6 +19,7 @@ import { Z_INDEXS } from 'constants/styles'
import useMixpanel, { MIXPANEL_TYPE } from 'hooks/useMixpanel'
import useTheme from 'hooks/useTheme'
import { useDetailAnnouncement, useRemovePopup } from 'state/application/hooks'
import { useNavigateToUrl } from 'utils/redirect'

const IMAGE_HEIGHT = '124px'
const PADDING_MOBILE = '16px'
Expand Down
2 changes: 1 addition & 1 deletion src/components/Announcement/Popups/TopBanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import { useMedia } from 'react-use'
import styled, { css, keyframes } from 'styled-components'

import CtaButton from 'components/Announcement/Popups/CtaButton'
import { useNavigateToUrl } from 'components/Announcement/helper'
import { AnnouncementTemplatePopup, PopupType } from 'components/Announcement/type'
import Announcement from 'components/Icons/Announcement'
import useMixpanel, { MIXPANEL_TYPE } from 'hooks/useMixpanel'
import useTheme from 'hooks/useTheme'
import { useActivePopups, useRemoveAllPopupByType } from 'state/application/hooks'
import { MEDIA_WIDTHS } from 'theme'
import { useNavigateToUrl } from 'utils/redirect'
import { escapeScriptHtml } from 'utils/string'

const BannerWrapper = styled.div<{ color?: string }>`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import {
RowItem,
Title,
} from 'components/Announcement/PrivateAnnoucement/styled'
import { useNavigateToUrl } from 'components/Announcement/helper'
import { AnnouncementTemplatePoolPosition } from 'components/Announcement/type'
import { DoubleCurrencyLogoV2 } from 'components/DoubleLogo'
import { MoneyBag } from 'components/Icons'
import { APP_PATHS } from 'constants/index'
import { NETWORKS_INFO } from 'constants/networks'
import useTheme from 'hooks/useTheme'
import { useNavigateToUrl } from 'utils/redirect'

function InboxItemBridge({
announcement,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import { Flex, Text } from 'rebass'
import { PrivateAnnouncementProp } from 'components/Announcement/PrivateAnnoucement'
import InboxIcon from 'components/Announcement/PrivateAnnoucement/Icon'
import { Dot, InboxItemRow, InboxItemWrapper, RowItem, Title } from 'components/Announcement/PrivateAnnoucement/styled'
import { useNavigateToUrl } from 'components/Announcement/helper'
import { AnnouncementTemplatePriceAlert } from 'components/Announcement/type'
import { ButtonLight } from 'components/Button'
import DeltaTokenAmount from 'components/WalletPopup/Transactions/DeltaTokenAmount'
import useTheme from 'hooks/useTheme'
import { HistoricalPriceAlert, PriceAlertType } from 'pages/NotificationCenter/const'
import { useNavigateToUrl } from 'utils/redirect'

export const getSwapUrlPriceAlert = (alert: HistoricalPriceAlert) => {
const { swapURL } = alert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import styled from 'styled-components'
import { PrivateAnnouncementProp } from 'components/Announcement/PrivateAnnoucement'
import InboxIcon from 'components/Announcement/PrivateAnnoucement/Icon'
import { Dot, InboxItemRow, InboxItemWrapper, RowItem, Title } from 'components/Announcement/PrivateAnnoucement/styled'
import { useNavigateToUrl } from 'components/Announcement/helper'
import { AnnouncementTemplatePopup } from 'components/Announcement/type'
import { useNavigateToUrl } from 'utils/redirect'
import { escapeScriptHtml } from 'utils/string'

const Desc = styled.div`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import styled from 'styled-components'
import { ReactComponent as DropdownSVG } from 'assets/svg/down.svg'
import InboxIcon from 'components/Announcement/PrivateAnnoucement/Icon'
import { PrivateAnnouncementPropCenter } from 'components/Announcement/PrivateAnnoucement/NotificationCenter'
import { useNavigateToUrl } from 'components/Announcement/helper'
import { AnnouncementTemplatePoolPosition } from 'components/Announcement/type'
import { DoubleCurrencyLogoV2 } from 'components/DoubleLogo'
import { MoneyBag } from 'components/Icons'
import { APP_PATHS } from 'constants/index'
import { NETWORKS_INFO } from 'constants/networks'
import useTheme from 'hooks/useTheme'
import { useNavigateToUrl } from 'utils/redirect'
import { formatTime } from 'utils/time'

import { Desc, Time, Title, Wrapper } from './styled'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import styled from 'styled-components'
import InboxIcon from 'components/Announcement/PrivateAnnoucement/Icon'
import { getSwapUrlPriceAlert } from 'components/Announcement/PrivateAnnoucement/InboxItemPriceAlert'
import { PrivateAnnouncementPropCenter } from 'components/Announcement/PrivateAnnoucement/NotificationCenter'
import { useNavigateToUrl } from 'components/Announcement/helper'
import { AnnouncementTemplatePriceAlert } from 'components/Announcement/type'
import AlertCondition from 'pages/NotificationCenter/PriceAlerts/AlertCondition'
import { useNavigateToUrl } from 'utils/redirect'
import { formatTime } from 'utils/time'

import { Desc, Time, Title, Wrapper } from './styled'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Flex } from 'rebass'

import InboxIcon from 'components/Announcement/PrivateAnnoucement/Icon'
import { PrivateAnnouncementPropCenter } from 'components/Announcement/PrivateAnnoucement/NotificationCenter'
import { useNavigateToUrl } from 'components/Announcement/helper'
import { AnnouncementTemplatePopup } from 'components/Announcement/type'
import { useNavigateToUrl } from 'utils/redirect'
import { escapeScriptHtml } from 'utils/string'
import { formatTime } from 'utils/time'

Expand Down
46 changes: 0 additions & 46 deletions src/components/Announcement/helper.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { ChainId } from '@kyberswap/ks-sdk-core'
import { useCallback } from 'react'
import { useNavigate } from 'react-router-dom'
import AnnouncementApi from 'services/announcement'

import { AnnouncementTemplatePopup, PopupContentAnnouncement, PopupItemType } from 'components/Announcement/type'
import { TIMES_IN_SECS } from 'constants/index'
import { useActiveWeb3React } from 'hooks'
import { useChangeNetwork } from 'hooks/web3/useChangeNetwork'
import { useAppDispatch } from 'state/hooks'
import { isValidRedirectURL } from 'utils/redirect'

const LsKey = 'ack-announcements'
export const getAnnouncementsAckMap = () => JSON.parse(localStorage[LsKey] || '{}')
Expand Down Expand Up @@ -47,48 +43,6 @@ export const isPopupCanShow = (
return !isRead && !isExpired && isRightChain && isOwn
}

/**
* this hook to navigate to specific url
* detect using window.open or navigate (react-router)
* check change chain if needed
*/
export const useNavigateToUrl = () => {
const navigate = useNavigate()
const { chainId: currentChain } = useActiveWeb3React()
const { changeNetwork } = useChangeNetwork()

const redirect = useCallback(
(actionURL: string) => {
if (actionURL && actionURL.startsWith('/')) {
navigate(actionURL)
return
}
const { pathname, host, search } = new URL(actionURL)
if (!isValidRedirectURL(actionURL, false)) return
if (window.location.host === host) {
navigate(`${pathname}${search}`)
} else {
window.open(actionURL)
}
},
[navigate],
)

return useCallback(
(actionURL: string, chainId?: ChainId) => {
try {
if (!actionURL) return
if (chainId && chainId !== currentChain) {
changeNetwork(chainId, () => redirect(actionURL), undefined, true)
} else {
redirect(actionURL)
}
} catch (error) {}
},
[changeNetwork, currentChain, redirect],
)
}

export const useInvalidateTags = (reducerPath: string) => {
const dispatch = useAppDispatch()
return useCallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import NotificationImage from 'assets/images/notification_default.png'
import { ReactComponent as DropdownSVG } from 'assets/svg/down.svg'
import CtaButton from 'components/Announcement/Popups/CtaButton'
import { formatCtaName } from 'components/Announcement/Popups/DetailAnnouncementPopup'
import { useNavigateToUrl } from 'components/Announcement/helper'
import { Announcement } from 'components/Announcement/type'
import { MEDIA_WIDTHS } from 'theme'
import { useNavigateToUrl } from 'utils/redirect'
import { escapeScriptHtml } from 'utils/string'
import { formatTime } from 'utils/time'

Expand Down
49 changes: 49 additions & 0 deletions src/utils/redirect.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import { ChainId } from '@kyberswap/ks-sdk-core'
import { useCallback } from 'react'
import { useNavigate } from 'react-router-dom'

import { useActiveWeb3React } from 'hooks'
import { useChangeNetwork } from 'hooks/web3/useChangeNetwork'

const whiteListDomains = [/https:\/\/(.+?\.)?kyberswap\.com$/, /https:\/\/(.+)\.kyberengineering\.io$/]
export const isValidRedirectURL = (url: string | undefined, checkWhitelist = true) => {
try {
Expand All @@ -15,3 +22,45 @@ export const isValidRedirectURL = (url: string | undefined, checkWhitelist = tru
return false
}
}

/**
* this hook to navigate to specific url
* detect using window.open or navigate (react-router)
* check change chain if needed
*/
export const useNavigateToUrl = () => {
const navigate = useNavigate()
const { chainId: currentChain } = useActiveWeb3React()
const { changeNetwork } = useChangeNetwork()

const redirect = useCallback(
(actionURL: string) => {
if (actionURL && actionURL.startsWith('/')) {
navigate(actionURL)
return
}
const { pathname, host, search } = new URL(actionURL)
if (!isValidRedirectURL(actionURL, false)) return
if (window.location.host === host) {
navigate(`${pathname}${search}`)
} else {
window.open(actionURL)
}
},
[navigate],
)

return useCallback(
(actionURL: string, chainId?: ChainId) => {
try {
if (!actionURL) return
if (chainId && chainId !== currentChain) {
changeNetwork(chainId, () => redirect(actionURL), undefined, true)
} else {
redirect(actionURL)
}
} catch (error) {}
},
[changeNetwork, currentChain, redirect],
)
}

0 comments on commit 9d3a0da

Please sign in to comment.