-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitHubIssueCreateEnhancer.tsx
More file actions
133 lines (121 loc) · 3.64 KB
/
GitHubIssueCreateEnhancer.tsx
File metadata and controls
133 lines (121 loc) · 3.64 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { IssueOpenedIcon } from "@primer/octicons-react"
import OverType, { type OverTypeInstance } from "overtype"
import { LinkOutOfPopup } from "@/components/LinkOutOfPopup"
import type {
CommentEnhancer,
CommentSpot,
StrippedLocation,
} from "../../enhancer"
import { logger } from "../../logger"
import { fixupOvertype, modifyDOM } from "../overtype-misc"
import {
commonGitHubOptions,
isProjectUrl,
prepareGitHubHighlighter,
} from "./github-common"
const GH_ISSUE_CREATE = "GH_ISSUE_CREATE" as const
export interface GitHubIssueCreateSpot extends CommentSpot {
type: typeof GH_ISSUE_CREATE
domain: string
slug: string // owner/repo
title: string
}
export class GitHubIssueCreateEnhancer
implements CommentEnhancer<GitHubIssueCreateSpot>
{
forSpotTypes(): string[] {
return [GH_ISSUE_CREATE]
}
tryToEnhance(
textarea: HTMLTextAreaElement,
location: StrippedLocation
): GitHubIssueCreateSpot | null {
if (textarea.id === "feedback") {
return null
}
if (location.host !== "github.com") {
return null
}
// Check for project board URLs first
if (isProjectUrl(location.pathname)) {
// Check if we're in a "Create new issue" dialog
const dialog = textarea.closest('[role="dialog"]')
if (dialog) {
const dialogHeading = dialog.querySelector("h1")?.textContent
const slugMatch = dialogHeading?.match(/Create new issue in (.+)/)
if (slugMatch) {
const slug = slugMatch[1]!
const unique_key = `github.com:${slug}:new`
const titleInput = document.querySelector(
'input[placeholder="Title"]'
) as HTMLInputElement
const title = titleInput?.value || ""
return {
domain: location.host,
slug,
title,
type: GH_ISSUE_CREATE,
unique_key,
}
}
}
return null
}
// Parse GitHub URL structure: /owner/repo/issues/123 or /owner/repo/pull/456
logger.debug(`${this.constructor.name} examing url`, location.pathname)
const match = location.pathname.match(
/^\/([^/]+)\/([^/]+)(?:\/issues\/new)/
)
logger.debug(`${this.constructor.name} found match`, location.pathname)
if (!match) return null
const [, owner, repo] = match
const slug = `${owner}/${repo}`
const unique_key = `github.com:${slug}:new`
const titleInput = document.querySelector(
'input[placeholder="Title"]'
) as HTMLInputElement
const title = titleInput?.value || ""
return {
domain: location.host,
slug,
title,
type: GH_ISSUE_CREATE,
unique_key,
}
}
enhance(
textArea: HTMLTextAreaElement,
_spot: GitHubIssueCreateSpot
): OverTypeInstance {
prepareGitHubHighlighter()
const overtypeContainer = modifyDOM(textArea)
return fixupOvertype(
new OverType(overtypeContainer, {
...commonGitHubOptions,
minHeight: "400px",
placeholder: "Type your description here...",
})
)
}
tableUpperDecoration(spot: GitHubIssueCreateSpot): React.ReactNode {
return (
<>
<span className="flex h-4 w-4 flex-shrink-0 items-center justify-center">
<IssueOpenedIcon size={16} />
</span>
<span>
<draft>{" "}
<LinkOutOfPopup href={`https://${spot.domain}/${spot.slug}`}>
{spot.slug}
</LinkOutOfPopup>
</span>
</>
)
}
tableTitle(spot: GitHubIssueCreateSpot): string {
return spot.title || "<untitled issue>"
}
buildUrl(spot: GitHubIssueCreateSpot): string {
return `https://${spot.domain}/${spot.slug}/issue/new`
}
}