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

Skip to content

refactor: avoid @emotion/css when possible #10807

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 5 commits into from
Nov 21, 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
57 changes: 29 additions & 28 deletions site/src/components/CopyButton/CopyButton.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import IconButton from "@mui/material/Button";
import Tooltip from "@mui/material/Tooltip";
import Check from "@mui/icons-material/Check";
import { css, type Interpolation, type Theme } from "@emotion/react";
import { type FC, type ReactNode } from "react";
import { useClipboard } from "hooks/useClipboard";
import { css } from "@emotion/react";
import { FileCopyIcon } from "../Icons/FileCopyIcon";

interface CopyButtonProps {
children?: ReactNode;
text: string;
ctaCopy?: string;
wrapperClassName?: string;
buttonClassName?: string;
wrapperStyles?: Interpolation<Theme>;
buttonStyles?: Interpolation<Theme>;
tooltipTitle?: string;
}

Expand All @@ -21,52 +23,51 @@ export const Language = {
/**
* Copy button used inside the CodeBlock component internally
*/
export const CopyButton: React.FC<React.PropsWithChildren<CopyButtonProps>> = ({
export const CopyButton: FC<CopyButtonProps> = ({
text,
ctaCopy,
wrapperClassName = "",
buttonClassName = "",
wrapperStyles,
buttonStyles,
tooltipTitle = Language.tooltipTitle,
}) => {
const { isCopied, copy: copyToClipboard } = useClipboard(text);

const fileCopyIconStyles = css`
width: 20px;
height: 20px;
`;

return (
<Tooltip title={tooltipTitle} placement="top">
<div
className={wrapperClassName}
css={{
display: "flex",
}}
>
<div css={[{ display: "flex" }, wrapperStyles]}>
<IconButton
className={buttonClassName}
css={(theme) => css`
border-radius: 8px;
padding: 8px;
min-width: 32px;
css={[
(theme) => css`
border-radius: 8px;
padding: 8px;
min-width: 32px;

&:hover {
background: ${theme.palette.background.paper};
}
`}
&:hover {
background: ${theme.palette.background.paper};
}
`,
buttonStyles,
]}
onClick={copyToClipboard}
size="small"
aria-label={Language.ariaLabel}
variant="text"
>
{isCopied ? (
<Check css={fileCopyIconStyles} />
<Check css={styles.copyIcon} />
) : (
<FileCopyIcon css={fileCopyIconStyles} />
<FileCopyIcon css={styles.copyIcon} />
)}
{ctaCopy && <div css={{ marginLeft: 8 }}>{ctaCopy}</div>}
</IconButton>
</div>
</Tooltip>
);
};

const styles = {
copyIcon: css`
width: 20px;
height: 20px;
`,
} satisfies Record<string, Interpolation<Theme>>;
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import type {
} from "api/typesGenerated";
import {
type FC,
type PropsWithChildren,
useMemo,
useEffect,
useState,
PropsWithChildren,
} from "react";
import prettyBytes from "pretty-bytes";
import BuildingIcon from "@mui/icons-material/Build";
Expand All @@ -23,7 +23,6 @@ import WebTerminalIcon from "@mui/icons-material/WebAsset";
import CollectedIcon from "@mui/icons-material/Compare";
import RefreshIcon from "@mui/icons-material/Refresh";
import Button from "@mui/material/Button";
import { css as className } from "@emotion/css";
import {
css,
type CSSObject,
Expand All @@ -40,44 +39,23 @@ import { getDisplayWorkspaceStatus } from "utils/workspace";
import { colors } from "theme/colors";
import { HelpTooltipTitle } from "components/HelpTooltip/HelpTooltip";
import { Stack } from "components/Stack/Stack";
import { type ClassName, useClassName } from "hooks/useClassName";

export const bannerHeight = 36;

const styles = {
group: css`
display: flex;
align-items: center;
`,
category: (theme) => ({
marginRight: 16,
color: theme.palette.text.primary,
}),
values: (theme) => ({
display: "flex",
gap: 8,
color: theme.palette.text.secondary,
}),
value: css`
display: flex;
align-items: center;
gap: 4px;

& svg {
width: 12px;
height: 12px;
}
`,
} satisfies Record<string, Interpolation<Theme>>;

export interface DeploymentBannerViewProps {
health?: HealthcheckReport;
stats?: DeploymentStats;
fetchStats?: () => void;
}

export const DeploymentBannerView: FC<DeploymentBannerViewProps> = (props) => {
const { health, stats, fetchStats } = props;
export const DeploymentBannerView: FC<DeploymentBannerViewProps> = ({
health,
stats,
fetchStats,
}) => {
const theme = useTheme();
const summaryTooltip = useClassName(classNames.summaryTooltip, []);

const aggregatedMinutes = useMemo(() => {
if (!stats) {
Expand Down Expand Up @@ -114,6 +92,7 @@ export const DeploymentBannerView: FC<DeploymentBannerViewProps> = (props) => {
clearTimeout(timeout);
};
}, [fetchStats, stats]);

const lastAggregated = useMemo(() => {
if (!stats) {
return;
Expand All @@ -127,34 +106,6 @@ export const DeploymentBannerView: FC<DeploymentBannerViewProps> = (props) => {
}, [timeUntilRefresh, stats]);

const unhealthy = health && !health.healthy;

const statusBadgeStyle = css`
display: flex;
align-items: center;
justify-content: center;
background-color: ${unhealthy ? colors.red[10] : undefined};
padding: 0 12px;
height: 100%;
color: #fff;

& svg {
width: 16px;
height: 16px;
}
`;

const statusSummaryStyle = className`
${theme.typography.body2 as CSSObject}

margin: 0 0 4px 12px;
width: 400px;
padding: 16px;
color: ${theme.palette.text.primary};
background-color: ${theme.palette.background.paper};
border: 1px solid ${theme.palette.divider};
pointer-events: none;
`;

const displayLatency = stats?.workspaces.connection_latency_ms.P50 || -1;

return (
Expand All @@ -178,7 +129,7 @@ export const DeploymentBannerView: FC<DeploymentBannerViewProps> = (props) => {
}}
>
<Tooltip
classes={{ tooltip: statusSummaryStyle }}
classes={{ tooltip: summaryTooltip }}
title={
unhealthy ? (
<>
Expand Down Expand Up @@ -214,11 +165,15 @@ export const DeploymentBannerView: FC<DeploymentBannerViewProps> = (props) => {
css={{ marginRight: -16 }}
>
{unhealthy ? (
<Link component={RouterLink} to="/health" css={statusBadgeStyle}>
<Link
component={RouterLink}
to="/health"
css={[styles.statusBadge, styles.unhealthy]}
>
<ErrorIcon />
</Link>
) : (
<div css={statusBadgeStyle}>
<div css={styles.statusBadge}>
<RocketIcon />
</div>
)}
Expand Down Expand Up @@ -380,19 +335,15 @@ export const DeploymentBannerView: FC<DeploymentBannerViewProps> = (props) => {
);
};

const ValueSeparator: FC = () => {
const theme = useTheme();
const separatorStyles = css`
color: ${theme.palette.text.disabled};
`;

return <div css={separatorStyles}>/</div>;
};

const WorkspaceBuildValue: FC<{
interface WorkspaceBuildValueProps {
status: WorkspaceStatus;
count?: number;
}> = ({ status, count }) => {
}

const WorkspaceBuildValue: FC<WorkspaceBuildValueProps> = ({
status,
count,
}) => {
const displayStatus = getDisplayWorkspaceStatus(status);
let statusText = displayStatus.text;
let icon = displayStatus.icon;
Expand All @@ -416,6 +367,10 @@ const WorkspaceBuildValue: FC<{
);
};

const ValueSeparator: FC = () => {
return <div css={styles.separator}>/</div>;
};

const HealthIssue: FC<PropsWithChildren> = ({ children }) => {
return (
<Stack direction="row" spacing={1} alignItems="center">
Expand All @@ -424,3 +379,62 @@ const HealthIssue: FC<PropsWithChildren> = ({ children }) => {
</Stack>
);
};

const classNames = {
summaryTooltip: (css, theme) => css`
${theme.typography.body2 as CSSObject}

margin: 0 0 4px 12px;
width: 400px;
padding: 16px;
color: ${theme.palette.text.primary};
background-color: ${theme.palette.background.paper};
border: 1px solid ${theme.palette.divider};
pointer-events: none;
`,
} satisfies Record<string, ClassName>;

const styles = {
statusBadge: css`
display: flex;
align-items: center;
justify-content: center;
padding: 0 12px;
height: 100%;
color: #fff;

& svg {
width: 16px;
height: 16px;
}
`,
unhealthy: css`
background-color: ${colors.red[10]};
`,
group: css`
display: flex;
align-items: center;
`,
category: (theme) => ({
marginRight: 16,
color: theme.palette.text.primary,
}),
values: (theme) => ({
display: "flex",
gap: 8,
color: theme.palette.text.secondary,
}),
value: css`
display: flex;
align-items: center;
gap: 4px;

& svg {
width: 12px;
height: 12px;
}
`,
separator: (theme) => ({
color: theme.palette.text.disabled,
}),
} satisfies Record<string, Interpolation<Theme>>;
33 changes: 17 additions & 16 deletions site/src/components/ErrorBoundary/RuntimeErrorState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import Link from "@mui/material/Link";
import RefreshOutlined from "@mui/icons-material/RefreshOutlined";
import { type FC, useEffect, useState } from "react";
import { Helmet } from "react-helmet-async";
import { css } from "@emotion/css";
import { type Interpolation, type Theme } from "@emotion/react";
import { css, type Interpolation, type Theme } from "@emotion/react";
import type { BuildInfoResponse } from "api/typesGenerated";
import { CopyButton } from "components/CopyButton/CopyButton";
import { CoderIcon } from "components/Icons/CoderIcon";
Expand Down Expand Up @@ -97,20 +96,7 @@ export const RuntimeErrorState: FC<RuntimeErrorStateProps> = ({ error }) => {
<div css={styles.stackHeader}>
Stacktrace
<CopyButton
buttonClassName={css`
background-color: transparent;
border: 0;
border-radius: 999px;
min-height: 32px;
min-width: 32px;
height: 32px;
width: 32px;

& svg {
width: 16px;
height: 16px;
}
`}
buttonStyles={styles.copyButton}
text={error.stack}
tooltipTitle="Copy stacktrace"
/>
Expand Down Expand Up @@ -209,4 +195,19 @@ const styles = {
fontSize: 12,
color: theme.palette.text.secondary,
}),

copyButton: css`
background-color: transparent;
border: 0;
border-radius: 999px;
min-height: 32px;
min-width: 32px;
height: 32px;
width: 32px;

& svg {
width: 16px;
height: 16px;
}
`,
} satisfies Record<string, Interpolation<Theme>>;
Loading