Skip to content

Commit

Permalink
Refactor usage of enabled layers for network
Browse files Browse the repository at this point in the history
  • Loading branch information
buberdds committed Feb 19, 2024
1 parent 55ce404 commit 46f82e7
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/app/components/LayerPicker/LayerMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export const LayerMenu: FC<LayerMenuProps> = ({
.filter(layer => !isLayerHidden(layer))
.map(layer => ({
layer,
enabled: RouteUtils.getEnabledLayersForNetwork(selectedNetwork || network).includes(layer),
enabled: RouteUtils.getAllLayersForNetwork(selectedNetwork || network).enabled.includes(layer),
}))
.sort(orderByLayer)

Expand Down
2 changes: 1 addition & 1 deletion src/app/components/LayerPicker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ const LayerPickerContent: FC<LayerPickerContentProps> = ({ isOutOfDate, onClose,
const [selectedNetwork, setSelectedNetwork] = useState<Network>(network)
const [tabletStep, setTabletStep] = useState<LayerPickerTabletStep>(LayerPickerTabletStep.ParaTimeDetails)
const selectNetwork = (newNetwork: Network) => {
const enabledLayers = RouteUtils.getEnabledLayersForNetwork(newNetwork)
const enabledLayers = RouteUtils.getAllLayersForNetwork(newNetwork).enabled
const targetLayer = enabledLayers.includes(selectedLayer) ? selectedLayer : enabledLayers[0]
setSelectedNetwork(newNetwork)
setSelectedLayer(targetLayer)
Expand Down
21 changes: 14 additions & 7 deletions src/app/pages/ConsensusDashboardPage/ParaTimesCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import Card from '@mui/material/Card'
import CardHeader from '@mui/material/CardHeader'
import CardContent from '@mui/material/CardContent'
import { styled } from '@mui/material/styles'
import { Layer, Runtime } from '../../../oasis-nexus/api'
import { isNotOnHiddenLayer } from '../../../types/layers'
import { COLORS } from '../../../styles/theme/colors'
import { CardHeaderWithCounter } from '../../components/CardHeaderWithCounter'
import { RouteUtils } from '../../utils/route-utils'
Expand Down Expand Up @@ -33,14 +35,19 @@ const StyledBox = styled(Box)(({ theme }) => ({
},
}))

function shouldIncludeLayer(layer: Layer) {
return layer !== Layer.consensus && isNotOnHiddenLayer({ layer })
}

type ParaTimesCardProps = { scope: SearchScope }

export const ParaTimesCard: FC<ParaTimesCardProps> = ({ scope }) => {
const { t } = useTranslation()
const { network } = scope
const enabledRuntimes = RouteUtils.getEnabledRuntimesForNetwork(network)
const disabledRuntimes = RouteUtils.getDisabledRuntimesForNetwork(network)
const runtimesNumber = enabledRuntimes.length + disabledRuntimes.length
const { enabled, disabled } = RouteUtils.getAllLayersForNetwork(network)
const enabledOasisRuntimes = enabled.filter(shouldIncludeLayer) as Runtime[]
const disabledOasisRuntimes = disabled.filter(shouldIncludeLayer) as Runtime[]
const runtimesNumber = enabledOasisRuntimes.length + disabledOasisRuntimes.length

return (
<Card>
Expand All @@ -57,12 +64,12 @@ export const ParaTimesCard: FC<ParaTimesCardProps> = ({ scope }) => {
/>
<CardContent>
<StyledBox>
{!!enabledRuntimes.length &&
enabledRuntimes.map(runtime => (
{!!enabledOasisRuntimes.length &&
enabledOasisRuntimes.map(runtime => (
<EnabledRuntimePreview key={runtime} network={scope.network} runtime={runtime} />
))}
{!!disabledRuntimes.length &&
disabledRuntimes.map(runtime => (
{!!disabledOasisRuntimes.length &&
disabledOasisRuntimes.map(runtime => (
<InactiveRuntimePreview key={runtime} network={scope.network} runtime={runtime} />
))}
</StyledBox>
Expand Down
6 changes: 3 additions & 3 deletions src/app/pages/HomePage/Graph/Graph/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ const GraphCmp: ForwardRefRenderFunction<SVGSVGElement, GraphProps> = (
}, [network])

const isLayerDisabled = (Layer: Layer) => {
return !RouteUtils.getEnabledLayersForNetwork(network).includes(Layer)
return !RouteUtils.getAllLayersForNetwork(network).enabled.includes(Layer)
}

const disabledMap: Partial<Record<Layer, boolean>> = {
Expand All @@ -300,7 +300,7 @@ const GraphCmp: ForwardRefRenderFunction<SVGSVGElement, GraphProps> = (
[Layer.sapphire]: isLayerDisabled(Layer.sapphire),
}

const enabledLayers: Layer[] = useMemo(() => RouteUtils.getEnabledLayersForNetwork(network), [network])
const enabledLayers: Layer[] = useMemo(() => RouteUtils.getAllLayersForNetwork(network).enabled, [network])

const onSelectLayer = (layer: Layer) => {
if (isMobile && isZoomedIn) {
Expand All @@ -312,7 +312,7 @@ const GraphCmp: ForwardRefRenderFunction<SVGSVGElement, GraphProps> = (

if (
((!isMobile && !isZoomedIn) || layer === selectedLayer) &&
RouteUtils.getEnabledLayersForNetwork(network).includes(layer)
RouteUtils.getAllLayersForNetwork(network).enabled.includes(layer)
) {
navigate(RouteUtils.getDashboardRoute({ network, layer }))

Expand Down
8 changes: 4 additions & 4 deletions src/app/pages/HomePage/Graph/GraphTooltipMobile/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ const layerTooltipBodyCaption = (t: TFunction, layer: Layer, enabled: boolean, o
}

const useLayerTooltipMap = (network: Network): Partial<Record<Layer, TooltipInfo>> => {
const isSapphireEnabled = RouteUtils.getEnabledLayersForNetwork(network).includes(Layer.sapphire)
const isEmeraldEnabled = RouteUtils.getEnabledLayersForNetwork(network).includes(Layer.emerald)
const isCipherEnabled = RouteUtils.getEnabledLayersForNetwork(network).includes(Layer.cipher)
const isConsensusEnabled = RouteUtils.getEnabledLayersForNetwork(network).includes(Layer.consensus)
const isSapphireEnabled = RouteUtils.getAllLayersForNetwork(network).enabled.includes(Layer.sapphire)
const isEmeraldEnabled = RouteUtils.getAllLayersForNetwork(network).enabled.includes(Layer.emerald)
const isCipherEnabled = RouteUtils.getAllLayersForNetwork(network).enabled.includes(Layer.cipher)
const isConsensusEnabled = RouteUtils.getAllLayersForNetwork(network).enabled.includes(Layer.consensus)

const isEmeraldOutOfDate = useRuntimeFreshness({ network, layer: Layer.emerald }).outOfDate
const isSapphireOutOfDate = useRuntimeFreshness({ network, layer: Layer.sapphire }).outOfDate
Expand Down
2 changes: 1 addition & 1 deletion src/app/pages/SearchResultsPage/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export function useNetworkProposalsConditionally(
nameFragment: string | undefined,
): ConditionalResults<Proposal> {
const queries = RouteUtils.getEnabledNetworks()
.filter(network => RouteUtils.getEnabledLayersForNetwork(network).includes(Layer.consensus))
.filter(network => RouteUtils.getAllLayersForNetwork(network).enabled.includes(Layer.consensus))
.map(network =>
// eslint-disable-next-line react-hooks/rules-of-hooks
useGetConsensusProposalsByName(network, nameFragment),
Expand Down
32 changes: 14 additions & 18 deletions src/app/utils/route-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { LoaderFunctionArgs } from 'react-router-dom'
import { isValidProposalId, isValidTxHash } from './helpers'
import { isValidBlockHeight, isValidOasisAddress, isValidEthAddress } from './helpers'
import { AppError, AppErrors } from '../../types/errors'
import { EvmTokenType, Layer, Runtime } from '../../oasis-nexus/api'
import { EvmTokenType, Layer } from '../../oasis-nexus/api'
import { Network } from '../../types/network'
import { SearchScope } from '../../types/searchScope'
import { isStableDeploy } from '../../config'
Expand Down Expand Up @@ -126,22 +126,18 @@ export abstract class RouteUtils {
contractAddress,
)}/instance/${encodeURIComponent(instanceId)}`

static getEnabledLayersForNetwork(network: Network): Layer[] {
return Object.values(Layer).filter(layer => RouteUtils.ENABLED_LAYERS_FOR_NETWORK[network][layer])
}
static getAllLayersForNetwork(network: Network): { enabled: Layer[]; disabled: Layer[] } {
const enabled: Layer[] = []
const disabled: Layer[] = []

static getRuntimesForNetwork(network: Network, isEnabled: boolean): Runtime[] {
return Object.values(Runtime).filter(
runtime => RouteUtils.ENABLED_LAYERS_FOR_NETWORK[network][runtime] === isEnabled,
Object.values(Layer).forEach(layer =>
RouteUtils.ENABLED_LAYERS_FOR_NETWORK[network][layer] ? enabled.push(layer) : disabled.push(layer),
)
}

static getEnabledRuntimesForNetwork(network: Network): Runtime[] {
return RouteUtils.getRuntimesForNetwork(network, true)
}

static getDisabledRuntimesForNetwork(network: Network): Runtime[] {
return RouteUtils.getRuntimesForNetwork(network, false)
return {
enabled,
disabled,
}
}

static getProposalsRoute = (network: Network) => {
Expand All @@ -154,19 +150,19 @@ export abstract class RouteUtils {

static getEnabledScopes(): SearchScope[] {
return RouteUtils.getEnabledNetworks().flatMap(network =>
RouteUtils.getEnabledLayersForNetwork(network).map(layer => ({ network, layer })),
RouteUtils.getAllLayersForNetwork(network).enabled.map(layer => ({ network, layer })),
)
}

static getEnabledNetworks(): Network[] {
return Object.values(Network).filter(network => {
return RouteUtils.getEnabledLayersForNetwork(network).length > 0
return RouteUtils.getAllLayersForNetwork(network).enabled.length > 0
})
}

static getEnabledSearchScopes(): SearchScope[] {
return RouteUtils.getEnabledNetworks().flatMap(network =>
RouteUtils.getEnabledLayersForNetwork(network).map(layer => ({ network, layer })),
RouteUtils.getAllLayersForNetwork(network).enabled.map(layer => ({ network, layer })),
)
}
}
Expand Down Expand Up @@ -261,7 +257,7 @@ export const assertEnabledScope = ({

if (
!layer || // missing param
!RouteUtils.getEnabledLayersForNetwork(network as Network).includes(layer as Layer) // unsupported on network
!RouteUtils.getAllLayersForNetwork(network as Network).enabled.includes(layer as Layer) // unsupported on network
) {
throw new AppError(AppErrors.UnsupportedLayer)
}
Expand Down

0 comments on commit 46f82e7

Please sign in to comment.