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

fix: add support for long texts in alert history page #5895

Merged
merged 1 commit into from
Sep 11, 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
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
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;
Loading