Skip to content

Commit 8957d4f

Browse files
committed
enhance code
1 parent 41778d5 commit 8957d4f

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

web_src/js/features/comp/TextExpander.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import {matchEmoji, matchMention, matchIssue} from '../../utils/match.ts';
22
import {emojiString} from '../emoji.ts';
33
import {svg} from '../../svg.ts';
4+
import {parseIssueHref} from '../../utils.ts';
45

5-
type Issue = {id: string; title: string; state: 'open' | 'closed'; pull_request?: {draft: boolean; merged: boolean}};
6+
type Issue = {id: number; title: string; state: 'open' | 'closed'; pull_request?: {draft: boolean; merged: boolean}};
67
function getIssueIcon(issue: Issue) {
78
if (issue.pull_request) {
89
if (issue.state === 'open') {
@@ -84,8 +85,8 @@ export function initTextExpander(expander) {
8485
provide({matched: true, fragment: ul});
8586
} else if (key === '#') {
8687
provide(new Promise(async (resolve) => {
87-
const url = window.location.href;
88-
const matches = await matchIssue(url, text);
88+
const {owner, repo, index} = parseIssueHref(window.location.href);
89+
const matches = await matchIssue(owner, repo, index, text);
8990
if (!matches.length) return resolve({matched: false});
9091

9192
const ul = document.createElement('ul');
@@ -102,7 +103,7 @@ export function initTextExpander(expander) {
102103

103104
const id = document.createElement('span');
104105
id.classList.add('id');
105-
id.textContent = issue.id;
106+
id.textContent = issue.id.toString();
106107
li.append(id);
107108

108109
const nameSpan = document.createElement('span');

web_src/js/utils/match.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import emojis from '../../../assets/emoji.json';
2-
import { request } from '../modules/fetch.ts';
2+
import {request} from '../modules/fetch.ts';
33

44
const maxMatches = 6;
55

@@ -45,19 +45,16 @@ export function matchMention(queryText: string): MentionSuggestion[] {
4545
}
4646

4747
type Issue = {id: number; title: string; state: 'open' | 'closed'; pull_request?: {draft: boolean; merged: boolean}};
48-
export async function matchIssue(url: string, queryText: string): Promise<Issue[]> {
48+
export async function matchIssue(owner: string, repo: string, _issueIndex: string, queryText: string): Promise<Issue[]> {
4949
const query = queryText.toLowerCase();
5050

51-
// TODO: support sub-path
52-
const repository = (new URL(url)).pathname.split('/').slice(1, 3).join('/');
53-
const issuePullRequestId = parseInt(url.split('/').slice(-1)[0]);
54-
55-
const res = await request(`/api/v1/repos/${repository}/issues?q=${query}`, {
51+
const res = await request(`/api/v1/repos/${owner}/${repo}/issues?q=${query}`, {
5652
method: 'GET',
5753
});
5854

5955
const issues: Issue[] = await res.json();
56+
const issueIndex = parseInt(_issueIndex);
6057

6158
// filter issue with same id
62-
return issues.filter((i) => i.id !== issuePullRequestId);
59+
return issues.filter((i) => i.id !== issueIndex);
6360
}

0 commit comments

Comments
 (0)