Skip to content

Commit

Permalink
refactor: remove duplicate code
Browse files Browse the repository at this point in the history
Removed duplication of formatting functions, which should
be part of useNetworkStore(), as they contain `currentNetwork`
(state of the app).

As a quickfix getEpochFromTimestamp() and getTimestampFromEpoch()
were `proped down` to DashboardChartTooltipHeader.vue
which is an `antipattern` and should be removed.

Reason: Due to the fact that DashboardChartTooltipHeader.vue was
used inside a `vue render()` in DashboardChartSummary.vue,
we do not have access to `nuxt hooks` so the
usage of useRuntimeConfig() in useNetworkStore() created an error.
  • Loading branch information
marcel-bitfly committed Jan 9, 2025
1 parent 1408b4d commit 7ebd9b7
Show file tree
Hide file tree
Showing 19 changed files with 129 additions and 364 deletions.
3 changes: 2 additions & 1 deletion frontend/components/bc/format/FormatTimePassed.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import type { AgeFormat } from '~/types/settings'
import { formatGoTimestamp } from '~/utils/format'
const {
formatEpochToDateTime, formatSlotToDateTime,
formatEpochToDateTime,
formatSlotToDateTime,
} = useFormat()
interface Props {
Expand Down
6 changes: 4 additions & 2 deletions frontend/components/bc/header/MainHeader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ defineProps<{
}>()
const { latestState } = useLatestStateStore()
const {
currentNetwork, networkInfo, slotToEpoch,
currentNetwork,
getEpochFromSlot,
networkInfo,
} = useNetworkStore()
const {
doLogout, isLoggedIn,
Expand Down Expand Up @@ -54,7 +56,7 @@ const rate = computed(() => {
const currentEpoch = computed(() =>
latestState.value?.current_slot !== undefined
? slotToEpoch(latestState.value.current_slot)
? getEpochFromSlot(latestState.value.current_slot)
: undefined,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,17 @@ const data = computed<Series[]>(() => {
total,
]
})
const {
getEpochFromTimestamp,
getTimestampFromEpoch,
} = useNetworkStore()
</script>

<template>
<div class="tooltip-container">
<DashboardChartTooltipHeader
:get-epoch-from-timestamp
:get-timestamp-from-epoch
:t
:start-epoch
/>
Expand Down
17 changes: 11 additions & 6 deletions frontend/components/dashboard/chart/DashboardChartSummary.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
TooltipComponent,
} from 'echarts/components'
import VChart from 'vue-echarts'
import SummaryChartTooltip from './DashboardChartSummaryTooltip.vue'
import {
getChartTextColor,
getChartTooltipBackgroundColor,
Expand All @@ -35,6 +34,7 @@ import {
SUMMARY_CHART_GROUP_TOTAL,
type SummaryChartFilter,
} from '~/types/dashboard/summary'
import { DashboardChartSummaryTooltip } from '#components'
use([
CanvasRenderer,
Expand All @@ -56,7 +56,10 @@ const { t: $t } = useTranslation()
const colorMode = useColorMode()
const { fetch } = useCustomFetch()
const {
secondsPerEpoch, slotToTs, tsToEpoch,
getEpochFromTimestamp,
getTimestampFromEpoch,
getTimestampFromSlot,
secondsPerEpoch,
} = useNetworkStore()
const { dashboardKey } = useDashboardKey()
const { overview } = useValidatorDashboardOverviewStore()
Expand Down Expand Up @@ -104,7 +107,7 @@ const categories = computed<number[]>(() => {
return []
}
const list: number[] = []
let latestTs = slotToTs(latestSlot.value - 7) || 0
let latestTs = getTimestampFromSlot(latestSlot.value - 7) || 0
let step = 0
switch (aggregation.value) {
case 'daily':
Expand All @@ -123,7 +126,7 @@ const categories = computed<number[]>(() => {
if (!step) {
return []
}
const minTs = Math.max(slotToTs(0) || 0, latestTs - maxSeconds)
const minTs = Math.max(getTimestampFromSlot(0) || 0, latestTs - maxSeconds)
while (latestTs > minTs) {
list.splice(0, 0, latestTs)
Expand Down Expand Up @@ -263,7 +266,7 @@ const formatTSToDate = (value: string) => {
)
}
const formatTSToEpoch = (value: string) => {
return `${$t('common.epoch')} ${tsToEpoch(Number(value))}`
return `${$t('common.epoch')} ${getEpochFromTimestamp(Number(value))}`
}
const formatToDateOrEpoch = (value: string) => {
if (aggregation.value === 'epoch') {
Expand Down Expand Up @@ -356,9 +359,11 @@ const option = computed<EChartsOption>(() => {
})
const d = document.createElement('div')
render(
h(SummaryChartTooltip, {
h(DashboardChartSummaryTooltip, {
aggregation: aggregation.value,
efficiencyType: props.filter?.efficiency || 'all',
getEpochFromTimestamp,
getTimestampFromEpoch,
groupInfos,
highlightGroup,
t: $t,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import type {
interface Props {
aggregation: AggregationTimeframe,
efficiencyType: EfficiencyType,
getEpochFromTimestamp: (timestamp: number) => number,
getTimestampFromEpoch: (epoch: number) => number,
groupInfos: {
color: string,
efficiency: number,
Expand All @@ -25,7 +27,9 @@ defineProps<Props>()
<div class="tooltip-container">
<DashboardChartTooltipHeader
:t
:ts
:timestamp="ts"
:get-epoch-from-timestamp
:get-timestamp-from-epoch
:aggregation
:efficiency-type
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<script lang="ts" setup>
import type { ComposerTranslation } from 'vue-i18n'
import { useNetworkStore } from '~/stores/useNetworkStore'
import type {
AggregationTimeframe,
EfficiencyType,
Expand All @@ -12,23 +11,21 @@ import {
interface Props {
aggregation?: AggregationTimeframe,
efficiencyType?: EfficiencyType,
getEpochFromTimestamp: (timestamp: number) => number,
getTimestampFromEpoch: (epoch: number) => number,
startEpoch?: number,
t: ComposerTranslation, // required as dynamically created components via render do not have the proper app context,
ts?: number,
}
const props = defineProps<Props>()
const {
epochToTs, tsToEpoch,
} = useNetworkStore()
const startTs = computed(() => {
if (props.ts) {
return props.ts
}
if (props.startEpoch) {
return epochToTs(props.startEpoch)
return props.getTimestampFromEpoch(props.startEpoch)
}
return undefined
})
Expand Down Expand Up @@ -81,11 +78,11 @@ const epochText = computed(() => {
if (!startTs.value) {
return
}
const startEpoch = tsToEpoch(startTs.value)
const startEpoch = props.getEpochFromTimestamp(startTs.value)
if (!endTs.value) {
return startEpoch
}
const endEpoch = tsToEpoch(endTs.value)
const endEpoch = props.getEpochFromTimestamp(endTs.value)
return `${startEpoch} - ${endEpoch}`
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
DashboardCreationDisplayMode,
DashboardCreationState,
} from '~/types/dashboard/creation'
import type { ChainIDs } from '~/types/network'
import type { ChainId } from '~/types/network'
const userDashboardStore = useUserDashboardStore()
const {
Expand All @@ -30,7 +30,7 @@ const visible = ref<boolean>(false)
const state = ref<DashboardCreationState>('')
const type = ref<'' | DashboardType>('')
const name = ref<string>('')
const network = ref<ChainIDs>(0)
const network = ref<ChainId>(0)
const forcedDashboardType = ref<'' | DashboardType>('')
const {
dashboardKey, publicEntities,
Expand Down
10 changes: 5 additions & 5 deletions frontend/components/dashboard/table/DashboardTableClDeposits.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const cursor = ref<Cursor>()
const pageSize = ref<number>(5)
const { t: $t } = useTranslation()
const { slotToEpoch } = useNetworkStore()
const { getEpochFromSlot } = useNetworkStore()
const {
deposits,
Expand Down Expand Up @@ -190,11 +190,11 @@ const isRowExpandable = (row: VDBConsensusDepositsTableRow) => {
<template #body="slotProps">
<BcLink
v-if="slotProps.data.index !== undefined"
:to="`/epoch/${slotToEpoch(slotProps.data.slot)}`"
:to="`/epoch/${getEpochFromSlot(slotProps.data.slot)}`"
target="_blank"
class="link"
>
<BcFormatNumber :value="slotToEpoch(slotProps.data.slot)" />
<BcFormatNumber :value="getEpochFromSlot(slotProps.data.slot)" />
</BcLink>
</template>
</Column>
Expand Down Expand Up @@ -303,11 +303,11 @@ const isRowExpandable = (row: VDBConsensusDepositsTableRow) => {
</div>
<BcLink
v-if="slotProps.data.index !== undefined"
:to="`/epoch/${slotToEpoch(slotProps.data.slot)}`"
:to="`/epoch/${getEpochFromSlot(slotProps.data.slot)}`"
target="_blank"
class="link"
>
<BcFormatNumber :value="slotToEpoch(slotProps.data.slot)" />
<BcFormatNumber :value="getEpochFromSlot(slotProps.data.slot)" />
</BcLink>
</div>
<div class="row">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const pageSize = ref<number>(10)
const { t: $t } = useTranslation()
const { latestState } = useLatestStateStore()
const { slotToEpoch } = useNetworkStore()
const { getEpochFromSlot } = useNetworkStore()
const {
getTotalAmount,
getWithdrawals,
Expand Down Expand Up @@ -151,7 +151,7 @@ const isRowExpandable = (row: ExtendedVDBWithdrawalsTableRow) => {
const isRowInFuture = (row: ExtendedVDBWithdrawalsTableRow) => {
if (latestState?.value) {
return row.epoch > slotToEpoch(latestState.value.current_slot)
return row.epoch > getEpochFromSlot(latestState.value.current_slot)
}
return false
Expand Down
4 changes: 2 additions & 2 deletions frontend/components/icon/IconNetwork.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup lang="ts">
import {
ChainFamily, type ChainIDs, ChainInfo,
ChainFamily, type ChainId, ChainInfo,
} from '~/types/network'
const colorMode = useColorMode()
Expand Down Expand Up @@ -35,7 +35,7 @@ const props = defineProps({
const family = computed(() =>
props.chainId in ChainInfo
? ChainInfo[props.chainId as ChainIDs].family
? ChainInfo[props.chainId as ChainId].family
: ChainFamily.Ethereum,
)
const coloring = computed(() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import IconValidator from '../icon/IconValidator.vue'
import IconAccount from '../icon/IconAccount.vue'
import type { Cursor } from '~/types/datatable'
import type { DashboardType } from '~/types/dashboard'
import type { ChainIDs } from '~/types/network'
import type { ChainId } from '~/types/network'
import type { NotificationDashboardsTableRow } from '~/types/api/notifications'
import { NotificationsDashboardDialogEntity } from '#components'
Expand All @@ -17,7 +17,7 @@ const { t: $t } = useTranslation()
// TODO: replace currentNetwork with selection from NETWORK_SWITCHER_COMPONENT that has yet to be implemented
const { currentNetwork } = useNetworkStore()
const networkId = ref<ChainIDs>(currentNetwork.value ?? 1)
const networkId = ref<ChainId>(currentNetwork.value ?? 1)
const {
isLoading,
Expand Down
36 changes: 0 additions & 36 deletions frontend/components/playground/PlaygroundIcons.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
<script setup lang="ts">
import {
type ChainIDs, getAllExistingChainIDs,
} from '~/types/network'
</script>

<template>
Expand Down Expand Up @@ -89,39 +86,6 @@ import {
</div>
</div>

<div>
<h2>Network icons</h2>
<div>Monochromous:</div>
<IconNetwork
v-for="id of getAllExistingChainIDs(false)"
:key="id"
:chain-id="id as ChainIDs"
:colored="false"
:harmonize-perceived-size="false"
class="icon-size"
/>
<div>Colored:</div>
<IconNetwork
v-for="id of getAllExistingChainIDs(false)"
:key="id"
:chain-id="id as ChainIDs"
:colored="true"
:harmonize-perceived-size="false"
class="icon-size"
/>
<div>
Harmonized sizes (for example Ethereum looks less skinny compared to the
others):
</div>
<IconNetwork
v-for="id of getAllExistingChainIDs(false)"
:key="id"
:chain-id="id as ChainIDs"
:colored="true"
:harmonize-perceived-size="true"
class="icon-size"
/>
</div>
<div>
<h2>Loading issues:</h2>
Example when props `inline` is true: "Something went wrong
Expand Down
5 changes: 2 additions & 3 deletions frontend/components/slot/viz/SlotVizViewer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ interface Props {
}
const props = defineProps<Props>()
const { tsToSlot } = useNetworkStore()
const { getSlotFromTimestamp } = useNetworkStore()
const { t: $t } = useTranslation()
const selectedCategories = useCookie<SlotVizCategories[]>(
Expand Down Expand Up @@ -97,8 +97,7 @@ const currentSlotId = computed(() => {
// in case of some backend issues Inan want's us to tick in the future ... so let's tick
return Math.max(
mostRecentScheduledSlotId.value ?? 0,
tsToSlot((props.timestamp ?? 0) / 1000) - 1,
)
getSlotFromTimestamp((props.timestamp ?? 0) / 1000) - 1)
})
watch(
Expand Down
Loading

0 comments on commit 7ebd9b7

Please sign in to comment.