From 0d83d3132c10ad946938e3836f449614931ce962 Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Thu, 4 Jul 2024 11:03:13 -0800 Subject: [PATCH 1/3] feat(sidebar): highlight selected route --- src/app.tsx | 2 +- src/components/Sidebar.tsx | 2 ++ src/components/buttons/SidebarButton.test.tsx | 19 ++++++++++++++++--- src/components/buttons/SidebarButton.tsx | 14 +++++++++++--- .../settings/SettingsFooter.test.tsx | 2 +- src/components/settings/SettingsFooter.tsx | 2 +- src/types.ts | 1 + 7 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/app.tsx b/src/app.tsx index 843b5ccc2..0654a9c39 100644 --- a/src/app.tsx +++ b/src/app.tsx @@ -61,7 +61,7 @@ export const App = () => { } /> diff --git a/src/components/Sidebar.tsx b/src/components/Sidebar.tsx index e5c007f45..7656a5b92 100644 --- a/src/components/Sidebar.tsx +++ b/src/components/Sidebar.tsx @@ -110,6 +110,7 @@ export const Sidebar: FC = () => { icon={FilterIcon} size={Size.MEDIUM} metric={filterCount} + route={'/filters'} onClick={() => toggleFilters()} /> @@ -117,6 +118,7 @@ export const Sidebar: FC = () => { title="Settings" icon={GearIcon} size={Size.MEDIUM} + route={'/settings'} onClick={() => toggleSettings()} /> diff --git a/src/components/buttons/SidebarButton.test.tsx b/src/components/buttons/SidebarButton.test.tsx index 7fb2f3172..f5166be54 100644 --- a/src/components/buttons/SidebarButton.test.tsx +++ b/src/components/buttons/SidebarButton.test.tsx @@ -1,5 +1,6 @@ import { MarkGithubIcon } from '@primer/octicons-react'; import { render } from '@testing-library/react'; +import { MemoryRouter } from 'react-router-dom'; import { Size } from '../../types'; import { type ISidebarButton, SidebarButton } from './SidebarButton'; @@ -10,7 +11,11 @@ describe('components/buttons/SidebarButton.tsx', () => { metric: 1, icon: MarkGithubIcon, }; - const tree = render(); + const tree = render( + + + , + ); expect(tree).toMatchSnapshot(); }); @@ -20,7 +25,11 @@ describe('components/buttons/SidebarButton.tsx', () => { metric: 0, icon: MarkGithubIcon, }; - const tree = render(); + const tree = render( + + + , + ); expect(tree).toMatchSnapshot(); }); @@ -31,7 +40,11 @@ describe('components/buttons/SidebarButton.tsx', () => { icon: MarkGithubIcon, size: Size.MEDIUM, }; - const tree = render(); + const tree = render( + + + , + ); expect(tree).toMatchSnapshot(); }); }); diff --git a/src/components/buttons/SidebarButton.tsx b/src/components/buttons/SidebarButton.tsx index ea9c11398..574f06e68 100644 --- a/src/components/buttons/SidebarButton.tsx +++ b/src/components/buttons/SidebarButton.tsx @@ -1,5 +1,6 @@ import type { Icon } from '@primer/octicons-react'; import type { FC } from 'react'; +import { useLocation } from 'react-router-dom'; import { IconColor, Size } from '../../types'; import { cn } from '../../utils/cn'; @@ -7,6 +8,7 @@ export interface ISidebarButton { title: string; metric?: number; icon: Icon; + route?: string; onClick?: () => void; size?: Size; loading?: boolean; @@ -14,16 +16,22 @@ export interface ISidebarButton { } export const SidebarButton: FC = (props: ISidebarButton) => { + const location = useLocation(); + const hasMetric = props?.metric > 0; + const isRoute = location.pathname.startsWith(props.route); + return (