Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow change network standalone widget #5306

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { useWalletInfo } from '@cowprotocol/wallet'

import ReactDOM from 'react-dom'



import { upToLarge, useMediaQuery } from 'legacy/hooks/useMediaQuery'

import { useToggleAccountModal } from 'modules/account'
Expand All @@ -21,21 +19,28 @@ import { useIsProviderNetworkUnsupported } from 'common/hooks/useIsProviderNetwo

import { BalanceText, Wrapper } from './styled'

import { NetworkSelector } from '../NetworkSelector'

interface AccountElementProps {
pendingActivities: string[]
standaloneMode?: boolean
className?: string
hideNetworkSelector?: boolean
}

export function AccountElement({ className, standaloneMode, pendingActivities }: AccountElementProps) {
export function AccountElement({
className,
standaloneMode,
pendingActivities,
hideNetworkSelector,
}: AccountElementProps) {
const { account, chainId } = useWalletInfo()
const isChainIdUnsupported = useIsProviderNetworkUnsupported()
const userEthBalance = useNativeCurrencyAmount(chainId, account)
const toggleAccountModal = useToggleAccountModal()
const nativeTokenSymbol = NATIVE_CURRENCIES[chainId].symbol
const isUpToLarge = useMediaQuery(upToLarge)


const unreadNotifications = useUnreadNotifications()
const unreadNotificationsCount = Object.keys(unreadNotifications).length

Expand All @@ -44,18 +49,25 @@ export function AccountElement({ className, standaloneMode, pendingActivities }:
return (
<>
<Wrapper className={className} active={!!account}>
{standaloneMode !== false && account && !isChainIdUnsupported && userEthBalance && chainId && !isUpToLarge && (
<BalanceText>
<TokenAmount amount={userEthBalance} tokenSymbol={{ symbol: nativeTokenSymbol }} />
</BalanceText>
)}
{!!hideNetworkSelector &&
standaloneMode !== false &&
account &&
!isChainIdUnsupported &&
userEthBalance &&
chainId &&
!isUpToLarge && (
Comment on lines +52 to +58
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These conditions are getting out of hand.
How about a variable/fn to abstract it away?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot agree more

<BalanceText>
<TokenAmount amount={userEthBalance} tokenSymbol={{ symbol: nativeTokenSymbol }} />
</BalanceText>
)}
{!hideNetworkSelector && <NetworkSelector />}
<Web3Status pendingActivities={pendingActivities} onClick={() => account && toggleAccountModal()} />
{account && (
<NotificationBell
unreadCount={unreadNotificationsCount}
onClick={() => {
clickNotifications(
unreadNotificationsCount === 0 ? 'click-bell' : 'click-bell-with-pending-notifications'
unreadNotificationsCount === 0 ? 'click-bell' : 'click-bell-with-pending-notifications',
)
setSidebarOpen(true)
}}
Expand All @@ -65,7 +77,7 @@ export function AccountElement({ className, standaloneMode, pendingActivities }:

{ReactDOM.createPortal(
<NotificationSidebar isOpen={isSidebarOpen} onClose={() => setSidebarOpen(false)} />,
document.body
document.body,
)}
</>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const scrollToMyOrders = () => {

export function TradeWidgetForm(props: TradeWidgetProps) {
const isInjectedWidgetMode = isInjectedWidget()
const { standaloneMode, hideOrdersTable } = useInjectedWidgetParams()
const { standaloneMode, hideOrdersTable, hideNetworkSelector } = useInjectedWidgetParams()
const isMobile = useMediaQuery(Media.upToSmall(false))

const isAlternativeOrderModalVisible = useIsAlternativeOrderModalVisible()
Expand Down Expand Up @@ -162,7 +162,11 @@ export function TradeWidgetForm(props: TradeWidgetProps) {
<styledEl.Header>
{isAlternativeOrderModalVisible ? <div></div> : <TradeWidgetLinks isDropdown={showDropdown} />}
{isInjectedWidgetMode && standaloneMode && (
<AccountElement standaloneMode pendingActivities={pendingActivity} />
<AccountElement
standaloneMode
pendingActivities={pendingActivity}
hideNetworkSelector={hideNetworkSelector}
/>
)}

{shouldShowMyOrdersButton && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export function useWidgetParams(configuratorState: ConfiguratorState): CowSwapWi
standaloneMode,
disableToastMessages,
disableProgressBar,
hideNetworkSelector,
hideBridgeInfo,
hideOrdersTable,
} = configuratorState
Expand Down Expand Up @@ -90,6 +91,7 @@ export function useWidgetParams(configuratorState: ConfiguratorState): CowSwapWi
standaloneMode,
disableToastMessages,
disableProgressBar,
hideNetworkSelector,

partnerFee:
partnerFeeBps > 0
Expand Down
18 changes: 18 additions & 0 deletions apps/widget-configurator/src/app/configurator/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ export function Configurator({ title }: { title: string }) {
const [disableProgressBar, setDisableProgressBar] = useState<boolean>(false)
const toggleDisableProgressBar = useCallback(() => setDisableProgressBar((curr) => !curr), [])

const [hideNetworkSelector, setHideNetworkSelector] = useState<boolean>(true)
const toggleHideNetworkSelector = useCallback(() => setHideNetworkSelector((curr) => !curr), [])

const [hideBridgeInfo, setHideBridgeInfo] = useState<boolean | undefined>(false)
const toggleHideBridgeInfo = useCallback(() => setHideBridgeInfo((curr) => !curr), [])

Expand Down Expand Up @@ -184,6 +187,7 @@ export function Configurator({ title }: { title: string }) {
standaloneMode,
disableToastMessages,
disableProgressBar,
hideNetworkSelector,
hideBridgeInfo,
hideOrdersTable,
}
Expand Down Expand Up @@ -335,6 +339,20 @@ export function Configurator({ title }: { title: string }) {
</RadioGroup>
</FormControl>

<FormControl component="fieldset">
<FormLabel component="legend">Network selector:</FormLabel>
<RadioGroup
row
aria-label="mode"
name="mode"
value={hideNetworkSelector}
onChange={toggleHideNetworkSelector}
>
<FormControlLabel value="false" control={<Radio />} label="Show network selector" />
<FormControlLabel value="true" control={<Radio />} label="Hide network selector" />
</RadioGroup>
</FormControl>

<FormControl component="fieldset">
<FormLabel component="legend">Progress bar:</FormLabel>
<RadioGroup row aria-label="mode" name="mode" value={disableProgressBar} onChange={toggleDisableProgressBar}>
Expand Down
1 change: 1 addition & 0 deletions apps/widget-configurator/src/app/configurator/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ export interface ConfiguratorState {
disableProgressBar: boolean
hideBridgeInfo: boolean | undefined
hideOrdersTable: boolean | undefined
hideNetworkSelector: boolean | undefined
}
Loading