Skip to content

Commit 43aabe2

Browse files
authored
Refactor the GitHub implementation to have consistent naming (#66)
2 parents 7d65141 + fa5d639 commit 43aabe2

File tree

11 files changed

+110
-108
lines changed

11 files changed

+110
-108
lines changed

src/lib/enhancers/github/githubEditComment.tsx renamed to src/lib/enhancers/github/GitHubEditEnhancer.tsx

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,20 @@ import type React from 'react'
33
import type { CommentEnhancer, CommentSpot, StrippedLocation } from '@/lib/enhancer'
44
import { logger } from '@/lib/logger'
55
import { modifyDOM } from '../modifyDOM'
6-
import { commonGithubOptions } from './ghOptions'
7-
import { prepareGitHubHighlighter } from './githubHighlighter'
6+
import { commonGitHubOptions, prepareGitHubHighlighter } from './github-common'
87

9-
export interface GitHubEditCommentSpot extends CommentSpot {
10-
type: 'GH_EDIT_COMMENT'
8+
const GH_EDIT = 'GH_EDIT' as const
9+
10+
export interface GitHubEditSpot extends CommentSpot {
11+
type: typeof GH_EDIT
1112
}
1213

13-
export class GitHubEditCommentEnhancer implements CommentEnhancer<GitHubEditCommentSpot> {
14+
export class GitHubEditEnhancer implements CommentEnhancer<GitHubEditSpot> {
1415
forSpotTypes(): string[] {
15-
return ['GH_EDIT_COMMENT']
16+
return [GH_EDIT]
1617
}
1718

18-
tryToEnhance(
19-
textarea: HTMLTextAreaElement,
20-
location: StrippedLocation,
21-
): GitHubEditCommentSpot | null {
19+
tryToEnhance(textarea: HTMLTextAreaElement, location: StrippedLocation): GitHubEditSpot | null {
2220
if (location.host !== 'github.com') {
2321
return null
2422
}
@@ -44,27 +42,27 @@ export class GitHubEditCommentEnhancer implements CommentEnhancer<GitHubEditComm
4442

4543
logger.debug(`${this.constructor.name} enhanced issue/PR body textarea`, unique_key)
4644
return {
47-
type: 'GH_EDIT_COMMENT',
45+
type: GH_EDIT,
4846
unique_key,
4947
}
5048
}
5149

52-
enhance(textArea: HTMLTextAreaElement, _spot: GitHubEditCommentSpot): OverTypeInstance {
50+
enhance(textArea: HTMLTextAreaElement, _spot: GitHubEditSpot): OverTypeInstance {
5351
prepareGitHubHighlighter()
5452
const overtypeContainer = modifyDOM(textArea)
5553
return new OverType(overtypeContainer, {
56-
...commonGithubOptions,
54+
...commonGitHubOptions,
5755
minHeight: '102px',
5856
padding: 'var(--base-size-8)',
5957
placeholder: 'Add your comment here...',
6058
})[0]!
6159
}
6260

63-
tableUpperDecoration(_spot: GitHubEditCommentSpot): React.ReactNode {
61+
tableUpperDecoration(_spot: GitHubEditSpot): React.ReactNode {
6462
return <span>N/A</span>
6563
}
6664

67-
tableTitle(_spot: GitHubEditCommentSpot): string {
65+
tableTitle(_spot: GitHubEditSpot): string {
6866
return 'N/A'
6967
}
7068
}

src/lib/enhancers/github/githubIssueAddComment.tsx renamed to src/lib/enhancers/github/GitHubIssueAppendEnhancer.tsx

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,27 @@ import type React from 'react'
44
import type { CommentEnhancer, CommentSpot, StrippedLocation } from '@/lib/enhancer'
55
import { logger } from '@/lib/logger'
66
import { modifyDOM } from '../modifyDOM'
7-
import { commonGithubOptions } from './ghOptions'
8-
import { prepareGitHubHighlighter } from './githubHighlighter'
7+
import { commonGitHubOptions, prepareGitHubHighlighter } from './github-common'
98

10-
export interface GitHubIssueAddCommentSpot extends CommentSpot {
11-
type: 'GH_ISSUE_ADD_COMMENT'
9+
const GH_ISSUE_APPEND = 'GH_ISSUE_APPEND' as const
10+
11+
export interface GitHubIssueAppendSpot extends CommentSpot {
12+
type: typeof GH_ISSUE_APPEND
1213
title: string
1314
domain: string
1415
slug: string // owner/repo
1516
number: number // issue number, undefined for new issues
1617
}
1718

18-
export class GitHubIssueAddCommentEnhancer implements CommentEnhancer<GitHubIssueAddCommentSpot> {
19+
export class GitHubIssueAppendEnhancer implements CommentEnhancer<GitHubIssueAppendSpot> {
1920
forSpotTypes(): string[] {
20-
return ['GH_ISSUE_ADD_COMMENT']
21+
return [GH_ISSUE_APPEND]
2122
}
2223

2324
tryToEnhance(
2425
textarea: HTMLTextAreaElement,
2526
location: StrippedLocation,
26-
): GitHubIssueAddCommentSpot | null {
27+
): GitHubIssueAppendSpot | null {
2728
if (textarea.id === 'feedback') {
2829
return null
2930
}
@@ -57,22 +58,22 @@ export class GitHubIssueAddCommentEnhancer implements CommentEnhancer<GitHubIssu
5758
number,
5859
slug,
5960
title,
60-
type: 'GH_ISSUE_ADD_COMMENT',
61+
type: GH_ISSUE_APPEND,
6162
unique_key,
6263
}
6364
}
6465

65-
enhance(textArea: HTMLTextAreaElement, _spot: GitHubIssueAddCommentSpot): OverTypeInstance {
66+
enhance(textArea: HTMLTextAreaElement, _spot: GitHubIssueAppendSpot): OverTypeInstance {
6667
prepareGitHubHighlighter()
6768
const overtypeContainer = modifyDOM(textArea)
6869
return new OverType(overtypeContainer, {
69-
...commonGithubOptions,
70+
...commonGitHubOptions,
7071
minHeight: '100px',
7172
placeholder: 'Use Markdown to format your comment',
7273
})[0]!
7374
}
7475

75-
tableUpperDecoration(spot: GitHubIssueAddCommentSpot): React.ReactNode {
76+
tableUpperDecoration(spot: GitHubIssueAppendSpot): React.ReactNode {
7677
return (
7778
<>
7879
<span className='flex h-4 w-4 flex-shrink-0 items-center justify-center'>
@@ -86,7 +87,7 @@ export class GitHubIssueAddCommentEnhancer implements CommentEnhancer<GitHubIssu
8687
)
8788
}
8889

89-
tableTitle(spot: GitHubIssueAddCommentSpot): string {
90+
tableTitle(spot: GitHubIssueAppendSpot): string {
9091
return spot.title
9192
}
9293
}

src/lib/enhancers/github/githubIssueNewComment.tsx renamed to src/lib/enhancers/github/GitHubIssueCreateEnhancer.tsx

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,26 @@ import OverType, { type OverTypeInstance } from 'overtype'
22
import type { CommentEnhancer, CommentSpot, StrippedLocation } from '../../enhancer'
33
import { logger } from '../../logger'
44
import { modifyDOM } from '../modifyDOM'
5-
import { commonGithubOptions } from './ghOptions'
6-
import { prepareGitHubHighlighter } from './githubHighlighter'
5+
import { commonGitHubOptions, prepareGitHubHighlighter } from './github-common'
76

8-
interface GitHubIssueNewCommentSpot extends CommentSpot {
9-
type: 'GH_ISSUE_NEW_COMMENT'
7+
const GH_ISSUE_CREATE = 'GH_ISSUE_CREATE' as const
8+
9+
interface GitHubIssueCreateSpot extends CommentSpot {
10+
type: typeof GH_ISSUE_CREATE
1011
domain: string
1112
slug: string // owner/repo
1213
title: string
1314
}
1415

15-
export class GitHubIssueNewCommentEnhancer implements CommentEnhancer<GitHubIssueNewCommentSpot> {
16+
export class GitHubIssueCreateEnhancer implements CommentEnhancer<GitHubIssueCreateSpot> {
1617
forSpotTypes(): string[] {
17-
return ['GH_ISSUE_NEW_COMMENT']
18+
return [GH_ISSUE_CREATE]
1819
}
1920

2021
tryToEnhance(
2122
textarea: HTMLTextAreaElement,
2223
location: StrippedLocation,
23-
): GitHubIssueNewCommentSpot | null {
24+
): GitHubIssueCreateSpot | null {
2425
if (textarea.id === 'feedback') {
2526
return null
2627
}
@@ -44,22 +45,22 @@ export class GitHubIssueNewCommentEnhancer implements CommentEnhancer<GitHubIssu
4445
domain: location.host,
4546
slug,
4647
title,
47-
type: 'GH_ISSUE_NEW_COMMENT',
48+
type: GH_ISSUE_CREATE,
4849
unique_key,
4950
}
5051
}
5152

52-
enhance(textArea: HTMLTextAreaElement, _spot: GitHubIssueNewCommentSpot): OverTypeInstance {
53+
enhance(textArea: HTMLTextAreaElement, _spot: GitHubIssueCreateSpot): OverTypeInstance {
5354
prepareGitHubHighlighter()
5455
const overtypeContainer = modifyDOM(textArea)
5556
return new OverType(overtypeContainer, {
56-
...commonGithubOptions,
57+
...commonGitHubOptions,
5758
minHeight: '400px',
5859
placeholder: 'Type your description here...',
5960
})[0]!
6061
}
6162

62-
tableUpperDecoration(spot: GitHubIssueNewCommentSpot): React.ReactNode {
63+
tableUpperDecoration(spot: GitHubIssueCreateSpot): React.ReactNode {
6364
const { slug } = spot
6465
return (
6566
<>
@@ -69,11 +70,11 @@ export class GitHubIssueNewCommentEnhancer implements CommentEnhancer<GitHubIssu
6970
)
7071
}
7172

72-
tableTitle(spot: GitHubIssueNewCommentSpot): string {
73+
tableTitle(spot: GitHubIssueCreateSpot): string {
7374
return spot.title || 'New Issue'
7475
}
7576

76-
buildUrl(spot: GitHubIssueNewCommentSpot): string {
77+
buildUrl(spot: GitHubIssueCreateSpot): string {
7778
return `https://${spot.domain}/${spot.slug}/issue/new`
7879
}
7980
}

src/lib/enhancers/github/githubPRAddComment.tsx renamed to src/lib/enhancers/github/GitHubPrAppendEnhancer.tsx

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,29 @@ import type React from 'react'
33
import type { CommentEnhancer, CommentSpot, StrippedLocation } from '@/lib/enhancer'
44
import { logger } from '@/lib/logger'
55
import { modifyDOM } from '../modifyDOM'
6-
import { commonGithubOptions } from './ghOptions'
7-
import { prepareGitHubHighlighter } from './githubHighlighter'
6+
import { commonGitHubOptions, prepareGitHubHighlighter } from './github-common'
87

9-
export interface GitHubPRAddCommentSpot extends CommentSpot {
10-
type: 'GH_PR_ADD_COMMENT' // Override to narrow from string to specific union
8+
const GH_PR_APPEND = 'GH_PR_APPEND' as const
9+
10+
export interface GitHubPrAppendSpot extends CommentSpot {
11+
type: typeof GH_PR_APPEND
1112
title: string
1213
domain: string
1314
slug: string // owner/repo
1415
number: number // issue/PR number, undefined for new issues and PRs
1516
}
1617

17-
export class GitHubPRAddCommentEnhancer implements CommentEnhancer<GitHubPRAddCommentSpot> {
18+
export class GitHubPrAppendEnhancer implements CommentEnhancer<GitHubPrAppendSpot> {
1819
forSpotTypes(): string[] {
19-
return ['GH_PR_ADD_COMMENT']
20+
return [GH_PR_APPEND]
2021
}
2122

2223
tryToEnhance(
23-
_textarea: HTMLTextAreaElement,
24+
textarea: HTMLTextAreaElement,
2425
location: StrippedLocation,
25-
): GitHubPRAddCommentSpot | null {
26+
): GitHubPrAppendSpot | null {
2627
// Only handle github.com domains TODO: identify GitHub Enterprise somehow
27-
if (location.host !== 'github.com' || _textarea.id !== 'new_comment_field') {
28+
if (location.host !== 'github.com' || textarea.id !== 'new_comment_field') {
2829
return null
2930
}
3031

@@ -47,23 +48,23 @@ export class GitHubPRAddCommentEnhancer implements CommentEnhancer<GitHubPRAddCo
4748
number,
4849
slug,
4950
title,
50-
type: 'GH_PR_ADD_COMMENT',
51+
type: GH_PR_APPEND,
5152
unique_key,
5253
}
5354
}
5455

55-
enhance(textArea: HTMLTextAreaElement, _spot: GitHubPRAddCommentSpot): OverTypeInstance {
56+
enhance(textArea: HTMLTextAreaElement, _spot: GitHubPrAppendSpot): OverTypeInstance {
5657
prepareGitHubHighlighter()
5758
const overtypeContainer = modifyDOM(textArea)
5859
return new OverType(overtypeContainer, {
59-
...commonGithubOptions,
60+
...commonGitHubOptions,
6061
minHeight: '102px',
6162
padding: 'var(--base-size-8)',
6263
placeholder: 'Add your comment here...',
6364
})[0]!
6465
}
6566

66-
tableUpperDecoration(spot: GitHubPRAddCommentSpot): React.ReactNode {
67+
tableUpperDecoration(spot: GitHubPrAppendSpot): React.ReactNode {
6768
const { slug, number } = spot
6869
return (
6970
<>
@@ -73,7 +74,7 @@ export class GitHubPRAddCommentEnhancer implements CommentEnhancer<GitHubPRAddCo
7374
)
7475
}
7576

76-
tableTitle(spot: GitHubPRAddCommentSpot): string {
77+
tableTitle(spot: GitHubPrAppendSpot): string {
7778
return spot.title
7879
}
7980
}

src/lib/enhancers/github/githubPRNewComment.tsx renamed to src/lib/enhancers/github/GitHubPrCreateEnhancer.tsx

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,28 @@ import OverType, { type OverTypeInstance } from 'overtype'
22
import type { CommentEnhancer, CommentSpot, StrippedLocation } from '../../enhancer'
33
import { logger } from '../../logger'
44
import { modifyDOM } from '../modifyDOM'
5-
import { commonGithubOptions } from './ghOptions'
6-
import { prepareGitHubHighlighter } from './githubHighlighter'
5+
import { commonGitHubOptions, prepareGitHubHighlighter } from './github-common'
76

8-
interface GitHubPRNewCommentSpot extends CommentSpot {
9-
type: 'GH_PR_NEW_COMMENT'
7+
const GH_PR_CREATE = 'GH_PR_CREATE' as const
8+
9+
interface GitHubPrCreateSpot extends CommentSpot {
10+
type: typeof GH_PR_CREATE
1011
domain: string
1112
slug: string // owner/repo
1213
title: string
1314
head: string // `user:repo:branch` where changes are implemented
1415
base: string // branch you want changes pulled into
1516
}
1617

17-
export class GitHubPRNewCommentEnhancer implements CommentEnhancer<GitHubPRNewCommentSpot> {
18+
export class GitHubPrCreateEnhancer implements CommentEnhancer<GitHubPrCreateSpot> {
1819
forSpotTypes(): string[] {
19-
return ['GH_PR_NEW_COMMENT']
20+
return [GH_PR_CREATE]
2021
}
2122

2223
tryToEnhance(
2324
textarea: HTMLTextAreaElement,
2425
location: StrippedLocation,
25-
): GitHubPRNewCommentSpot | null {
26+
): GitHubPrCreateSpot | null {
2627
if (textarea.id === 'feedback') {
2728
return null
2829
}
@@ -54,22 +55,22 @@ export class GitHubPRNewCommentEnhancer implements CommentEnhancer<GitHubPRNewCo
5455
head,
5556
slug,
5657
title,
57-
type: 'GH_PR_NEW_COMMENT',
58+
type: GH_PR_CREATE,
5859
unique_key,
5960
}
6061
}
6162

62-
enhance(textArea: HTMLTextAreaElement, _spot: GitHubPRNewCommentSpot): OverTypeInstance {
63+
enhance(textArea: HTMLTextAreaElement, _spot: GitHubPrCreateSpot): OverTypeInstance {
6364
prepareGitHubHighlighter()
6465
const overtypeContainer = modifyDOM(textArea)
6566
return new OverType(overtypeContainer, {
66-
...commonGithubOptions,
67+
...commonGitHubOptions,
6768
minHeight: '250px',
6869
placeholder: 'Type your description here...',
6970
})[0]!
7071
}
7172

72-
tableUpperDecoration(spot: GitHubPRNewCommentSpot): React.ReactNode {
73+
tableUpperDecoration(spot: GitHubPrCreateSpot): React.ReactNode {
7374
const { slug } = spot
7475
return (
7576
<>
@@ -79,11 +80,11 @@ export class GitHubPRNewCommentEnhancer implements CommentEnhancer<GitHubPRNewCo
7980
)
8081
}
8182

82-
tableTitle(spot: GitHubPRNewCommentSpot): string {
83+
tableTitle(spot: GitHubPrCreateSpot): string {
8384
return spot.title || 'New Pull Request'
8485
}
8586

86-
buildUrl(spot: GitHubPRNewCommentSpot): string {
87+
buildUrl(spot: GitHubPrCreateSpot): string {
8788
return `https://${spot.domain}/${spot.slug}/issue/new`
8889
}
8990
}

src/lib/enhancers/github/ghOptions.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/lib/enhancers/github/githubHighlighter.ts renamed to src/lib/enhancers/github/github-common.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import hljs from 'highlight.js'
2+
import type { Options } from 'overtype'
23
import OverType from 'overtype'
34
import { oncePerRefresh } from '@/lib/once-per-refresh'
45

6+
export const commonGitHubOptions: Options = {
7+
autoResize: true,
8+
lineHeight: 'var(--text-body-lineHeight-medium, 1.4285)',
9+
padding: 'var(--base-size-16)',
10+
}
11+
512
export function prepareGitHubHighlighter() {
613
oncePerRefresh('github-highlighter', () => {
714
OverType.setCodeHighlighter(githubHighlighter)

0 commit comments

Comments
 (0)