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

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/lib/gitalk.tsx CHANGED
@@ -267,7 +267,7 @@ const Gitalk: React.FC<GitalkProps> = (props) => {
267
267
  () => (propsPerPage > 100 ? 100 : propsPerPage < 0 ? 10 : propsPerPage),
268
268
  [propsPerPage],
269
269
  );
270
- const [commentsLoaded, setCommentsLoaded] = useState<boolean>(false);
270
+ /** Current sort order, have effect when user is logged */
271
271
  const [commentsPagerDirection, setCommentsPagerDirection] =
272
272
  useState(pagerDirection);
273
273
  const defaultUser = useMemo(
@@ -490,6 +490,7 @@ const Gitalk: React.FC<GitalkProps> = (props) => {
490
490
  const {
491
491
  data: comments = [],
492
492
  mutate: setComments,
493
+ run: runGetComments,
493
494
  loading: getCommentsLoading,
494
495
  } = useRequest(
495
496
  async (): Promise<CommentType[]> => {
@@ -500,7 +501,7 @@ const Gitalk: React.FC<GitalkProps> = (props) => {
500
501
  if (user) {
501
502
  // Get comments via GraphQL, witch requires being logged and able to sort
502
503
  const query = getIssueCommentsQL({
503
- pagerDirection,
504
+ pagerDirection: commentsPagerDirection,
504
505
  });
505
506
 
506
507
  const getIssueCommentsRes: IssueCommentsQLResponse =
@@ -540,11 +541,14 @@ const Gitalk: React.FC<GitalkProps> = (props) => {
540
541
  _comments,
541
542
  );
542
543
 
543
- if (_comments.length < commentsPerPage) {
544
- setCommentsLoaded(true);
545
- }
544
+ const commentsPageInfo =
545
+ getIssueCommentsRes.repository.issue.comments.pageInfo;
546
+ const commentsPageCursor =
547
+ commentsPageInfo.startCursor || commentsPageInfo.endCursor || "";
548
+ setCommentsCursor(commentsPageCursor);
546
549
 
547
- if (pagerDirection === "last") return [..._comments, ...comments];
550
+ if (commentsPagerDirection === "last")
551
+ return [..._comments, ...comments];
548
552
  else return [...comments, ..._comments];
549
553
  } else {
550
554
  setAlert(
@@ -589,9 +593,7 @@ const Gitalk: React.FC<GitalkProps> = (props) => {
589
593
  _comments,
590
594
  );
591
595
 
592
- if (_comments.length < commentsPerPage) {
593
- setCommentsLoaded(true);
594
- }
596
+ setCommentsPage((prev) => prev + 1);
595
597
 
596
598
  return [...comments, ..._comments];
597
599
  } else {
@@ -608,8 +610,8 @@ const Gitalk: React.FC<GitalkProps> = (props) => {
608
610
  return comments;
609
611
  },
610
612
  {
611
- ready: !!owner && !!repo && !!issue && !getUserLoading && !commentsLoaded,
612
- refreshDeps: [commentsPage, issue, user, pagerDirection],
613
+ manual: true,
614
+ ready: !!owner && !!repo && !!issue && !getUserLoading,
613
615
  },
614
616
  );
615
617
 
@@ -649,6 +651,8 @@ const Gitalk: React.FC<GitalkProps> = (props) => {
649
651
  logger.s(`Create issue comment successfully.`);
650
652
 
651
653
  setInputComment("");
654
+ setCommentsCount((prev) => prev + 1);
655
+
652
656
  onCreateComment?.(createdIssueComment);
653
657
 
654
658
  return localComments.concat([createdIssueComment]);
@@ -666,34 +670,50 @@ const Gitalk: React.FC<GitalkProps> = (props) => {
666
670
 
667
671
  useEffect(() => {
668
672
  setComments([]);
669
- setCommentsCount(0);
673
+ setCommentsCount(issue?.comments ?? 0);
670
674
  setCommentsCursor("");
671
675
  setCommentsPage(1);
672
- setCommentsLoaded(false);
673
676
  setLocalComments([]);
674
677
 
675
- if (issue) {
676
- setCommentsCount(issue.comments);
677
- }
678
- }, [issue, user, pagerDirection, setComments, setLocalComments]);
678
+ setTimeout(() => {
679
+ runGetComments();
680
+ });
681
+ }, [issue, runGetComments, setComments, setLocalComments]);
682
+
683
+ useEffect(() => {
684
+ setComments([]);
685
+ setCommentsCursor("");
686
+ setCommentsPage(1);
687
+
688
+ setTimeout(() => {
689
+ runGetComments();
690
+ });
691
+ }, [user, commentsPagerDirection, setComments, runGetComments]);
679
692
 
680
693
  /** sorted all comments */
681
- const allComments = useMemo(() => {
682
- const _allComments = comments.concat(localComments);
694
+ const loadedComments = useMemo(() => {
695
+ const _loadedComments: CommentType[] = [];
696
+
697
+ // filter duplicate comments if exist
698
+ const commentIdsSet = new Set();
699
+ for (const comment of comments.concat(localComments)) {
700
+ if (!commentIdsSet.has(comment.id)) {
701
+ commentIdsSet.add(comment.id);
702
+ _loadedComments.push(comment);
703
+ }
704
+ }
683
705
 
684
- if (commentsPagerDirection === "last" && !!user) {
706
+ if (!!user && commentsPagerDirection === "last") {
685
707
  // sort comments by date DESC
686
- _allComments.reverse();
708
+ _loadedComments.reverse();
687
709
  }
688
710
 
689
- return _allComments;
711
+ return _loadedComments;
690
712
  }, [comments, commentsPagerDirection, localComments, user]);
691
713
 
692
- const allCommentsCount = commentsCount + (localComments ?? []).length;
693
-
694
714
  useEffect(() => {
695
- updateCountCallback?.(allCommentsCount);
696
- }, [allCommentsCount, updateCountCallback]);
715
+ updateCountCallback?.(commentsCount);
716
+ }, [commentsCount, updateCountCallback]);
697
717
 
698
718
  const {
699
719
  data: commentHtml = "",
@@ -1127,20 +1147,20 @@ const Gitalk: React.FC<GitalkProps> = (props) => {
1127
1147
  return (
1128
1148
  <div className="gt-comments" key="comments">
1129
1149
  <FlipMove {...flipMoveOptions}>
1130
- {allComments.map((comment) => (
1150
+ {loadedComments.map((comment) => (
1131
1151
  <CommentWithForwardedRef key={comment.id} comment={comment} />
1132
1152
  ))}
1133
1153
  </FlipMove>
1134
- {!allCommentsCount && (
1154
+ {!commentsCount && (
1135
1155
  <p className="gt-comments-null">
1136
1156
  {polyglot.t("first-comment-person")}
1137
1157
  </p>
1138
1158
  )}
1139
- {!commentsLoaded && allCommentsCount ? (
1159
+ {commentsCount > loadedComments.length ? (
1140
1160
  <div className="gt-comments-controls">
1141
1161
  <Button
1142
1162
  className="gt-btn-loadmore"
1143
- onClick={() => setCommentsPage((prev) => prev + 1)}
1163
+ onClick={runGetComments}
1144
1164
  isLoading={getCommentsLoading}
1145
1165
  text={polyglot.t("load-more")}
1146
1166
  />
@@ -1159,8 +1179,8 @@ const Gitalk: React.FC<GitalkProps> = (props) => {
1159
1179
  className="gt-counts"
1160
1180
  dangerouslySetInnerHTML={{
1161
1181
  __html: polyglot.t("counts", {
1162
- counts: `<a class="gt-link gt-link-counts" href="${issue?.html_url}" target="_blank" rel="noopener noreferrer">${allCommentsCount}</a>`,
1163
- smart_count: allCommentsCount,
1182
+ counts: `<a class="gt-link gt-link-counts" href="${issue?.html_url}" target="_blank" rel="noopener noreferrer">${commentsCount}</a>`,
1183
+ smart_count: commentsCount,
1164
1184
  }),
1165
1185
  }}
1166
1186
  />
package/lib/i18n/index.ts CHANGED
@@ -13,7 +13,7 @@ import RU from "./ru.json";
13
13
  import ZHCN from "./zh-CN.json";
14
14
  import ZHTW from "./zh-TW.json";
15
15
 
16
- const i18nMap = {
16
+ export const i18nMap = {
17
17
  zh: ZHCN,
18
18
  "zh-CN": ZHCN,
19
19
  "zh-TW": ZHTW,
package/package.json CHANGED
@@ -1,13 +1,17 @@
1
1
  {
2
2
  "name": "gitalk-react",
3
- "version": "1.0.0-beta.5",
3
+ "version": "1.0.0-beta.6",
4
4
  "private": false,
5
5
  "author": {
6
6
  "name": "LolipopJ",
7
7
  "email": "mail@towind.fun",
8
8
  "url": "https://github.com/LolipopJ"
9
9
  },
10
- "homepage": "https://github.com/LolipopJ/gitalk-react",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/LolipopJ/gitalk-react.git"
13
+ },
14
+ "homepage": "https://lolipopj.github.io/gitalk-react",
11
15
  "license": "MIT",
12
16
  "type": "module",
13
17
  "files": [