Skip to content

Commit

Permalink
Simplify code, eliminate the GlobalNetwork constant
Browse files Browse the repository at this point in the history
  • Loading branch information
csillag committed May 24, 2023
1 parent 14a3b47 commit dc31473
Show file tree
Hide file tree
Showing 19 changed files with 130 additions and 156 deletions.
13 changes: 2 additions & 11 deletions .storybook/preview.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import React from 'react'
import { Preview } from '@storybook/react'
import { defaultTheme } from '../src/styles/theme'
import { ThemeProvider } from '@mui/material/styles'
import CssBaseline from '@mui/material/CssBaseline'
import { INITIAL_VIEWPORTS } from '@storybook/addon-viewport'
import '../src/locales/i18n'
import { withDefaultTheme } from '../src/app/components/NetworkThemeBubble'

const preview: Preview = {
decorators: [
Story => (
<ThemeProvider theme={defaultTheme}>
<CssBaseline />
<Story />
</ThemeProvider>
),
],
decorators: [Story => withDefaultTheme(<Story />)],
parameters: {
controls: {
matchers: {
Expand Down
10 changes: 6 additions & 4 deletions src/app/components/AppendMobileSearch/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { styled, useTheme } from '@mui/material/styles'
import Box from '@mui/material/Box'
import { Search } from '../Search'
import useMediaQuery from '@mui/material/useMediaQuery'
import { NetworkOrGlobal } from '../../../types/network'
import { Network } from '../../../types/network'

interface AppendMobileSearchProps {
action?: ReactNode
Expand Down Expand Up @@ -33,9 +33,11 @@ const SearchWrapper = styled(Box)(() => ({
marginLeft: 'auto',
}))

export const AppendMobileSearch: FC<
PropsWithChildren<AppendMobileSearchProps> & { network: NetworkOrGlobal }
> = ({ network, children, action }) => {
export const AppendMobileSearch: FC<PropsWithChildren<AppendMobileSearchProps> & { network?: Network }> = ({
network,
children,
action,
}) => {
const theme = useTheme()
const isMobile = useMediaQuery(theme.breakpoints.down('sm'))

Expand Down
18 changes: 14 additions & 4 deletions src/app/components/NetworkThemeBubble/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import React, { FC } from 'react'
import { NetworkOrGlobal } from '../../../types/network'
import React, { FC, ReactNode } from 'react'
import { Network } from '../../../types/network'
import { ThemeProvider } from '@mui/material/styles'
import { getThemesForNetworks } from '../../../styles/theme'
import CssBaseline from '@mui/material/CssBaseline'

export const NetworkThemeBubble: FC<{ network: NetworkOrGlobal; children: React.ReactNode }> = ({
export const NetworkThemeBubble: FC<{ network: Network; children: React.ReactNode }> = ({
network,
children,
}) => <ThemeProvider theme={getThemesForNetworks()[network]}>{children}</ThemeProvider>
}) => (
<ThemeProvider theme={getThemesForNetworks()[network]}>
<CssBaseline />
{children}
</ThemeProvider>
)

export const withDefaultTheme = (node: ReactNode) => (
<NetworkThemeBubble network={Network.mainnet} children={node} />

Check failure on line 18 in src/app/components/NetworkThemeBubble/index.tsx

View workflow job for this annotation

GitHub Actions / lint

Do not pass children as props. Instead, nest children between the opening and closing tags
)
4 changes: 2 additions & 2 deletions src/app/components/PageLayout/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import useMediaQuery from '@mui/material/useMediaQuery'
import { styled, useTheme } from '@mui/material/styles'
import { useConstant } from '../../hooks/useConstant'
import { AppendMobileSearch } from '../AppendMobileSearch'
import { NetworkOrGlobal } from '../../../types/network'
import { Network } from '../../../types/network'

const FooterBox = styled(Box)(({ theme }) => ({
display: 'flex',
Expand All @@ -20,7 +20,7 @@ const FooterBox = styled(Box)(({ theme }) => ({
}))

interface FooterProps {
network: NetworkOrGlobal
network?: Network
mobileSearchAction?: ReactNode
}

Expand Down
4 changes: 2 additions & 2 deletions src/app/components/Search/SearchSuggestionsLinks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { Link as RouterLink } from 'react-router-dom'
import Link from '@mui/material/Link'
import { RouteUtils } from '../../utils/route-utils'
import { OptionalBreak } from '../OptionalBreak'
import { NetworkOrGlobal } from '../../../types/network'
import { Network } from '../../../types/network'

interface Props {
network: NetworkOrGlobal
network?: Network
}

export const SearchSuggestionsLinks: FC<Props> = ({ network }) => {
Expand Down
4 changes: 2 additions & 2 deletions src/app/components/Search/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import useMediaQuery from '@mui/material/useMediaQuery'
import HighlightOffIcon from '@mui/icons-material/HighlightOff'
import IconButton from '@mui/material/IconButton'
import { SearchSuggestionsButtons } from './SearchSuggestionsButtons'
import { NetworkOrGlobal } from '../../../types/network'
import { Network } from '../../../types/network'

export type SearchVariant = 'button' | 'icon' | 'expandable'

Expand Down Expand Up @@ -84,7 +84,7 @@ SearchButton.defaultProps = {
}

export interface SearchProps {
network: NetworkOrGlobal
network?: Network
variant: SearchVariant
disabled?: boolean
onFocusChange?: (hasFocus: boolean) => void
Expand Down
7 changes: 2 additions & 5 deletions src/app/components/Search/search-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
isValidEthAddress,
getEvmBech32Address,
} from '../../utils/helpers'
import { GlobalNetwork, Network } from '../../../types/network'
import { Network } from '../../../types/network'
import { RouteUtils } from '../../utils/route-utils'
import { AppError, AppErrors } from '../../../types/errors'

Expand Down Expand Up @@ -65,10 +65,7 @@ export function isSearchValid(searchTerm: string) {

export const searchParamLoader = async ({ request, params }: LoaderFunctionArgs) => {
const { network } = params
if (
!network ||
(network !== GlobalNetwork && !RouteUtils.getEnabledNetworks().includes(network as Network))
) {
if (!!network && !RouteUtils.getEnabledNetworks().includes(network as Network)) {
throw new AppError(AppErrors.InvalidUrl)
}

Expand Down
9 changes: 4 additions & 5 deletions src/app/hooks/useNetworkParam.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import { useParams } from 'react-router-dom'
import { Network, NetworkOrGlobal } from '../../types/network'
import { Network } from '../../types/network'
import { RouteUtils } from '../utils/route-utils'
import { AppError, AppErrors } from '../../types/errors'

export const useNetworkParam = (): NetworkOrGlobal => {
export const useNetworkParam = (): Network | undefined => {
const { network } = useParams()

return network as NetworkOrGlobal
return network as Network | undefined
}

/**
* Use this in situations where we can be sure that the network has already been checked
*/
export const useSafeNetworkParam = (): Network => {
const network = useNetworkParam()
if (!RouteUtils.getEnabledNetworks().includes(network as any)) {
if (!network || !RouteUtils.getEnabledNetworks().includes(network as any)) {
throw new AppError(AppErrors.UnsupportedNetwork)
}
return network as Network
Expand Down
19 changes: 2 additions & 17 deletions src/app/pages/HomePage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ import InfoOutlinedIcon from '@mui/icons-material/InfoOutlined'
import { useTranslation } from 'react-i18next'
import { ParaTimeSelectorStep } from './Graph/types'
import { BuildPreviewBanner } from '../../components/BuildPreviewBanner'
import { GlobalNetwork } from '../../../types/network'
import { defaultTheme } from '../../../styles/theme'
import CssBaseline from '@mui/material/CssBaseline'

export const zIndexHomePage = {
paraTimeSelector: 1,
Expand Down Expand Up @@ -135,12 +132,7 @@ export const HomePage: FC = () => {
</LogotypeBox>
<SearchInputContainer>
<SearchInputBox>
<Search
network={GlobalNetwork}
disabled={isApiOffline}
variant={searchVariant}
onFocusChange={onFocusChange}
/>
<Search disabled={isApiOffline} variant={searchVariant} onFocusChange={onFocusChange} />
</SearchInputBox>
{isApiOffline && (
<Box sx={{ display: 'flex', justifyContent: 'center' }}>
Expand All @@ -159,16 +151,9 @@ export const HomePage: FC = () => {
<InfoOutlinedIcon fontSize="medium" sx={{ color: 'white' }} />
</IconButton>
)}
{!isMobile && <Footer network={GlobalNetwork} />}
{!isMobile && <Footer />}
</FooterStyled>
</HomepageLayout>
</>
)
}

export const ThemedHomePage: FC = () => (
<ThemeProvider theme={defaultTheme}>
<CssBaseline />
<HomePage />
</ThemeProvider>
)
11 changes: 5 additions & 6 deletions src/app/pages/SearchResultsPage/NoResults.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ import Link from '@mui/material/Link'
import { SearchSuggestionsLinks } from '../../components/Search/SearchSuggestionsLinks'
import { OptionalBreak } from '../../components/OptionalBreak'
import { useTheme } from '@mui/material/styles'
import { getNetworkNames, GlobalNetwork, NetworkOrGlobal } from '../../../types/network'
import { getNetworkNames, Network } from '../../../types/network'

export const NoResults: FC<{ network: NetworkOrGlobal }> = ({ network }) => {
export const NoResults: FC<{ network?: Network }> = ({ network }) => {
const { t } = useTranslation()
const theme = useTheme()
const title =
network === GlobalNetwork
? t('search.noResults.header')
: t('search.noResults.networkHeader', { network: getNetworkNames(t)[network] })
const title = !network
? t('search.noResults.header')
: t('search.noResults.networkHeader', { network: getNetworkNames(t)[network] })

return (
<EmptyState
Expand Down
75 changes: 35 additions & 40 deletions src/app/pages/SearchResultsPage/ResultsOnForeignNetwork.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Trans, useTranslation } from 'react-i18next'
import { styled, useTheme } from '@mui/material/styles'
import { getThemesForNetworks } from '../../../styles/theme'
import useMediaQuery from '@mui/material/useMediaQuery'
import { NetworkThemeBubble } from '../../components/NetworkThemeBubble'
import Box from '@mui/material/Box'
import { SearchResultsList } from './SearchResultsList'
import { SearchQueries } from './hooks'
Expand Down Expand Up @@ -55,50 +54,46 @@ export const ResultsOnForeignNetwork: FC<{

if (!open) {
return (
<NetworkThemeBubble network={network}>
<NotificationBox className={'foreignNote'} onClick={() => setOpen(true)}>
<ZoomIn />
{/*{countLabel}*/}
<Trans
t={t}
i18nKey={
hasLocalMatches ? 'search.otherResults.clickToShowMore' : 'search.otherResults.clickToShowThem'
}
values={{
countLabel: t(hasLocalMatches ? 'search.results.moreCount' : 'search.results.count', {
count: matches,
}),
networkName,
}}
/>
</NotificationBox>
</NetworkThemeBubble>
<NotificationBox theme={otherTheme} className={'foreignNote'} onClick={() => setOpen(true)}>
<ZoomIn />
{/*{countLabel}*/}
<Trans
t={t}
i18nKey={
hasLocalMatches ? 'search.otherResults.clickToShowMore' : 'search.otherResults.clickToShowThem'
}
values={{
countLabel: t(hasLocalMatches ? 'search.results.moreCount' : 'search.results.count', {
count: matches,
}),
networkName,
}}
/>
</NotificationBox>
)
}
return (
<NetworkThemeBubble network={network}>
<Box
<Box
sx={{
marginTop: 50,
pt: 4,
px: isMobile ? 0 : '4%',
border: isMobile ? 'none' : `solid 15px ${otherTheme.palette.layout.border}`,
background: otherTheme.palette.background.default,
}}
>
<NotificationBox
sx={{
marginTop: 50,
pt: 4,
px: isMobile ? 0 : '4%',
border: isMobile ? 'none' : `solid 15px ${otherTheme.palette.layout.border}`,
background: otherTheme.palette.background.default,
background: COLORS.white, // TODO should come from theme
color: COLORS.grayDark, // TODO should come from theme
border: `2px solid ${otherTheme.palette.layout.border}`,
}}
onClick={() => setOpen(false)}
>
<NotificationBox
sx={{
background: COLORS.white, // TODO should come from theme
color: COLORS.grayDark, // TODO should come from theme
border: `2px solid ${otherTheme.palette.layout.border}`,
}}
onClick={() => setOpen(false)}
>
<Warning />
{t('search.otherResults.clickToHide', { networkName })}
</NotificationBox>
<SearchResultsList network={network} searchQueries={searchQueries} roseFiatValue={roseFiatValue} />
</Box>
</NetworkThemeBubble>
<Warning />
{t('search.otherResults.clickToHide', { networkName })}
</NotificationBox>
<SearchResultsList network={network} searchQueries={searchQueries} roseFiatValue={roseFiatValue} />
</Box>
)
}
26 changes: 13 additions & 13 deletions src/app/pages/SearchResultsPage/ResultsOnNetwork.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ export const ResultsOnNetwork: FC<{
{network === Network.mainnet ? (
content
) : (
<NetworkThemeBubble network={network}>
<Box
sx={{
marginTop: 50,
pt: 4,
px: isMobile ? 0 : '4%',
border: isMobile ? 'none' : `solid 15px ${otherTheme.palette.layout.border}`,
background: otherTheme.palette.background.default,
}}
>
{content}
</Box>
</NetworkThemeBubble>
// <NetworkThemeBubble network={network}>
<Box
sx={{
marginTop: 50,
pt: 4,
px: isMobile ? 0 : '4%',
border: isMobile ? 'none' : `solid 15px ${otherTheme.palette.layout.border}`,
background: otherTheme.palette.background.default,
}}
>
{content}
</Box>
// </NetworkThemeBubble>
)}
</>
)
Expand Down
6 changes: 3 additions & 3 deletions src/app/pages/SearchResultsPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { TextSkeleton } from '../../components/Skeleton'
import { useRedirectIfSingleResult } from './useRedirectIfSingleResult'
import { NoResults } from './NoResults'
import { RouteUtils } from '../../utils/route-utils'
import { GlobalNetwork, Network, NetworkOrGlobal } from '../../../types/network'
import { Network } from '../../../types/network'
import { useNetworkParam } from '../../hooks/useNetworkParam'
import { SearchResultsList } from './SearchResultsList'
import {
Expand Down Expand Up @@ -46,7 +46,7 @@ export const SearchResultsPage: FC = () => {
}

export const SearchResultsView: FC<{
wantedNetwork: NetworkOrGlobal
wantedNetwork?: Network
searchQueries: SearchQueries
roseFiatValue: number | undefined
}> = ({ wantedNetwork, searchQueries, roseFiatValue }) => {
Expand All @@ -64,7 +64,7 @@ export const SearchResultsView: FC<{

const matchingNetworkList = allNetworks.filter(net => !!matchesInNetworks[net])

const isGlobal = wantedNetwork === GlobalNetwork
const isGlobal = !wantedNetwork

const hasNoResultsWhatsoever = allResults.length === 0
const hasNoResultsOnWantedNetwork = !isGlobal && matchesInNetworks[wantedNetwork] === 0
Expand Down

0 comments on commit dc31473

Please sign in to comment.