Skip to content
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

Replace deprecated Hidden component with useMediaQuery hook #3246

Merged
merged 15 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 14 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
10 changes: 7 additions & 3 deletions src/components/App/tests/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ThemeProvider } from "@mui/material/styles";
import { render } from "@testing-library/react";
import "jest-canvas-mock";
import { act } from "react";
Expand All @@ -7,6 +8,7 @@ import thunk from "redux-thunk";

import App from "components/App";
import { defaultState } from "rootRedux/types";
import theme from "types/theme";

jest.mock("react-router-dom");

Expand All @@ -22,9 +24,11 @@ describe("App", () => {
it("renders without crashing", async () => {
await act(async () => {
render(
<Provider store={mockStore}>
<App />
</Provider>
<ThemeProvider theme={theme}>
<Provider store={mockStore}>
<App />
</Provider>
</ThemeProvider>
);
});
});
Expand Down
18 changes: 8 additions & 10 deletions src/components/AppBar/Logo.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Button, Hidden } from "@mui/material";
import { Button, Theme, useMediaQuery } from "@mui/material";
import { ReactElement } from "react";
import { useNavigate } from "react-router-dom";

Expand All @@ -10,6 +10,8 @@ import { themeColors } from "types/theme";

/** A button that redirects to the home page */
export default function Logo(): ReactElement {
const isSmDown = useMediaQuery<Theme>((t) => t.breakpoints.down("sm"));
const isMdDown = useMediaQuery<Theme>((t) => t.breakpoints.down("md"));
const navigate = useNavigate();
return (
<Button
Expand All @@ -24,15 +26,11 @@ export default function Logo(): ReactElement {
padding: 2,
}}
>
<Hidden mdDown>
<img src={logo} height="45" alt="Logo" />
</Hidden>
<Hidden smDown mdUp>
<img src={smallLogo} height="30" alt="Logo" />
</Hidden>
<Hidden smUp>
<img src={smallLogo} height="20" alt="Logo" />
</Hidden>
<img
alt="Logo"
height={isSmDown ? "20" : isMdDown ? "30" : "45"}
src={isMdDown ? smallLogo : logo}
/>
</Button>
);
}
15 changes: 11 additions & 4 deletions src/components/AppBar/NavigationButtons.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { PlaylistAdd, Rule } from "@mui/icons-material";
import { Button, Hidden, Tooltip, Typography } from "@mui/material";
import { ReactElement, useEffect, useState } from "react";
import {
Button,
type Theme,
Tooltip,
Typography,
useMediaQuery,
} from "@mui/material";
import { type ReactElement, useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import { useNavigate } from "react-router-dom";

Expand Down Expand Up @@ -71,6 +77,7 @@ function NavButton(props: NavButtonProps): ReactElement {
const { t } = useTranslation();
const navigate = useNavigate();
const { windowWidth } = useWindowSize();
const showText = useMediaQuery<Theme>((t) => t.breakpoints.up("sm"));

return (
<Button
Expand All @@ -90,11 +97,11 @@ function NavButton(props: NavButtonProps): ReactElement {
}}
>
<Tooltip title={t(props.textId)}>{props.icon}</Tooltip>
<Hidden smDown>
{showText && (
<Typography style={{ marginLeft: 5, marginRight: 5 }}>
{t(props.textId)}
</Typography>
</Hidden>
)}
</Button>
);
}
31 changes: 19 additions & 12 deletions src/components/AppBar/ProjectButtons.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { BarChart, Settings } from "@mui/icons-material";
import { Button, Hidden, Tooltip, Typography } from "@mui/material";
import {
Button,
Theme,
Tooltip,
Typography,
useMediaQuery,
} from "@mui/material";
import { ReactElement, useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import { useSelector } from "react-redux";
Expand Down Expand Up @@ -42,6 +48,15 @@ export default function ProjectButtons(props: TabProps): ReactElement {
const { t } = useTranslation();
const navigate = useNavigate();

const isMdUp = useMediaQuery<Theme>((t) => t.breakpoints.up("md"));
const isLg = useMediaQuery<Theme>((t) => t.breakpoints.only("lg"));
const isXl = useMediaQuery<Theme>((t) => t.breakpoints.only("xl"));
const nameLength = isXl
? projNameLength.xl
: isLg
? projNameLength.lg
: projNameLength.md;

useEffect(() => {
hasPermission(Permission.Statistics).then(setHasStatsPermission);
}, []);
Expand Down Expand Up @@ -77,22 +92,14 @@ export default function ProjectButtons(props: TabProps): ReactElement {
}}
>
<Settings />
<Hidden mdDown>
{isMdUp && (
<Typography
display="inline"
style={{ marginLeft: 5, marginRight: 5 }}
>
<Hidden xlDown>
{shortenName(projectName, projNameLength.xl)}
</Hidden>
<Hidden lgDown xlUp>
{shortenName(projectName, projNameLength.lg)}
</Hidden>
<Hidden mdDown lgUp>
{shortenName(projectName, projNameLength.md)}
</Hidden>
{shortenName(projectName, nameLength)}
</Typography>
</Hidden>
)}
</Button>
</Tooltip>
{showSpeaker && <SpeakerMenu />}
Expand Down
23 changes: 10 additions & 13 deletions src/components/AppBar/UserMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import {
import {
Avatar,
Button,
Hidden,
Menu,
MenuItem,
type Theme,
Typography,
useMediaQuery,
} from "@mui/material";
import {
type CSSProperties,
type ForwardedRef,
Fragment,
type MouseEvent,
type ReactElement,
useState,
Expand Down Expand Up @@ -53,6 +53,10 @@ export default function UserMenu(props: TabProps): ReactElement {
const [isAdmin, setIsAdmin] = useState(false);
const username = LocalStorage.getCurrentUser()?.username;

const isLgUp = useMediaQuery<Theme>((t) => t.breakpoints.up("lg"));
const isXl = useMediaQuery<Theme>((t) => t.breakpoints.only("xl"));
const nameLength = isXl ? usernameLength.xl : usernameLength.lg;

function handleClick(event: MouseEvent<HTMLButtonElement>): void {
setAnchorElement(event.currentTarget);
}
Expand All @@ -78,17 +82,10 @@ export default function UserMenu(props: TabProps): ReactElement {
padding: 0,
}}
>
{username ? (
<Hidden lgDown>
<Typography style={{ marginLeft: 5, marginRight: 5 }}>
<Hidden xlDown>{shortenName(username, usernameLength.xl)}</Hidden>
<Hidden xlUp lgDown>
{shortenName(username, usernameLength.lg)}
</Hidden>
</Typography>
</Hidden>
) : (
<Fragment />
{!!username && isLgUp && (
<Typography style={{ marginLeft: 5, marginRight: 5 }}>
{shortenName(username, nameLength)}
</Typography>
)}
{avatar ? (
<Avatar alt="User avatar" src={avatar} />
Expand Down
14 changes: 9 additions & 5 deletions src/components/AppBar/tests/AppBarComponent.test.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { ThemeProvider } from "@mui/material/styles";
import { Provider } from "react-redux";
import { MemoryRouter } from "react-router-dom";
import { act, create } from "react-test-renderer";
import configureMockStore from "redux-mock-store";

import AppBar from "components/AppBar/AppBarComponent";
import { defaultState } from "rootRedux/types";
import theme from "types/theme";

jest.mock("backend", () => ({
isSiteAdmin: () => mockIsSiteAdmin(),
Expand All @@ -26,11 +28,13 @@ describe("AppBar", () => {
it("renders", async () => {
await act(async () => {
create(
<Provider store={mockStore}>
<MemoryRouter>
<AppBar />
</MemoryRouter>
</Provider>
<ThemeProvider theme={theme}>
<Provider store={mockStore}>
<MemoryRouter>
<AppBar />
</MemoryRouter>
</Provider>
</ThemeProvider>
);
});
});
Expand Down
8 changes: 7 additions & 1 deletion src/components/AppBar/tests/Logo.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Button } from "@mui/material";
import { ThemeProvider } from "@mui/material/styles";
import renderer from "react-test-renderer";

import Logo from "components/AppBar/Logo";
import { Path } from "types/path";
import theme from "types/theme";

jest.mock("react-router-dom", () => ({
useNavigate:
Expand All @@ -17,7 +19,11 @@ let testRenderer: renderer.ReactTestRenderer;

beforeAll(() => {
renderer.act(() => {
testRenderer = renderer.create(<Logo />);
testRenderer = renderer.create(
<ThemeProvider theme={theme}>
<Logo />
</ThemeProvider>
);
});
});

Expand Down
11 changes: 7 additions & 4 deletions src/components/AppBar/tests/NavigationButtons.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ThemeProvider } from "@mui/material/styles";
import { Provider } from "react-redux";
import renderer, { type ReactTestInstance } from "react-test-renderer";
import configureMockStore from "redux-mock-store";
Expand All @@ -8,7 +9,7 @@ import NavigationButtons, {
dataEntryButtonId,
} from "components/AppBar/NavigationButtons";
import { Path } from "types/path";
import { themeColors } from "types/theme";
import theme, { themeColors } from "types/theme";

jest.mock("react-router-dom", () => ({
useNavigate: jest.fn(),
Expand All @@ -34,9 +35,11 @@ const renderNavButtons = async (
mockGetCurrentPermissions.mockResolvedValue([permission]);
await renderer.act(async () => {
testRenderer = renderer.create(
<Provider store={mockStore}>
<NavigationButtons currentTab={path} />
</Provider>
<ThemeProvider theme={theme}>
<Provider store={mockStore}>
<NavigationButtons currentTab={path} />
</Provider>
</ThemeProvider>
);
});
};
Expand Down
11 changes: 7 additions & 4 deletions src/components/AppBar/tests/ProjectButtons.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Button } from "@mui/material";
import { ThemeProvider } from "@mui/material/styles";
import { Provider } from "react-redux";
import { ReactTestRenderer, act, create } from "react-test-renderer";
import configureMockStore, { MockStoreEnhanced } from "redux-mock-store";
Expand All @@ -14,7 +15,7 @@ import { MergeDups } from "goals/MergeDuplicates/MergeDupsTypes";
import { ReviewEntries } from "goals/ReviewEntries/ReviewEntriesTypes";
import { Goal, GoalStatus } from "types/goals";
import { Path } from "types/path";
import { themeColors } from "types/theme";
import theme, { themeColors } from "types/theme";

jest.mock("react-router-dom", () => ({
useNavigate: jest.fn(),
Expand Down Expand Up @@ -46,9 +47,11 @@ const renderProjectButtons = async (
): Promise<void> => {
await act(async () => {
testRenderer = create(
<Provider store={mockStore(goal)}>
<ProjectButtons currentTab={path} />
</Provider>
<ThemeProvider theme={theme}>
<Provider store={mockStore(goal)}>
<ProjectButtons currentTab={path} />
</Provider>
</ThemeProvider>
);
});
};
Expand Down
10 changes: 7 additions & 3 deletions src/components/AppBar/tests/UserMenu.test.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Button, MenuItem } from "@mui/material";
import { ThemeProvider } from "@mui/material/styles";
import { Provider } from "react-redux";
import { act, create, ReactTestRenderer } from "react-test-renderer";
import configureMockStore from "redux-mock-store";

import UserMenu, { UserMenuList } from "components/AppBar/UserMenu";
import { Path } from "types/path";
import theme from "types/theme";

jest.mock("backend", () => ({
isSiteAdmin: () => mockIsSiteAdmin(),
Expand Down Expand Up @@ -37,9 +39,11 @@ describe("UserMenu", () => {
it("renders", async () => {
await act(async () => {
testRenderer = create(
<Provider store={mockStore}>
<UserMenu currentTab={Path.Root} />
</Provider>
<ThemeProvider theme={theme}>
<Provider store={mockStore}>
<UserMenu currentTab={Path.Root} />
</Provider>
</ThemeProvider>
);
});
expect(testRenderer.root.findAllByType(Button).length).toEqual(1);
Expand Down
22 changes: 15 additions & 7 deletions src/components/LandingPage/TopBar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { AppBar, Hidden, Stack, Toolbar, Typography } from "@mui/material";
import {
AppBar,
Stack,
Theme,
Toolbar,
Typography,
useMediaQuery,
} from "@mui/material";
import { ReactElement } from "react";
import { useTranslation } from "react-i18next";

Expand All @@ -8,6 +15,8 @@ export const topBarHeight = 70;

/** A bar shown at the top of the landing page. */
export default function TopBar(): ReactElement {
const showSubtitle = useMediaQuery<Theme>((t) => t.breakpoints.up("sm"));
const isMdUp = useMediaQuery<Theme>((t) => t.breakpoints.up("md"));
const { t } = useTranslation();

return (
Expand All @@ -22,12 +31,11 @@ export default function TopBar(): ReactElement {
style={{ width: "100%" }}
>
<img src={logo} height="50" alt="Logo" />
<Hidden smDown mdUp>
<Typography variant="h6">{t("landingPage.subtitle")}</Typography>
</Hidden>
<Hidden mdDown>
<Typography variant="h5">{t("landingPage.subtitle")}</Typography>
</Hidden>
{showSubtitle && (
<Typography variant={isMdUp ? "h5" : "h6"}>
{t("landingPage.subtitle")}
</Typography>
)}
</Stack>
</Toolbar>
</AppBar>
Expand Down
Loading
Loading