-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithubPRNewComment.tsx
More file actions
83 lines (72 loc) · 2.5 KB
/
githubPRNewComment.tsx
File metadata and controls
83 lines (72 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import OverType, { type OverTypeInstance } from 'overtype'
import type { CommentEnhancer, CommentSpot, StrippedLocation } from '../../enhancer'
import { logger } from '../../logger'
import { modifyDOM } from '../modifyDOM'
import { commonGithubOptions } from './ghOptions'
import { githubHighlighter } from './githubHighlighter'
interface GitHubPRNewCommentSpot extends CommentSpot {
type: 'GH_PR_NEW_COMMENT'
domain: string
slug: string // owner/repo/base-branch/compare-branch
}
export class GitHubPRNewCommentEnhancer implements CommentEnhancer<GitHubPRNewCommentSpot> {
forSpotTypes(): string[] {
return ['GH_PR_NEW_COMMENT']
}
tryToEnhance(
textarea: HTMLTextAreaElement,
location: StrippedLocation,
): GitHubPRNewCommentSpot | null {
if (textarea.id === 'feedback') {
return null
}
if (location.host !== 'github.com') {
return null
}
// /owner/repo/compare/feature/more-enhancers?expand=1
// or /owner/repo/compare/feat/issue-static-and-dynamic...feature/more-enhancers?expand=1
logger.info(`${this.constructor.name} examing url`, location.pathname)
const match = location.pathname.match(
/^\/([^/]+)\/([^/]+)\/compare\/(?:([^.?]+)\.\.\.)?([^?]+)/,
)
logger.info(`${this.constructor.name} found match`, location.pathname, match)
if (!match) return null
const [, owner, repo, baseBranch, compareBranch] = match
const slug = baseBranch
? `${owner}/${repo}/${baseBranch}...${compareBranch}`
: `${owner}/${repo}/${compareBranch}`
const unique_key = `github.com:${slug}`
return {
domain: location.host,
slug,
type: 'GH_PR_NEW_COMMENT',
unique_key,
}
}
prepareForFirstEnhancement(): void {
OverType.setCodeHighlighter(githubHighlighter)
}
enhance(textArea: HTMLTextAreaElement, _spot: GitHubPRNewCommentSpot): OverTypeInstance {
const overtypeContainer = modifyDOM(textArea)
return new OverType(overtypeContainer, {
...commonGithubOptions,
minHeight: '250px',
placeholder: 'Type your description here...',
})[0]!
}
tableUpperDecoration(spot: GitHubPRNewCommentSpot): React.ReactNode {
const { slug } = spot
return (
<>
<span>New PR</span>
<span className='font-mono text-muted-foreground text-sm'> {slug} </span>
</>
)
}
tableTitle(_spot: GitHubPRNewCommentSpot): string {
return 'TITLE_TODO'
}
buildUrl(spot: GitHubPRNewCommentSpot): string {
return `https://${spot.domain}/${spot.slug}/issue/new`
}
}