gitalk-react 1.0.0-beta.2 → 1.0.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README-zh-CN.md +43 -16
- package/README.md +47 -20
- package/dist/gitalk.d.ts +9 -3
- package/dist/gitalk.js +2152 -2155
- package/dist/gitalk.umd.cjs +20 -18
- package/lib/constants/index.ts +6 -0
- package/lib/gitalk.tsx +59 -53
- package/lib/utils/logger.ts +12 -2
- package/package.json +1 -1
package/lib/constants/index.ts
CHANGED
|
@@ -14,6 +14,7 @@ import {
|
|
|
14
14
|
} from "date-fns/locale";
|
|
15
15
|
|
|
16
16
|
import packageJson from "../../package.json";
|
|
17
|
+
import { type GitalkProps } from "../gitalk";
|
|
17
18
|
import { type Lang } from "../i18n";
|
|
18
19
|
|
|
19
20
|
export const VERSION = packageJson.version;
|
|
@@ -23,6 +24,11 @@ export const DEFAULT_LANG: Lang = "en";
|
|
|
23
24
|
export const DEFAULT_LABELS = ["Gitalk"];
|
|
24
25
|
export const DEFAULT_AVATAR =
|
|
25
26
|
"https://cdn.jsdelivr.net/npm/gitalk@1/src/assets/icon/github.svg";
|
|
27
|
+
export const DEFAULT_USER: GitalkProps["defaultUser"] = {
|
|
28
|
+
avatar_url: "//avatars1.githubusercontent.com/u/29697133?s=50",
|
|
29
|
+
login: "null",
|
|
30
|
+
html_url: "",
|
|
31
|
+
};
|
|
26
32
|
|
|
27
33
|
export const ACCESS_TOKEN_KEY = "GT_ACCESS_TOKEN";
|
|
28
34
|
export const STORED_COMMENTS_KEY = "GT_COMMENT";
|
package/lib/gitalk.tsx
CHANGED
|
@@ -23,6 +23,7 @@ import {
|
|
|
23
23
|
ACCESS_TOKEN_KEY,
|
|
24
24
|
DATE_FNS_LOCALE_MAP,
|
|
25
25
|
DEFAULT_LABELS,
|
|
26
|
+
DEFAULT_USER,
|
|
26
27
|
HOMEPAGE,
|
|
27
28
|
VERSION,
|
|
28
29
|
} from "./constants";
|
|
@@ -72,14 +73,12 @@ export interface GitalkProps
|
|
|
72
73
|
* The unique id of the page.
|
|
73
74
|
* Length must less than 50.
|
|
74
75
|
*
|
|
75
|
-
* @default location.
|
|
76
|
+
* @default location.host + location.pathname
|
|
76
77
|
*/
|
|
77
78
|
id?: string;
|
|
78
79
|
/**
|
|
79
80
|
* The issue ID of the page.
|
|
80
81
|
* If the number attribute is not defined, issue will be located using id.
|
|
81
|
-
*
|
|
82
|
-
* @default -1
|
|
83
82
|
*/
|
|
84
83
|
number?: number;
|
|
85
84
|
/**
|
|
@@ -184,6 +183,12 @@ export interface GitalkProps
|
|
|
184
183
|
* @param count comments number
|
|
185
184
|
*/
|
|
186
185
|
updateCountCallback?: (count: number) => void;
|
|
186
|
+
/**
|
|
187
|
+
* Callback method invoked when a new issue is successfully created.
|
|
188
|
+
*
|
|
189
|
+
* @param issue created issue
|
|
190
|
+
*/
|
|
191
|
+
onCreateIssue?: (issue: IssueType) => void;
|
|
187
192
|
/**
|
|
188
193
|
* Callback method invoked when a new comment is successfully created.
|
|
189
194
|
*
|
|
@@ -201,14 +206,15 @@ const Gitalk: React.FC<GitalkProps> = (props) => {
|
|
|
201
206
|
owner,
|
|
202
207
|
repo,
|
|
203
208
|
admin,
|
|
204
|
-
id: propsIssueId = location.
|
|
205
|
-
number:
|
|
206
|
-
labels:
|
|
209
|
+
id: propsIssueId = location.host + location.pathname,
|
|
210
|
+
number: propsIssueNumber,
|
|
211
|
+
labels: propsIssueLabels = DEFAULT_LABELS,
|
|
207
212
|
title: issueTitle = document.title,
|
|
208
213
|
body: issueBody = location.href +
|
|
209
|
-
|
|
214
|
+
"\n\n" +
|
|
215
|
+
(document
|
|
210
216
|
?.querySelector('meta[name="description"]')
|
|
211
|
-
?.getAttribute("content")
|
|
217
|
+
?.getAttribute("content") ?? ""),
|
|
212
218
|
language = navigator.language as Lang,
|
|
213
219
|
perPage: propsPerPage = 10,
|
|
214
220
|
pagerDirection = "last",
|
|
@@ -225,28 +231,23 @@ const Gitalk: React.FC<GitalkProps> = (props) => {
|
|
|
225
231
|
defaultUser: propsDefaultUser,
|
|
226
232
|
defaultAuthor: propsDefaultAuthor,
|
|
227
233
|
updateCountCallback,
|
|
234
|
+
onCreateIssue,
|
|
228
235
|
onCreateComment,
|
|
229
236
|
className = "",
|
|
230
237
|
...restProps
|
|
231
238
|
} = props;
|
|
232
|
-
const issueId = propsIssueId.slice(0, 50);
|
|
233
|
-
const commentsPerPage =
|
|
234
|
-
propsPerPage > 100 ? 100 : propsPerPage < 0 ? 10 : propsPerPage;
|
|
235
|
-
const defaultUser = propsDefaultUser
|
|
236
|
-
? propsDefaultUser
|
|
237
|
-
: propsDefaultAuthor
|
|
238
|
-
? {
|
|
239
|
-
avatar_url: propsDefaultAuthor.avatarUrl,
|
|
240
|
-
login: propsDefaultAuthor.login,
|
|
241
|
-
html_url: propsDefaultAuthor.url,
|
|
242
|
-
}
|
|
243
|
-
: {
|
|
244
|
-
avatar_url: "//avatars1.githubusercontent.com/u/29697133?s=50",
|
|
245
|
-
login: "null",
|
|
246
|
-
html_url: "",
|
|
247
|
-
};
|
|
248
239
|
|
|
249
|
-
|
|
240
|
+
const [issue, setIssue] = useState<IssueType>();
|
|
241
|
+
const issueNumber = useMemo(
|
|
242
|
+
() =>
|
|
243
|
+
propsIssueNumber && propsIssueNumber > 0 ? propsIssueNumber : undefined,
|
|
244
|
+
[propsIssueNumber],
|
|
245
|
+
);
|
|
246
|
+
const issueId = useMemo(() => propsIssueId.slice(0, 50), [propsIssueId]);
|
|
247
|
+
const issueLabels = useMemo(
|
|
248
|
+
() => propsIssueLabels.concat([issueId]),
|
|
249
|
+
[propsIssueLabels, issueId],
|
|
250
|
+
);
|
|
250
251
|
|
|
251
252
|
const textareaRef = useRef<HTMLTextAreaElement>(null);
|
|
252
253
|
const [inputComment, setInputComment] = useState<string>("");
|
|
@@ -256,9 +257,26 @@ const Gitalk: React.FC<GitalkProps> = (props) => {
|
|
|
256
257
|
const [commentsCount, setCommentsCount] = useState<number>(0);
|
|
257
258
|
const [commentsCursor, setCommentsCursor] = useState("");
|
|
258
259
|
const [commentsPage, setCommentsPage] = useState<number>(1);
|
|
260
|
+
const commentsPerPage = useMemo(
|
|
261
|
+
() => (propsPerPage > 100 ? 100 : propsPerPage < 0 ? 10 : propsPerPage),
|
|
262
|
+
[propsPerPage],
|
|
263
|
+
);
|
|
259
264
|
const [commentsLoaded, setCommentsLoaded] = useState<boolean>(false);
|
|
260
265
|
const [commentsPagerDirection, setCommentsPagerDirection] =
|
|
261
266
|
useState(pagerDirection);
|
|
267
|
+
const defaultUser = useMemo(
|
|
268
|
+
() =>
|
|
269
|
+
propsDefaultUser
|
|
270
|
+
? propsDefaultUser
|
|
271
|
+
: propsDefaultAuthor
|
|
272
|
+
? {
|
|
273
|
+
avatar_url: propsDefaultAuthor.avatarUrl,
|
|
274
|
+
login: propsDefaultAuthor.login,
|
|
275
|
+
html_url: propsDefaultAuthor.url,
|
|
276
|
+
}
|
|
277
|
+
: DEFAULT_USER,
|
|
278
|
+
[propsDefaultAuthor, propsDefaultUser],
|
|
279
|
+
);
|
|
262
280
|
|
|
263
281
|
const [showPopup, setShowPopup] = useState<boolean>(false);
|
|
264
282
|
|
|
@@ -336,12 +354,7 @@ const Gitalk: React.FC<GitalkProps> = (props) => {
|
|
|
336
354
|
);
|
|
337
355
|
}, [admin, user]);
|
|
338
356
|
|
|
339
|
-
const
|
|
340
|
-
() => issueBaseLabels.concat([issueId]),
|
|
341
|
-
[issueBaseLabels, issueId],
|
|
342
|
-
);
|
|
343
|
-
|
|
344
|
-
const { loading: createIssueLoading, runAsync: runCreateIssue } = useRequest(
|
|
357
|
+
const { loading: createIssueLoading, run: runCreateIssue } = useRequest(
|
|
345
358
|
async () => {
|
|
346
359
|
logger.i(`Creating issue...`);
|
|
347
360
|
|
|
@@ -359,11 +372,13 @@ const Gitalk: React.FC<GitalkProps> = (props) => {
|
|
|
359
372
|
if (createIssueRes.status === 201) {
|
|
360
373
|
const _issue = createIssueRes.data;
|
|
361
374
|
logger.s(`Create issue successfully:`, _issue);
|
|
362
|
-
|
|
375
|
+
|
|
376
|
+
onCreateIssue?.(_issue);
|
|
377
|
+
|
|
378
|
+
setIssue(_issue);
|
|
363
379
|
} else {
|
|
364
380
|
setAlert(`Create issue failed: ${createIssueRes}`);
|
|
365
381
|
logger.e(`Create issue failed:`, createIssueRes);
|
|
366
|
-
return undefined;
|
|
367
382
|
}
|
|
368
383
|
},
|
|
369
384
|
{
|
|
@@ -373,11 +388,7 @@ const Gitalk: React.FC<GitalkProps> = (props) => {
|
|
|
373
388
|
},
|
|
374
389
|
);
|
|
375
390
|
|
|
376
|
-
const {
|
|
377
|
-
data: issue,
|
|
378
|
-
mutate: setIssue,
|
|
379
|
-
loading: getIssueLoading,
|
|
380
|
-
} = useRequest(
|
|
391
|
+
const { loading: getIssueLoading } = useRequest(
|
|
381
392
|
async () => {
|
|
382
393
|
if (issueNumber) {
|
|
383
394
|
const getIssueRes = await octokit.request(
|
|
@@ -395,15 +406,14 @@ const Gitalk: React.FC<GitalkProps> = (props) => {
|
|
|
395
406
|
`Locate issue ${issueNumber} in repository ${owner}/${repo} successfully:`,
|
|
396
407
|
_issue,
|
|
397
408
|
);
|
|
398
|
-
|
|
409
|
+
setIssue(_issue);
|
|
399
410
|
} else if (getIssueRes.status === 404) {
|
|
400
411
|
logger.w(
|
|
401
412
|
`Issue ${issueNumber} in repository ${owner}/${repo} was not found.`,
|
|
402
413
|
);
|
|
403
414
|
|
|
404
415
|
if (isAdmin && !createIssueManually) {
|
|
405
|
-
|
|
406
|
-
return _issue;
|
|
416
|
+
runCreateIssue();
|
|
407
417
|
}
|
|
408
418
|
} else {
|
|
409
419
|
setAlert(
|
|
@@ -433,15 +443,14 @@ const Gitalk: React.FC<GitalkProps> = (props) => {
|
|
|
433
443
|
`Locate issue with labels ${issueLabels} in repository ${owner}/${repo} successfully:`,
|
|
434
444
|
_issue,
|
|
435
445
|
);
|
|
436
|
-
|
|
446
|
+
setIssue(_issue);
|
|
437
447
|
} else {
|
|
438
448
|
logger.w(
|
|
439
449
|
`Issue with labels ${issueLabels} in repository ${owner}/${repo} was not found.`,
|
|
440
450
|
);
|
|
441
451
|
|
|
442
452
|
if (isAdmin && !createIssueManually) {
|
|
443
|
-
|
|
444
|
-
return _issue;
|
|
453
|
+
runCreateIssue();
|
|
445
454
|
}
|
|
446
455
|
}
|
|
447
456
|
} else if (getIssuesStatus === 404) {
|
|
@@ -450,8 +459,7 @@ const Gitalk: React.FC<GitalkProps> = (props) => {
|
|
|
450
459
|
);
|
|
451
460
|
|
|
452
461
|
if (isAdmin && !createIssueManually) {
|
|
453
|
-
|
|
454
|
-
return _issue;
|
|
462
|
+
runCreateIssue();
|
|
455
463
|
}
|
|
456
464
|
} else {
|
|
457
465
|
setAlert(
|
|
@@ -463,8 +471,6 @@ const Gitalk: React.FC<GitalkProps> = (props) => {
|
|
|
463
471
|
);
|
|
464
472
|
}
|
|
465
473
|
}
|
|
466
|
-
|
|
467
|
-
return undefined;
|
|
468
474
|
},
|
|
469
475
|
{
|
|
470
476
|
ready: !!owner && !!repo && (!!issueNumber || !!issueId),
|
|
@@ -481,7 +487,7 @@ const Gitalk: React.FC<GitalkProps> = (props) => {
|
|
|
481
487
|
loading: getCommentsLoading,
|
|
482
488
|
} = useRequest(
|
|
483
489
|
async (): Promise<CommentType[]> => {
|
|
484
|
-
const { number:
|
|
490
|
+
const { number: currentIssueNumber } = issue as IssueType;
|
|
485
491
|
const from = (commentsPage - 1) * commentsPerPage + 1;
|
|
486
492
|
const to = commentsPage * commentsPerPage;
|
|
487
493
|
|
|
@@ -495,7 +501,7 @@ const Gitalk: React.FC<GitalkProps> = (props) => {
|
|
|
495
501
|
await octokit.graphql(query, {
|
|
496
502
|
owner,
|
|
497
503
|
repo,
|
|
498
|
-
id:
|
|
504
|
+
id: currentIssueNumber,
|
|
499
505
|
pageSize: commentsPerPage,
|
|
500
506
|
...(commentsCursor ? { cursor: commentsCursor } : {}),
|
|
501
507
|
});
|
|
@@ -550,7 +556,7 @@ const Gitalk: React.FC<GitalkProps> = (props) => {
|
|
|
550
556
|
{
|
|
551
557
|
owner,
|
|
552
558
|
repo,
|
|
553
|
-
issue_number:
|
|
559
|
+
issue_number: currentIssueNumber,
|
|
554
560
|
page: commentsPage,
|
|
555
561
|
per_page: commentsPerPage,
|
|
556
562
|
headers: {
|
|
@@ -608,14 +614,14 @@ const Gitalk: React.FC<GitalkProps> = (props) => {
|
|
|
608
614
|
run: runCreateIssueComment,
|
|
609
615
|
} = useRequest(
|
|
610
616
|
async (): Promise<CommentType[]> => {
|
|
611
|
-
const { number:
|
|
617
|
+
const { number: currentIssueNumber } = issue as IssueType;
|
|
612
618
|
|
|
613
619
|
const createIssueCommentRes = await octokit.request(
|
|
614
620
|
"POST /repos/{owner}/{repo}/issues/{issue_number}/comments",
|
|
615
621
|
{
|
|
616
622
|
owner,
|
|
617
623
|
repo,
|
|
618
|
-
issue_number:
|
|
624
|
+
issue_number: currentIssueNumber,
|
|
619
625
|
body: inputComment,
|
|
620
626
|
headers: {
|
|
621
627
|
accept: "application/vnd.github.v3.full+json",
|
package/lib/utils/logger.ts
CHANGED
|
@@ -6,10 +6,20 @@ enum LogLevel {
|
|
|
6
6
|
NO_LOG = Infinity,
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
-
class Logger {
|
|
9
|
+
export class Logger {
|
|
10
10
|
prefix = "Gitalk";
|
|
11
11
|
logLevel = import.meta.env.PROD ? LogLevel.WARNING : LogLevel.INFO;
|
|
12
12
|
|
|
13
|
+
constructor({ prefix, logLevel }: { prefix?: string; logLevel?: LogLevel }) {
|
|
14
|
+
if (prefix) {
|
|
15
|
+
this.prefix = prefix;
|
|
16
|
+
}
|
|
17
|
+
if (logLevel !== undefined) {
|
|
18
|
+
this.logLevel = logLevel;
|
|
19
|
+
}
|
|
20
|
+
return this;
|
|
21
|
+
}
|
|
22
|
+
|
|
13
23
|
i(...infos: unknown[]) {
|
|
14
24
|
if (this.logLevel > LogLevel.INFO) return;
|
|
15
25
|
|
|
@@ -51,6 +61,6 @@ class Logger {
|
|
|
51
61
|
}
|
|
52
62
|
}
|
|
53
63
|
|
|
54
|
-
const logger = new Logger();
|
|
64
|
+
const logger = new Logger({});
|
|
55
65
|
|
|
56
66
|
export default logger;
|