Skip to content

Commit d35d4ae

Browse files
author
ntwigg
committed
Fixup unnecessary splitting.
1 parent 88564d9 commit d35d4ae

File tree

2 files changed

+62
-85
lines changed

2 files changed

+62
-85
lines changed

browser-extension/src/handlers/github-handler.ts

Lines changed: 41 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -36,85 +36,73 @@ export class GitHubHandler implements TextareaHandler<GitHubContext> {
3636
return null;
3737
}
3838

39-
const type = this.determineType(textarea);
40-
const context = this.extractContext(textarea);
41-
42-
if (type && context) {
43-
return { element: textarea, context: { ...context, type } };
44-
}
45-
46-
return null;
47-
}
48-
49-
50-
private extractContext(textarea: HTMLTextAreaElement): GitHubContext | null {
5139
const pathname = window.location.pathname;
5240

5341
// Parse GitHub URL structure: /owner/repo/issues/123 or /owner/repo/pull/456
5442
const match = pathname.match(/^\/([^\/]+)\/([^\/]+)(?:\/(issues|pull)\/(\d+))?/);
5543
if (!match) return null;
5644

57-
const [, owner, repo, type, numberStr] = match;
45+
const [, owner, repo, urlType, numberStr] = match;
5846
const slug = `${owner}/${repo}`;
5947
const number = numberStr ? parseInt(numberStr, 10) : undefined;
6048

49+
// Check if editing existing comment
50+
const commentId = this.getCommentId(textarea);
51+
52+
// Determine comment type
53+
let type: GitHubCommentType;
54+
55+
// New issue
56+
if (pathname.includes('/issues/new')) {
57+
type = 'GH_ISSUE_NEW';
58+
}
59+
// New PR
60+
else if (pathname.includes('/compare/') || pathname.endsWith('/compare')) {
61+
type = 'GH_PR_NEW';
62+
}
63+
// Existing issue or PR page
64+
else if (urlType && number) {
65+
const isEditingComment = commentId !== null;
66+
67+
if (urlType === 'issues') {
68+
type = isEditingComment ? 'GH_ISSUE_EDIT_COMMENT' : 'GH_ISSUE_ADD_COMMENT';
69+
} else {
70+
// Check if it's a code comment (in Files Changed tab)
71+
const isCodeComment = textarea.closest('.js-inline-comment-form') !== null ||
72+
textarea.closest('[data-path]') !== null;
73+
74+
if (isCodeComment) {
75+
type = 'GH_PR_CODE_COMMENT';
76+
} else {
77+
type = isEditingComment ? 'GH_PR_EDIT_COMMENT' : 'GH_PR_ADD_COMMENT';
78+
}
79+
}
80+
} else {
81+
return null;
82+
}
83+
6184
// Generate unique key based on context
6285
let unique_key = `github:${slug}`;
6386
if (number) {
64-
unique_key += `:${type}:${number}`;
87+
unique_key += `:${urlType}:${number}`;
6588
} else {
6689
unique_key += ':new';
6790
}
68-
69-
// Check if editing existing comment
70-
const commentId = this.getCommentId(textarea);
91+
7192
if (commentId) {
7293
unique_key += `:edit:${commentId}`;
7394
}
7495

75-
return {
96+
const context: GitHubContext = {
7697
unique_key,
77-
type: '', // Will be set by caller
98+
type,
7899
domain: window.location.hostname,
79100
slug,
80101
number,
81102
commentId: commentId || undefined
82103
};
83-
}
84104

85-
private determineType(textarea: HTMLTextAreaElement): GitHubCommentType | null {
86-
const pathname = window.location.pathname;
87-
88-
// New issue
89-
if (pathname.includes('/issues/new')) {
90-
return 'GH_ISSUE_NEW';
91-
}
92-
93-
// New PR
94-
if (pathname.includes('/compare/') || pathname.endsWith('/compare')) {
95-
return 'GH_PR_NEW';
96-
}
97-
98-
// Check if we're on an issue or PR page
99-
const match = pathname.match(/\/(issues|pull)\/(\d+)/);
100-
if (!match) return null;
101-
102-
const [, type] = match;
103-
const isEditingComment = this.getCommentId(textarea) !== null;
104-
105-
if (type === 'issues') {
106-
return isEditingComment ? 'GH_ISSUE_EDIT_COMMENT' : 'GH_ISSUE_ADD_COMMENT';
107-
} else {
108-
// Check if it's a code comment (in Files Changed tab)
109-
const isCodeComment = textarea.closest('.js-inline-comment-form') !== null ||
110-
textarea.closest('[data-path]') !== null;
111-
112-
if (isCodeComment) {
113-
return 'GH_PR_CODE_COMMENT';
114-
}
115-
116-
return isEditingComment ? 'GH_PR_EDIT_COMMENT' : 'GH_PR_ADD_COMMENT';
117-
}
105+
return { element: textarea, context };
118106
}
119107

120108
generateDisplayTitle(context: GitHubContext): string {

browser-extension/src/handlers/reddit-handler.ts

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,6 @@ export class RedditHandler implements TextareaHandler<RedditContext> {
2727
return null;
2828
}
2929

30-
const type = this.determineType(textarea);
31-
const context = this.extractContext(textarea);
32-
33-
if (type && context) {
34-
return { element: textarea, context: { ...context, type } };
35-
}
36-
37-
return null;
38-
}
39-
40-
41-
private extractContext(textarea: HTMLTextAreaElement): RedditContext | null {
4230
const pathname = window.location.pathname;
4331

4432
// Parse Reddit URL structure: /r/subreddit/comments/postid/title/
@@ -61,6 +49,24 @@ export class RedditHandler implements TextareaHandler<RedditContext> {
6149
return null;
6250
}
6351

52+
// Check if editing existing comment
53+
const commentId = this.getCommentId(textarea);
54+
55+
// Determine comment type
56+
let type: RedditCommentType;
57+
58+
// New post submission
59+
if (pathname.includes('/submit')) {
60+
type = 'REDDIT_POST_NEW';
61+
}
62+
// Check if we're on a post page
63+
else if (pathname.match(/\/r\/[^\/]+\/comments\/[^\/]+/)) {
64+
const isEditingComment = commentId !== null;
65+
type = isEditingComment ? 'REDDIT_COMMENT_EDIT' : 'REDDIT_COMMENT_NEW';
66+
} else {
67+
return null;
68+
}
69+
6470
// Generate unique key
6571
let unique_key = `reddit:${subreddit}`;
6672
if (postId) {
@@ -69,36 +75,19 @@ export class RedditHandler implements TextareaHandler<RedditContext> {
6975
unique_key += ':new';
7076
}
7177

72-
// Check if editing existing comment
73-
const commentId = this.getCommentId(textarea);
7478
if (commentId) {
7579
unique_key += `:edit:${commentId}`;
7680
}
7781

78-
return {
82+
const context: RedditContext = {
7983
unique_key,
80-
type: '', // Will be set by caller
84+
type,
8185
subreddit,
8286
postId,
8387
commentId: commentId || undefined
8488
};
85-
}
8689

87-
private determineType(textarea: HTMLTextAreaElement): RedditCommentType | null {
88-
const pathname = window.location.pathname;
89-
90-
// New post submission
91-
if (pathname.includes('/submit')) {
92-
return 'REDDIT_POST_NEW';
93-
}
94-
95-
// Check if we're on a post page
96-
if (pathname.match(/\/r\/[^\/]+\/comments\/[^\/]+/)) {
97-
const isEditingComment = this.getCommentId(textarea) !== null;
98-
return isEditingComment ? 'REDDIT_COMMENT_EDIT' : 'REDDIT_COMMENT_NEW';
99-
}
100-
101-
return null;
90+
return { element: textarea, context };
10291
}
10392

10493
generateDisplayTitle(context: RedditContext): string {

0 commit comments

Comments
 (0)