Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c182357

Browse files
committed
general cleanup
1 parent 0125d4f commit c182357

File tree

11 files changed

+117
-113
lines changed

11 files changed

+117
-113
lines changed

site/src/components/AuditLogRow/AuditLogDescription.tsx

Lines changed: 0 additions & 91 deletions
This file was deleted.

site/src/components/AuditLogRow/AuditLogDescription.test.tsx renamed to site/src/components/AuditLogRow/AuditLogDescription/AuditLogDescription.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import {
77
MockAuditLogUnsuccessfulLoginUnknownUser,
88
} from "testHelpers/entities"
99
import { AuditLogDescription } from "./AuditLogDescription"
10-
import { AuditLogRow } from "./AuditLogRow"
11-
import { render } from "../../testHelpers/renderHelpers"
10+
import { AuditLogRow } from "../AuditLogRow"
11+
import { render } from "../../../testHelpers/renderHelpers"
1212
import { screen } from "@testing-library/react"
1313

1414
const getByTextContent = (text: string) => {
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { FC } from "react"
2+
import { AuditLog } from "api/typesGenerated"
3+
import { Link as RouterLink } from "react-router-dom"
4+
import Link from "@material-ui/core/Link"
5+
import { Trans, useTranslation } from "react-i18next"
6+
import { BuildAuditDescription } from "./BuildAuditDescription"
7+
8+
export const AuditLogDescription: FC<{ auditLog: AuditLog }> = ({
9+
auditLog,
10+
}): JSX.Element => {
11+
const { t } = useTranslation("auditLog")
12+
13+
let target = auditLog.resource_target.trim()
14+
const user = auditLog.user ? auditLog.user.username.trim() : "an unknown user"
15+
16+
if (auditLog.resource_type === "workspace_build") {
17+
return <BuildAuditDescription auditLog={auditLog} />
18+
}
19+
20+
// SSH key entries have no links
21+
if (auditLog.resource_type === "git_ssh_key") {
22+
target = ""
23+
}
24+
25+
const truncatedDescription = auditLog.description
26+
.replace("{user}", `${user}`)
27+
.replace("{target}", "")
28+
29+
// logs for workspaces created on behalf of other users indicate ownership in the description
30+
const onBehalfOf =
31+
auditLog.additional_fields.workspace_owner &&
32+
auditLog.additional_fields.workspace_owner !== "unknown" &&
33+
auditLog.additional_fields.workspace_owner !== auditLog.user?.username &&
34+
`on behalf of ${auditLog.additional_fields.workspace_owner}`
35+
36+
if (auditLog.resource_link) {
37+
return (
38+
<span>
39+
<Trans
40+
t={t}
41+
i18nKey="table.logRow.description.linkedAuditDescription"
42+
values={{ truncatedDescription, target, onBehalfOf }}
43+
>
44+
{"{{truncatedDescription}}"}
45+
<Link component={RouterLink} to={auditLog.resource_link}>
46+
<strong>{"{{target}}"}</strong>
47+
</Link>
48+
{"{{onBehalfOf}}"}
49+
</Trans>
50+
</span>
51+
)
52+
}
53+
54+
return (
55+
<span>
56+
<Trans
57+
t={t}
58+
i18nKey="table.logRow.description.unlinkedAuditDescription"
59+
values={{ truncatedDescription, target, onBehalfOf }}
60+
>
61+
{"{{truncatedDescription}}"}
62+
<strong>{"{{target}}"}</strong>
63+
{"{{onBehalfOf}}"}
64+
</Trans>
65+
</span>
66+
)
67+
}

site/src/components/AuditLogRow/BuildAuditDescription.tsx renamed to site/src/components/AuditLogRow/AuditLogDescription/BuildAuditDescription.tsx

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export const BuildAuditDescription: FC<{ auditLog: AuditLog }> = ({
99
}): JSX.Element => {
1010
const { t } = useTranslation("auditLog")
1111

12-
// audit logs with a resource_type of workspace build use workspace name as a target
1312
const workspaceName = auditLog.additional_fields?.workspace_name?.trim()
1413
// workspaces can be started/stopped by a user, or kicked off automatically by Coder
1514
const user =
@@ -20,18 +19,33 @@ export const BuildAuditDescription: FC<{ auditLog: AuditLog }> = ({
2019

2120
const action = auditLog.action === "start" ? "started" : "stopped"
2221

22+
if (auditLog.resource_link) {
23+
return (
24+
<span>
25+
<Trans
26+
t={t}
27+
i18nKey="table.logRow.description.linkedWorkspaceBuild"
28+
values={{ user, action, workspaceName }}
29+
>
30+
{"{{user}}"}
31+
<Link component={RouterLink} to={auditLog.resource_link}>
32+
{"{{action}}"}
33+
</Link>
34+
workspace{"{{workspaceName}}"}
35+
</Trans>
36+
</span>
37+
)
38+
}
39+
2340
return (
2441
<span>
2542
<Trans
2643
t={t}
27-
i18nKey="table.logRow.workspaceBuild"
44+
i18nKey="table.logRow.description.unlinkedWorkspaceBuild"
2845
values={{ user, action, workspaceName }}
2946
>
3047
{"{{user}}"}
31-
<Link component={RouterLink} to={auditLog.resource_link}>
32-
{"{{action}}"}
33-
</Link>
34-
workspace{"{{workspaceName}}"}
48+
{"{{action}}"}workspace{"{{workspaceName}}"}
3549
</Trans>
3650
</span>
3751
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {AuditLogDescription} from "./AuditLogDescription"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export {AuditLogDiff} from './AuditLogDiff'
2+
export {determineGroupDiff} from './auditUtils'

site/src/components/AuditLogRow/AuditLogRow.tsx

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ import { UserAvatar } from "components/UserAvatar/UserAvatar"
1313
import { useState } from "react"
1414
import { PaletteIndex } from "theme/palettes"
1515
import userAgentParser from "ua-parser-js"
16-
import { AuditLogDiff } from "./AuditLogDiff"
17-
import i18next from "i18next"
16+
import { AuditLogDiff, determineGroupDiff } from "./AuditLogDiff"
17+
import { useTranslation } from "react-i18next"
1818
import { AuditLogDescription } from "./AuditLogDescription"
19-
import { determineGroupDiff } from "./auditUtils"
2019

2120
const httpStatusColor = (httpStatus: number): PaletteIndex => {
2221
if (httpStatus >= 300 && httpStatus < 500) {
@@ -41,14 +40,14 @@ export const AuditLogRow: React.FC<AuditLogRowProps> = ({
4140
defaultIsDiffOpen = false,
4241
}) => {
4342
const styles = useStyles()
44-
const { t } = i18next
43+
const { t } = useTranslation("auditLog")
4544
const [isDiffOpen, setIsDiffOpen] = useState(defaultIsDiffOpen)
4645
const diffs = Object.entries(auditLog.diff)
4746
const shouldDisplayDiff = diffs.length > 0
4847
const { os, browser } = userAgentParser(auditLog.user_agent)
4948
const displayBrowserInfo = browser.name
5049
? `${browser.name} ${browser.version}`
51-
: t("auditLog:table.logRow.notAvailable")
50+
: t("table.logRow.notAvailable")
5251

5352
let auditDiff = auditLog.diff
5453

@@ -110,6 +109,11 @@ export const AuditLogRow: React.FC<AuditLogRowProps> = ({
110109
spacing={1}
111110
>
112111
<AuditLogDescription auditLog={auditLog} />
112+
{auditLog.is_deleted && (
113+
<span className={styles.deletedLabel}>
114+
<>{t("table.logRow.deletedLabel")}</>
115+
</span>
116+
)}
113117
<span className={styles.auditLogTime}>
114118
{new Date(auditLog.time).toLocaleTimeString()}
115119
</span>
@@ -119,23 +123,23 @@ export const AuditLogRow: React.FC<AuditLogRowProps> = ({
119123
<Stack direction="row" spacing={1} alignItems="baseline">
120124
{auditLog.ip && (
121125
<span className={styles.auditLogInfo}>
122-
<>{t("auditLog:table.logRow.ip")}</>
126+
<>{t("table.logRow.ip")}</>
123127
<strong>{auditLog.ip}</strong>
124128
</span>
125129
)}
126130

127131
<span className={styles.auditLogInfo}>
128-
<>{t("auditLog:table.logRow.os")}</>
132+
<>{t("table.logRow.os")}</>
129133
<strong>
130134
{os.name
131135
? os.name
132136
: // https://github.com/i18next/next-i18next/issues/1795
133-
t<string>("auditLog:table.logRow.notAvailable")}
137+
t<string>("table.logRow.notAvailable")}
134138
</strong>
135139
</span>
136140

137141
<span className={styles.auditLogInfo}>
138-
<>{t("auditLog:table.logRow.browser")}</>
142+
<>{t("table.logRow.browser")}</>
139143
<strong>{displayBrowserInfo}</strong>
140144
</span>
141145
</Stack>
@@ -215,4 +219,9 @@ const useStyles = makeStyles((theme) => ({
215219
paddingRight: 10,
216220
fontWeight: 600,
217221
},
222+
223+
deletedLabel: {
224+
...theme.typography.caption,
225+
color: theme.palette.text.secondary,
226+
},
218227
}))

site/src/i18n/en/auditLog.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@
88
"emptyPage": "No audit logs available on this page",
99
"noLogs": "No audit logs available",
1010
"logRow": {
11+
"description": {
12+
"linkedWorkspaceBuild": "{{user}} <1>{{action}}</1> workspace <strong>{{workspaceName}}</strong>",
13+
"unlinkedWorkspaceBuild": "{{user}} {{action}} workspace <strong>{{workspaceName}}</strong>",
14+
"linkedAuditDescription": "{{truncatedDescription}} <1>{{target}}</1> {{onBehalfOf}}",
15+
"unlinkedAuditDescription": "{{truncatedDescription}} {{target}} {{onBehalfOf}}"
16+
},
1117
"deletedLabel": " (deleted)",
1218
"ip": "IP: ",
1319
"os": "OS: ",
14-
"browser": "Browser: ",
15-
"onBehalfOf": " on behalf of {{owner}}",
16-
"unknownUser": "an unknown user",
17-
"workspaceBuild": "{{user}} <1>{{action}}</1> workspace <strong>{{workspaceName}}</strong>",
18-
"auditDescription": "{{truncatedDescription}} <1>{{target}}</1>"
20+
"browser": "Browser: "
1921
}
2022
},
2123
"paywall": {

0 commit comments

Comments
 (0)