gitalk-react 1.0.0-beta.3 → 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.
@@ -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,7 +73,7 @@ export interface GitalkProps
72
73
  * The unique id of the page.
73
74
  * Length must less than 50.
74
75
  *
75
- * @default location.pathname
76
+ * @default location.host + location.pathname
76
77
  */
77
78
  id?: string;
78
79
  /**
@@ -182,6 +183,12 @@ export interface GitalkProps
182
183
  * @param count comments number
183
184
  */
184
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;
185
192
  /**
186
193
  * Callback method invoked when a new comment is successfully created.
187
194
  *
@@ -199,14 +206,15 @@ const Gitalk: React.FC<GitalkProps> = (props) => {
199
206
  owner,
200
207
  repo,
201
208
  admin,
202
- id: propsIssueId = location.pathname,
209
+ id: propsIssueId = location.host + location.pathname,
203
210
  number: propsIssueNumber,
204
- labels: issueBaseLabels = DEFAULT_LABELS,
211
+ labels: propsIssueLabels = DEFAULT_LABELS,
205
212
  title: issueTitle = document.title,
206
213
  body: issueBody = location.href +
207
- document
214
+ "\n\n" +
215
+ (document
208
216
  ?.querySelector('meta[name="description"]')
209
- ?.getAttribute("content") || "",
217
+ ?.getAttribute("content") ?? ""),
210
218
  language = navigator.language as Lang,
211
219
  perPage: propsPerPage = 10,
212
220
  pagerDirection = "last",
@@ -223,30 +231,23 @@ const Gitalk: React.FC<GitalkProps> = (props) => {
223
231
  defaultUser: propsDefaultUser,
224
232
  defaultAuthor: propsDefaultAuthor,
225
233
  updateCountCallback,
234
+ onCreateIssue,
226
235
  onCreateComment,
227
236
  className = "",
228
237
  ...restProps
229
238
  } = props;
230
- const issueId = propsIssueId.slice(0, 50);
231
- const issueNumber =
232
- propsIssueNumber && propsIssueNumber > 0 ? propsIssueNumber : undefined;
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
- logger.i("re-rendered.");
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 issueLabels = useMemo(
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
- return _issue;
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
- return _issue;
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
- const _issue = await runCreateIssue();
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
- return _issue;
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
- const _issue = await runCreateIssue();
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
- const _issue = await runCreateIssue();
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),
@@ -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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gitalk-react",
3
- "version": "1.0.0-beta.3",
3
+ "version": "1.0.0-beta.4",
4
4
  "private": false,
5
5
  "author": {
6
6
  "name": "LolipopJ",