Skip to content

refactor(site): improve the overall user table design #9342

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions site/src/components/EditRolesButton/EditRolesButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const Option: React.FC<{
onChange(e.currentTarget.value)
}}
/>
<Stack spacing={0.5}>
<Stack spacing={0}>
<strong>{name}</strong>
<span className={styles.optionDescription}>{description}</span>
</Stack>
Expand Down Expand Up @@ -142,7 +142,7 @@ export const EditRolesButton: FC<EditRolesButtonProps> = ({
<div className={styles.footer}>
<Stack direction="row" alignItems="flex-start">
<UserIcon className={styles.userIcon} />
<Stack spacing={0.5}>
<Stack spacing={0}>
<strong>{t("member")}</strong>
<span className={styles.optionDescription}>
{t("roleDescription.member")}
Expand Down Expand Up @@ -182,14 +182,15 @@ const useStyles = makeStyles((theme) => ({
padding: 0,

"&:disabled": {
opacity: 0.5,
opacity: 0,
},
},
options: {
padding: theme.spacing(3),
},
option: {
cursor: "pointer",
fontSize: 14,
},
checkbox: {
padding: 0,
Expand All @@ -202,13 +203,15 @@ const useStyles = makeStyles((theme) => ({
},
},
optionDescription: {
fontSize: 12,
fontSize: 13,
color: theme.palette.text.secondary,
lineHeight: "160%",
},
footer: {
padding: theme.spacing(3),
backgroundColor: theme.palette.background.paper,
borderTop: `1px solid ${theme.palette.divider}`,
fontSize: 14,
},
userIcon: {
width: theme.spacing(2.5), // Same as the checkbox
Expand Down
113 changes: 60 additions & 53 deletions site/src/components/UsersTable/UsersTable.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,73 +1,80 @@
import { ComponentMeta, Story } from "@storybook/react"
import {
MockUser,
MockUser2,
MockAssignableSiteRoles,
MockAuthMethods,
} from "testHelpers/entities"
import { UsersTable, UsersTableProps } from "./UsersTable"
import { UsersTable } from "./UsersTable"
import type { Meta, StoryObj } from "@storybook/react"

export default {
const meta: Meta<typeof UsersTable> = {
title: "components/UsersTable",
component: UsersTable,
args: {
isNonInitialPage: false,
authMethods: MockAuthMethods,
},
} as ComponentMeta<typeof UsersTable>
}

const Template: Story<UsersTableProps> = (args) => <UsersTable {...args} />
export default meta
type Story = StoryObj<typeof UsersTable>

export const Example = Template.bind({})
Example.args = {
users: [MockUser, MockUser2],
roles: MockAssignableSiteRoles,
canEditUsers: false,
export const Example: Story = {
args: {
users: [MockUser, MockUser2],
roles: MockAssignableSiteRoles,
canEditUsers: false,
},
}

export const Editable = Template.bind({})
Editable.args = {
users: [
MockUser,
MockUser2,
{
...MockUser,
username: "John Doe",
email: "john.doe@coder.com",
roles: [],
status: "dormant",
},
{
...MockUser,
username: "Roger Moore",
email: "roger.moore@coder.com",
roles: [],
status: "suspended",
},
{
...MockUser,
username: "OIDC User",
email: "oidc.user@coder.com",
roles: [],
status: "active",
login_type: "oidc",
},
],
roles: MockAssignableSiteRoles,
canEditUsers: true,
canViewActivity: true,
export const Editable: Story = {
args: {
users: [
MockUser,
MockUser2,
{
...MockUser,
username: "John Doe",
email: "john.doe@coder.com",
roles: [],
status: "dormant",
},
{
...MockUser,
username: "Roger Moore",
email: "roger.moore@coder.com",
roles: [],
status: "suspended",
},
{
...MockUser,
username: "OIDC User",
email: "oidc.user@coder.com",
roles: [],
status: "active",
login_type: "oidc",
},
],
roles: MockAssignableSiteRoles,
canEditUsers: true,
canViewActivity: true,
},
}

export const Empty = Template.bind({})
Empty.args = {
users: [],
roles: MockAssignableSiteRoles,
export const Empty: Story = {
args: {
users: [],
roles: MockAssignableSiteRoles,
},
}

export const Loading = Template.bind({})
Loading.args = {
users: [],
roles: MockAssignableSiteRoles,
isLoading: true,
}
Loading.parameters = {
chromatic: { pauseAnimationAtEnd: true },
export const Loading: Story = {
args: {
users: [],
roles: MockAssignableSiteRoles,
isLoading: true,
},
parameters: {
chromatic: { pauseAnimationAtEnd: true },
},
}
9 changes: 5 additions & 4 deletions site/src/components/UsersTable/UsersTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export interface UsersTableProps {
isNonInitialPage: boolean
actorID: string
oidcRoleSyncEnabled: boolean
authMethods?: TypesGen.AuthMethods
}

export const UsersTable: FC<React.PropsWithChildren<UsersTableProps>> = ({
Expand All @@ -57,6 +58,7 @@ export const UsersTable: FC<React.PropsWithChildren<UsersTableProps>> = ({
isNonInitialPage,
actorID,
oidcRoleSyncEnabled,
authMethods,
}) => {
return (
<TableContainer>
Expand All @@ -70,10 +72,8 @@ export const UsersTable: FC<React.PropsWithChildren<UsersTableProps>> = ({
<UserRoleHelpTooltip />
</Stack>
</TableCell>
<TableCell width="10%">{Language.loginTypeLabel}</TableCell>
<TableCell width="10%">{Language.statusLabel}</TableCell>
<TableCell width="10%">{Language.lastSeenLabel}</TableCell>

<TableCell width="15%">{Language.loginTypeLabel}</TableCell>
<TableCell width="15%">{Language.statusLabel}</TableCell>
{/* 1% is a trick to make the table cell width fit the content */}
{canEditUsers && <TableCell width="1%" />}
</TableRow>
Expand All @@ -96,6 +96,7 @@ export const UsersTable: FC<React.PropsWithChildren<UsersTableProps>> = ({
isNonInitialPage={isNonInitialPage}
actorID={actorID}
oidcRoleSyncEnabled={oidcRoleSyncEnabled}
authMethods={authMethods}
/>
</TableBody>
</Table>
Expand Down
113 changes: 104 additions & 9 deletions site/src/components/UsersTable/UsersTableBody.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import Box from "@mui/material/Box"
import { makeStyles } from "@mui/styles"
import Box, { BoxProps } from "@mui/material/Box"
import { makeStyles, useTheme } from "@mui/styles"
import TableCell from "@mui/material/TableCell"
import TableRow from "@mui/material/TableRow"
import { ChooseOne, Cond } from "components/Conditionals/ChooseOne"
import { LastUsed } from "components/LastUsed/LastUsed"
import { Pill } from "components/Pill/Pill"
import { FC } from "react"
import { useTranslation } from "react-i18next"
Expand All @@ -16,6 +15,16 @@ import { TableRowMenu } from "../TableRowMenu/TableRowMenu"
import { EditRolesButton } from "components/EditRolesButton/EditRolesButton"
import { Stack } from "components/Stack/Stack"
import { EnterpriseBadge } from "components/DeploySettingsLayout/Badges"
import dayjs from "dayjs"
import { SxProps, Theme } from "@mui/material/styles"
import HideSourceOutlined from "@mui/icons-material/HideSourceOutlined"
import KeyOutlined from "@mui/icons-material/KeyOutlined"
import GitHub from "@mui/icons-material/GitHub"
import PasswordOutlined from "@mui/icons-material/PasswordOutlined"
import relativeTime from "dayjs/plugin/relativeTime"
import ShieldOutlined from "@mui/icons-material/ShieldOutlined"

dayjs.extend(relativeTime)

const isOwnerRole = (role: TypesGen.Role): boolean => {
return role.name === "owner"
Expand All @@ -31,6 +40,7 @@ const sortRoles = (roles: TypesGen.Role[]) => {

interface UsersTableBodyProps {
users?: TypesGen.User[]
authMethods?: TypesGen.AuthMethods
roles?: TypesGen.AssignableRoles[]
isUpdatingUserRoles?: boolean
canEditUsers?: boolean
Expand Down Expand Up @@ -58,6 +68,7 @@ export const UsersTableBody: FC<
React.PropsWithChildren<UsersTableBodyProps>
> = ({
users,
authMethods,
roles,
onSuspendUser,
onDeleteUser,
Expand All @@ -80,7 +91,7 @@ export const UsersTableBody: FC<
return (
<ChooseOne>
<Cond condition={Boolean(isLoading)}>
<TableLoaderSkeleton columns={5} useAvatarData />
<TableLoaderSkeleton columns={canEditUsers ? 5 : 4} useAvatarData />
</Cond>
<Cond condition={!users || users.length === 0}>
<ChooseOne>
Expand Down Expand Up @@ -156,7 +167,10 @@ export const UsersTableBody: FC<
</Stack>
</TableCell>
<TableCell>
<pre>{user.login_type}</pre>
<LoginType
authMethods={authMethods!}
value={user.login_type}
/>
</TableCell>
<TableCell
className={combineClasses([
Expand All @@ -166,11 +180,10 @@ export const UsersTableBody: FC<
: undefined,
])}
>
{user.status}
</TableCell>
<TableCell>
<LastUsed lastUsedAt={user.last_seen_at} />
<Box>{user.status}</Box>
<LastSeen value={user.last_seen_at} sx={{ fontSize: 12 }} />
</TableCell>

{canEditUsers && (
<TableCell>
<TableRowMenu
Expand Down Expand Up @@ -236,6 +249,88 @@ export const UsersTableBody: FC<
)
}

const LoginType = ({
authMethods,
value,
}: {
authMethods: TypesGen.AuthMethods
value: TypesGen.LoginType
}) => {
let displayName = value as string
let icon = <></>
const iconStyles: SxProps = { width: 14, height: 14 }

if (value === "password") {
displayName = "Password"
icon = <PasswordOutlined sx={iconStyles} />
} else if (value === "none") {
displayName = "None"
icon = <HideSourceOutlined sx={iconStyles} />
} else if (value === "github") {
displayName = "GitHub"
icon = <GitHub sx={iconStyles} />
} else if (value === "token") {
displayName = "Token"
icon = <KeyOutlined sx={iconStyles} />
} else if (value === "oidc") {
displayName =
authMethods.oidc.signInText === "" ? "OIDC" : authMethods.oidc.signInText
icon =
authMethods.oidc.iconUrl === "" ? (
<ShieldOutlined sx={iconStyles} />
) : (
<Box
component="img"
alt="Open ID Connect icon"
src={authMethods.oidc.iconUrl}
sx={iconStyles}
/>
)
}

return (
<Box sx={{ display: "flex", alignItems: "center", gap: 1, fontSize: 14 }}>
{icon}
{displayName}
</Box>
)
}

const LastSeen = ({ value, ...boxProps }: { value: string } & BoxProps) => {
const theme: Theme = useTheme()
const t = dayjs(value)
const now = dayjs()

let message = t.fromNow()
let color = theme.palette.text.secondary

if (t.isAfter(now.subtract(1, "hour"))) {
color = theme.palette.success.light
// Since the agent reports on a 10m interval,
// the last_used_at can be inaccurate when recent.
message = "Now"
} else if (t.isAfter(now.subtract(3, "day"))) {
color = theme.palette.text.secondary
} else if (t.isAfter(now.subtract(1, "month"))) {
color = theme.palette.warning.light
} else if (t.isAfter(now.subtract(100, "year"))) {
color = theme.palette.error.light
} else {
message = "Never"
}

return (
<Box
component="span"
data-chromatic="ignore"
{...boxProps}
sx={{ color, ...boxProps.sx }}
>
{message}
</Box>
)
}

const useStyles = makeStyles((theme) => ({
status: {
textTransform: "capitalize",
Expand Down
Loading