Skip to content

chore: use emotion for styling (pt. 3) #10026

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 16 commits into from
Oct 5, 2023
Prev Previous commit
Next Next commit
emotion: ResourceCard
  • Loading branch information
aslilac committed Oct 3, 2023
commit d995bcd5719462c1620a24f057dce3f115d6210e
164 changes: 81 additions & 83 deletions site/src/components/Resources/ResourceCard.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,66 @@
import { makeStyles } from "@mui/styles";
import { FC, useState } from "react";
import { type FC, useState } from "react";
import IconButton from "@mui/material/IconButton";
import Tooltip from "@mui/material/Tooltip";
import { type CSSObject, type Interpolation, type Theme } from "@emotion/react";
import { WorkspaceAgent, WorkspaceResource } from "api/typesGenerated";
import { DropdownArrow } from "components/DropdownArrow/DropdownArrow";
import { CopyableValue } from "components/CopyableValue/CopyableValue";
import { Stack } from "../Stack/Stack";
import { ResourceAvatar } from "./ResourceAvatar";
import { SensitiveValue } from "./SensitiveValue";
import { DropdownArrow } from "components/DropdownArrow/DropdownArrow";
import IconButton from "@mui/material/IconButton";
import Tooltip from "@mui/material/Tooltip";
import { CopyableValue } from "components/CopyableValue/CopyableValue";
import { type Theme } from "@mui/material/styles";

const styles = {
resourceCard: (theme) => ({
background: theme.palette.background.paper,
borderRadius: theme.shape.borderRadius,
border: `1px solid ${theme.palette.divider}`,

"&:not(:first-of-type)": {
borderTop: 0,
borderTopLeftRadius: 0,
borderTopRightRadius: 0,
},

"&:not(:last-child)": {
borderBottomLeftRadius: 0,
borderBottomRightRadius: 0,
},
}),

resourceCardProfile: {
flexShrink: 0,
width: "fit-content",
},

resourceCardHeader: (theme) => ({
padding: theme.spacing(3, 4),
borderBottom: `1px solid ${theme.palette.divider}`,

"&:last-child": {
borderBottom: 0,
},
}),

metadata: (theme) => ({
...(theme.typography.body2 as CSSObject),
lineHeight: "120%",
}),

metadataLabel: (theme) => ({
fontSize: 12,
color: theme.palette.text.secondary,
textOverflow: "ellipsis",
overflow: "hidden",
whiteSpace: "nowrap",
}),

metadataValue: (theme) => ({
textOverflow: "ellipsis",
overflow: "hidden",
whiteSpace: "nowrap",
...(theme.typography.body1 as CSSObject),
}),
} satisfies Record<string, Interpolation<Theme>>;

export interface ResourceCardProps {
resource: WorkspaceResource;
Expand All @@ -30,44 +82,53 @@ export const ResourceCard: FC<ResourceCardProps> = ({ resource, agentRow }) => {
metadataLength += 1;
visibleMetadata.pop();
}
const styles = useStyles({ metadataLength });

return (
<div key={resource.id} className={`${styles.resourceCard} resource-card`}>
<Stack
direction="row"
alignItems="flex-start"
className={styles.resourceCardHeader}
css={styles.resourceCardHeader}
spacing={10}
>
<Stack
direction="row"
alignItems="center"
className={styles.resourceCardProfile}
css={styles.resourceCardProfile}
>
<div>
<ResourceAvatar resource={resource} />
</div>
<div className={styles.metadata}>
<div className={styles.metadataLabel}>{resource.type}</div>
<div className={styles.metadataValue}>{resource.name}</div>
<div css={styles.metadata}>
<div css={styles.metadataLabel}>{resource.type}</div>
<div css={styles.metadataValue}>{resource.name}</div>
</div>
</Stack>

<div className={styles.metadataHeader}>
<div
css={(theme) => ({
flexGrow: 2,
display: "grid",
gridTemplateColumns: `repeat(${
metadataLength === 1 ? 1 : 4
}, minmax(0, 1fr))`,
gap: theme.spacing(5),
rowGap: theme.spacing(3),
})}
>
{resource.daily_cost > 0 && (
<div className={styles.metadata}>
<div className={styles.metadataLabel}>
<div css={styles.metadata}>
<div css={styles.metadataLabel}>
<b>cost</b>
</div>
<div className={styles.metadataValue}>{resource.daily_cost}</div>
<div css={styles.metadataValue}>{resource.daily_cost}</div>
</div>
)}
{visibleMetadata.map((meta) => {
return (
<div className={styles.metadata} key={meta.key}>
<div className={styles.metadataLabel}>{meta.key}</div>
<div className={styles.metadataValue}>
<div css={styles.metadata} key={meta.key}>
<div css={styles.metadataLabel}>{meta.key}</div>
<div css={styles.metadataValue}>
{meta.sensitive ? (
<SensitiveValue value={meta.value} />
) : (
Expand Down Expand Up @@ -104,66 +165,3 @@ export const ResourceCard: FC<ResourceCardProps> = ({ resource, agentRow }) => {
</div>
);
};

const useStyles = makeStyles<Theme, { metadataLength: number }>((theme) => ({
resourceCard: {
background: theme.palette.background.paper,
borderRadius: theme.shape.borderRadius,
border: `1px solid ${theme.palette.divider}`,

"&:not(:first-of-type)": {
borderTop: 0,
borderTopLeftRadius: 0,
borderTopRightRadius: 0,
},

"&:not(:last-child)": {
borderBottomLeftRadius: 0,
borderBottomRightRadius: 0,
},
},

resourceCardProfile: {
flexShrink: 0,
width: "fit-content",
},

resourceCardHeader: {
padding: theme.spacing(3, 4),
borderBottom: `1px solid ${theme.palette.divider}`,

"&:last-child": {
borderBottom: 0,
},
},

metadataHeader: (props) => ({
flexGrow: 2,
display: "grid",
gridTemplateColumns: `repeat(${
props.metadataLength === 1 ? 1 : 4
}, minmax(0, 1fr))`,
gap: theme.spacing(5),
rowGap: theme.spacing(3),
}),

metadata: {
...theme.typography.body2,
lineHeight: "120%",
},

metadataLabel: {
fontSize: 12,
color: theme.palette.text.secondary,
textOverflow: "ellipsis",
overflow: "hidden",
whiteSpace: "nowrap",
},

metadataValue: {
textOverflow: "ellipsis",
overflow: "hidden",
whiteSpace: "nowrap",
...theme.typography.body1,
},
}));