Skip to content

Commit

Permalink
fix: add support for long texts in alert history page
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmadshaheer committed Sep 9, 2024
1 parent 74c994f commit a5ff59d
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 4 deletions.
5 changes: 4 additions & 1 deletion frontend/src/pages/AlertDetails/AlertHeader/AlertHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import './AlertHeader.styles.scss';

import LineClampedText from 'periscope/components/LineClampedText/LineClampedText';
import { useAlertRule } from 'providers/Alert';
import { useEffect, useMemo } from 'react';

Expand Down Expand Up @@ -42,7 +43,9 @@ function AlertHeader({ alertDetails }: AlertHeaderProps): JSX.Element {
<div className="top-section">
<div className="alert-title-wrapper">
<AlertState state={isAlertRuleDisabled ? 'disabled' : state} />
<div className="alert-title">{alert}</div>
<div className="alert-title">
<LineClampedText text={alert} />
</div>
</div>
</div>
<div className="bottom-section">
Expand Down
25 changes: 22 additions & 3 deletions frontend/src/periscope/components/KeyValueLabel/KeyValueLabel.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
import './KeyValueLabel.styles.scss';

type KeyValueLabelProps = { badgeKey: string; badgeValue: string };
import { Tooltip } from 'antd';

import TrimmedText from '../TrimmedText/TrimmedText';

type KeyValueLabelProps = {
badgeKey: string;
badgeValue: string;
maxCharacters?: number;
};

export default function KeyValueLabel({
badgeKey,
badgeValue,
maxCharacters = 20,
}: KeyValueLabelProps): JSX.Element | null {
if (!badgeKey || !badgeValue) {
return null;
}
return (
<div className="key-value-label">
<div className="key-value-label__key">{badgeKey}</div>
<div className="key-value-label__value">{badgeValue}</div>
<div className="key-value-label__key">
<TrimmedText text={badgeKey} maxCharacters={maxCharacters} />
</div>
<Tooltip title={badgeValue}>
<div className="key-value-label__value">
<TrimmedText text={badgeValue} maxCharacters={maxCharacters} />
</div>
</Tooltip>
</div>
);
}

KeyValueLabel.defaultProps = {
maxCharacters: 20,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.line-clamped-text {
display: -webkit-box;
-webkit-box-orient: vertical;
overflow: hidden;
text-overflow: ellipsis;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import './LineClampedText.styles.scss';

import { Tooltip } from 'antd';
import { useEffect, useRef, useState } from 'react';

function LineClampedText({
text,
lines,
}: {
text: string;
lines?: number;
}): JSX.Element {
const [isOverflowing, setIsOverflowing] = useState(false);
const textRef = useRef<HTMLDivElement>(null);

useEffect(() => {
const checkOverflow = (): void => {
if (textRef.current) {
setIsOverflowing(
textRef.current.scrollHeight > textRef.current.clientHeight,
);
}
};

checkOverflow();
window.addEventListener('resize', checkOverflow);

return (): void => {
window.removeEventListener('resize', checkOverflow);
};
}, [text, lines]);

const content = (
<div
ref={textRef}
className="line-clamped-text"
style={{
WebkitLineClamp: lines,
}}
>
{text}
</div>
);

return isOverflowing ? <Tooltip title={text}>{content}</Tooltip> : content;
}

LineClampedText.defaultProps = {
lines: 1,
};

export default LineClampedText;
30 changes: 30 additions & 0 deletions frontend/src/periscope/components/TrimmedText/TrimmedText.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Tooltip } from 'antd';
import { useEffect, useState } from 'react';

function TrimmedText({
text,
maxCharacters,
}: {
text: string;
maxCharacters: number;
}): JSX.Element {
const [displayText, setDisplayText] = useState(text);

useEffect(() => {
if (text.length > maxCharacters) {
setDisplayText(`${text.slice(0, maxCharacters)}...`);
} else {
setDisplayText(text);
}
}, [text, maxCharacters]);

return text.length > maxCharacters ? (
<Tooltip title={text}>
<span>{displayText}</span>
</Tooltip>
) : (
<span>{displayText}</span>
);
}

export default TrimmedText;

0 comments on commit a5ff59d

Please sign in to comment.