DiscussionBox.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { useState } from "react";
  2. import { Button, Divider, Drawer, Space } from "antd";
  3. import { FullscreenOutlined, FullscreenExitOutlined } from "@ant-design/icons";
  4. import DiscussionTopic from "./DiscussionTopic";
  5. import DiscussionListCard, { TResType } from "./DiscussionListCard";
  6. import { IComment } from "./DiscussionItem";
  7. import DiscussionAnchor from "./DiscussionAnchor";
  8. import { Link } from "react-router-dom";
  9. export interface IAnswerCount {
  10. id: string;
  11. count: number;
  12. }
  13. interface IWidget {
  14. trigger?: JSX.Element;
  15. resId?: string;
  16. resType?: TResType;
  17. onCommentCountChange?: Function;
  18. }
  19. const DiscussionBoxWidget = ({
  20. trigger,
  21. resId,
  22. resType,
  23. onCommentCountChange,
  24. }: IWidget) => {
  25. const [open, setOpen] = useState(false);
  26. const [childrenDrawer, setChildrenDrawer] = useState(false);
  27. const [topicComment, setTopicComment] = useState<IComment>();
  28. const [answerCount, setAnswerCount] = useState<IAnswerCount>();
  29. const drawerMinWidth = 720;
  30. const drawerMaxWidth = 1100;
  31. const [drawerWidth, setDrawerWidth] = useState(drawerMinWidth);
  32. const showChildrenDrawer = (comment: IComment) => {
  33. setChildrenDrawer(true);
  34. setTopicComment(comment);
  35. };
  36. return (
  37. <>
  38. <span
  39. onClick={() => {
  40. setOpen(true);
  41. }}
  42. >
  43. {trigger}
  44. </span>
  45. <Drawer
  46. title="Discussion"
  47. destroyOnClose
  48. extra={
  49. <Space>
  50. <Link to={`/discussion/show/${resType}/${resId}`} target="_blank">
  51. 在新窗口打开
  52. </Link>
  53. {drawerWidth === drawerMinWidth ? (
  54. <Button
  55. type="link"
  56. icon={<FullscreenOutlined />}
  57. onClick={() => setDrawerWidth(drawerMaxWidth)}
  58. />
  59. ) : (
  60. <Button
  61. type="link"
  62. icon={<FullscreenExitOutlined />}
  63. onClick={() => setDrawerWidth(drawerMinWidth)}
  64. />
  65. )}
  66. </Space>
  67. }
  68. width={drawerWidth}
  69. onClose={() => {
  70. setOpen(false);
  71. if (document.getElementsByTagName("body")[0].hasAttribute("style")) {
  72. document.getElementsByTagName("body")[0].removeAttribute("style");
  73. }
  74. }}
  75. open={open}
  76. maskClosable={false}
  77. >
  78. <DiscussionAnchor resId={resId} resType={resType} />
  79. <Divider></Divider>
  80. <DiscussionListCard
  81. resId={resId}
  82. resType={resType}
  83. onSelect={(
  84. e: React.MouseEvent<HTMLSpanElement, MouseEvent>,
  85. comment: IComment
  86. ) => showChildrenDrawer(comment)}
  87. onReply={(comment: IComment) => showChildrenDrawer(comment)}
  88. changedAnswerCount={answerCount}
  89. onItemCountChange={(count: number) => {
  90. if (typeof onCommentCountChange !== "undefined") {
  91. onCommentCountChange(count);
  92. }
  93. }}
  94. />
  95. <Drawer
  96. title="Answer"
  97. width={700}
  98. onClose={() => {
  99. setChildrenDrawer(false);
  100. }}
  101. open={childrenDrawer}
  102. >
  103. <DiscussionTopic
  104. topicId={topicComment?.id}
  105. onItemCountChange={(count: number, parent: string) => {
  106. setAnswerCount({ id: parent, count: count });
  107. }}
  108. />
  109. </Drawer>
  110. </Drawer>
  111. </>
  112. );
  113. };
  114. export default DiscussionBoxWidget;