|
| 1 | +import { makeStyles } from "@material-ui/core/styles" |
| 2 | +import { AuditLog } from "api/typesGenerated" |
| 3 | +import { colors } from "theme/colors" |
| 4 | +import { MONOSPACE_FONT_FAMILY } from "theme/constants" |
| 5 | +import { combineClasses } from "util/combineClasses" |
| 6 | + |
| 7 | +const getDiffValue = (value: number | string | boolean) => { |
| 8 | + if (typeof value === "string") { |
| 9 | + return `"${value}"` |
| 10 | + } |
| 11 | + |
| 12 | + return value.toString() |
| 13 | +} |
| 14 | + |
| 15 | +export const AuditLogDiff: React.FC<{ diff: AuditLog["diff"] }> = ({ diff }) => { |
| 16 | + const styles = useStyles() |
| 17 | + const diffEntries = Object.entries(diff) |
| 18 | + |
| 19 | + return ( |
| 20 | + <div className={styles.diff}> |
| 21 | + <div className={combineClasses([styles.diffColumn, styles.diffOld])}> |
| 22 | + {diffEntries.map(([attrName, valueDiff], index) => ( |
| 23 | + <div key={attrName} className={styles.diffRow}> |
| 24 | + <div className={styles.diffLine}>{index + 1}</div> |
| 25 | + <div className={styles.diffIcon}>-</div> |
| 26 | + <div> |
| 27 | + {attrName}:{" "} |
| 28 | + <span className={combineClasses([styles.diffValue, styles.diffValueOld])}> |
| 29 | + {getDiffValue(valueDiff.old)} |
| 30 | + </span> |
| 31 | + </div> |
| 32 | + </div> |
| 33 | + ))} |
| 34 | + </div> |
| 35 | + <div className={combineClasses([styles.diffColumn, styles.diffNew])}> |
| 36 | + {diffEntries.map(([attrName, valueDiff], index) => ( |
| 37 | + <div key={attrName} className={styles.diffRow}> |
| 38 | + <div className={styles.diffLine}>{index + 1}</div> |
| 39 | + <div className={styles.diffIcon}>+</div> |
| 40 | + <div> |
| 41 | + {attrName}:{" "} |
| 42 | + <span className={combineClasses([styles.diffValue, styles.diffValueNew])}> |
| 43 | + {getDiffValue(valueDiff.new)} |
| 44 | + </span> |
| 45 | + </div> |
| 46 | + </div> |
| 47 | + ))} |
| 48 | + </div> |
| 49 | + </div> |
| 50 | + ) |
| 51 | +} |
| 52 | + |
| 53 | +const useStyles = makeStyles((theme) => ({ |
| 54 | + diff: { |
| 55 | + display: "flex", |
| 56 | + alignItems: "flex-start", |
| 57 | + fontSize: theme.typography.body2.fontSize, |
| 58 | + borderTop: `1px solid ${theme.palette.divider}`, |
| 59 | + fontFamily: MONOSPACE_FONT_FAMILY, |
| 60 | + }, |
| 61 | + |
| 62 | + diffColumn: { |
| 63 | + flex: 1, |
| 64 | + paddingTop: theme.spacing(2), |
| 65 | + paddingBottom: theme.spacing(2.5), |
| 66 | + lineHeight: "160%", |
| 67 | + }, |
| 68 | + |
| 69 | + diffOld: { |
| 70 | + backgroundColor: theme.palette.error.dark, |
| 71 | + color: theme.palette.error.contrastText, |
| 72 | + }, |
| 73 | + |
| 74 | + diffRow: { |
| 75 | + display: "flex", |
| 76 | + alignItems: "baseline", |
| 77 | + }, |
| 78 | + |
| 79 | + diffLine: { |
| 80 | + opacity: 0.5, |
| 81 | + width: theme.spacing(8), |
| 82 | + textAlign: "right", |
| 83 | + flexShrink: 0, |
| 84 | + }, |
| 85 | + |
| 86 | + diffIcon: { |
| 87 | + width: theme.spacing(4), |
| 88 | + textAlign: "center", |
| 89 | + fontSize: theme.typography.body1.fontSize, |
| 90 | + }, |
| 91 | + |
| 92 | + diffNew: { |
| 93 | + backgroundColor: theme.palette.success.dark, |
| 94 | + color: theme.palette.success.contrastText, |
| 95 | + }, |
| 96 | + |
| 97 | + diffValue: { |
| 98 | + padding: 1, |
| 99 | + borderRadius: theme.shape.borderRadius / 2, |
| 100 | + }, |
| 101 | + |
| 102 | + diffValueOld: { |
| 103 | + backgroundColor: colors.red[12], |
| 104 | + }, |
| 105 | + |
| 106 | + diffValueNew: { |
| 107 | + backgroundColor: colors.green[12], |
| 108 | + }, |
| 109 | +})) |
0 commit comments