InteractiveArea.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { useEffect, useState } from "react";
  2. import { Tabs } from "antd";
  3. import type { TResType } from "./DiscussionListCard"
  4. import Discussion from "./Discussion";
  5. import { get } from "../../request";
  6. import QaBox from "./QaBox";
  7. interface IInteractive {
  8. ok: boolean;
  9. data: ITypeData;
  10. message: string;
  11. }
  12. interface ITypeData {
  13. qa: IPower;
  14. help: IPower;
  15. discussion: IPower;
  16. }
  17. interface IPower {
  18. can_create: boolean;
  19. can_reply: boolean;
  20. count: number;
  21. }
  22. interface IWidget {
  23. resId?: string;
  24. resType?: TResType;
  25. }
  26. const InteractiveAreaWidget = ({ resId, resType }: IWidget) => {
  27. const [showQa, setShowQa] = useState(false);
  28. const [_qaCanEdit, setQaCanEdit] = useState(false);
  29. const [showHelp, setShowHelp] = useState(false);
  30. const [showDiscussion, setShowDiscussion] = useState(false);
  31. useEffect(() => {
  32. get<IInteractive>(`/v2/interactive/${resId}?res_type=${resType}`).then(
  33. (json) => {
  34. if (json.ok) {
  35. console.debug("interactive", json);
  36. if (json.data.qa.can_create || json.data.qa.can_reply) {
  37. setShowQa(true);
  38. setQaCanEdit(true);
  39. } else if (json.data.qa.count > 0) {
  40. setShowQa(true);
  41. } else {
  42. setShowQa(false);
  43. }
  44. if (json.data.help.can_create) {
  45. setShowHelp(true);
  46. } else if (json.data.help.can_reply) {
  47. if (json.data.help.count > 0) {
  48. setShowHelp(true);
  49. }
  50. } else {
  51. setShowHelp(false);
  52. }
  53. if (
  54. json.data.discussion.can_create ||
  55. json.data.discussion.can_reply ||
  56. json.data.discussion.count > 0
  57. ) {
  58. setShowDiscussion(true);
  59. } else {
  60. setShowDiscussion(false);
  61. }
  62. }
  63. }
  64. );
  65. }, [resId, resType]);
  66. return showQa || showHelp || showDiscussion ? (
  67. <Tabs
  68. size="small"
  69. items={[
  70. {
  71. label: `问答`,
  72. key: "qa",
  73. children: <QaBox resId={resId} resType={resType} />,
  74. },
  75. {
  76. label: `求助`,
  77. key: "help",
  78. children: <Discussion resId={resId} resType={resType} type="help" />,
  79. },
  80. {
  81. label: `讨论`,
  82. key: "discussion",
  83. children: (
  84. <Discussion resId={resId} resType={resType} type="discussion" />
  85. ),
  86. },
  87. ].filter((value) => {
  88. if (value.key === "qa") {
  89. return showQa;
  90. } else if (value.key === "help") {
  91. return showHelp;
  92. } else if (value.key === "discussion") {
  93. return showDiscussion;
  94. } else {
  95. return false;
  96. }
  97. })}
  98. />
  99. ) : (
  100. <></>
  101. );
  102. };
  103. export default InteractiveAreaWidget;