|
| 1 | +import Collapse from "@material-ui/core/Collapse" |
| 2 | +import Link from "@material-ui/core/Link" |
| 3 | +import { makeStyles } from "@material-ui/core/styles" |
| 4 | +import TableCell from "@material-ui/core/TableCell" |
| 5 | +import TableRow from "@material-ui/core/TableRow" |
| 6 | +import { AuditLog, Template, Workspace } from "api/typesGenerated" |
| 7 | +import { CloseDropdown, OpenDropdown } from "components/DropdownArrows/DropdownArrows" |
| 8 | +import { Pill } from "components/Pill/Pill" |
| 9 | +import { Stack } from "components/Stack/Stack" |
| 10 | +import { UserAvatar } from "components/UserAvatar/UserAvatar" |
| 11 | +import { useState } from "react" |
| 12 | +import { Link as RouterLink } from "react-router-dom" |
| 13 | +import { createDayString } from "util/createDayString" |
| 14 | +import { AuditDiff } from "./AuditLogDiff" |
| 15 | + |
| 16 | +const getResourceLabel = (resource: AuditLog["resource"]): string => { |
| 17 | + if ("name" in resource) { |
| 18 | + return resource.name |
| 19 | + } |
| 20 | + |
| 21 | + return resource.username |
| 22 | +} |
| 23 | + |
| 24 | +const getResourceHref = ( |
| 25 | + resource: AuditLog["resource"], |
| 26 | + resourceType: AuditLog["resource_type"], |
| 27 | +): string | undefined => { |
| 28 | + switch (resourceType) { |
| 29 | + case "user": |
| 30 | + return `/users` |
| 31 | + case "template": |
| 32 | + return `/templates/${(resource as Template).name}` |
| 33 | + case "workspace": |
| 34 | + return `/workspaces/@${(resource as Workspace).owner_name}/${(resource as Workspace).name}` |
| 35 | + case "organization": |
| 36 | + return |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +const ResourceLink: React.FC<{ |
| 41 | + resource: AuditLog["resource"] |
| 42 | + resourceType: AuditLog["resource_type"] |
| 43 | +}> = ({ resource, resourceType }) => { |
| 44 | + const href = getResourceHref(resource, resourceType) |
| 45 | + const label = <strong>{getResourceLabel(resource)}</strong> |
| 46 | + |
| 47 | + if (!href) { |
| 48 | + return label |
| 49 | + } |
| 50 | + |
| 51 | + return ( |
| 52 | + <Link component={RouterLink} to={href}> |
| 53 | + {label} |
| 54 | + </Link> |
| 55 | + ) |
| 56 | +} |
| 57 | + |
| 58 | +const actionLabelByAction: Record<AuditLog["action"], string> = { |
| 59 | + create: "created a new", |
| 60 | + write: "updated", |
| 61 | + delete: "deleted", |
| 62 | +} |
| 63 | + |
| 64 | +const resourceLabelByResourceType: Record<AuditLog["resource_type"], string> = { |
| 65 | + organization: "organization", |
| 66 | + template: "template", |
| 67 | + template_version: "template version", |
| 68 | + user: "user", |
| 69 | + workspace: "workspace", |
| 70 | +} |
| 71 | + |
| 72 | +const readableActionMessage = (auditLog: AuditLog) => { |
| 73 | + return `${actionLabelByAction[auditLog.action]} ${ |
| 74 | + resourceLabelByResourceType[auditLog.resource_type] |
| 75 | + }` |
| 76 | +} |
| 77 | + |
| 78 | +export interface AuditLogRowProps { |
| 79 | + auditLog: AuditLog |
| 80 | + // Useful for Storybook |
| 81 | + defaultIsDiffOpen?: boolean |
| 82 | +} |
| 83 | + |
| 84 | +export const AuditLogRow: React.FC<AuditLogRowProps> = ({ |
| 85 | + auditLog, |
| 86 | + defaultIsDiffOpen = false, |
| 87 | +}) => { |
| 88 | + const styles = useStyles() |
| 89 | + const [isDiffOpen, setIsDiffOpen] = useState(defaultIsDiffOpen) |
| 90 | + const diffs = Object.entries(auditLog.diff) |
| 91 | + const shouldDisplayDiff = diffs.length > 0 |
| 92 | + |
| 93 | + const toggle = () => { |
| 94 | + if (shouldDisplayDiff) { |
| 95 | + setIsDiffOpen((v) => !v) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return ( |
| 100 | + <TableRow key={auditLog.id} hover={shouldDisplayDiff}> |
| 101 | + <TableCell className={styles.auditLogCell}> |
| 102 | + <Stack |
| 103 | + style={{ cursor: shouldDisplayDiff ? "pointer" : undefined }} |
| 104 | + direction="row" |
| 105 | + alignItems="center" |
| 106 | + className={styles.auditLogRow} |
| 107 | + tabIndex={0} |
| 108 | + onClick={toggle} |
| 109 | + onKeyDown={(event) => { |
| 110 | + if (event.key === "Enter") { |
| 111 | + toggle() |
| 112 | + } |
| 113 | + }} |
| 114 | + > |
| 115 | + <Stack |
| 116 | + direction="row" |
| 117 | + alignItems="center" |
| 118 | + justifyContent="space-between" |
| 119 | + className={styles.auditLogRowInfo} |
| 120 | + > |
| 121 | + <Stack direction="row" alignItems="center"> |
| 122 | + <UserAvatar username={auditLog.user?.username ?? ""} /> |
| 123 | + <div> |
| 124 | + <span className={styles.auditLogResume}> |
| 125 | + <strong>{auditLog.user?.username}</strong> {readableActionMessage(auditLog)}{" "} |
| 126 | + <ResourceLink |
| 127 | + resource={auditLog.resource} |
| 128 | + resourceType={auditLog.resource_type} |
| 129 | + /> |
| 130 | + </span> |
| 131 | + <span className={styles.auditLogTime}>{createDayString(auditLog.time)}</span> |
| 132 | + </div> |
| 133 | + </Stack> |
| 134 | + |
| 135 | + <Stack direction="column" alignItems="flex-end" spacing={1}> |
| 136 | + <Pill type="success" text={auditLog.status_code.toString()} /> |
| 137 | + <Stack direction="row" alignItems="center" className={styles.auditLogExtraInfo}> |
| 138 | + <div> |
| 139 | + <strong>IP</strong> {auditLog.ip} |
| 140 | + </div> |
| 141 | + <div> |
| 142 | + <strong>Agent</strong> {auditLog.user_agent} |
| 143 | + </div> |
| 144 | + </Stack> |
| 145 | + </Stack> |
| 146 | + </Stack> |
| 147 | + |
| 148 | + <div className={shouldDisplayDiff ? undefined : styles.disabledDropdownIcon}> |
| 149 | + {isDiffOpen ? <CloseDropdown /> : <OpenDropdown />} |
| 150 | + </div> |
| 151 | + </Stack> |
| 152 | + |
| 153 | + {shouldDisplayDiff && ( |
| 154 | + <Collapse in={isDiffOpen}> |
| 155 | + <AuditDiff diff={auditLog.diff} /> |
| 156 | + </Collapse> |
| 157 | + )} |
| 158 | + </TableCell> |
| 159 | + </TableRow> |
| 160 | + ) |
| 161 | +} |
| 162 | + |
| 163 | +const useStyles = makeStyles((theme) => ({ |
| 164 | + auditLogCell: { |
| 165 | + padding: "0 !important", |
| 166 | + }, |
| 167 | + |
| 168 | + auditLogRow: { |
| 169 | + padding: theme.spacing(2, 4), |
| 170 | + }, |
| 171 | + |
| 172 | + auditLogRowInfo: { |
| 173 | + flex: 1, |
| 174 | + }, |
| 175 | + |
| 176 | + auditLogResume: { |
| 177 | + ...theme.typography.body1, |
| 178 | + fontFamily: "inherit", |
| 179 | + display: "block", |
| 180 | + }, |
| 181 | + |
| 182 | + auditLogTime: { |
| 183 | + ...theme.typography.body2, |
| 184 | + fontFamily: "inherit", |
| 185 | + color: theme.palette.text.secondary, |
| 186 | + display: "block", |
| 187 | + }, |
| 188 | + |
| 189 | + auditLogExtraInfo: { |
| 190 | + ...theme.typography.body2, |
| 191 | + fontFamily: "inherit", |
| 192 | + color: theme.palette.text.secondary, |
| 193 | + }, |
| 194 | + |
| 195 | + disabledDropdownIcon: { |
| 196 | + opacity: 0.5, |
| 197 | + }, |
| 198 | +})) |
0 commit comments