2
0

DiscussionAnchor.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { Skeleton } from "antd";
  2. import { useEffect, useState } from "react";
  3. import { get } from "../../request";
  4. import type { IArticleResponse } from "../../api/Article";
  5. import type { ICommentAnchorResponse } from "../../api/Comment";
  6. import type { ISentenceData, ISentenceResponse } from "../../api/Corpus";
  7. import MdView from "../template/MdView";
  8. import AnchorCard from "./AnchorCard";
  9. import type { TResType } from "./DiscussionListCard";
  10. import { Link } from "react-router";
  11. export interface IAnchor {
  12. type: TResType;
  13. sentence?: ISentenceData;
  14. }
  15. interface IWidget {
  16. resId?: string;
  17. resType?: TResType;
  18. topicId?: string;
  19. onLoad?: Function;
  20. }
  21. const DiscussionAnchorWidget = ({
  22. resId,
  23. resType,
  24. topicId,
  25. onLoad,
  26. }: IWidget) => {
  27. const [title, setTitle] = useState<React.ReactNode>();
  28. const [content, setContent] = useState<string>();
  29. const [loading, setLoading] = useState(false);
  30. useEffect(() => {
  31. if (typeof topicId === "string") {
  32. const url = `/v2/discussion-anchor/${topicId}`;
  33. console.info("api request", url);
  34. get<ICommentAnchorResponse>(url).then((json) => {
  35. console.debug("api response", json);
  36. if (json.ok) {
  37. setContent(json.data);
  38. }
  39. });
  40. }
  41. }, [topicId]);
  42. useEffect(() => {
  43. let url: string;
  44. switch (resType) {
  45. case "sentence":
  46. url = `/v2/sentence/${resId}`;
  47. console.info("api request", url);
  48. setLoading(true);
  49. get<ISentenceResponse>(url)
  50. .then((json) => {
  51. console.info("api response", json);
  52. if (json.ok) {
  53. const id = `${json.data.book}-${json.data.paragraph}-${json.data.word_start}-${json.data.word_end}`;
  54. const channel = json.data.channel.id;
  55. const url = `/v2/corpus-sent/${id}?mode=edit&channels=${channel}`;
  56. console.log("url", url);
  57. get<IArticleResponse>(url).then((json) => {
  58. if (json.ok) {
  59. setContent(json.data.content);
  60. }
  61. });
  62. if (typeof onLoad !== "undefined") {
  63. onLoad({ type: resType, sentence: json.data });
  64. }
  65. }
  66. })
  67. .finally(() => setLoading(false));
  68. break;
  69. case "article":
  70. url = `/v2/article/${resId}`;
  71. console.info("url", url);
  72. setLoading(true);
  73. get<IArticleResponse>(url)
  74. .then((json) => {
  75. if (json.ok) {
  76. setTitle(
  77. <Link to={`/article/article/${resId}`}>{json.data.title}</Link>
  78. );
  79. setContent(json.data.content?.substring(0, 200));
  80. }
  81. })
  82. .finally(() => setLoading(false));
  83. break;
  84. default:
  85. break;
  86. }
  87. }, [resId, resType]);
  88. return (
  89. <AnchorCard title={title}>
  90. {loading ? (
  91. <Skeleton title={{ width: 200 }} paragraph={{ rows: 4 }} active />
  92. ) : (
  93. <div>
  94. <MdView html={content} />
  95. </div>
  96. )}
  97. </AnchorCard>
  98. );
  99. };
  100. export default DiscussionAnchorWidget;