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

Fix issue with shifting account header #976

Merged
merged 2 commits into from
Apr 24, 2024
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
16 changes: 8 additions & 8 deletions packages/ui/components/ui/account-switcher.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ describe('<AccountSwitcher />', () => {
expect(onChange).toHaveBeenCalledWith(122);
});

it("does not render when we're at account 0", () => {
it("is disabled when we're at account 0", () => {
const { queryByLabelText } = render(<AccountSwitcher account={0} onChange={vi.fn()} />);

expect(queryByLabelText('Previous account')).toBeNull();
expect(queryByLabelText('Previous account')?.parentElement).toBeDisabled();
});

describe('when a filter has been passed', () => {
Expand All @@ -74,12 +74,12 @@ describe('<AccountSwitcher />', () => {
expect(onChange).toHaveBeenCalledWith(100);
});

it("does not render when we're at the lowest account index in the filter", () => {
it("is disabled when we're at the lowest account index in the filter", () => {
const { queryByLabelText } = render(
<AccountSwitcher account={100} onChange={vi.fn()} filter={[123, 100]} />,
);

expect(queryByLabelText('Previous account')).toBeNull();
expect(queryByLabelText('Previous account')?.parentElement).toBeDisabled();
});
});
});
Expand All @@ -94,10 +94,10 @@ describe('<AccountSwitcher />', () => {
expect(onChange).toHaveBeenCalledWith(124);
});

it("does not render when we're at the maximum account index", () => {
it("is disabled when we're at the maximum account index", () => {
const { queryByLabelText } = render(<AccountSwitcher account={2 ** 32} onChange={vi.fn()} />);

expect(queryByLabelText('Next account')).toBeNull();
expect(queryByLabelText('Next account')?.parentElement).toBeDisabled();
});

describe('when a filter has been passed', () => {
Expand All @@ -112,12 +112,12 @@ describe('<AccountSwitcher />', () => {
expect(onChange).toHaveBeenCalledWith(123);
});

it("does not render when we're at the highest account index in the filter", () => {
it("is disabled when we're at the highest account index in the filter", () => {
const { queryByLabelText } = render(
<AccountSwitcher account={123} onChange={vi.fn()} filter={[123, 100]} />,
);

expect(queryByLabelText('Next account')).toBeNull();
expect(queryByLabelText('Next account')?.parentElement).toBeDisabled();
});
});
});
Expand Down
64 changes: 29 additions & 35 deletions packages/ui/components/ui/account-switcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,29 +50,26 @@ export const AccountSwitcher = ({
}
};

const shouldShowPreviousButton =
const previousButtonEnabled =
account !== 0 && (!sortedFilter || sortedFilter.indexOf(account) > 0);
const shouldShowNextButton =
const nextButtonEnabled =
account !== MAX_INDEX &&
(!sortedFilter || sortedFilter.indexOf(account) < sortedFilter.length - 1);

return (
<div className='flex items-center justify-between'>
{shouldShowPreviousButton ? (
<Button
variant='ghost'
className={cn('hover:bg-inherit hover:text-slate-400', account === 0 && 'cursor-default')}
>
<ArrowLeftIcon
aria-label='Previous account'
role='button'
onClick={handleClickPrevious}
className='size-6 hover:cursor-pointer'
/>
</Button>
) : (
<span className='size-6' />
)}
<Button
variant='ghost'
className={cn('hover:bg-inherit hover:text-slate-400', account === 0 && 'cursor-default')}
disabled={!previousButtonEnabled}
>
<ArrowLeftIcon
aria-label='Previous account'
role='button'
onClick={handleClickPrevious}
className='size-6 hover:cursor-pointer'
/>
</Button>
<div className='select-none text-center font-headline text-xl font-semibold leading-[30px]'>
<div className='flex flex-row flex-wrap items-end gap-[6px]'>
<span>Account</span>
Expand Down Expand Up @@ -109,24 +106,21 @@ export const AccountSwitcher = ({
</div>
</div>
</div>
{shouldShowNextButton ? (
<Button
variant='ghost'
className={cn(
'hover:bg-inherit hover:text-slate-400',
account === MAX_INDEX && 'cursor-default',
)}
>
<ArrowRightIcon
aria-label='Next account'
role='button'
onClick={handleClickNext}
className='size-6 hover:cursor-pointer'
/>
</Button>
) : (
<span className='size-6' />
)}
<Button
variant='ghost'
className={cn(
'hover:bg-inherit hover:text-slate-400',
account === MAX_INDEX && 'cursor-default',
)}
disabled={!nextButtonEnabled}
>
<ArrowRightIcon
aria-label='Next account'
role='button'
onClick={handleClickNext}
className='size-6 hover:cursor-pointer'
/>
</Button>
</div>
);
};
Loading