DiscussionItem.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { Avatar } from "antd";
  2. import { useEffect, useState } from "react";
  3. import type { IUser } from "../auth/User"
  4. import DiscussionShow from "./DiscussionShow";
  5. import DiscussionEdit from "./DiscussionEdit";
  6. import type { TResType } from "./DiscussionListCard"
  7. import type { TDiscussionType } from "./Discussion"
  8. export interface IComment {
  9. id?: string; //id未提供为新建
  10. resId?: string;
  11. resType?: TResType;
  12. type: TDiscussionType;
  13. tplId?: string;
  14. user: IUser;
  15. parent?: string | null;
  16. title?: string;
  17. content?: string;
  18. html?: string;
  19. summary?: string;
  20. status?: "active" | "close";
  21. children?: IComment[];
  22. childrenCount?: number;
  23. newTpl?: boolean;
  24. createdAt?: string;
  25. updatedAt?: string;
  26. }
  27. interface IWidget {
  28. data: IComment;
  29. isFocus?: boolean;
  30. hideTitle?: boolean;
  31. onSelect?: Function;
  32. onCreated?: Function;
  33. onDelete?: Function;
  34. onReply?: Function;
  35. onClose?: Function;
  36. onConvert?: Function;
  37. }
  38. const DiscussionItemWidget = ({
  39. data,
  40. isFocus = false,
  41. hideTitle = false,
  42. onSelect,
  43. onCreated,
  44. onDelete,
  45. onReply,
  46. onClose,
  47. onConvert,
  48. }: IWidget) => {
  49. const [edit, setEdit] = useState(false);
  50. const [currData, setCurrData] = useState<IComment>(data);
  51. useEffect(() => {
  52. setCurrData(data);
  53. }, [data]);
  54. return (
  55. <div
  56. id={`answer-${data.id}`}
  57. style={{
  58. display: "flex",
  59. width: "100%",
  60. border: isFocus ? "2px solid blue" : "unset",
  61. borderRadius: 10,
  62. padding: 5,
  63. }}
  64. >
  65. <div style={{ width: "2em", display: "none" }}>
  66. <Avatar size="small">{data.user?.nickName?.slice(0, 1)}</Avatar>
  67. </div>
  68. <div style={{ width: "100%" }}>
  69. {edit ? (
  70. <DiscussionEdit
  71. data={currData}
  72. onUpdated={(e: IComment) => {
  73. setCurrData(e);
  74. setEdit(false);
  75. }}
  76. onCreated={(e: IComment) => {
  77. if (typeof onCreated !== "undefined") {
  78. onCreated(e);
  79. }
  80. }}
  81. onClose={() => setEdit(false)}
  82. />
  83. ) : (
  84. <DiscussionShow
  85. data={currData}
  86. hideTitle={hideTitle}
  87. onEdit={() => {
  88. setEdit(true);
  89. }}
  90. onSelect={(e: React.MouseEvent<HTMLSpanElement, MouseEvent>) => {
  91. if (typeof onSelect !== "undefined") {
  92. onSelect(e, currData);
  93. }
  94. }}
  95. onDelete={(_id: string) => {
  96. if (typeof onDelete !== "undefined") {
  97. onDelete();
  98. }
  99. }}
  100. onReply={() => {
  101. if (typeof onReply !== "undefined") {
  102. onReply(currData);
  103. }
  104. }}
  105. onClose={(value: boolean) => {
  106. if (typeof onClose !== "undefined") {
  107. onClose(value);
  108. }
  109. }}
  110. onConvert={(value: TDiscussionType) => {
  111. if (typeof onConvert !== "undefined") {
  112. onConvert(value);
  113. }
  114. }}
  115. />
  116. )}
  117. </div>
  118. </div>
  119. );
  120. };
  121. export default DiscussionItemWidget;