Skip to content

Commit

Permalink
Merge branch 'custom-watchlist' of https://github.com/KyberNetwork/ky…
Browse files Browse the repository at this point in the history
…berswap-interface into custom-watchlist
  • Loading branch information
XiaoYhun committed Sep 29, 2023
2 parents 5ee3fb6 + 450867a commit 6cc6bd6
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/components/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function Select({
style?: CSSProperties
menuStyle?: CSSProperties
optionStyle?: CSSProperties
onChange: (value: any) => void
onChange?: (value: any) => void
forceMenuPlacementTop?: boolean
arrowColor?: string
}) {
Expand Down Expand Up @@ -148,7 +148,7 @@ function Select({
if (item.onSelect) item.onSelect?.()
else {
setSelected(value)
onChange(value)
onChange?.(value)
}
}
return (
Expand Down
20 changes: 18 additions & 2 deletions src/pages/TrueSightV2/components/TokenFilter.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ChainId } from '@kyberswap/ks-sdk-core'
import { t } from '@lingui/macro'
import { ReactNode, useCallback, useEffect, useMemo, useRef, useState } from 'react'
import Skeleton from 'react-loading-skeleton'
import { useMedia } from 'react-use'
Expand All @@ -13,8 +14,10 @@ import useShowLoadingAtLeastTime from 'hooks/useShowLoadingAtLeastTime'
import useTheme from 'hooks/useTheme'
import MultipleChainSelect from 'pages/MyEarnings/MultipleChainSelect'
import SubscribeButtonKyberAI from 'pages/TrueSightV2/components/SubscireButtonKyberAI'
import WatchlistButton from 'pages/TrueSightV2/components/WatchlistButton'
import { NETWORK_TO_CHAINID, SUPPORTED_NETWORK_KYBERAI, Z_INDEX_KYBER_AI } from 'pages/TrueSightV2/constants'
import { useGetFilterCategoriesQuery } from 'pages/TrueSightV2/hooks/useKyberAIData'
import { ICustomWatchlists } from 'pages/TrueSightV2/types'
import { useSessionInfo } from 'state/authen/hooks'
import { MEDIA_WIDTHS } from 'theme'

Expand Down Expand Up @@ -141,8 +144,6 @@ export default function TokenFilter({
[defaultFilter, chainFilter],
)

// todo watch list chains

const theme = useTheme()
const [selectedChains, setSelectChains] = useState<ChainId[]>([])
const handleChainChange = useCallback(
Expand All @@ -157,6 +158,12 @@ export default function TokenFilter({
[chainFilter, onChangeFilter, allChainIds, onTrackingSelectChain],
)

const [watchListInfo, setWatchlistInfo] = useState<ICustomWatchlists>()
const handleChangeWatchlist = (watchlist: ICustomWatchlists) => {
setWatchlistInfo(watchlist)
onChangeFilter('watchlist', watchlist.id + '')
}

const isInit = useRef(false)
useEffect(() => {
if (isInit.current || defaultChains.length + allChainIds.length === 0) return
Expand Down Expand Up @@ -213,6 +220,15 @@ export default function TokenFilter({
}}
/>
))}
<WatchlistButton
trigger={
<StyledSelect
options={[]}
activeRender={() => activeRender('Watchlist', watchListInfo?.name || t`All Tokens`)}
/>
}
onSelectWatchlist={handleChangeWatchlist}
/>
</>
)}
</SelectGroup>
Expand Down
71 changes: 49 additions & 22 deletions src/pages/TrueSightV2/components/WatchlistButton.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Trans, t } from '@lingui/macro'
import { AnimatePresence, Reorder, useDragControls } from 'framer-motion'
import { CSSProperties, memo, useEffect, useRef, useState } from 'react'
import { CSSProperties, ReactNode, memo, useEffect, useRef, useState } from 'react'
import { Check, Plus, X } from 'react-feather'
import { useDispatch } from 'react-redux'
import { Text } from 'rebass'
Expand Down Expand Up @@ -352,11 +352,15 @@ function WatchlistButton({
symbol,
size,
wrapperStyle,
onSelectWatchlist,
trigger,
}: {
assetId?: string
symbol?: string
size?: number
wrapperStyle?: CSSProperties
onSelectWatchlist?: (watchListId: ICustomWatchlists) => void
trigger?: ReactNode
}) {
const theme = useTheme()
const dispatch = useDispatch()
Expand Down Expand Up @@ -415,6 +419,32 @@ function WatchlistButton({
debounce(() => updateWatchlistsPriorities({ orderedIds }), 1000)
}

const onSelect = (watchlist: ICustomWatchlists, watched: boolean) => {
if (onSelectWatchlist) {
onSelectWatchlist(watchlist)
return
}
watched ? handleRemoveFromWatchlist(watchlist.id) : handleAddtoWatchlist(watchlist.id)
}

const btnStar = (
<MouseoverTooltip
text={t`You can only watch up to ${MAX_LIMIT_WATCHED_TOKEN} tokens`}
disableTooltip={!isReachMaxLimit}
>
<StarWithAnimation
stopPropagation
loading={false}
watched={!!assetId && !!watchlists && watchlists?.some(item => item.assetIds?.includes(+assetId))}
onClick={() => {
!isReachMaxLimit && setOpenMenu(prev => !prev)
}}
wrapperStyle={wrapperStyle}
size={size}
/>
</MouseoverTooltip>
)

return (
<div onClick={e => e.stopPropagation()}>
<Popover
Expand All @@ -425,14 +455,13 @@ function WatchlistButton({
{watchlists?.map((watchlists: ICustomWatchlists) => {
const watched = !!assetId && !!watchlists.assetIds && watchlists.assetIds.includes(+assetId)
return (
<MenuOption key={watchlists.id}>
<StarWithAnimation
watched={watched}
size={16}
onClick={() => {
watched ? handleRemoveFromWatchlist(watchlists.id) : handleAddtoWatchlist(watchlists.id)
}}
/>
<MenuOption
key={watchlists.id}
onClick={() => {
onSelect(watchlists, watched)
}}
>
<StarWithAnimation watched={watched} size={16} />
{watchlists.name} ({watchlists.assetNumber})
</MenuOption>
)
Expand All @@ -454,20 +483,18 @@ function WatchlistButton({
placement="bottom-start"
noArrow={true}
>
<MouseoverTooltip
text={t`You can only watch up to ${MAX_LIMIT_WATCHED_TOKEN} tokens`}
disableTooltip={!isReachMaxLimit}
>
<StarWithAnimation
loading={false}
watched={!!assetId && !!watchlists && watchlists?.some(item => item.assetIds?.includes(+assetId))}
onClick={() => {
!isReachMaxLimit && setOpenMenu(prev => !prev)
{trigger ? (
<div
onClick={e => {
e.stopPropagation()
setOpenMenu(v => !v)
}}
wrapperStyle={wrapperStyle}
size={size}
/>
</MouseoverTooltip>
>
{trigger}
</div>
) : (
btnStar
)}
</Popover>
<Modal isOpen={openManageModal} width="380px">
<ModalWrapper onClick={e => e.stopPropagation()}>
Expand Down
4 changes: 3 additions & 1 deletion src/pages/TrueSightV2/components/WatchlistStar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ export const StarWithAnimation = ({
size,
disabled,
wrapperStyle,
stopPropagation,
}: {
watched: boolean
loading?: boolean
onClick?: (e: any) => void
size?: number
disabled?: boolean
wrapperStyle?: CSSProperties
stopPropagation?: boolean
}) => {
const theme = useTheme()
const variants = {
Expand All @@ -37,7 +39,7 @@ export const StarWithAnimation = ({
transition={{ scale: { type: 'spring', damping: 10, stiffness: 800, restDelta: 0.2 } }}
style={{ ...wrapperStyle, display: 'flex', alignItems: 'center', justifyContent: 'center', cursor: 'pointer' }}
onClick={e => {
e.stopPropagation()
stopPropagation && e.stopPropagation()
if (!disabled) {
onClick?.(e)
}
Expand Down

0 comments on commit 6cc6bd6

Please sign in to comment.