Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show LongDataDisplay controls only when needed #1287

Merged
merged 2 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
90 changes: 62 additions & 28 deletions src/app/components/LongDataDisplay/index.tsx
Original file line number Diff line number Diff line change
@@ -1,47 +1,81 @@
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"
ref={textRef}
sx={{
fontWeight,
maxHeight: isExpanded ? 'none' : collapsedContainerMaxHeight,
overflow: 'hidden',
lineHeight: `${lineHeight}px`,
overflowWrap: 'anywhere',
display: '-webkit-box',
'-webkit-line-clamp': isExpanded ? 'none' : `${collapsedLinesNumber}`, // keep it as string to avoid MUI adding px
'-webkit-box-orient': 'vertical',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: fix warning in console

}}
>
{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')}
buberdds marked this conversation as resolved.
Show resolved Hide resolved
</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
Loading