Skip to content

Commit 55dd4b0

Browse files
committed
Refactor last seen table value
1 parent 3b1ecd3 commit 55dd4b0

File tree

4 files changed

+48
-12
lines changed

4 files changed

+48
-12
lines changed

site/src/components/UsersTable/UsersTable.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,8 @@ export const UsersTable: FC<React.PropsWithChildren<UsersTableProps>> = ({
7070
<UserRoleHelpTooltip />
7171
</Stack>
7272
</TableCell>
73-
<TableCell width="10%">{Language.loginTypeLabel}</TableCell>
74-
<TableCell width="10%">{Language.statusLabel}</TableCell>
75-
<TableCell width="10%">{Language.lastSeenLabel}</TableCell>
76-
73+
<TableCell width="15%">{Language.loginTypeLabel}</TableCell>
74+
<TableCell width="15%">{Language.statusLabel}</TableCell>
7775
{/* 1% is a trick to make the table cell width fit the content */}
7876
{canEditUsers && <TableCell width="1%" />}
7977
</TableRow>

site/src/components/UsersTable/UsersTableBody.tsx

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import Box from "@mui/material/Box"
2-
import { makeStyles } from "@mui/styles"
1+
import Box, { BoxProps } from "@mui/material/Box"
2+
import { makeStyles, useTheme } from "@mui/styles"
33
import TableCell from "@mui/material/TableCell"
44
import TableRow from "@mui/material/TableRow"
55
import { ChooseOne, Cond } from "components/Conditionals/ChooseOne"
6-
import { LastUsed } from "components/LastUsed/LastUsed"
76
import { Pill } from "components/Pill/Pill"
87
import { FC } from "react"
98
import { useTranslation } from "react-i18next"
@@ -16,6 +15,8 @@ import { TableRowMenu } from "../TableRowMenu/TableRowMenu"
1615
import { EditRolesButton } from "components/EditRolesButton/EditRolesButton"
1716
import { Stack } from "components/Stack/Stack"
1817
import { EnterpriseBadge } from "components/DeploySettingsLayout/Badges"
18+
import dayjs from "dayjs"
19+
import { Theme } from "@mui/material/styles"
1920

2021
const isOwnerRole = (role: TypesGen.Role): boolean => {
2122
return role.name === "owner"
@@ -166,11 +167,10 @@ export const UsersTableBody: FC<
166167
: undefined,
167168
])}
168169
>
169-
{user.status}
170-
</TableCell>
171-
<TableCell>
172-
<LastUsed lastUsedAt={user.last_seen_at} />
170+
<Box>{user.status}</Box>
171+
<LastSeen value={user.last_seen_at} sx={{ fontSize: 12 }} />
173172
</TableCell>
173+
174174
{canEditUsers && (
175175
<TableCell>
176176
<TableRowMenu
@@ -236,6 +236,44 @@ export const UsersTableBody: FC<
236236
)
237237
}
238238

239+
export const LastSeen = ({
240+
value,
241+
...boxProps
242+
}: { value: string } & BoxProps) => {
243+
const theme: Theme = useTheme()
244+
const t = dayjs(value)
245+
const now = dayjs()
246+
247+
let message = t.fromNow()
248+
let color = theme.palette.text.secondary
249+
250+
if (t.isAfter(now.subtract(1, "hour"))) {
251+
color = theme.palette.success.light
252+
// Since the agent reports on a 10m interval,
253+
// the last_used_at can be inaccurate when recent.
254+
message = "Now"
255+
} else if (t.isAfter(now.subtract(3, "day"))) {
256+
color = theme.palette.text.secondary
257+
} else if (t.isAfter(now.subtract(1, "month"))) {
258+
color = theme.palette.warning.light
259+
} else if (t.isAfter(now.subtract(100, "year"))) {
260+
color = theme.palette.error.light
261+
} else {
262+
message = "Never"
263+
}
264+
265+
return (
266+
<Box
267+
component="span"
268+
data-chromatic="ignore"
269+
{...boxProps}
270+
sx={{ color, ...boxProps.sx }}
271+
>
272+
{message}
273+
</Box>
274+
)
275+
}
276+
239277
const useStyles = makeStyles((theme) => ({
240278
status: {
241279
textTransform: "capitalize",

site/src/pages/WorkspacesPage/WorkspacesTable.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import Box from "@mui/material/Box"
2727
import { AvatarData } from "components/AvatarData/AvatarData"
2828
import { Avatar } from "components/Avatar/Avatar"
2929
import { Stack } from "components/Stack/Stack"
30-
import { LastUsed } from "components/LastUsed/LastUsed"
30+
import { LastUsed } from "pages/WorkspacesPage/LastUsed"
3131
import { WorkspaceOutdatedTooltip } from "components/Tooltips"
3232
import { WorkspaceStatusBadge } from "components/WorkspaceStatusBadge/WorkspaceStatusBadge"
3333
import { getDisplayWorkspaceTemplateName } from "utils/workspace"

0 commit comments

Comments
 (0)