Skip to content

Commit

Permalink
Show LongDataDisplay controls only when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
buberdds committed Feb 23, 2024
1 parent 846c7bb commit 8621046
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 38 deletions.
1 change: 1 addition & 0 deletions .changelog/1287.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Show LongDataDisplay controls only when needed
91 changes: 61 additions & 30 deletions src/app/components/LongDataDisplay/index.tsx
Original file line number Diff line number Diff line change
@@ -1,47 +1,78 @@
import Collapse from '@mui/material/Collapse'
import Link from '@mui/material/Link'
import { FC, useState } from 'react'
import { FC, useEffect, useRef, useState } from 'react'
import { useTranslation } from 'react-i18next'
import Button from '@mui/material/Button'
import Typography from '@mui/material/Typography'
import { styled } from '@mui/material/styles'
import { COLORS } from '../../../styles/theme/colors'

export const LongDataDisplay: FC<{ data: string; threshold: number; fontWeight?: number }> = ({
const StyledButton = styled(Button)(({ theme }) => ({
color: COLORS.brandDark,
fontWeight: 700,
minWidth: 'auto',
height: 'auto',
padding: 0,
[theme.breakpoints.down('sm')]: {
margin: theme.spacing(3, 0, 0, 0),
},
[theme.breakpoints.up('sm')]: {
margin: theme.spacing(4, 0, 0, 0),
},
'&&:hover, &&:active, &&:focus-visible': {
color: COLORS.brandDark,
textDecoration: 'none',
borderRadius: 0,
},
}))

const lineHeight = 22

export const LongDataDisplay: FC<{ data: string; fontWeight?: number; collapsedLinesNumber?: number }> = ({
data,
threshold,
fontWeight = 700,
collapsedLinesNumber = 2,
}) => {
const { t } = useTranslation()
const [showData, setShowData] = useState(false)
const needsHiding = data.length > threshold
if (!needsHiding) {
return (
const [isExpanded, setIsExpanded] = useState(false)
const [isOverflowing, setIsOverflowing] = useState(false)
const textRef = useRef<HTMLDivElement | null>(null)
const collapsedContainerMaxHeight = collapsedLinesNumber * lineHeight

useEffect(() => {
const checkOverflow = () => {
if (textRef.current) {
const isOverflow = textRef.current.scrollHeight > textRef.current.clientHeight
setIsOverflowing(isOverflow)
}
}

checkOverflow()
const handleResize = () => {
checkOverflow()
}

window.addEventListener('resize', handleResize)
return () => window.removeEventListener('resize', handleResize)
}, [data])

return (
<div>
<Typography
variant="mono"
sx={{
ref={textRef}
style={{
fontWeight,
maxHeight: isExpanded ? 'none' : collapsedContainerMaxHeight,
overflow: 'hidden',
display: 'flex',
lineHeight: `${lineHeight}px`,
overflowWrap: 'anywhere',
}}
>
{data}
</Typography>
)
}
return (
<div>
<Collapse orientation="vertical" in={showData} onClick={() => setShowData(true)} collapsedSize="3em">
<Typography
variant="mono"
sx={{
fontWeight,
overflowWrap: 'anywhere',
}}
>
{data}
</Typography>
</Collapse>
{data.length > threshold && (
<Link sx={{ cursor: 'pointer' }} onClick={() => setShowData(!showData)}>
{showData ? t('common.hide') : t('common.show')}
</Link>
{(isOverflowing || isExpanded) && (
<StyledButton onClick={() => setIsExpanded(!isExpanded)}>
{isExpanded ? t('common.hide') : t('common.show')}
</StyledButton>
)}
</div>
)
Expand Down
1 change: 0 additions & 1 deletion src/app/components/RuntimeEvents/RuntimeEventDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ export const RuntimeEventDetails: FC<{
<br />
<LongDataDisplay
data={`0x${Buffer.from(event.body.data, 'base64').toString('hex')}`}
threshold={300}
fontWeight={400}
/>
</div>
Expand Down
10 changes: 3 additions & 7 deletions src/app/pages/RuntimeTransactionDetailPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ export const RuntimeTransactionDetailView: FC<{
<>
<dt>{t('transaction.rawData')}</dt>
<dd>
<LongDataDisplay data={base64ToHex(transaction.body.data)} threshold={300} />
<LongDataDisplay data={base64ToHex(transaction.body.data)} />
</dd>
</>
)}
Expand Down Expand Up @@ -392,7 +392,7 @@ export const RuntimeTransactionDetailView: FC<{
<>
<dt>{t('transactions.encryption.encryptedData')}</dt>
<dd>
<LongDataDisplay data={transaction.encryption_envelope.data} threshold={300} />
<LongDataDisplay data={transaction.encryption_envelope.data} />
</dd>
</>
)}
Expand All @@ -412,11 +412,7 @@ export const RuntimeTransactionDetailView: FC<{
<>
<dt>{t('transactions.encryption.encryptedResult')}</dt>
<dd>
<LongDataDisplay
data={transaction.encryption_envelope.result}
fontWeight={400}
threshold={300}
/>
<LongDataDisplay data={transaction.encryption_envelope.result} fontWeight={400} />
</dd>
</>
)}
Expand Down

0 comments on commit 8621046

Please sign in to comment.