Skip to content

Commit f6dfe03

Browse files
committed
Add Issue New
1 parent f38d51c commit f6dfe03

File tree

5 files changed

+161
-8
lines changed

5 files changed

+161
-8
lines changed

browser-extension/src/lib/config.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ const MODES = ['PROD', 'PLAYGROUNDS_PR'] as const
22

33
export type ModeType = (typeof MODES)[number]
44

5+
const LOG_LEVELS = ['DEBUG', 'INFO', 'WARN', 'ERROR'] as const
6+
7+
export type LogLevel = (typeof LOG_LEVELS)[number]
8+
59
export const CONFIG = {
610
ADDED_OVERTYPE_CLASS: 'gitcasso-overtype',
7-
DEBUG: true, // enabled debug logging
811
EXTENSION_NAME: 'gitcasso', // decorates logs
12+
LOG_LEVEL: 'INFO' satisfies LogLevel,
913
MODE: 'PROD' satisfies ModeType,
1014
} as const
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import OverType, { type OverTypeInstance } from 'overtype'
2+
import type { CommentEnhancer, CommentSpot } from '../../enhancer'
3+
import { logger } from '../../logger'
4+
import { modifyDOM } from '../modifyDOM'
5+
import { githubHighlighter } from './githubHighlighter'
6+
7+
interface GitHubIssueAddCommentSpot extends CommentSpot {
8+
type: 'GH_ISSUE_NEW_COMMENT'
9+
domain: string
10+
slug: string // owner/repo
11+
}
12+
13+
export class GitHubIssueNewCommentEnhancer implements CommentEnhancer<GitHubIssueAddCommentSpot> {
14+
forSpotTypes(): string[] {
15+
return ['GH_ISSUE_NEW_COMMENT']
16+
}
17+
18+
tryToEnhance(_textarea: HTMLTextAreaElement): GitHubIssueAddCommentSpot | null {
19+
if (document.querySelector('meta[name="hostname"]')?.getAttribute('content') !== 'github.com') {
20+
return null
21+
}
22+
23+
// Parse GitHub URL structure: /owner/repo/issues/123 or /owner/repo/pull/456
24+
logger.info(`${this.constructor.name} examing url`, window.location.pathname)
25+
26+
const match = window.location.pathname.match(/^\/([^/]+)\/([^/]+)(?:\/issues\/new)/)
27+
logger.info(`${this.constructor.name} found match`, window.location.pathname)
28+
29+
if (!match) return null
30+
const [, owner, repo] = match
31+
const slug = `${owner}/${repo}`
32+
const unique_key = `github.com:${slug}:new`
33+
return {
34+
domain: 'github.com',
35+
slug,
36+
type: 'GH_ISSUE_NEW_COMMENT',
37+
unique_key,
38+
}
39+
}
40+
41+
prepareForFirstEnhancement(): void {
42+
OverType.setCodeHighlighter(githubHighlighter)
43+
}
44+
45+
enhance(textArea: HTMLTextAreaElement, _spot: GitHubIssueAddCommentSpot): OverTypeInstance {
46+
const overtypeContainer = modifyDOM(textArea)
47+
return new OverType(overtypeContainer, {
48+
autoResize: true,
49+
minHeight: '400px',
50+
padding: 'var(--base-size-16)',
51+
placeholder: 'Type your description here...',
52+
})[0]!
53+
}
54+
55+
tableTitle(spot: GitHubIssueAddCommentSpot): string {
56+
const { slug } = spot
57+
return `${slug} New Issue`
58+
}
59+
60+
tableIcon(_: GitHubIssueAddCommentSpot): string {
61+
return '🔄' // PR icon TODO: icon urls in /public
62+
}
63+
64+
buildUrl(spot: GitHubIssueAddCommentSpot): string {
65+
return `https://${spot.domain}/${spot.slug}/issue/new`
66+
}
67+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import OverType, { type OverTypeInstance } from 'overtype'
2+
import type { CommentEnhancer, CommentSpot } from '../../enhancer'
3+
import { logger } from '../../logger'
4+
import { modifyDOM } from '../modifyDOM'
5+
import { githubHighlighter } from './githubHighlighter'
6+
7+
interface GitHubIssueAddCommentSpot extends CommentSpot {
8+
type: 'GH_ISSUE_NEW_COMMENT'
9+
domain: string
10+
slug: string // owner/repo
11+
}
12+
13+
export class GitHubIssueNewCommentEnhancer implements CommentEnhancer<GitHubIssueAddCommentSpot> {
14+
forSpotTypes(): string[] {
15+
return ['GH_ISSUE_NEW_COMMENT']
16+
}
17+
18+
tryToEnhance(_textarea: HTMLTextAreaElement): GitHubIssueAddCommentSpot | null {
19+
if (document.querySelector('meta[name="hostname"]')?.getAttribute('content') !== 'github.com') {
20+
return null
21+
}
22+
23+
// Parse GitHub URL structure: /owner/repo/issues/123 or /owner/repo/pull/456
24+
logger.info(`${this.constructor.name} examing url`, window.location.pathname)
25+
26+
const match = window.location.pathname.match(/^\/([^/]+)\/([^/]+)(?:\/issues\/new)/)
27+
logger.info(`${this.constructor.name} found match`, window.location.pathname)
28+
29+
if (!match) return null
30+
const [, owner, repo] = match
31+
const slug = `${owner}/${repo}`
32+
const unique_key = `github.com:${slug}:new`
33+
return {
34+
domain: 'github.com',
35+
slug,
36+
type: 'GH_ISSUE_NEW_COMMENT',
37+
unique_key,
38+
}
39+
}
40+
41+
prepareForFirstEnhancement(): void {
42+
OverType.setCodeHighlighter(githubHighlighter)
43+
}
44+
45+
enhance(textArea: HTMLTextAreaElement, _spot: GitHubIssueAddCommentSpot): OverTypeInstance {
46+
const overtypeContainer = modifyDOM(textArea)
47+
return new OverType(overtypeContainer, {
48+
autoResize: true,
49+
minHeight: '400px',
50+
padding: 'var(--base-size-16)',
51+
placeholder: 'Type your description here...',
52+
})[0]!
53+
}
54+
55+
tableTitle(spot: GitHubIssueAddCommentSpot): string {
56+
const { slug } = spot
57+
return `${slug} New Issue`
58+
}
59+
60+
tableIcon(_: GitHubIssueAddCommentSpot): string {
61+
return '🔄' // PR icon TODO: icon urls in /public
62+
}
63+
64+
buildUrl(spot: GitHubIssueAddCommentSpot): string {
65+
return `https://${spot.domain}/${spot.slug}/issue/new`
66+
}
67+
}
Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CONFIG } from './config'
1+
import { CONFIG, type LogLevel } from './config'
22

33
/**
44
* Simple logging utilities for the extension
@@ -9,12 +9,25 @@ const prefix = `[${CONFIG.EXTENSION_NAME}]`
99
// No-op function for disabled logging
1010
const noop = () => {}
1111

12+
// Log level hierarchy - index represents priority
13+
const LOG_LEVEL_PRIORITY: Record<LogLevel, number> = {
14+
DEBUG: 0,
15+
ERROR: 3,
16+
INFO: 1,
17+
WARN: 2,
18+
}
19+
20+
// Helper function to check if a log level is enabled
21+
const shouldLog = (level: LogLevel): boolean => {
22+
return LOG_LEVEL_PRIORITY[level] >= LOG_LEVEL_PRIORITY[CONFIG.LOG_LEVEL]
23+
}
24+
1225
// Export simple logging functions
1326
export const logger = {
14-
debug: CONFIG.DEBUG ? console.log.bind(console, prefix) : noop,
15-
error: console.error.bind(console, prefix),
16-
info: CONFIG.DEBUG ? console.log.bind(console, prefix) : noop,
17-
time: CONFIG.DEBUG ? console.time.bind(console) : noop,
18-
timeEnd: CONFIG.DEBUG ? console.timeEnd.bind(console) : noop,
19-
warn: console.warn.bind(console, prefix),
27+
debug: shouldLog('DEBUG') ? console.log.bind(console, prefix) : noop,
28+
error: shouldLog('ERROR') ? console.error.bind(console, prefix) : noop,
29+
info: shouldLog('INFO') ? console.log.bind(console, prefix) : noop,
30+
time: shouldLog('INFO') ? console.time.bind(console) : noop,
31+
timeEnd: shouldLog('INFO') ? console.timeEnd.bind(console) : noop,
32+
warn: shouldLog('WARN') ? console.warn.bind(console, prefix) : noop,
2033
}

browser-extension/src/lib/registries.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { OverTypeInstance } from 'overtype'
22
import type { CommentEnhancer, CommentSpot } from './enhancer'
33
import { GitHubIssueAddCommentEnhancer } from './enhancers/github/githubIssueAddComment'
4+
import { GitHubIssueNewCommentEnhancer } from './enhancers/github/githubIssueNewComment'
45
import { GitHubPRAddCommentEnhancer } from './enhancers/github/githubPRAddComment'
56

67
export interface EnhancedTextarea<T extends CommentSpot = CommentSpot> {
@@ -19,6 +20,7 @@ export class EnhancerRegistry {
1920
// Register all available handlers
2021
this.register(new GitHubIssueAddCommentEnhancer())
2122
this.register(new GitHubPRAddCommentEnhancer())
23+
this.register(new GitHubIssueNewCommentEnhancer())
2224
}
2325

2426
private register<T extends CommentSpot>(enhancer: CommentEnhancer<T>): void {

0 commit comments

Comments
 (0)