2
0

DiscussionTopic.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { useEffect, useState } from "react";
  2. import DiscussionTopicInfo from "./DiscussionTopicInfo";
  3. import DiscussionTopicChildren from "./DiscussionTopicChildren";
  4. import type { IComment } from "./DiscussionItem"
  5. import type { TResType } from "./DiscussionListCard"
  6. import type { TDiscussionType } from "./Discussion"
  7. interface IWidget {
  8. resType?: TResType;
  9. topicId?: string;
  10. topic?: IComment;
  11. focus?: string;
  12. hideTitle?: boolean;
  13. hideReply?: boolean;
  14. onItemCountChange?: Function;
  15. onTopicReady?: Function;
  16. onTopicDelete?: Function;
  17. onConvert?: Function;
  18. }
  19. const DiscussionTopicWidget = ({
  20. resType,
  21. topicId,
  22. topic,
  23. focus,
  24. hideTitle = false,
  25. hideReply = false,
  26. onTopicReady,
  27. onItemCountChange,
  28. onTopicDelete,
  29. onConvert,
  30. }: IWidget) => {
  31. const [count, setCount] = useState<number>();
  32. const [_currResId, setCurrResId] = useState<string>();
  33. const [currTopicId, setCurrTopicId] = useState(topicId);
  34. const [currTopic, setCurrTopic] = useState<IComment | undefined>(topic);
  35. useEffect(() => {
  36. setCurrTopic(topic);
  37. }, [topic]);
  38. return (
  39. <>
  40. <DiscussionTopicInfo
  41. topicId={currTopicId}
  42. topic={currTopic}
  43. hideTitle={hideTitle}
  44. childrenCount={count}
  45. onReady={(value: IComment) => {
  46. setCurrResId(value.resId);
  47. setCurrTopic(value);
  48. console.log("discussion onReady", value);
  49. if (typeof onTopicReady !== "undefined") {
  50. onTopicReady(value);
  51. }
  52. }}
  53. onDelete={() => {
  54. if (typeof onTopicDelete !== "undefined") {
  55. onTopicDelete();
  56. }
  57. }}
  58. onConvert={(value: TDiscussionType) => {
  59. if (typeof onConvert !== "undefined") {
  60. onConvert(value);
  61. }
  62. }}
  63. />
  64. <DiscussionTopicChildren
  65. topic={currTopic}
  66. resId={currTopic?.resId}
  67. resType={resType}
  68. focus={focus}
  69. topicId={topicId}
  70. hideReply={hideReply}
  71. onItemCountChange={(count: number, e: string) => {
  72. //把新建回答的消息传出去。
  73. setCount(count);
  74. if (typeof onItemCountChange !== "undefined") {
  75. onItemCountChange(count, e);
  76. }
  77. }}
  78. onTopicCreate={(value: IComment) => {
  79. console.log("onTopicCreate", value);
  80. setCurrTopicId(value.id);
  81. }}
  82. />
  83. </>
  84. );
  85. };
  86. export default DiscussionTopicWidget;