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

Long cluster names overlap in PVC card #4192

Merged
merged 1 commit into from
Feb 5, 2025
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
35 changes: 32 additions & 3 deletions src/routes/components/cluster/cluster.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import messages from 'locales/messages';
import React from 'react';
import type { WrappedComponentProps } from 'react-intl';
import { injectIntl } from 'react-intl';
import { getResizeObserver } from 'routes/components/charts/common/chartUtils';
import { getComputedReportItems } from 'routes/utils/computedReport/getComputedReportItems';
import { noop } from 'routes/utils/noop';

import { styles } from './cluster.styles';
import { ClusterModal } from './modal/clusterModal';
Expand All @@ -20,14 +22,18 @@ interface ClusterOwnProps {
interface ClusterState {
isOpen?: boolean;
showAll?: boolean;
width?: number;
}

type ClusterProps = ClusterOwnProps & WrappedComponentProps;

class ClusterBase extends React.Component<ClusterProps, ClusterState> {
private containerRef = React.createRef<HTMLDivElement>();
private observer: any = noop;
protected defaultState: ClusterState = {
isOpen: false,
showAll: false,
width: 0,
};
public state: ClusterState = { ...this.defaultState };

Expand All @@ -37,6 +43,16 @@ class ClusterBase extends React.Component<ClusterProps, ClusterState> {
this.handleOpen = this.handleOpen.bind(this);
}

public componentDidMount() {
this.observer = getResizeObserver(this.containerRef?.current, this.handleResize);
}

public componentWillUnmount() {
if (this.observer) {
this.observer();
}
}

public handleClose = (isOpen: boolean) => {
this.setState({ isOpen });
};
Expand All @@ -47,11 +63,21 @@ class ClusterBase extends React.Component<ClusterProps, ClusterState> {
return false;
};

private handleResize = () => {
const { width } = this.state;
const { clientWidth = 0 } = this.containerRef?.current || {};

if (clientWidth !== width) {
this.setState({ width: clientWidth });
}
};

public render() {
const { clusters, groupBy, intl, report, title } = this.props;
const { isOpen, showAll } = this.state;
const { isOpen, showAll, width } = this.state;

const maxCharsPerName = 45; // Max (non-whitespace) characters that fit without overlapping card
// Cluster name may be up to 256 chars, while the ID is 50
let maxCharsPerName = 55; // Max (non-whitespace) characters that fit without overlapping card
const maxItems = 2; // Max items to show before adding "more" link
const someClusters = [];

Expand Down Expand Up @@ -95,6 +121,9 @@ class ClusterBase extends React.Component<ClusterProps, ClusterState> {
someClusters.push(clusterString);
} else if (someClusters.length < maxItems) {
if (clusterString.length > maxCharsPerName) {
if (width < 475) {
maxCharsPerName = 40;
}
clusterString = clusterString.slice(0, maxCharsPerName).trim().concat('...');
someClusters.push(
<Tooltip content={cluster} enableFlip>
Expand All @@ -108,7 +137,7 @@ class ClusterBase extends React.Component<ClusterProps, ClusterState> {
}

return (
<div style={styles.clustersContainer}>
<div ref={this.containerRef} style={styles.clustersContainer}>
{someClusters && someClusters.map((cluster, index) => <span key={index}>{cluster}</span>)}
{someClusters.length < allClusters.length && (
<a data-testid="cluster-lnk" href="#/" onClick={this.handleOpen}>
Expand Down
28 changes: 27 additions & 1 deletion src/routes/details/components/pvcChart/pvcChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
TextListItem,
TextListItemVariants,
TextListVariants,
Tooltip,
} from '@patternfly/react-core';
import type { OcpQuery } from 'api/queries/ocpQuery';
import { parseQuery } from 'api/queries/ocpQuery';
Expand Down Expand Up @@ -214,6 +215,31 @@ class PvcChartBase extends React.Component<PvcChartProps, PvcChartState> {
);
};

private getClusterName = item => {
const { width } = this.state;

// Cluster name may be up to 256 chars, while the ID is 50
let maxCharsPerName = 50;
const clusterName = item?.clusters ? item.clusters[0] : null;

// Max (non-whitespace) characters that fit without overlapping card
if (width < 475) {
maxCharsPerName = 25; // Provide more space for tooltip at smallest window size
} else if (width < 800) {
const k = 100 + (800 - width) / 8;
maxCharsPerName = (width / k) * 5;
}

if (clusterName?.length > maxCharsPerName) {
return (
<Tooltip content={clusterName} enableFlip>
<span>{clusterName.slice(0, maxCharsPerName).trim().concat('...')}</span>
</Tooltip>
);
}
return clusterName;
};

private getDescription = item => {
const { intl } = this.props;

Expand All @@ -226,7 +252,7 @@ class PvcChartBase extends React.Component<PvcChartProps, PvcChartState> {
</TextListItem>
<TextListItem component={TextListItemVariants.dd}>{item.persistent_volume_claim}</TextListItem>
<TextListItem component={TextListItemVariants.dt}>{intl.formatMessage(messages.cluster)}</TextListItem>
<TextListItem component={TextListItemVariants.dd}>{item.clusters ? item.clusters[0] : null}</TextListItem>
<TextListItem component={TextListItemVariants.dd}>{this.getClusterName(item)}</TextListItem>
</TextList>
</TextContent>
<TextContent className="textContentOverride">
Expand Down