Skip to content

Commit 60c0ced

Browse files
authored
refactor: extract feature based utils (#1672)
Signed-off-by: Adam Setch <adam.setch@outlook.com>
1 parent e9c5293 commit 60c0ced

File tree

8 files changed

+147
-142
lines changed

8 files changed

+147
-142
lines changed

src/renderer/components/NotificationRow.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ import { AppContext } from '../context/App';
1010
import { Opacity, Size } from '../types';
1111
import type { Notification } from '../typesGitHub';
1212
import { cn } from '../utils/cn';
13-
import {
14-
formatForDisplay,
15-
isMarkAsDoneFeatureSupported,
16-
} from '../utils/helpers';
13+
import { isMarkAsDoneFeatureSupported } from '../utils/features';
14+
import { formatForDisplay } from '../utils/helpers';
1715
import {
1816
getNotificationTypeIcon,
1917
getNotificationTypeIconColor,

src/renderer/components/RepositoryNotifications.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ import { AppContext } from '../context/App';
44
import { Opacity, Size } from '../types';
55
import type { Notification } from '../typesGitHub';
66
import { cn } from '../utils/cn';
7-
import {
8-
getChevronDetails,
9-
isMarkAsDoneFeatureSupported,
10-
} from '../utils/helpers';
7+
import { isMarkAsDoneFeatureSupported } from '../utils/features';
8+
import { getChevronDetails } from '../utils/helpers';
119
import { openRepository } from '../utils/links';
1210
import { HoverGroup } from './HoverGroup';
1311
import { NotificationRow } from './NotificationRow';

src/renderer/hooks/useNotifications.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
markNotificationThreadAsDone,
1414
markNotificationThreadAsRead,
1515
} from '../utils/api/client';
16-
import { isMarkAsDoneFeatureSupported } from '../utils/helpers';
16+
import { isMarkAsDoneFeatureSupported } from '../utils/features';
1717
import {
1818
getAllNotifications,
1919
setTrayIconColor,

src/renderer/utils/api/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import type {
2222
Release,
2323
UserDetails,
2424
} from '../../typesGitHub';
25-
import { isAnsweredDiscussionFeatureSupported } from '../helpers';
25+
import { isAnsweredDiscussionFeatureSupported } from '../features';
2626
import { QUERY_SEARCH_DISCUSSIONS } from './graphql/discussions';
2727
import { formatAsGitHubSearchSyntax } from './graphql/utils';
2828
import { apiRequestAuth } from './request';

src/renderer/utils/features.test.ts

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import {
2+
mockGitHubCloudAccount,
3+
mockGitHubEnterpriseServerAccount,
4+
} from '../__mocks__/state-mocks';
5+
6+
import {
7+
isAnsweredDiscussionFeatureSupported,
8+
isMarkAsDoneFeatureSupported,
9+
} from './features';
10+
11+
describe('renderer/utils/features.ts', () => {
12+
describe('isMarkAsDoneFeatureSupported', () => {
13+
it('should return true for GitHub Cloud', () => {
14+
expect(isMarkAsDoneFeatureSupported(mockGitHubCloudAccount)).toBe(true);
15+
});
16+
17+
it('should return false for GitHub Enterprise Server < v3.13', () => {
18+
const account = {
19+
...mockGitHubEnterpriseServerAccount,
20+
version: '3.12.0',
21+
};
22+
23+
expect(isMarkAsDoneFeatureSupported(account)).toBe(false);
24+
});
25+
26+
it('should return true for GitHub Enterprise Server >= v3.13', () => {
27+
const account = {
28+
...mockGitHubEnterpriseServerAccount,
29+
version: '3.13.3',
30+
};
31+
32+
expect(isMarkAsDoneFeatureSupported(account)).toBe(true);
33+
});
34+
35+
it('should return false for GitHub Enterprise Server when partial version available', () => {
36+
const account = {
37+
...mockGitHubEnterpriseServerAccount,
38+
version: '3',
39+
};
40+
41+
expect(isMarkAsDoneFeatureSupported(account)).toBe(false);
42+
});
43+
44+
it('should return false for GitHub Enterprise Server when no version available', () => {
45+
const account = {
46+
...mockGitHubEnterpriseServerAccount,
47+
version: null,
48+
};
49+
50+
expect(isMarkAsDoneFeatureSupported(account)).toBe(false);
51+
});
52+
});
53+
54+
describe('isAnsweredDiscussionFeatureSupported', () => {
55+
it('should return true for GitHub Cloud', () => {
56+
expect(isAnsweredDiscussionFeatureSupported(mockGitHubCloudAccount)).toBe(
57+
true,
58+
);
59+
});
60+
61+
it('should return false for GitHub Enterprise Server < v3.12', () => {
62+
const account = {
63+
...mockGitHubEnterpriseServerAccount,
64+
version: '3.11.0',
65+
};
66+
67+
expect(isAnsweredDiscussionFeatureSupported(account)).toBe(false);
68+
});
69+
70+
it('should return true for GitHub Enterprise Server >= v3.12', () => {
71+
const account = {
72+
...mockGitHubEnterpriseServerAccount,
73+
version: '3.12.3',
74+
};
75+
76+
expect(isAnsweredDiscussionFeatureSupported(account)).toBe(true);
77+
});
78+
79+
it('should return false for GitHub Enterprise Server when partial version available', () => {
80+
const account = {
81+
...mockGitHubEnterpriseServerAccount,
82+
version: '3',
83+
};
84+
85+
expect(isAnsweredDiscussionFeatureSupported(account)).toBe(false);
86+
});
87+
88+
it('should return false for GitHub Enterprise Server when no version available', () => {
89+
const account = {
90+
...mockGitHubEnterpriseServerAccount,
91+
version: null,
92+
};
93+
94+
expect(isMarkAsDoneFeatureSupported(account)).toBe(false);
95+
});
96+
});
97+
});

src/renderer/utils/features.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import type { Account } from '../types';
2+
import { isEnterpriseServerHost } from './helpers';
3+
4+
/**
5+
* Check if the "Mark as done" feature is supported for the given account.
6+
*
7+
* GitHub Cloud or GitHub Enterprise Server 3.13 or newer is required to support this feature.
8+
*/
9+
10+
export function isMarkAsDoneFeatureSupported(account: Account): boolean {
11+
if (isEnterpriseServerHost(account.hostname)) {
12+
if (account.version) {
13+
const version = account?.version.split('.').map(Number);
14+
return version[0] >= 3 && version[1] >= 13;
15+
}
16+
17+
return false;
18+
}
19+
20+
return true;
21+
}
22+
/**
23+
* Check if the "answered" discussions are supported for the given account.
24+
*
25+
* GitHub Cloud or GitHub Enterprise Server 3.12 or newer is required to support this feature.
26+
*/
27+
28+
export function isAnsweredDiscussionFeatureSupported(
29+
account: Account,
30+
): boolean {
31+
if (isEnterpriseServerHost(account.hostname)) {
32+
if (account.version) {
33+
const version = account?.version.split('.').map(Number);
34+
return version[0] >= 3 && version[1] >= 12;
35+
}
36+
37+
return false;
38+
}
39+
40+
return true;
41+
}

src/renderer/utils/helpers.test.ts

Lines changed: 2 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import type { AxiosPromise, AxiosResponse } from 'axios';
2-
import {
3-
mockGitHubCloudAccount,
4-
mockGitHubEnterpriseServerAccount,
5-
mockPersonalAccessTokenAccount,
6-
} from '../__mocks__/state-mocks';
2+
import { mockPersonalAccessTokenAccount } from '../__mocks__/state-mocks';
73

84
import {
95
ChevronDownIcon,
@@ -19,6 +15,7 @@ import {
1915
mockSingleNotification,
2016
} from './api/__mocks__/response-mocks';
2117
import * as apiRequests from './api/request';
18+
2219
import {
2320
formatForDisplay,
2421
formatNotificationUpdatedAt,
@@ -27,9 +24,7 @@ import {
2724
getChevronDetails,
2825
getFilterCount,
2926
getPlatformFromHostname,
30-
isAnsweredDiscussionFeatureSupported,
3127
isEnterpriseServerHost,
32-
isMarkAsDoneFeatureSupported,
3328
} from './helpers';
3429

3530
describe('renderer/utils/helpers.ts', () => {
@@ -69,92 +64,6 @@ describe('renderer/utils/helpers.ts', () => {
6964
});
7065
});
7166

72-
describe('isMarkAsDoneFeatureSupported', () => {
73-
it('should return true for GitHub Cloud', () => {
74-
expect(isMarkAsDoneFeatureSupported(mockGitHubCloudAccount)).toBe(true);
75-
});
76-
77-
it('should return false for GitHub Enterprise Server < v3.13', () => {
78-
const account = {
79-
...mockGitHubEnterpriseServerAccount,
80-
version: '3.12.0',
81-
};
82-
83-
expect(isMarkAsDoneFeatureSupported(account)).toBe(false);
84-
});
85-
86-
it('should return true for GitHub Enterprise Server >= v3.13', () => {
87-
const account = {
88-
...mockGitHubEnterpriseServerAccount,
89-
version: '3.13.3',
90-
};
91-
92-
expect(isMarkAsDoneFeatureSupported(account)).toBe(true);
93-
});
94-
95-
it('should return false for GitHub Enterprise Server when partial version available', () => {
96-
const account = {
97-
...mockGitHubEnterpriseServerAccount,
98-
version: '3',
99-
};
100-
101-
expect(isMarkAsDoneFeatureSupported(account)).toBe(false);
102-
});
103-
104-
it('should return false for GitHub Enterprise Server when no version available', () => {
105-
const account = {
106-
...mockGitHubEnterpriseServerAccount,
107-
version: null,
108-
};
109-
110-
expect(isMarkAsDoneFeatureSupported(account)).toBe(false);
111-
});
112-
});
113-
114-
describe('isAnsweredDiscussionFeatureSupported', () => {
115-
it('should return true for GitHub Cloud', () => {
116-
expect(isAnsweredDiscussionFeatureSupported(mockGitHubCloudAccount)).toBe(
117-
true,
118-
);
119-
});
120-
121-
it('should return false for GitHub Enterprise Server < v3.12', () => {
122-
const account = {
123-
...mockGitHubEnterpriseServerAccount,
124-
version: '3.11.0',
125-
};
126-
127-
expect(isAnsweredDiscussionFeatureSupported(account)).toBe(false);
128-
});
129-
130-
it('should return true for GitHub Enterprise Server >= v3.12', () => {
131-
const account = {
132-
...mockGitHubEnterpriseServerAccount,
133-
version: '3.12.3',
134-
};
135-
136-
expect(isAnsweredDiscussionFeatureSupported(account)).toBe(true);
137-
});
138-
139-
it('should return false for GitHub Enterprise Server when partial version available', () => {
140-
const account = {
141-
...mockGitHubEnterpriseServerAccount,
142-
version: '3',
143-
};
144-
145-
expect(isAnsweredDiscussionFeatureSupported(account)).toBe(false);
146-
});
147-
148-
it('should return false for GitHub Enterprise Server when no version available', () => {
149-
const account = {
150-
...mockGitHubEnterpriseServerAccount,
151-
version: null,
152-
};
153-
154-
expect(isMarkAsDoneFeatureSupported(account)).toBe(false);
155-
});
156-
});
157-
15867
describe('generateNotificationReferrerId', () => {
15968
it('should generate the notification_referrer_id', () => {
16069
const referrerId = generateNotificationReferrerId(mockSingleNotification);

src/renderer/utils/helpers.ts

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
import { formatDistanceToNowStrict, parseISO } from 'date-fns';
77
import log from 'electron-log';
88
import { defaultSettings } from '../context/App';
9-
import type { Account, Chevron, Hostname, Link, SettingsState } from '../types';
9+
import type { Chevron, Hostname, Link, SettingsState } from '../types';
1010
import type { Notification } from '../typesGitHub';
1111
import { getHtmlUrl, getLatestDiscussion } from './api/client';
1212
import type { PlatformType } from './auth/types';
@@ -27,44 +27,6 @@ export function isEnterpriseServerHost(hostname: Hostname): boolean {
2727
return !hostname.endsWith(Constants.DEFAULT_AUTH_OPTIONS.hostname);
2828
}
2929

30-
/**
31-
* Check if the "Mark as done" feature is supported for the given account.
32-
*
33-
* GitHub Cloud or GitHub Enterprise Server 3.13 or newer is required to support this feature.
34-
*/
35-
export function isMarkAsDoneFeatureSupported(account: Account): boolean {
36-
if (isEnterpriseServerHost(account.hostname)) {
37-
if (account.version) {
38-
const version = account?.version.split('.').map(Number);
39-
return version[0] >= 3 && version[1] >= 13;
40-
}
41-
42-
return false;
43-
}
44-
45-
return true;
46-
}
47-
48-
/**
49-
* Check if the "answered" discussions are supported for the given account.
50-
*
51-
* GitHub Cloud or GitHub Enterprise Server 3.12 or newer is required to support this feature.
52-
*/
53-
export function isAnsweredDiscussionFeatureSupported(
54-
account: Account,
55-
): boolean {
56-
if (isEnterpriseServerHost(account.hostname)) {
57-
if (account.version) {
58-
const version = account?.version.split('.').map(Number);
59-
return version[0] >= 3 && version[1] >= 12;
60-
}
61-
62-
return false;
63-
}
64-
65-
return true;
66-
}
67-
6830
export function generateNotificationReferrerId(
6931
notification: Notification,
7032
): string {

0 commit comments

Comments
 (0)