QaBox.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { useEffect, useState } from "react";
  2. import { ArrowLeftOutlined } from "@ant-design/icons";
  3. import DiscussionTopic from "./DiscussionTopic";
  4. import type { TResType } from "./DiscussionListCard"
  5. import type { IComment } from "./DiscussionItem"
  6. import { Button, Space, Typography } from "antd";
  7. import type { TDiscussionType } from "./Discussion"
  8. import QaList from "./QaList";
  9. const { Text } = Typography;
  10. interface IWidget {
  11. resId?: string;
  12. resType?: TResType;
  13. showTopicId?: string;
  14. focus?: string;
  15. onTopicReady?: Function;
  16. }
  17. const DiscussionWidget = ({
  18. resId,
  19. resType,
  20. showTopicId,
  21. focus,
  22. onTopicReady,
  23. }: IWidget) => {
  24. const [childrenDrawer, setChildrenDrawer] = useState(false);
  25. const [topicId, setTopicId] = useState<string>();
  26. const [topic, setTopic] = useState<IComment>();
  27. const [topicTitle, setTopicTitle] = useState<string>();
  28. useEffect(() => {
  29. if (showTopicId) {
  30. setChildrenDrawer(true);
  31. setTopicId(showTopicId);
  32. } else {
  33. setChildrenDrawer(false);
  34. }
  35. }, [showTopicId]);
  36. const showChildrenDrawer = (comment: IComment) => {
  37. console.debug("discussion comment", comment);
  38. setChildrenDrawer(true);
  39. if (comment.id) {
  40. setTopicId(comment.id);
  41. setTopic(undefined);
  42. } else {
  43. setTopicId(undefined);
  44. setTopic(comment);
  45. }
  46. };
  47. return (
  48. <>
  49. {childrenDrawer ? (
  50. <div>
  51. <Space>
  52. <Button
  53. shape="circle"
  54. icon={<ArrowLeftOutlined />}
  55. onClick={() => setChildrenDrawer(false)}
  56. />
  57. <Text strong style={{ fontSize: 16 }}>
  58. {topic ? topic.title : topicTitle}
  59. </Text>
  60. </Space>
  61. <DiscussionTopic
  62. resType={resType}
  63. topicId={topicId}
  64. topic={topic}
  65. focus={focus}
  66. hideTitle
  67. onTopicReady={(value: IComment) => {
  68. setTopicTitle(value.title);
  69. if (typeof onTopicReady !== "undefined") {
  70. onTopicReady(value);
  71. }
  72. }}
  73. onTopicDelete={() => {
  74. setChildrenDrawer(false);
  75. }}
  76. onConvert={(_value: TDiscussionType) => {
  77. setChildrenDrawer(false);
  78. }}
  79. />
  80. </div>
  81. ) : (
  82. <QaList
  83. resId={resId}
  84. resType={resType}
  85. onSelect={(
  86. _e: React.MouseEvent<HTMLSpanElement, MouseEvent>,
  87. comment: IComment
  88. ) => showChildrenDrawer(comment)}
  89. onReply={(comment: IComment) => showChildrenDrawer(comment)}
  90. />
  91. )}
  92. </>
  93. );
  94. };
  95. export default DiscussionWidget;