Skip to content

Commit

Permalink
Split the TimeFrameSelector into separate components and update the r…
Browse files Browse the repository at this point in the history
…esponsive styles
  • Loading branch information
AlexeyTripleA committed Oct 21, 2024
1 parent 8681cbd commit c3bd28e
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 56 deletions.
65 changes: 24 additions & 41 deletions packages/frontend/src/app/validator/[hash]/BlocksChart.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { useState, useEffect, useRef } from 'react'
import { fetchHandlerSuccess, fetchHandlerError } from '../../../util'
import { useState, useEffect } from 'react'
import { LineChart } from './../../../components/charts'
import { LineChart, TimeframeMenu } from './../../../components/charts'
import * as Api from '../../../util/Api'
import { Button } from '@chakra-ui/react'
import { CalendarIcon } from './../../../components/ui/icons'
import { ErrorMessageBlock } from '../../../components/Errors'
import { DateRangePicker } from '../../../components/calendar'
import './TimeframeSelector.scss'
import './TimeframeMenu.scss'
import './TabsChart.scss'

const chartConfig = {
Expand All @@ -34,13 +32,14 @@ const chartConfig = {
}
}

const TimeframeSelector = ({ config, isActive, changeCallback, openStateCallback }) => {
const TimeframeSelector = ({ config, isActive, changeCallback, openStateCallback, menuRef }) => {
const [timespan, setTimespan] = useState(chartConfig.timespan.values[chartConfig.timespan.defaultIndex])
const [menuIsOpen, setMenuIsOpen] = useState(false)

const changeHandler = (value) => {
setTimespan(value)
if (typeof changeCallback === 'function') changeCallback(value)
setMenuIsOpen(false)
}

useEffect(() => {
Expand All @@ -51,43 +50,14 @@ const TimeframeSelector = ({ config, isActive, changeCallback, openStateCallback
if (typeof openStateCallback === 'function') openStateCallback(menuIsOpen)
}, [menuIsOpen])

const calendarHandler = (value) => {}

return (
<div className={`TimeframeSelector ${menuIsOpen ? 'TimeframeSelector--MenuActive' : ''}`}>
<div className={'TimeframeSelector__Menu TimeframeMenu'}>
<div className={'TimeframeMenu__ValuesContainer'}>
<div className={'TimeframeMenu__ValuesTitle'}>
Select a day, period or Timeframe:
</div>

<div className={'TimeframeMenu__Values'}>
{config.timespan.values.map((iTimespan, i) => (
<Button
className={`TimeframeMenu__ValueButton ${iTimespan.range === timespan.range ? 'TimeframeMenu__ValueButton--Active' : ''}`}
onClick={() => {
changeHandler(iTimespan)
setMenuIsOpen(false)
}}
key={i}
size={'xs'}
>
{iTimespan.label}
</Button>
))}
</div>
</div>

<div className={'TimeframeMenu__Calendar TimeframeMenu__Calendar--Stub'}>
<DateRangePicker
disableFutureDates={true}
monthsToShow={7}
noTopNavigation={true}
noWeekDay={true}
changeHandler={calendarHandler}
/>
</div>
</div>
<TimeframeMenu
ref={menuRef}
className={'TimeframeSelector__Menu'}
config={config}
changeCallback={changeHandler}
/>

<Button
className={`TimeframeSelector__Button ${menuIsOpen ? 'TimeframeSelector__Button--Active' : ''}`}
Expand All @@ -104,21 +74,34 @@ export default function BlocksChart ({ hash, isActive }) {
const [blocksHistory, setBlocksHistory] = useState({ data: {}, loading: true, error: false })
const [timespan, setTimespan] = useState(chartConfig.timespan.values[chartConfig.timespan.defaultIndex])
const [menuIsOpen, setMenuIsOpen] = useState(false)
const TimeframeMenuRef = useRef(null)
const [selectorHeight, setSelectorHeight] = useState(0)

useEffect(() => {
Api.getBlocksStatsByValidator(hash, timespan.range)
.then(res => fetchHandlerSuccess(setBlocksHistory, { resultSet: res }))
.catch(err => fetchHandlerError(setBlocksHistory, err))
}, [timespan])

useEffect(() => {
if (menuIsOpen && TimeframeMenuRef.current) {
const element = TimeframeMenuRef.current
const height = element.getBoundingClientRect().height
setSelectorHeight(height)
} else {
setSelectorHeight(0)
}
}, [menuIsOpen, TimeframeMenuRef])

if (blocksHistory.error || (!blocksHistory.loading && !blocksHistory.data?.resultSet)) {
return (<ErrorMessageBlock/>)
}

return (
<div style={{ height: menuIsOpen ? '400px' : '350px' }} className={'TabsChart'}>
<div style={{ height: menuIsOpen ? `${Math.max(selectorHeight, 350)}px` : '350px' }} className={'TabsChart'}>
{!blocksHistory.loading &&
<TimeframeSelector
menuRef={TimeframeMenuRef}
className={'TabsChart__TimeframeSelector'}
config={chartConfig}
changeCallback={setTimespan}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
}

&__Menu {
padding: 0 12px !important;
top: 12px;
position: absolute;
z-index: 10;
width: 100%;
height: 100%;
top: 0;
// height: 100%;
height: max-content;
left: 0;
backdrop-filter: $blockBackgroundBlur;
visibility: hidden;
Expand Down
19 changes: 9 additions & 10 deletions packages/frontend/src/app/validator/[hash]/Validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import Pagination from '../../../components/pagination'
import { ErrorMessageBlock } from '../../../components/Errors'
import BlocksList from '../../../components/blocks/BlocksList'
import TransactionsList from '../../../components/transactions/TransactionsList'
import TransfersList from '../../../components/transfers/TransfersList'
import BlocksChart from './BlocksChart'
import Link from 'next/link'
import { Identifier, DateBlock, Endpoint, IpAddress, InfoLine } from '../../../components/data'
Expand All @@ -31,7 +30,7 @@ function Validator ({ hash }) {
const pageSize = 13
const [currentPage, setCurrentPage] = useState(1)
const [transactions, setTransactions] = useState({ data: {}, props: { currentPage: 0 }, loading: true, error: false })
const [transfers, setTransfers] = useState({ data: {}, props: { currentPage: 0 }, loading: true, error: false })
const [withdrawals, setWithdrawals] = useState({ data: {}, props: { currentPage: 0 }, loading: true, error: false })
const [activeChartTab, setActiveChartTab] = useState(0)
const baseUrl = process.env.NEXT_PUBLIC_BASE_URL
const activeNetwork = networks.find(network => network.apiBaseUrl === baseUrl)
Expand Down Expand Up @@ -77,12 +76,12 @@ function Validator ({ hash }) {
useEffect(() => {
if (!validator.data?.identity) return

setTransfers(state => ({ ...state, loading: true }))
setWithdrawals(state => ({ ...state, loading: true }))

Api.getTransfersByIdentity(validator.data.identity, transfers.props.currentPage + 1, pageSize, 'desc')
.then(res => fetchHandlerSuccess(setTransfers, res))
.catch(err => fetchHandlerError(setTransfers, err))
}, [validator, transfers.props.currentPage])
Api.getWithdrawalsByIdentity(validator.data.identity, withdrawals.props.currentPage + 1, pageSize, 'desc')
.then(res => fetchHandlerSuccess(setWithdrawals, res))
.catch(err => fetchHandlerError(setWithdrawals, err))
}, [validator, withdrawals.props.currentPage])

function paginationHandler (setter, currentPage) {
setter(state => ({
Expand Down Expand Up @@ -468,9 +467,9 @@ function Validator ({ hash }) {
}
</TabPanel>
<TabPanel className={'ValidatorPage__ListContainer'}>
{!transfers.error
? !transfers.loading
? <TransfersList transfers={transfers.data.resultSet} identityId={validator.data?.identity}/>
{!withdrawals.error
? !withdrawals.loading
? <></>
: <LoadingList itemsCount={pageSize}/>
: <ErrorMessageBlock/>}
</TabPanel>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ $font-color: #fff;
font-family: $font-heading;
font-size: 0.75rem;
white-space: nowrap;
padding: 16px 8px;

&:disabled {
color: rgba(255, 255, 255, 0.5) !important;
Expand Down Expand Up @@ -199,4 +200,12 @@ $font-color: #fff;
}
}
}

@media screen and (max-width: $breakpoint-xl) {
.react-calendar {
&__tile {
padding: 12px 6px;
}
}
}
}
86 changes: 86 additions & 0 deletions packages/frontend/src/components/charts/TimeframeMenu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { useState, useEffect, forwardRef } from 'react'
import { Button } from '@chakra-ui/react'
import { DateRangePicker } from '../calendar'
import './TimeframeMenu.scss'

const chartConfig = {
timespan: {
defaultIndex: 3,
values: [
{
label: '1 hour',
range: '1h'
},
{
label: '24 hours',
range: '24h'
},
{
label: '3 days',
range: '3d'
},
{
label: '1 week',
range: '1w'
}
]
}
}

const TimeframeMenu = forwardRef(function TimeframeMenu ({ config, isActive, changeCallback, openStateCallback, className }, ref) {
const [timespan, setTimespan] = useState(chartConfig.timespan.values[chartConfig.timespan.defaultIndex])
const [menuIsOpen, setMenuIsOpen] = useState(false)

const changeHandler = (value) => {
setTimespan(value)
if (typeof changeCallback === 'function') changeCallback(value)
}

useEffect(() => {
if (!isActive) setMenuIsOpen(false)
}, [isActive])

useEffect(() => {
if (typeof openStateCallback === 'function') openStateCallback(menuIsOpen)
}, [menuIsOpen])

const calendarHandler = (value) => {}

return (
<div ref={ref} className={`TimeframeMenu ${className || ''}`}>
<div className={'TimeframeMenu__ValuesContainer'}>
<div className={'TimeframeMenu__ValuesTitle'}>
Select a day, period or Timeframe:
</div>

<div className={'TimeframeMenu__Values'}>
{config.timespan.values.map((iTimespan, i) => (
<Button
className={`TimeframeMenu__ValueButton ${iTimespan.range === timespan.range ? 'TimeframeMenu__ValueButton--Active' : ''}`}
onClick={() => {
changeHandler(iTimespan)
setMenuIsOpen(false)
}}
key={i}
size={'xs'}
>
{iTimespan.label}
</Button>
))}
</div>
</div>

<div className={'TimeframeMenu__Calendar TimeframeMenu__Calendar--Stub'}>
<DateRangePicker
disableFutureDates={true}
monthsToShow={7}
noTopNavigation={true}
noWeekDay={true}
changeHandler={calendarHandler}
/>
</div>
</div>
)
})

export default TimeframeMenu
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@use '../../../styles/mixins.scss';
@import '../../../styles/variables.scss';
@use '../../styles/mixins.scss';
@import '../../styles/variables.scss';

.TimeframeMenu {
padding: 12px;
Expand Down
4 changes: 3 additions & 1 deletion packages/frontend/src/components/charts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as d3 from 'd3'
import './charts.scss'
import { Container } from '@chakra-ui/react'
import theme from '../../styles/theme'
import TimeframeMenu from './TimeframeMenu'

function getDatesTicks (dates, numTicks) {
if (!dates.length) return []
Expand Down Expand Up @@ -438,5 +439,6 @@ const LineGraph = ({
}

export {
LineChart
LineChart,
TimeframeMenu
}

0 comments on commit c3bd28e

Please sign in to comment.