Skip to content

Commit 57b2203

Browse files
committed
feat: format display attributes on GitifyNotification
Signed-off-by: Adam Setch <[email protected]>
1 parent ce3e5c8 commit 57b2203

File tree

7 files changed

+47
-31
lines changed

7 files changed

+47
-31
lines changed

src/renderer/components/notifications/NotificationFooter.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { RelativeTime, Stack, Text } from '@primer/react';
55
import { type GitifyNotification, Opacity, Size } from '../../types';
66
import { cn } from '../../utils/cn';
77
import { openUserProfile } from '../../utils/links';
8-
import { createNotificationHandler } from '../../utils/notifications/handlers';
98
import { AvatarWithFallback } from '../avatars/AvatarWithFallback';
109
import { MetricGroup } from '../metrics/MetricGroup';
1110

@@ -16,8 +15,6 @@ interface NotificationFooterProps {
1615
export const NotificationFooter: FC<NotificationFooterProps> = ({
1716
notification,
1817
}: NotificationFooterProps) => {
19-
const handler = createNotificationHandler(notification);
20-
2118
return (
2219
<Stack
2320
align="center"
@@ -47,7 +44,7 @@ export const NotificationFooter: FC<NotificationFooterProps> = ({
4744
) : (
4845
<AvatarWithFallback
4946
size={Size.SMALL}
50-
userType={handler.defaultUserType()}
47+
userType={notification.display.defaultUserType}
5148
/>
5249
)}
5350

src/renderer/components/notifications/NotificationHeader.tsx

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { type GitifyNotification, Opacity, Size } from '../../types';
77
import { cn } from '../../utils/cn';
88
import { openRepository } from '../../utils/links';
99
import { isGroupByDate } from '../../utils/notifications/group';
10-
import { createNotificationHandler } from '../../utils/notifications/handlers';
1110
import { AvatarWithFallback } from '../avatars/AvatarWithFallback';
1211

1312
interface NotificationHeaderProps {
@@ -19,11 +18,6 @@ export const NotificationHeader: FC<NotificationHeaderProps> = ({
1918
}: NotificationHeaderProps) => {
2019
const { settings } = useAppContext();
2120

22-
const repoSlug = notification.repository.fullName;
23-
24-
const handler = createNotificationHandler(notification);
25-
const notificationNumber = handler.formattedNotificationNumber(notification);
26-
2721
return (
2822
isGroupByDate(settings) && (
2923
<div className="py-0.5">
@@ -40,8 +34,8 @@ export const NotificationHeader: FC<NotificationHeaderProps> = ({
4034
type="button"
4135
>
4236
<AvatarWithFallback
43-
alt={repoSlug}
44-
name={repoSlug}
37+
alt={notification.repository.fullName}
38+
name={notification.repository.fullName}
4539
size={Size.SMALL}
4640
src={notification.repository.owner.avatarUrl}
4741
userType={notification.repository.owner.type}
@@ -54,7 +48,7 @@ export const NotificationHeader: FC<NotificationHeaderProps> = ({
5448
!settings.showNumber && 'hidden',
5549
)}
5650
>
57-
{notificationNumber}
51+
{notification.display.number}
5852
</div>
5953
</Stack>
6054
</div>

src/renderer/components/notifications/NotificationRow.tsx

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { cn } from '../../utils/cn';
99
import { isMarkAsDoneFeatureSupported } from '../../utils/features';
1010
import { openNotification } from '../../utils/links';
1111
import { isGroupByDate } from '../../utils/notifications/group';
12-
import { createNotificationHandler } from '../../utils/notifications/handlers';
1312
import { HoverButton } from '../primitives/HoverButton';
1413
import { HoverGroup } from '../primitives/HoverGroup';
1514
import { NotificationFooter } from './NotificationFooter';
@@ -62,13 +61,7 @@ export const NotificationRow: FC<NotificationRowProps> = ({
6261
unsubscribeNotification(notification);
6362
};
6463

65-
const handler = createNotificationHandler(notification);
66-
const NotificationIcon = handler.iconType(notification);
67-
const iconColor = handler.iconColor(notification);
68-
const notificationType = handler.formattedNotificationType(notification);
69-
const notificationNumber = handler.formattedNotificationNumber(notification);
70-
const notificationTitle = handler.formattedNotificationTitle(notification);
71-
64+
const NotificationIcon = notification.display.icon.type;
7265
const isNotificationRead = !notification.unread;
7366

7467
return (
@@ -84,11 +77,11 @@ export const NotificationRow: FC<NotificationRowProps> = ({
8477
id={notification.id}
8578
>
8679
<Stack align="center" direction="horizontal" gap="condensed">
87-
<Tooltip direction="e" text={notificationType}>
80+
<Tooltip direction="e" text={notification.display.type}>
8881
<button type="button">
8982
<NotificationIcon
90-
aria-label={notificationType}
91-
className={iconColor}
83+
aria-label={notification.display.type}
84+
className={notification.display.icon.color}
9285
size={Size.LARGE}
9386
/>
9487
</button>
@@ -115,7 +108,7 @@ export const NotificationRow: FC<NotificationRowProps> = ({
115108
direction="horizontal"
116109
gap="condensed"
117110
justify="space-between"
118-
title={notificationTitle}
111+
title={notification.display.title}
119112
>
120113
<Text className={!settings.wrapNotificationTitle && 'truncate'}>
121114
{notification.subject.title}
@@ -127,7 +120,7 @@ export const NotificationRow: FC<NotificationRowProps> = ({
127120
(isGroupByDate(settings) || !settings.showNumber) && 'hidden',
128121
)}
129122
>
130-
{notificationNumber}
123+
{notification.display.number}
131124
</Text>
132125
</Stack>
133126

src/renderer/types.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { FC } from 'react';
22

3-
import type { OcticonProps } from '@primer/octicons-react';
3+
import type { Icon, OcticonProps } from '@primer/octicons-react';
44

55
import type {
66
DiscussionStateReason,
@@ -286,6 +286,17 @@ export interface GitifyNotification {
286286
account: Account;
287287
/** UI ordering index */
288288
order: number;
289+
290+
display: {
291+
title: string;
292+
type: string;
293+
number: string;
294+
icon: {
295+
type: Icon;
296+
color: IconColor;
297+
};
298+
defaultUserType: UserType;
299+
};
289300
}
290301

291302
/**

src/renderer/utils/api/transform.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ export function transformNotification(
3434
repository: transformRepository(raw.repository),
3535
account,
3636
order,
37+
// Display fields start as undefined, populated later post-enrichment
38+
display: undefined,
3739
};
3840
}
3941

src/renderer/utils/notifications/handlers/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { OcticonProps } from '@primer/octicons-react';
55
import type {
66
GitifyNotification,
77
GitifySubject,
8+
IconColor,
89
Link,
910
SettingsState,
1011
SubjectType,
@@ -40,7 +41,7 @@ export interface NotificationTypeHandler {
4041
/**
4142
* Return the icon color for this notification type.
4243
*/
43-
iconColor(notification: GitifyNotification): string;
44+
iconColor(notification: GitifyNotification): IconColor;
4445

4546
/**
4647
* Return the formatted notification type for this notification.

src/renderer/utils/notifications/notifications.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,10 @@ export async function getAllNotifications(
7979
try {
8080
const rawNotifications = (await accountNotifications.notifications)
8181
.data;
82-
let notifications = rawNotifications.map((raw) =>
83-
transformNotification(raw, accountNotifications.account),
84-
);
82+
83+
let notifications = rawNotifications.map((raw) => {
84+
return transformNotification(raw, accountNotifications.account);
85+
});
8586

8687
notifications = filterBaseNotifications(
8788
notifications,
@@ -98,6 +99,23 @@ export async function getAllNotifications(
9899
state.settings,
99100
);
100101

102+
notifications = notifications.map((notification) => {
103+
const handler = createNotificationHandler(notification);
104+
return {
105+
...notification,
106+
display: {
107+
title: handler.formattedNotificationTitle(notification),
108+
type: handler.formattedNotificationType(notification),
109+
number: handler.formattedNotificationNumber(notification),
110+
icon: {
111+
type: handler.iconType(notification),
112+
color: handler.iconColor(notification),
113+
},
114+
defaultUserType: handler.defaultUserType(),
115+
},
116+
};
117+
});
118+
101119
return {
102120
account: accountNotifications.account,
103121
notifications: notifications,

0 commit comments

Comments
 (0)