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

feat(sidebar): highlight selected route #1330

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 1 addition & 1 deletion src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const App = () => {
}
/>
<Route
path="/accounts"
path="/settings/accounts"
element={
<RequireAuth>
<AccountsRoute />
Expand Down
2 changes: 2 additions & 0 deletions src/components/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,15 @@ export const Sidebar: FC = () => {
icon={FilterIcon}
size={Size.MEDIUM}
metric={filterCount}
route={'/filters'}
onClick={() => toggleFilters()}
/>

<SidebarButton
title="Settings"
icon={GearIcon}
size={Size.MEDIUM}
route={'/settings'}
onClick={() => toggleSettings()}
/>
</>
Expand Down
32 changes: 16 additions & 16 deletions src/components/__snapshots__/Sidebar.test.tsx.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 16 additions & 3 deletions src/components/buttons/SidebarButton.test.tsx
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -10,7 +11,11 @@ describe('components/buttons/SidebarButton.tsx', () => {
metric: 1,
icon: MarkGithubIcon,
};
const tree = render(<SidebarButton {...props} />);
const tree = render(
<MemoryRouter>
<SidebarButton {...props} />
</MemoryRouter>,
);
expect(tree).toMatchSnapshot();
});

Expand All @@ -20,7 +25,11 @@ describe('components/buttons/SidebarButton.tsx', () => {
metric: 0,
icon: MarkGithubIcon,
};
const tree = render(<SidebarButton {...props} />);
const tree = render(
<MemoryRouter>
<SidebarButton {...props} />
</MemoryRouter>,
);
expect(tree).toMatchSnapshot();
});

Expand All @@ -31,7 +40,11 @@ describe('components/buttons/SidebarButton.tsx', () => {
icon: MarkGithubIcon,
size: Size.MEDIUM,
};
const tree = render(<SidebarButton {...props} />);
const tree = render(
<MemoryRouter>
<SidebarButton {...props} />
</MemoryRouter>,
);
expect(tree).toMatchSnapshot();
});
});
13 changes: 10 additions & 3 deletions src/components/buttons/SidebarButton.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,36 @@
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';

export interface ISidebarButton {
title: string;
metric?: number;
icon: Icon;
route?: string;
onClick?: () => void;
size?: Size;
loading?: boolean;
disabled?: boolean;
}

export const SidebarButton: FC<ISidebarButton> = (props: ISidebarButton) => {
const location = useLocation();

const hasMetric = props?.metric > 0;

const isRoute = location.pathname.startsWith(props.route);

return (
<button
type="button"
className={cn(
'flex justify-evenly items-center w-full my-1 cursor-pointer text-xs font-extrabold focus:outline-none disabled:text-gray-500 disabled:cursor-default',
'flex justify-evenly items-center w-full my-1 cursor-pointer text-xs font-extrabold rounded-md focus:outline-none disabled:text-gray-500 disabled:cursor-default',
hasMetric
? `${IconColor.GREEN} hover:text-green-700`
: `${IconColor.WHITE} hover:text-gray-500`,
? `${IconColor.GREEN} hover:text-green-700 shadow-green-500`
: `${IconColor.WHITE} hover:text-gray-500 shadow-white`,
isRoute && 'shadow',
props.loading ? 'animate-spin' : undefined,
props.size ? 'py-2' : 'py-1',
)}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/components/settings/SettingsFooter.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ describe('routes/components/settings/SettingsFooter.tsx', () => {
});

fireEvent.click(screen.getByTitle('Accounts'));
expect(mockNavigate).toHaveBeenCalledWith('/accounts');
expect(mockNavigate).toHaveBeenCalledWith('/settings/accounts');
});

it('should quit the app', async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/settings/SettingsFooter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const SettingsFooter: FC = () => {
className={BUTTON_CLASS_NAME}
title="Accounts"
onClick={() => {
navigate('/accounts');
navigate('/settings/accounts');
}}
>
<PersonIcon size={Size.LARGE} aria-label="Accounts" />
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ export interface FormattedReason {
export enum IconColor {
GRAY = 'text-gray-500 dark:text-gray-300',
GREEN = 'text-green-500',
ORANGE = 'text-orange-500',
PURPLE = 'text-purple-500',
RED = 'text-red-500',
YELLOW = 'text-yellow-500 dark:text-yellow-300',
Expand Down