gitalk-react 1.0.0-beta.3 → 1.0.0-beta.5

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";
@@ -45,7 +46,13 @@ import logger from "./utils/logger";
45
46
  import { parseSearchQuery, stringifySearchQuery } from "./utils/query";
46
47
 
47
48
  export interface GitalkProps
48
- extends Omit<React.HTMLAttributes<HTMLDivElement>, "id" | "title"> {
49
+ extends Omit<
50
+ React.DetailedHTMLProps<
51
+ React.HTMLAttributes<HTMLDivElement>,
52
+ HTMLDivElement
53
+ >,
54
+ "id" | "title"
55
+ > {
49
56
  /**
50
57
  * GitHub Application Client ID.
51
58
  */
@@ -72,7 +79,7 @@ export interface GitalkProps
72
79
  * The unique id of the page.
73
80
  * Length must less than 50.
74
81
  *
75
- * @default location.pathname
82
+ * @default location.host + location.pathname
76
83
  */
77
84
  id?: string;
78
85
  /**
@@ -112,7 +119,7 @@ export interface GitalkProps
112
119
  perPage?: number;
113
120
  /**
114
121
  * Comment sorting direction.
115
- * Available values are last and first.
122
+ * Available values are `last` and `first`.
116
123
  *
117
124
  * @default "last"
118
125
  */
@@ -182,6 +189,12 @@ export interface GitalkProps
182
189
  * @param count comments number
183
190
  */
184
191
  updateCountCallback?: (count: number) => void;
192
+ /**
193
+ * Callback method invoked when a new issue is successfully created.
194
+ *
195
+ * @param issue created issue
196
+ */
197
+ onCreateIssue?: (issue: IssueType) => void;
185
198
  /**
186
199
  * Callback method invoked when a new comment is successfully created.
187
200
  *
@@ -199,14 +212,15 @@ const Gitalk: React.FC<GitalkProps> = (props) => {
199
212
  owner,
200
213
  repo,
201
214
  admin,
202
- id: propsIssueId = location.pathname,
215
+ id: propsIssueId = location.host + location.pathname,
203
216
  number: propsIssueNumber,
204
- labels: issueBaseLabels = DEFAULT_LABELS,
217
+ labels: propsIssueLabels = DEFAULT_LABELS,
205
218
  title: issueTitle = document.title,
206
219
  body: issueBody = location.href +
207
- document
220
+ "\n\n" +
221
+ (document
208
222
  ?.querySelector('meta[name="description"]')
209
- ?.getAttribute("content") || "",
223
+ ?.getAttribute("content") ?? ""),
210
224
  language = navigator.language as Lang,
211
225
  perPage: propsPerPage = 10,
212
226
  pagerDirection = "last",
@@ -223,30 +237,23 @@ const Gitalk: React.FC<GitalkProps> = (props) => {
223
237
  defaultUser: propsDefaultUser,
224
238
  defaultAuthor: propsDefaultAuthor,
225
239
  updateCountCallback,
240
+ onCreateIssue,
226
241
  onCreateComment,
227
242
  className = "",
228
243
  ...restProps
229
244
  } = 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
245
 
249
- logger.i("re-rendered.");
246
+ const [issue, setIssue] = useState<IssueType>();
247
+ const issueNumber = useMemo(
248
+ () =>
249
+ propsIssueNumber && propsIssueNumber > 0 ? propsIssueNumber : undefined,
250
+ [propsIssueNumber],
251
+ );
252
+ const issueId = useMemo(() => propsIssueId.slice(0, 50), [propsIssueId]);
253
+ const issueLabels = useMemo(
254
+ () => propsIssueLabels.concat([issueId]),
255
+ [propsIssueLabels, issueId],
256
+ );
250
257
 
251
258
  const textareaRef = useRef<HTMLTextAreaElement>(null);
252
259
  const [inputComment, setInputComment] = useState<string>("");
@@ -256,9 +263,26 @@ const Gitalk: React.FC<GitalkProps> = (props) => {
256
263
  const [commentsCount, setCommentsCount] = useState<number>(0);
257
264
  const [commentsCursor, setCommentsCursor] = useState("");
258
265
  const [commentsPage, setCommentsPage] = useState<number>(1);
266
+ const commentsPerPage = useMemo(
267
+ () => (propsPerPage > 100 ? 100 : propsPerPage < 0 ? 10 : propsPerPage),
268
+ [propsPerPage],
269
+ );
259
270
  const [commentsLoaded, setCommentsLoaded] = useState<boolean>(false);
260
271
  const [commentsPagerDirection, setCommentsPagerDirection] =
261
272
  useState(pagerDirection);
273
+ const defaultUser = useMemo(
274
+ () =>
275
+ propsDefaultUser
276
+ ? propsDefaultUser
277
+ : propsDefaultAuthor
278
+ ? {
279
+ avatar_url: propsDefaultAuthor.avatarUrl,
280
+ login: propsDefaultAuthor.login,
281
+ html_url: propsDefaultAuthor.url,
282
+ }
283
+ : DEFAULT_USER,
284
+ [propsDefaultAuthor, propsDefaultUser],
285
+ );
262
286
 
263
287
  const [showPopup, setShowPopup] = useState<boolean>(false);
264
288
 
@@ -336,12 +360,7 @@ const Gitalk: React.FC<GitalkProps> = (props) => {
336
360
  );
337
361
  }, [admin, user]);
338
362
 
339
- const issueLabels = useMemo(
340
- () => issueBaseLabels.concat([issueId]),
341
- [issueBaseLabels, issueId],
342
- );
343
-
344
- const { loading: createIssueLoading, runAsync: runCreateIssue } = useRequest(
363
+ const { loading: createIssueLoading, run: runCreateIssue } = useRequest(
345
364
  async () => {
346
365
  logger.i(`Creating issue...`);
347
366
 
@@ -359,11 +378,13 @@ const Gitalk: React.FC<GitalkProps> = (props) => {
359
378
  if (createIssueRes.status === 201) {
360
379
  const _issue = createIssueRes.data;
361
380
  logger.s(`Create issue successfully:`, _issue);
362
- return _issue;
381
+
382
+ onCreateIssue?.(_issue);
383
+
384
+ setIssue(_issue);
363
385
  } else {
364
386
  setAlert(`Create issue failed: ${createIssueRes}`);
365
387
  logger.e(`Create issue failed:`, createIssueRes);
366
- return undefined;
367
388
  }
368
389
  },
369
390
  {
@@ -373,11 +394,7 @@ const Gitalk: React.FC<GitalkProps> = (props) => {
373
394
  },
374
395
  );
375
396
 
376
- const {
377
- data: issue,
378
- mutate: setIssue,
379
- loading: getIssueLoading,
380
- } = useRequest(
397
+ const { loading: getIssueLoading } = useRequest(
381
398
  async () => {
382
399
  if (issueNumber) {
383
400
  const getIssueRes = await octokit.request(
@@ -395,15 +412,14 @@ const Gitalk: React.FC<GitalkProps> = (props) => {
395
412
  `Locate issue ${issueNumber} in repository ${owner}/${repo} successfully:`,
396
413
  _issue,
397
414
  );
398
- return _issue;
415
+ setIssue(_issue);
399
416
  } else if (getIssueRes.status === 404) {
400
417
  logger.w(
401
418
  `Issue ${issueNumber} in repository ${owner}/${repo} was not found.`,
402
419
  );
403
420
 
404
421
  if (isAdmin && !createIssueManually) {
405
- const _issue = await runCreateIssue();
406
- return _issue;
422
+ runCreateIssue();
407
423
  }
408
424
  } else {
409
425
  setAlert(
@@ -433,15 +449,14 @@ const Gitalk: React.FC<GitalkProps> = (props) => {
433
449
  `Locate issue with labels ${issueLabels} in repository ${owner}/${repo} successfully:`,
434
450
  _issue,
435
451
  );
436
- return _issue;
452
+ setIssue(_issue);
437
453
  } else {
438
454
  logger.w(
439
455
  `Issue with labels ${issueLabels} in repository ${owner}/${repo} was not found.`,
440
456
  );
441
457
 
442
458
  if (isAdmin && !createIssueManually) {
443
- const _issue = await runCreateIssue();
444
- return _issue;
459
+ runCreateIssue();
445
460
  }
446
461
  }
447
462
  } else if (getIssuesStatus === 404) {
@@ -450,8 +465,7 @@ const Gitalk: React.FC<GitalkProps> = (props) => {
450
465
  );
451
466
 
452
467
  if (isAdmin && !createIssueManually) {
453
- const _issue = await runCreateIssue();
454
- return _issue;
468
+ runCreateIssue();
455
469
  }
456
470
  } else {
457
471
  setAlert(
@@ -463,8 +477,6 @@ const Gitalk: React.FC<GitalkProps> = (props) => {
463
477
  );
464
478
  }
465
479
  }
466
-
467
- return undefined;
468
480
  },
469
481
  {
470
482
  ready: !!owner && !!repo && (!!issueNumber || !!issueId),
@@ -1214,7 +1226,7 @@ const Gitalk: React.FC<GitalkProps> = (props) => {
1214
1226
  value={{ language, polyglot, dateFnsLocaleMap: DATE_FNS_LOCALE_MAP }}
1215
1227
  >
1216
1228
  <div
1217
- className={`gt-container ${isInputFocused ? "gt-input-focused" : ""} ${className}`}
1229
+ className={`gt-container${isInputFocused ? " gt-input-focused" : ""} ${className}`}
1218
1230
  {...restProps}
1219
1231
  >
1220
1232
  {alert && <div className="gt-error">{alert}</div>}
@@ -568,7 +568,6 @@ $gt-breakpoint-mobile: 479px;
568
568
 
569
569
  &::after {
570
570
  position: fixed;
571
- inset: 0 0 100%;
572
571
  content: '';
573
572
  opacity: 0;
574
573
  }
@@ -577,12 +576,11 @@ $gt-breakpoint-mobile: 479px;
577
576
  position: relative;
578
577
 
579
578
  &::after {
579
+ inset: 0;
580
580
  z-index: $gt-mask-z-index;
581
- background: #000;
582
- opacity: 0.6;
583
- transition:
584
- opacity 0.3s,
585
- bottom 0s;
581
+ background: #171717;
582
+ opacity: 0.8;
583
+ transition: opacity ease 0.3s;
586
584
  }
587
585
 
588
586
  .gt-header-comment {
@@ -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;
@@ -0,0 +1 @@
1
+ /// <reference types="vite/client" />
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.5",
4
4
  "private": false,
5
5
  "author": {
6
6
  "name": "LolipopJ",
@@ -26,19 +26,22 @@
26
26
  "require": "./dist/gitalk.umd.cjs"
27
27
  },
28
28
  "./gitalk.css": "./dist/gitalk-light.css",
29
+ "./light.css": "./dist/gitalk-light.css",
29
30
  "./gitalk-light.css": "./dist/gitalk-light.css",
31
+ "./dark.css": "./dist/gitalk-dark.css",
30
32
  "./gitalk-dark.css": "./dist/gitalk-dark.css"
31
33
  },
32
34
  "types": "./dist/gitalk.d.ts",
33
35
  "scripts": {
34
- "dev": "vite",
35
- "build": "npm run build:ts && npm run build:scss",
36
+ "dev": "vite --config vite.config.preview.ts",
37
+ "build": "npm run build:lib && npm run build:preview",
38
+ "build:lib": "npm run build:ts && npm run build:scss",
36
39
  "build:ts": "tsc -b && vite build",
37
40
  "build:scss": "vite build --config vite.config.scss.ts",
41
+ "build:preview": "vite build --config vite.config.preview.ts",
38
42
  "lint": "npm run lint:ts && npm run lint:scss",
39
43
  "lint:ts": "eslint . --fix",
40
- "lint:scss": "stylelint lib/**/*.scss --fix",
41
- "preview": "vite preview"
44
+ "lint:scss": "stylelint **/*.scss --fix"
42
45
  },
43
46
  "dependencies": {
44
47
  "ahooks": "^3.0.0",