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

Skip to content

Commit fe019ea

Browse files
committed
added update tooltip
1 parent bdb5cb3 commit fe019ea

File tree

6 files changed

+68
-15
lines changed

6 files changed

+68
-15
lines changed

site/src/components/Tooltips/HelpTooltip/HelpTooltip.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export const HelpTooltip: React.FC<HelpTooltipProps> = ({ children, open, size =
3333
const styles = useStyles({ size })
3434
const anchorRef = useRef<HTMLButtonElement>(null)
3535
const [isOpen, setIsOpen] = useState(!!open)
36-
const id = isOpen ? "help-popover" : undefined
36+
const id = isOpen ? "help-popover" : ""
3737

3838
const onClose = () => {
3939
setIsOpen(false)
@@ -110,16 +110,17 @@ export const HelpTooltipLink: React.FC<{ href: string }> = ({ children, href })
110110
)
111111
}
112112

113-
export const HelpTooltipAction: React.FC<{ icon: Icon; onClick: () => void }> = ({
114-
children,
115-
icon: Icon,
116-
onClick,
117-
}) => {
113+
export const HelpTooltipAction: React.FC<{
114+
icon: Icon
115+
onClick: () => void
116+
ariaLabel?: string
117+
}> = ({ children, icon: Icon, onClick, ariaLabel }) => {
118118
const styles = useStyles()
119119
const tooltip = useHelpTooltip()
120120

121121
return (
122122
<button
123+
aria-label={ariaLabel ?? ""}
123124
className={styles.action}
124125
onClick={(event) => {
125126
event.stopPropagation()

site/src/components/Tooltips/OutdatedHelpTooltip.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,24 @@ import {
88
HelpTooltipTitle,
99
} from "./HelpTooltip"
1010

11-
const Language = {
11+
export const Language = {
1212
outdatedLabel: "Outdated",
1313
versionTooltipText: "This workspace version is outdated and a newer version is available.",
1414
updateVersionLabel: "Update version",
1515
}
1616

1717
interface TooltipProps {
18-
onUpdateVersion: () => void
18+
onUpdateVersion: () => void,
19+
ariaLabel?: string,
1920
}
2021

21-
export const OutdatedHelpTooltip: FC<TooltipProps> = ({ onUpdateVersion }) => {
22+
export const OutdatedHelpTooltip: FC<TooltipProps> = ({ onUpdateVersion, ariaLabel }) => {
2223
return (
2324
<HelpTooltip size="small">
2425
<HelpTooltipTitle>{Language.outdatedLabel}</HelpTooltipTitle>
2526
<HelpTooltipText>{Language.versionTooltipText}</HelpTooltipText>
2627
<HelpTooltipLinksGroup>
27-
<HelpTooltipAction icon={RefreshIcon} onClick={onUpdateVersion}>
28+
<HelpTooltipAction icon={RefreshIcon} onClick={onUpdateVersion} ariaLabel={ariaLabel}>
2829
{Language.updateVersionLabel}
2930
</HelpTooltipAction>
3031
</HelpTooltipLinksGroup>

site/src/components/Workspace/Workspace.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export const Workspace: FC<WorkspaceProps> = ({
9494
handleClick={() => navigate(`/templates`)}
9595
/>
9696

97-
<WorkspaceStats workspace={workspace} />
97+
<WorkspaceStats workspace={workspace} handleUpdate={handleUpdate} />
9898

9999
{!!resources && !!resources.length && (
100100
<Resources
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { fireEvent, screen } from "@testing-library/react"
2+
import { Language } from "components/Tooltips/OutdatedHelpTooltip"
3+
import { WorkspaceStats } from "components/WorkspaceStats/WorkspaceStats"
4+
import { MockOutdatedWorkspace } from "testHelpers/entities"
5+
import { renderWithAuth } from "testHelpers/renderHelpers"
6+
import * as CreateDayString from "./createDayString"
7+
8+
describe("WorkspaceStats", () => {
9+
it("shows an outdated tooltip", async () => {
10+
// Mocking the dayjs module within the createDayString file
11+
const mock = jest.spyOn(CreateDayString, "createDayString")
12+
mock.mockImplementation(() => "a minute ago")
13+
14+
const handleUpdateMock = jest.fn()
15+
renderWithAuth(
16+
<WorkspaceStats handleUpdate={handleUpdateMock} workspace={MockOutdatedWorkspace} />,
17+
{
18+
route: `/@${MockOutdatedWorkspace.owner_name}/${MockOutdatedWorkspace.name}`,
19+
path: "/@:username/:workspace",
20+
},
21+
)
22+
const tooltipButton = await screen.findByRole("button")
23+
fireEvent.click(tooltipButton)
24+
expect(await screen.findByText(Language.versionTooltipText)).toBeInTheDocument()
25+
const updateButton = screen.getByRole("button", {
26+
name: "update version",
27+
})
28+
fireEvent.click(updateButton)
29+
expect(handleUpdateMock).toBeCalledTimes(1)
30+
})
31+
})

site/src/components/WorkspaceStats/WorkspaceStats.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import Link from "@material-ui/core/Link"
22
import { makeStyles, useTheme } from "@material-ui/core/styles"
3-
import dayjs from "dayjs"
3+
import { OutdatedHelpTooltip } from "components/Tooltips"
44
import { FC } from "react"
55
import { Link as RouterLink } from "react-router-dom"
66
import { Workspace } from "../../api/typesGenerated"
77
import { MONOSPACE_FONT_FAMILY } from "../../theme/constants"
88
import { combineClasses } from "../../util/combineClasses"
99
import { getDisplayStatus, getDisplayWorkspaceBuildInitiatedBy } from "../../util/workspace"
10+
import { createDayString } from "./createDayString"
1011

1112
const Language = {
1213
workspaceDetails: "Workspace Details",
@@ -21,9 +22,10 @@ const Language = {
2122

2223
export interface WorkspaceStatsProps {
2324
workspace: Workspace
25+
handleUpdate: () => void
2426
}
2527

26-
export const WorkspaceStats: FC<WorkspaceStatsProps> = ({ workspace }) => {
28+
export const WorkspaceStats: FC<WorkspaceStatsProps> = ({ workspace, handleUpdate }) => {
2729
const styles = useStyles()
2830
const theme = useTheme()
2931
const status = getDisplayStatus(theme, workspace.latest_build)
@@ -46,7 +48,10 @@ export const WorkspaceStats: FC<WorkspaceStatsProps> = ({ workspace }) => {
4648
<span className={styles.statsLabel}>{Language.versionLabel}</span>
4749
<span className={styles.statsValue}>
4850
{workspace.outdated ? (
49-
<span style={{ color: theme.palette.error.main }}>{Language.outdated}</span>
51+
<span className={styles.outdatedLabel}>
52+
{Language.outdated}
53+
<OutdatedHelpTooltip onUpdateVersion={handleUpdate} ariaLabel="update version" />
54+
</span>
5055
) : (
5156
<span style={{ color: theme.palette.text.secondary }}>{Language.upToDate}</span>
5257
)}
@@ -56,7 +61,7 @@ export const WorkspaceStats: FC<WorkspaceStatsProps> = ({ workspace }) => {
5661
<div className={styles.statItem}>
5762
<span className={styles.statsLabel}>{Language.lastBuiltLabel}</span>
5863
<span className={styles.statsValue} data-chromatic="ignore">
59-
{dayjs().to(dayjs(workspace.latest_build.created_at))}
64+
{createDayString(workspace.latest_build.created_at)}
6065
</span>
6166
</div>
6267
<div className={styles.statsDivider} />
@@ -133,4 +138,10 @@ const useStyles = makeStyles((theme) => ({
133138
color: theme.palette.text.primary,
134139
fontWeight: 600,
135140
},
141+
outdatedLabel: {
142+
color: theme.palette.error.main,
143+
display: "flex",
144+
alignItems: "center",
145+
gap: theme.spacing(0.5),
146+
},
136147
}))
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import dayjs from "dayjs"
2+
3+
/**
4+
* Returns a human-readable string describing when the workspace was created
5+
* Broken into its own module for testing purposes
6+
*/
7+
export function createDayString(createdAt: string): string {
8+
return dayjs().to(dayjs(createdAt))
9+
}

0 commit comments

Comments
 (0)