Skip to content

Commit fa65359

Browse files
committed
feat: mark repository as done
1 parent 1c3c248 commit fa65359

File tree

3 files changed

+75
-3
lines changed

3 files changed

+75
-3
lines changed

src/components/Repository.tsx

+19-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useCallback, useContext } from 'react';
2-
import { ReadIcon } from '@primer/octicons-react';
2+
import { ReadIcon, CheckIcon } from '@primer/octicons-react';
33
import { CSSTransition, TransitionGroup } from 'react-transition-group';
44

55
import { AppContext } from '../context/App';
@@ -18,7 +18,8 @@ export const RepositoryNotifications: React.FC<IProps> = ({
1818
repoNotifications,
1919
hostname,
2020
}) => {
21-
const { markRepoNotifications } = useContext(AppContext);
21+
const { markRepoNotifications, markRepoNotificationsDone } =
22+
useContext(AppContext);
2223

2324
const openBrowser = useCallback(() => {
2425
const url = repoNotifications[0].repository.html_url;
@@ -30,6 +31,11 @@ export const RepositoryNotifications: React.FC<IProps> = ({
3031
markRepoNotifications(repoSlug, hostname);
3132
}, [repoNotifications, hostname]);
3233

34+
const markRepoAsDone = useCallback(() => {
35+
const repoSlug = repoNotifications[0].repository.full_name;
36+
markRepoNotificationsDone(repoSlug, hostname);
37+
}, [repoNotifications, hostname]);
38+
3339
const avatarUrl = repoNotifications[0].repository.owner.avatar_url;
3440

3541
return (
@@ -40,7 +46,17 @@ export const RepositoryNotifications: React.FC<IProps> = ({
4046
<span onClick={openBrowser}>{repoName}</span>
4147
</div>
4248

43-
<div className="flex justify-center items-center">
49+
<div className="flex justify-center items-center gap-2">
50+
<button
51+
className="focus:outline-none h-full hover:text-green-500"
52+
title="Mark Repository as Done"
53+
onClick={markRepoAsDone}
54+
>
55+
<CheckIcon size={16} aria-label="Mark Repository as Done" />
56+
</button>
57+
58+
<div className="w-[14px]" />
59+
4460
<button
4561
className="focus:outline-none h-full hover:text-green-500"
4662
title="Mark Repository as Read"

src/context/App.tsx

+9
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ interface AppContextState {
5656
markNotificationDone: (id: string, hostname: string) => Promise<void>;
5757
unsubscribeNotification: (id: string, hostname: string) => Promise<void>;
5858
markRepoNotifications: (id: string, hostname: string) => Promise<void>;
59+
markRepoNotificationsDone: (id: string, hostname: string) => Promise<void>;
5960

6061
settings: SettingsState;
6162
updateSetting: (name: keyof SettingsState, value: any) => void;
@@ -75,6 +76,7 @@ export const AppProvider = ({ children }: { children: React.ReactNode }) => {
7576
markNotificationDone,
7677
unsubscribeNotification,
7778
markRepoNotifications,
79+
markRepoNotificationsDone,
7880
} = useNotifications(settings.colors);
7981

8082
useEffect(() => {
@@ -197,6 +199,12 @@ export const AppProvider = ({ children }: { children: React.ReactNode }) => {
197199
[accounts, notifications],
198200
);
199201

202+
const markRepoNotificationsDoneWithAccounts = useCallback(
203+
async (repoSlug: string, hostname: string) =>
204+
await markRepoNotificationsDone(accounts, repoSlug, hostname),
205+
[accounts, notifications],
206+
);
207+
200208
return (
201209
<AppContext.Provider
202210
value={{
@@ -215,6 +223,7 @@ export const AppProvider = ({ children }: { children: React.ReactNode }) => {
215223
markNotificationDone: markNotificationDoneWithAccounts,
216224
unsubscribeNotification: unsubscribeNotificationWithAccounts,
217225
markRepoNotifications: markRepoNotificationsWithAccounts,
226+
markRepoNotificationsDone: markRepoNotificationsDoneWithAccounts,
218227

219228
settings,
220229
updateSetting,

src/hooks/useNotifications.ts

+47
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ interface NotificationsState {
4343
repoSlug: string,
4444
hostname: string,
4545
) => Promise<void>;
46+
markRepoNotificationsDone: (
47+
accounts: AuthState,
48+
repoSlug: string,
49+
hostname: string,
50+
) => Promise<void>;
4651
isFetching: boolean;
4752
requestFailed: boolean;
4853
}
@@ -312,6 +317,47 @@ export const useNotifications = (colors: boolean): NotificationsState => {
312317
[notifications],
313318
);
314319

320+
const markRepoNotificationsDone = useCallback(
321+
async (accounts, repoSlug, hostname) => {
322+
setIsFetching(true);
323+
324+
try {
325+
const accountIndex = notifications.findIndex(
326+
(accountNotifications) => accountNotifications.hostname === hostname,
327+
);
328+
329+
if (accountIndex !== -1) {
330+
const notificationsToRemove = notifications[
331+
accountIndex
332+
].notifications.filter(
333+
(notification) => notification.repository.full_name === repoSlug,
334+
);
335+
336+
for (const notification of notificationsToRemove) {
337+
await markNotificationDone(
338+
accounts,
339+
notification.id,
340+
notifications[accountIndex].hostname,
341+
);
342+
}
343+
}
344+
345+
const updatedNotifications = removeNotifications(
346+
repoSlug,
347+
notifications,
348+
hostname,
349+
);
350+
351+
setNotifications(updatedNotifications);
352+
setTrayIconColor(updatedNotifications);
353+
setIsFetching(false);
354+
} catch (err) {
355+
setIsFetching(false);
356+
}
357+
},
358+
[notifications],
359+
);
360+
315361
return {
316362
isFetching,
317363
requestFailed,
@@ -322,5 +368,6 @@ export const useNotifications = (colors: boolean): NotificationsState => {
322368
markNotificationDone,
323369
unsubscribeNotification,
324370
markRepoNotifications,
371+
markRepoNotificationsDone,
325372
};
326373
};

0 commit comments

Comments
 (0)