Skip to content

Commit

Permalink
refactor color naming
Browse files Browse the repository at this point in the history
  • Loading branch information
fdelemarre committed Jul 8, 2024
1 parent 2dd88a3 commit de6212c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 22 deletions.
27 changes: 16 additions & 11 deletions src/webapp/components/table/statistic-table/ColoredCell.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
import React from "react";
import styled from "styled-components";
import { TableCell } from "@material-ui/core";
import { CellStatus, CellStatusValues } from "../../../hooks/useTableCell";

type ColoredCellProps = {
value: string;
color?: string;
boldUnderline?: boolean;
color?: CellStatusValues;
};

const StyledTableCell = styled(TableCell)<{ color?: string; boldUnderline?: boolean }>`
background-color: ${props =>
props.color ? props.theme.palette.common[props.color] : "initial"};
color: ${props => (props.color ? props.theme.palette.common.white : "initial")};
text-decoration: ${props => (props.boldUnderline ? "underline" : "none")};
font-weight: ${props => (props.boldUnderline || props.color ? "600" : "initial")};
`;
const cellStatusColor = {
[CellStatus.Valid]: "green",
[CellStatus.Alert]: "red",
[CellStatus.Warning]: "orange",
};

export const ColoredCell: React.FC<ColoredCellProps> = ({ value, color, boldUnderline }) => {
export const ColoredCell: React.FC<ColoredCellProps> = ({ value, color }) => {
return (
<StyledTableCell color={color} boldUnderline={boldUnderline}>
<StyledTableCell color={color ? cellStatusColor[color] : undefined}>
{value}
</StyledTableCell>
);
};

const StyledTableCell = styled(TableCell)<{ color?: string }>`
background-color: ${props =>
props.color ? props.theme.palette.common[props.color] : "initial"};
color: ${props => (props.color ? props.theme.palette.common.white : "initial")};
font-weight: ${props => (props.color ? "600" : "initial")};
`;
32 changes: 24 additions & 8 deletions src/webapp/components/table/statistic-table/StatisticTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,25 @@ export const StatisticTable: React.FC<StatisticTableProps> = React.memo(
<TableBody>
{filteredRows.map((row, rowIndex) => (
<TableRow key={rowIndex}>
{columns.map((column, columnIndex) => (
<ColoredCell
key={`${rowIndex}-${column.value}`}
value={row[column.value] || ""}
color={getCellColor(row[column.value], column.value)}
boldUnderline={columnIndex === 0}
/>
))}
{columns.map((column, columnIndex) =>
calculateColumns.includes(column.value) ? (
<ColoredCell
key={`${rowIndex}-${column.value}`}
value={row[column.value] || ""}
color={getCellColor(
row[column.value],
column.value
)}
/>
) : (
<StyledTableCell
key={`${rowIndex}-${column.value}`}
boldUnderline={columnIndex === 0}
>
{row[column.value] || ""}
</StyledTableCell>
)
)}
</TableRow>
))}
<MedianRow
Expand Down Expand Up @@ -166,6 +177,11 @@ const HeadTableCell = styled(TableCell)<{ $dark?: boolean }>`
font-weight: 600;
`;

const StyledTableCell = styled(TableCell)<{ boldUnderline?: boolean }>`
text-decoration: ${props => (props.boldUnderline ? "underline" : "none")};
font-weight: ${props => (props.boldUnderline ? "600" : "initial")};
`;

const Container = styled.div`
display: flex;
justify-content: space-between;
Expand Down
15 changes: 12 additions & 3 deletions src/webapp/hooks/useTableCell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,29 @@ import { useCallback } from "react";
import { Maybe } from "../../utils/ts-utils";
import _ from "../../domain/entities/generic/Collection";

export const enum CellStatus {
Valid = "valid",
Alert = "alert",
Warning = "warning",
}
export type CellStatusValues = `${CellStatus}`;

export const useTableCell = (
editRiskAssessmentColumns: string[],
columnRules: { [key: string]: number }
) => {
const getCellColor = useCallback(
(cellValue: Maybe<string>, column: string) => {
if (!cellValue) {
return editRiskAssessmentColumns.includes(column) ? "orange" : undefined;
return editRiskAssessmentColumns.includes(column) ? CellStatus.Warning : undefined;
}

const value = Number(cellValue);

if (editRiskAssessmentColumns.includes(column)) {
return columnRules.respond7d && value > columnRules.respond7d ? "red" : undefined;
return columnRules.respond7d && value > columnRules.respond7d
? CellStatus.Alert
: undefined;
}

const rule = columnRules[column];
Expand All @@ -24,7 +33,7 @@ export const useTableCell = (
return undefined;
}

return value <= rule ? "green" : "red";
return value <= rule ? CellStatus.Valid : CellStatus.Alert;
},
[editRiskAssessmentColumns, columnRules]
);
Expand Down

0 comments on commit de6212c

Please sign in to comment.