Skip to content

Commit 038710c

Browse files
author
ntwigg
committed
Idiomatic typescript.
1 parent 3e676e3 commit 038710c

File tree

4 files changed

+44
-48
lines changed

4 files changed

+44
-48
lines changed

browser-extension/src/datamodel/handler-registry.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CommentType, CommentContext, TextareaHandler, TextareaInfo } from './textarea-handler';
1+
import { CommentContext, TextareaHandler, TextareaInfo } from './textarea-handler';
22
import { GitHubHandler } from '../handlers/github-handler';
33
import { RedditHandler } from '../handlers/reddit-handler';
44

@@ -15,7 +15,7 @@ export class HandlerRegistry {
1515
this.handlers.add(handler);
1616
}
1717

18-
getHandlerForType(type: CommentType): TextareaHandler<any> | null {
18+
getHandlerForType(type: string): TextareaHandler<any> | null {
1919
for (const handler of this.handlers) {
2020
if (handler.forCommentTypes().includes(type)) {
2121
return handler;
@@ -43,7 +43,7 @@ export class HandlerRegistry {
4343
return Array.from(this.handlers);
4444
}
4545

46-
getCommentTypesForHandler(handler: TextareaHandler<any>): CommentType[] {
46+
getCommentTypesForHandler(handler: TextareaHandler<any>): string[] {
4747
return handler.forCommentTypes();
4848
}
4949
}

browser-extension/src/datamodel/textarea-handler.ts

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,17 @@
1-
export type CommentType =
2-
| 'GH_ISSUE_NEW'
3-
| 'GH_PR_NEW'
4-
| 'GH_ISSUE_ADD_COMMENT'
5-
| 'GH_ISSUE_EDIT_COMMENT'
6-
| 'GH_PR_ADD_COMMENT'
7-
| 'GH_PR_EDIT_COMMENT'
8-
| 'GH_PR_CODE_COMMENT'
9-
| 'REDDIT_POST_NEW'
10-
| 'REDDIT_COMMENT_NEW'
11-
| 'REDDIT_COMMENT_EDIT'
12-
| 'GL_ISSUE_NEW'
13-
| 'GL_MR_NEW'
14-
| 'GL_ISSUE_ADD_COMMENT'
15-
| 'GL_MR_ADD_COMMENT'
16-
| 'BB_ISSUE_NEW'
17-
| 'BB_PR_NEW'
18-
| 'BB_ISSUE_ADD_COMMENT'
19-
| 'BB_PR_ADD_COMMENT';
201

212
export interface CommentContext {
223
unique_key: string;
234
}
245

256
export interface TextareaInfo<T extends CommentContext = CommentContext> {
267
element: HTMLTextAreaElement;
27-
type: CommentType;
8+
type: string;
289
context: T;
2910
}
3011

3112
export interface TextareaHandler<T extends CommentContext = CommentContext> {
3213
// Handler metadata
33-
forCommentTypes(): CommentType[];
14+
forCommentTypes(): string[];
3415

3516
// Content script functionality
3617
identify(): TextareaInfo<T>[];
@@ -40,11 +21,11 @@ export interface TextareaHandler<T extends CommentContext = CommentContext> {
4021

4122
// Context extraction
4223
extractContext(textarea: HTMLTextAreaElement): T | null;
43-
determineType(textarea: HTMLTextAreaElement): CommentType | null;
24+
determineType(textarea: HTMLTextAreaElement): string | null;
4425

4526
// Popup functionality helpers
4627
generateDisplayTitle(context: T): string;
47-
generateIcon(type: CommentType): string;
28+
generateIcon(type: string): string;
4829
buildUrl(context: T, withDraft?: boolean): string;
4930
}
5031

@@ -55,12 +36,12 @@ export abstract class BaseTextareaHandler<T extends CommentContext = CommentCont
5536
this.domain = domain;
5637
}
5738

58-
abstract forCommentTypes(): CommentType[];
39+
abstract forCommentTypes(): string[];
5940
abstract identify(): TextareaInfo<T>[];
6041
abstract extractContext(textarea: HTMLTextAreaElement): T | null;
61-
abstract determineType(textarea: HTMLTextAreaElement): CommentType | null;
42+
abstract determineType(textarea: HTMLTextAreaElement): string | null;
6243
abstract generateDisplayTitle(context: T): string;
63-
abstract generateIcon(type: CommentType): string;
44+
abstract generateIcon(type: string): string;
6445
abstract buildUrl(context: T, withDraft?: boolean): string;
6546

6647
readContent(textarea: HTMLTextAreaElement): string {

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
1-
import { CommentType, CommentContext, BaseTextareaHandler, TextareaInfo } from '../datamodel/textarea-handler';
1+
import { CommentContext, BaseTextareaHandler, TextareaInfo } from '../datamodel/textarea-handler';
2+
3+
export type GitHubCommentType =
4+
| 'GH_ISSUE_NEW'
5+
| 'GH_PR_NEW'
6+
| 'GH_ISSUE_ADD_COMMENT'
7+
| 'GH_ISSUE_EDIT_COMMENT'
8+
| 'GH_PR_ADD_COMMENT'
9+
| 'GH_PR_EDIT_COMMENT'
10+
| 'GH_PR_CODE_COMMENT';
211

312
export interface GitHubContext extends CommentContext {
413
domain: string;
514
slug: string; // owner/repo
6-
number?: number; // issue/PR number
7-
commentId?: string; // for editing existing comments
15+
number?: number | undefined; // issue/PR number
16+
commentId?: string | undefined; // for editing existing comments
817
}
918

1019
export class GitHubHandler extends BaseTextareaHandler<GitHubContext> {
1120
constructor() {
1221
super('github.com');
1322
}
1423

15-
forCommentTypes(): CommentType[] {
24+
forCommentTypes(): string[] {
1625
return [
1726
'GH_ISSUE_NEW',
1827
'GH_PR_NEW',
@@ -41,7 +50,6 @@ export class GitHubHandler extends BaseTextareaHandler<GitHubContext> {
4150
}
4251

4352
extractContext(textarea: HTMLTextAreaElement): GitHubContext | null {
44-
const url = window.location.href;
4553
const pathname = window.location.pathname;
4654

4755
// Parse GitHub URL structure: /owner/repo/issues/123 or /owner/repo/pull/456
@@ -71,11 +79,11 @@ export class GitHubHandler extends BaseTextareaHandler<GitHubContext> {
7179
domain: window.location.hostname,
7280
slug,
7381
number,
74-
commentId
82+
commentId: commentId || undefined
7583
};
7684
}
7785

78-
determineType(textarea: HTMLTextAreaElement): CommentType | null {
86+
determineType(textarea: HTMLTextAreaElement): GitHubCommentType | null {
7987
const pathname = window.location.pathname;
8088

8189
// New issue
@@ -124,7 +132,7 @@ export class GitHubHandler extends BaseTextareaHandler<GitHubContext> {
124132
return `New ${window.location.pathname.includes('/issues/') ? 'issue' : 'PR'} in ${slug}`;
125133
}
126134

127-
generateIcon(type: CommentType): string {
135+
generateIcon(type: string): string {
128136
switch (type) {
129137
case 'GH_ISSUE_NEW':
130138
case 'GH_ISSUE_ADD_COMMENT':
@@ -141,7 +149,7 @@ export class GitHubHandler extends BaseTextareaHandler<GitHubContext> {
141149
}
142150
}
143151

144-
buildUrl(context: GitHubContext, withDraft?: boolean): string {
152+
buildUrl(context: GitHubContext): string {
145153
const baseUrl = `https://${context.domain}/${context.slug}`;
146154

147155
if (context.number) {

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

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1-
import { CommentType, CommentContext, BaseTextareaHandler, TextareaInfo } from '../datamodel/textarea-handler';
1+
import { CommentContext, BaseTextareaHandler, TextareaInfo } from '../datamodel/textarea-handler';
2+
3+
export type RedditCommentType =
4+
| 'REDDIT_POST_NEW'
5+
| 'REDDIT_COMMENT_NEW'
6+
| 'REDDIT_COMMENT_EDIT';
27

38
export interface RedditContext extends CommentContext {
49
subreddit: string;
5-
postId?: string;
6-
commentId?: string; // for editing existing comments
10+
postId?: string | undefined;
11+
commentId?: string | undefined; // for editing existing comments
712
}
813

914
export class RedditHandler extends BaseTextareaHandler<RedditContext> {
1015
constructor() {
1116
super('reddit.com');
1217
}
1318

14-
forCommentTypes(): CommentType[] {
19+
forCommentTypes(): string[] {
1520
return [
1621
'REDDIT_POST_NEW',
1722
'REDDIT_COMMENT_NEW',
@@ -43,7 +48,7 @@ export class RedditHandler extends BaseTextareaHandler<RedditContext> {
4348
const submitMatch = pathname.match(/^\/r\/([^\/]+)\/submit/);
4449
const subredditMatch = pathname.match(/^\/r\/([^\/]+)/);
4550

46-
let subreddit: string;
51+
let subreddit: string | undefined;
4752
let postId: string | undefined;
4853

4954
if (postMatch) {
@@ -52,7 +57,9 @@ export class RedditHandler extends BaseTextareaHandler<RedditContext> {
5257
[, subreddit] = submitMatch;
5358
} else if (subredditMatch) {
5459
[, subreddit] = subredditMatch;
55-
} else {
60+
}
61+
62+
if (!subreddit) {
5663
return null;
5764
}
5865

@@ -74,11 +81,11 @@ export class RedditHandler extends BaseTextareaHandler<RedditContext> {
7481
unique_key,
7582
subreddit,
7683
postId,
77-
commentId
84+
commentId: commentId || undefined
7885
};
7986
}
8087

81-
determineType(textarea: HTMLTextAreaElement): CommentType | null {
88+
determineType(textarea: HTMLTextAreaElement): RedditCommentType | null {
8289
const pathname = window.location.pathname;
8390

8491
// New post submission
@@ -109,7 +116,7 @@ export class RedditHandler extends BaseTextareaHandler<RedditContext> {
109116
return `New post in r/${subreddit}`;
110117
}
111118

112-
generateIcon(type: CommentType): string {
119+
generateIcon(type: string): string {
113120
switch (type) {
114121
case 'REDDIT_POST_NEW':
115122
return '📝'; // Post icon
@@ -122,7 +129,7 @@ export class RedditHandler extends BaseTextareaHandler<RedditContext> {
122129
}
123130
}
124131

125-
buildUrl(context: RedditContext, withDraft?: boolean): string {
132+
buildUrl(context: RedditContext): string {
126133
const baseUrl = `https://reddit.com/r/${context.subreddit}`;
127134

128135
if (context.postId) {

0 commit comments

Comments
 (0)