Skip to content

Commit 4e16e0a

Browse files
authored
refactor: new pill component (#1222)
1 parent 2a867d9 commit 4e16e0a

File tree

6 files changed

+199
-67
lines changed

6 files changed

+199
-67
lines changed

src/components/NotificationRow.tsx

Lines changed: 35 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
import { type FC, type MouseEvent, useCallback, useContext } from 'react';
1212

1313
import { AppContext } from '../context/App';
14-
import { PILL_CLASS_NAME } from '../styles/gitify';
1514
import { type Account, IconColor } from '../types';
1615
import type { Notification } from '../typesGitHub';
1716
import {
@@ -25,6 +24,7 @@ import {
2524
} from '../utils/icons';
2625
import { openNotification, openUserProfile } from '../utils/links';
2726
import { formatReason } from '../utils/reason';
27+
import { PillButton } from './buttons/PillButton';
2828

2929
interface IProps {
3030
account: Account;
@@ -143,77 +143,56 @@ export const NotificationRow: FC<IProps> = ({ notification, account }) => {
143143
{settings.showPills && (
144144
<div>
145145
{notification.subject?.linkedIssues?.length > 0 && (
146-
<span title={linkedIssuesPillDescription}>
147-
<button type="button" className={PILL_CLASS_NAME}>
148-
<IssueClosedIcon
149-
size={12}
150-
className={`mr-1 ${IconColor.GREEN}`}
151-
aria-label={linkedIssuesPillDescription}
152-
/>
153-
{notification.subject.linkedIssues.length}
154-
</button>
155-
</span>
146+
<PillButton
147+
title={linkedIssuesPillDescription}
148+
metric={notification.subject.linkedIssues.length}
149+
icon={IssueClosedIcon}
150+
color={IconColor.GREEN}
151+
/>
156152
)}
153+
157154
{notification.subject.reviews?.map((review) => {
158155
const icon = getPullRequestReviewIcon(review);
159156
if (!icon) {
160157
return null;
161158
}
162159

163160
return (
164-
<span key={review.state} title={icon.description}>
165-
<button type="button" className={PILL_CLASS_NAME}>
166-
<icon.type
167-
size={12}
168-
className={`mr-1 ${icon.color}`}
169-
aria-label={icon.description}
170-
/>
171-
{review.users.length}
172-
</button>
173-
</span>
161+
<PillButton
162+
key={review.state}
163+
title={icon.description}
164+
metric={review.users.length}
165+
icon={icon.type}
166+
color={icon.color}
167+
/>
174168
);
175169
})}
176170
{notification.subject?.comments > 0 && (
177-
<span title={commentsPillDescription}>
178-
<button type="button" className={PILL_CLASS_NAME}>
179-
<CommentIcon
180-
size={12}
181-
className={`mr-1 ${IconColor.GRAY}`}
182-
aria-label={commentsPillDescription}
183-
/>
184-
{notification.subject.comments}
185-
</button>
186-
</span>
171+
<PillButton
172+
title={commentsPillDescription}
173+
metric={notification.subject.comments}
174+
icon={CommentIcon}
175+
color={IconColor.GRAY}
176+
/>
187177
)}
188178
{notification.subject?.labels?.length > 0 && (
189-
<span title={labelsPillDescription}>
190-
<button type="button" className={PILL_CLASS_NAME}>
191-
<TagIcon
192-
size={12}
193-
className={`mr-1 ${IconColor.GRAY}`}
194-
aria-label={labelsPillDescription}
195-
/>
196-
{notification.subject.labels.length}
197-
</button>
198-
</span>
179+
<PillButton
180+
title={labelsPillDescription}
181+
metric={notification.subject.labels.length}
182+
icon={TagIcon}
183+
color={IconColor.GRAY}
184+
/>
199185
)}
200186
{notification.subject.milestone && (
201-
<span
202-
className="ml-1"
187+
<PillButton
203188
title={notification.subject.milestone.title}
204-
>
205-
<button type="button" className={PILL_CLASS_NAME}>
206-
<MilestoneIcon
207-
size={12}
208-
className={
209-
notification.subject.milestone.state === 'open'
210-
? IconColor.GREEN
211-
: IconColor.RED
212-
}
213-
aria-label={notification.subject.milestone.title}
214-
/>
215-
</button>
216-
</span>
189+
icon={MilestoneIcon}
190+
color={
191+
notification.subject.milestone.state === 'open'
192+
? IconColor.GREEN
193+
: IconColor.RED
194+
}
195+
/>
217196
)}
218197
</div>
219198
)}

src/components/__snapshots__/NotificationRow.test.tsx.snap

Lines changed: 4 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { MarkGithubIcon } from '@primer/octicons-react';
2+
import { render } from '@testing-library/react';
3+
import { IconColor } from '../../types';
4+
import { type IPillButton, PillButton } from './PillButton';
5+
6+
describe('components/buttons/PillButton.tsx', () => {
7+
it('should render', () => {
8+
const props: IPillButton = {
9+
title: 'Mock Pill',
10+
metric: 1,
11+
icon: MarkGithubIcon,
12+
color: IconColor.GREEN,
13+
};
14+
const tree = render(<PillButton {...props} />);
15+
expect(tree).toMatchSnapshot();
16+
});
17+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import type { Icon } from '@primer/octicons-react';
2+
import type { FC } from 'react';
3+
import type { IconColor } from '../../types';
4+
5+
export interface IPillButton {
6+
key?: string;
7+
title: string;
8+
metric?: number;
9+
icon: Icon;
10+
color: IconColor;
11+
}
12+
13+
export const PillButton: FC<IPillButton> = (props: IPillButton) => {
14+
return (
15+
<span title={props.title}>
16+
<button
17+
type="button"
18+
className="rounded-full text-xss px-1 m-0.5 bg-gray-100 hover:bg-gray-200 dark:bg-gray-800 dark:hover:bg-gray-700"
19+
>
20+
<props.icon
21+
size={12}
22+
className={`mr-1 ${props.color}`}
23+
aria-label={props.title}
24+
/>
25+
{props.metric}
26+
</button>
27+
</span>
28+
);
29+
};

src/components/buttons/__snapshots__/PillButton.test.tsx.snap

Lines changed: 114 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/styles/gitify.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,4 @@ export const BUTTON_CLASS_NAME =
44
export const BUTTON_SIDEBAR_CLASS_NAME =
55
'flex justify-evenly items-center bg-transparent border-0 w-full text-sm text-white my-1 py-2 cursor-pointer hover:text-gray-500 focus:outline-none disabled:text-gray-500 disabled:cursor-default';
66

7-
export const PILL_CLASS_NAME =
8-
'rounded-full text-xss px-1 m-0.5 bg-gray-100 hover:bg-gray-200 dark:bg-gray-800 dark:hover:bg-gray-700';
9-
107
export const READ_NOTIFICATION_CLASS_NAME = 'opacity-50 dark:opacity-50';

0 commit comments

Comments
 (0)