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: overflowing bulkdeployment texts #292

Merged
merged 1 commit into from
Aug 7, 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
5 changes: 3 additions & 2 deletions src/components/BulkDeployments/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { FC } from 'react';

import CancelDeployment from 'components/CancelDeployment';
import { getDeploymentDuration } from 'components/Deployment';
import { formatString } from 'components/DeploymentsByFilter';
import HoverTag from 'components/HoverTag';
import DeploymentLink from 'components/link/Deployment';
import DeploymentsLink from 'components/link/Deployments';
Expand Down Expand Up @@ -49,15 +50,15 @@ const BulkDeployments: FC<BulkDeploymentsProps> = ({ deployments }) => (
<div className="data-row" key={idx} data-deployment={deployment.id}>
<div className="project">
<ProjectLink projectSlug={deployment.environment.project.name}>
{deployment.environment.project.name}
{formatString(deployment.environment.project.name, 'project')}
</ProjectLink>
</div>
<div className="environment">
<DeploymentsLink
environmentSlug={deployment.environment.openshiftProjectName}
projectSlug={deployment.environment.project.name}
>
{deployment.environment.name}
{formatString(deployment.environment.name, 'environment')}
</DeploymentsLink>
</div>
<div className="name">
Expand Down
98 changes: 49 additions & 49 deletions src/components/DeploymentsByFilter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,55 @@ import moment from 'moment';
import useSortableData from '../../lib/withSortedItems';
import { Deployments, DeploymentsDataTable, DeploymentsHeader } from './StyledDeploymentsByFilter';

export const formatString = (textToEdit, labelClassToQuery) => {
// if the string is bigger than the data-row container, then add new lines.
const getTextWidth = string => {
// gets the width of a string that *will* be rendered
const canvas = new OffscreenCanvas(500, 200);
const context = canvas.getContext('2d');
context.font = getComputedStyle(document.body).font;
return context.measureText(string).width;
};

const labelWidth = document.querySelector(`.data-row > .${labelClassToQuery}`)?.getBoundingClientRect().width;

const editString = string => {
// find all "-" or "/" and replace with line breaks prefixed by the symbol found.
const regex = /[-\/]/g;
return string.replace(regex, match => match + '\n');
};

const combine = strings => {
let combined = '';
let line = '';

if (strings.length === 1) return strings[0];

for (let i = 0; i < strings.length; i++) {
const newLine = line + strings[i];
const width = getTextWidth(newLine);

if (width <= labelWidth + 30) {
line = newLine;
} else {
combined += line + '\n';
line = strings[i];
}
if (i === strings.length - 1 && getTextWidth(line) <= labelWidth + 30) {
combined += line;
} else if (i === strings.length - 1) {
combined += '\n' + line;
}
}
return combined;
};

if (labelWidth < getTextWidth(textToEdit)) {
return combine(editString(textToEdit).split('\n'));
}
return textToEdit;
};

/**
* The primary list of running deployments.
*/
Expand All @@ -22,55 +71,6 @@ const DeploymentsByFilter = ({ deployments }) => {
const [searchTerm, setSearchTerm] = useState('');
const [hasFilter, setHasFilter] = useState(false);

const formatString = (textToEdit, labelClassToQuery) => {
// if the string is bigger than the data-row container, then add new lines.
const getTextWidth = string => {
// gets the width of a string that *will* be rendered
const canvas = new OffscreenCanvas(500, 200);
const context = canvas.getContext('2d');
context.font = getComputedStyle(document.body).font;
return context.measureText(string).width;
};

const labelWidth = document.querySelector(`.data-row > .${labelClassToQuery}`)?.getBoundingClientRect().width;

const editString = string => {
// find all "-" or "/" and replace with line breaks prefixed by the symbol found.
const regex = /[-\/]/g;
return string.replace(regex, match => match + '\n');
};

const combine = strings => {
let combined = '';
let line = '';

if (strings.length === 1) return strings[0];

for (let i = 0; i < strings.length; i++) {
const newLine = line + strings[i];
const width = getTextWidth(newLine);

if (width <= labelWidth + 30) {
line = newLine;
} else {
combined += line + '\n';
line = strings[i];
}
if (i === strings.length - 1 && getTextWidth(line) <= labelWidth + 30) {
combined += line;
} else if (i === strings.length - 1) {
combined += '\n' + line;
}
}
return combined;
};

if (labelWidth < getTextWidth(textToEdit)) {
return combine(editString(textToEdit).split('\n'));
}
return textToEdit;
};

const handleSearchFilterChange = event => {
setHasFilter(false);

Expand Down