Refactor HistoryDisplay component to streamline imports and enhance layout structure. Consolidate loading state handling and improve chart rendering logic for better performance and user experience.

This commit is contained in:
Tom Butcher 2026-07-18 18:59:51 +01:00
parent 7ca78280b3
commit cd30be7677

View File

@ -1,13 +1,5 @@
import { useEffect, useState, useContext, useMemo } from 'react' import { useEffect, useState, useContext, useMemo } from 'react'
import { import { Card, Segmented, Flex, Popover, DatePicker, Button, Space } from 'antd'
Card,
Segmented,
Flex,
Popover,
DatePicker,
Button,
Space
} from 'antd'
import { import {
ResponsiveContainer, ResponsiveContainer,
BarChart, BarChart,
@ -25,9 +17,9 @@ import { ApiServerContext } from '../context/ApiServerContext'
import { AuthContext } from '../context/AuthContext' import { AuthContext } from '../context/AuthContext'
import dayjs from 'dayjs' import dayjs from 'dayjs'
import { useThemeContext } from '../context/ThemeContext' import { useThemeContext } from '../context/ThemeContext'
import LoadingPlaceholder from './LoadingPlaceholder'
import MissingPlaceholder from './MissingPlaceholder' import MissingPlaceholder from './MissingPlaceholder'
import CheckIcon from '../../Icons/CheckIcon' import CheckIcon from '../../Icons/CheckIcon'
import { LoadingOutlined } from '@ant-design/icons'
const HistoryDisplay = ({ const HistoryDisplay = ({
objectType, objectType,
@ -291,7 +283,8 @@ const HistoryDisplay = ({
}) })
const chartRows = Array.from( const chartRows = Array.from(
chartData.reduce((acc, item) => { chartData
.reduce((acc, item) => {
const existing = acc.get(item.date) || { const existing = acc.get(item.date) || {
date: item.date, date: item.date,
dateFormatted: item.dateFormatted dateFormatted: item.dateFormatted
@ -299,7 +292,8 @@ const HistoryDisplay = ({
existing[item.category] = item.value existing[item.category] = item.value
acc.set(item.date, existing) acc.set(item.date, existing)
return acc return acc
}, new Map()).values() }, new Map())
.values()
).sort((a, b) => new Date(a.date) - new Date(b.date)) ).sort((a, b) => new Date(a.date) - new Date(b.date))
const customTimeRangeContent = ( const customTimeRangeContent = (
@ -334,6 +328,7 @@ const HistoryDisplay = ({
style={{ width: '100%' }} style={{ width: '100%' }}
styles={{ body: { padding: '12px', ...styles } }} styles={{ body: { padding: '12px', ...styles } }}
> >
<Flex gap='small' vertical>
{!startDate && !endDate && ( {!startDate && !endDate && (
<Flex justify='space-between'> <Flex justify='space-between'>
<Flex align='center' gap='5px'> <Flex align='center' gap='5px'>
@ -354,6 +349,8 @@ const HistoryDisplay = ({
</Popover> </Popover>
</Flex> </Flex>
<Flex gap='middle'>
{loading == true && <LoadingOutlined />}
<Segmented <Segmented
size='small' size='small'
options={[ options={[
@ -368,10 +365,6 @@ const HistoryDisplay = ({
disabled={loading} disabled={loading}
/> />
</Flex> </Flex>
)}
{loading == true && (
<Flex justify='center' align='center' style={{ height: `${height}px` }}>
<LoadingPlaceholder message='Loading history data...' />
</Flex> </Flex>
)} )}
{chartRows.length > 0 && ( {chartRows.length > 0 && (
@ -384,10 +377,16 @@ const HistoryDisplay = ({
/> />
<XAxis <XAxis
dataKey='dateFormatted' dataKey='dateFormatted'
tick={{ fill: isDarkMode ? '#d9d9d9' : '#595959', fontSize: 12 }} tick={{
fill: isDarkMode ? '#d9d9d9' : '#595959',
fontSize: 12
}}
/> />
<YAxis <YAxis
tick={{ fill: isDarkMode ? '#d9d9d9' : '#595959', fontSize: 12 }} tick={{
fill: isDarkMode ? '#d9d9d9' : '#595959',
fontSize: 12
}}
/> />
<Tooltip <Tooltip
contentStyle={{ contentStyle={{
@ -413,10 +412,15 @@ const HistoryDisplay = ({
</div> </div>
)} )}
{loading == false && chartRows.length == 0 && ( {loading == false && chartRows.length == 0 && (
<Flex justify='center' align='center' style={{ height: `${height}px` }}> <Flex
justify='center'
align='center'
style={{ height: `${height}px` }}
>
<MissingPlaceholder message='No data available.' /> <MissingPlaceholder message='No data available.' />
</Flex> </Flex>
)} )}
</Flex>
</Card> </Card>
) )
} }