Skip to content

Commit 2f5d81d

Browse files
committed
simplify GitHubEnhancer down to GitHubAddCommentEnhancer
1 parent 1deaf66 commit 2f5d81d

File tree

2 files changed

+23
-64
lines changed

2 files changed

+23
-64
lines changed

browser-extension/src/lib/enhancers/github.ts

Lines changed: 21 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { OverType } from '../../overtype/mock-overtype'
22
import type { CommentEnhancer, CommentSpot } from '../enhancer'
33

44
const GITHUB_SPOT_TYPES = [
5+
'GH_PR_ADD_COMMENT',
6+
/* TODO
57
'GH_ISSUE_NEW',
68
'GH_PR_NEW',
79
'GH_ISSUE_ADD_COMMENT',
8-
'GH_PR_ADD_COMMENT',
9-
/* TODO
1010
'GH_ISSUE_EDIT_COMMENT',
1111
'GH_PR_EDIT_COMMENT',
1212
'GH_PR_CODE_COMMENT',
@@ -15,95 +15,54 @@ const GITHUB_SPOT_TYPES = [
1515

1616
export type GitHubSpotType = (typeof GITHUB_SPOT_TYPES)[number]
1717

18-
export interface GitHubSpot extends CommentSpot {
18+
export interface GitHubAddCommentSpot extends CommentSpot {
1919
type: GitHubSpotType // Override to narrow from string to specific union
2020
domain: string
2121
slug: string // owner/repo
22-
number?: number | undefined // issue/PR number, undefined for new issues and PRs
22+
number: number // issue/PR number, undefined for new issues and PRs
2323
}
2424

25-
export class GitHubEnhancer implements CommentEnhancer<GitHubSpot> {
25+
export class GitHubAddCommentEnhancer implements CommentEnhancer<GitHubAddCommentSpot> {
2626
forSpotTypes(): string[] {
2727
return [...GITHUB_SPOT_TYPES]
2828
}
2929

30-
tryToEnhance(textarea: HTMLTextAreaElement): [OverType, GitHubSpot] | null {
31-
// Only handle GitHub domains
32-
if (!window.location.hostname.includes('github')) {
30+
tryToEnhance(textarea: HTMLTextAreaElement): [OverType, GitHubAddCommentSpot] | null {
31+
// Only handle github.com domains TODO: identify GitHub Enterprise somehow
32+
if (window.location.hostname !== 'github.com') {
3333
return null
3434
}
3535

36-
const pathname = window.location.pathname
37-
3836
// Parse GitHub URL structure: /owner/repo/issues/123 or /owner/repo/pull/456
39-
const match = pathname.match(/^\/([^/]+)\/([^/]+)(?:\/(issues|pull)\/(\d+))?/)
37+
const match = window.location.pathname.match(/^\/([^/]+)\/([^/]+)(?:\/pull\/(\d+))/)
4038
if (!match) return null
41-
42-
const [, owner, repo, urlType, numberStr] = match
39+
const [, owner, repo, numberStr] = match
4340
const slug = `${owner}/${repo}`
44-
const number = numberStr ? parseInt(numberStr, 10) : undefined
45-
46-
// Determine comment type
47-
let type: GitHubSpotType
41+
const number = parseInt(numberStr!, 10)
4842

49-
if (pathname.includes('/issues/new')) {
50-
type = 'GH_ISSUE_NEW'
51-
} else if (pathname.includes('/compare/') || pathname.endsWith('/compare')) {
52-
type = 'GH_PR_NEW'
53-
} else if (urlType && number) {
54-
if (urlType === 'issues') {
55-
type = 'GH_ISSUE_ADD_COMMENT'
56-
} else {
57-
type = 'GH_PR_ADD_COMMENT'
58-
}
59-
} else {
60-
return null
61-
}
62-
63-
// Generate unique key based on context
64-
let unique_key = `github:${slug}`
65-
if (number) {
66-
unique_key += `:${urlType}:${number}`
67-
} else {
68-
unique_key += ':new'
69-
}
43+
const unique_key = `github.com:${slug}:${number}`
7044

71-
const spot: GitHubSpot = {
72-
domain: window.location.hostname,
45+
const spot: GitHubAddCommentSpot = {
46+
domain: 'github.com',
7347
number,
7448
slug,
75-
type,
49+
type: 'GH_PR_ADD_COMMENT',
7650
unique_key,
7751
}
7852
const overtype = new OverType(textarea)
7953
return [overtype, spot]
8054
}
8155

82-
tableTitle(spot: GitHubSpot): string {
56+
tableTitle(spot: GitHubAddCommentSpot): string {
8357
const { slug, number } = spot
84-
if (number) {
85-
return `Comment on ${slug} #${number}`
86-
}
87-
return `New ${window.location.pathname.includes('/issues/') ? 'issue' : 'PR'} in ${slug}`
58+
return `${slug} PR #${number}`
8859
}
8960

90-
tableIcon(spot: GitHubSpot): string {
91-
switch (spot.type) {
92-
case 'GH_ISSUE_NEW':
93-
case 'GH_ISSUE_ADD_COMMENT':
94-
return '🐛' // Issue icon
95-
case 'GH_PR_NEW':
96-
case 'GH_PR_ADD_COMMENT':
97-
return '🔄' // PR icon
98-
}
61+
tableIcon(_: GitHubAddCommentSpot): string {
62+
return '🔄' // PR icon TODO: icon urls in /public
9963
}
10064

101-
buildUrl(spot: GitHubSpot): string {
102-
const baseUrl = `https://${spot.domain}/${spot.slug}`
103-
if (spot.number) {
104-
const type = spot.type.indexOf('ISSUE') ? 'issues' : 'pull'
105-
return `${baseUrl}/${type}/${spot.number}`
106-
}
107-
return baseUrl
65+
buildUrl(spot: GitHubAddCommentSpot): string {
66+
return `https://${spot.domain}/${spot.slug}/pull/${spot.number}`
10867
}
10968
}

browser-extension/src/lib/registries.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { OverType } from '../overtype/mock-overtype'
22
import type { CommentEnhancer, CommentSpot } from './enhancer'
3-
import { GitHubEnhancer } from './enhancers/github'
3+
import { GitHubAddCommentEnhancer } from './enhancers/github'
44

55
export interface EnhancedTextarea<T extends CommentSpot = CommentSpot> {
66
textarea: HTMLTextAreaElement
@@ -14,7 +14,7 @@ export class EnhancerRegistry {
1414

1515
constructor() {
1616
// Register all available handlers
17-
this.register(new GitHubEnhancer())
17+
this.register(new GitHubAddCommentEnhancer())
1818
}
1919

2020
private register<T extends CommentSpot>(handler: CommentEnhancer<T>): void {

0 commit comments

Comments
 (0)