Term.tsx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. import { useEffect, useState } from "react";
  2. import { Link } from "react-router";
  3. import { Button, Popover, Skeleton, Space, Tag } from "antd";
  4. import { Typography } from "antd";
  5. import { SearchOutlined, EditOutlined } from "@ant-design/icons";
  6. import store from "../../store";
  7. import TermModal from "../term/TermModal";
  8. import type { ITerm } from "../term/TermEdit";
  9. import type { ITermDataResponse, ITermResponse } from "../../api/Term";
  10. import {
  11. changedTerm,
  12. refresh,
  13. termCache,
  14. upgrade,
  15. } from "../../reducers/term-change";
  16. import { useAppSelector } from "../../hooks";
  17. import { get } from "../../request";
  18. import { fullUrl } from "../../utils";
  19. import lodash from "lodash";
  20. import { order, push } from "../../reducers/term-order";
  21. import { click } from "../../reducers/term-click";
  22. const { Text, Title } = Typography;
  23. const dataMap = (input?: ITermDataResponse): ITerm => {
  24. return {
  25. id: input?.guid,
  26. word: input?.word,
  27. meaning: input?.meaning,
  28. meaning2: input?.other_meaning?.split(","),
  29. summary: input?.summary ?? "",
  30. channelId: input?.channal,
  31. studioId: input?.studio.id,
  32. summary_is_community: input?.summary_is_community,
  33. };
  34. };
  35. interface ITermExtra {
  36. pali?: string;
  37. meaning2?: string[];
  38. }
  39. const TermExtra = ({ pali, meaning2 }: ITermExtra) => (
  40. <>
  41. {" "}
  42. {"("}
  43. <Text italic>{pali}</Text>
  44. {meaning2 ? <Text>{`,${meaning2}`}</Text> : undefined}
  45. {")"}
  46. </>
  47. );
  48. export interface IWidgetTermCtl {
  49. id?: string;
  50. word?: string;
  51. meaning?: string;
  52. meaning2?: string;
  53. channel?: string /**该术语在term表中的channel_id */;
  54. parentChannelId?: string /**该术语所在译文的channel_id */;
  55. parentStudioId?: string /**该术语所在译文的studio_id */;
  56. summary?: string;
  57. isCommunity?: boolean;
  58. compact?: boolean;
  59. }
  60. export const TermCtl = ({
  61. id,
  62. word,
  63. meaning,
  64. meaning2,
  65. channel,
  66. parentChannelId,
  67. parentStudioId,
  68. summary,
  69. isCommunity,
  70. compact = false,
  71. }: IWidgetTermCtl) => {
  72. const [openPopover, setOpenPopover] = useState(false);
  73. const [termData, setTermData] = useState<ITerm>({
  74. id: id,
  75. word: word,
  76. meaning: meaning,
  77. meaning2: meaning2?.split(","),
  78. summary: summary,
  79. channelId: channel,
  80. });
  81. const [isInit, setIsInit] = useState(true);
  82. const [loading, setLoading] = useState(false);
  83. const [community, setCommunity] = useState(isCommunity);
  84. const newTerm: ITermDataResponse | undefined = useAppSelector(changedTerm);
  85. const cache = useAppSelector(termCache);
  86. const [isFirst, setIsFirst] = useState(true);
  87. const [uid] = useState<string>(
  88. lodash.times(20, () => lodash.random(35).toString(36)).join("")
  89. );
  90. const termOrder = useAppSelector(order);
  91. useEffect(() => {
  92. if (word && parentChannelId) {
  93. const currTerm = {
  94. word: word,
  95. channelId: parentChannelId,
  96. first: uid,
  97. };
  98. store.dispatch(push(currTerm));
  99. }
  100. }, [parentChannelId, uid, word]);
  101. useEffect(() => {
  102. const index = termOrder?.findIndex(
  103. (value) =>
  104. value.word === word &&
  105. value.channelId === parentChannelId &&
  106. value.first === uid
  107. );
  108. if (index === -1) {
  109. setIsFirst(false);
  110. } else {
  111. setIsFirst(true);
  112. }
  113. }, [parentChannelId, termOrder, uid, word]);
  114. useEffect(() => {
  115. if (newTerm?.word === word && parentStudioId === newTerm?.studio.id) {
  116. console.debug("Term studio 匹配", newTerm);
  117. const newData = dataMap(newTerm);
  118. if (
  119. termData.channelId &&
  120. termData.channelId !== "" &&
  121. newTerm?.channal === termData.channelId
  122. ) {
  123. console.debug("Term channel 匹配");
  124. setTermData(newData);
  125. setIsInit(false);
  126. setCommunity(false);
  127. } else {
  128. console.debug("Term channel 不 匹配");
  129. setTermData(newData);
  130. setIsInit(false);
  131. setCommunity(false);
  132. }
  133. }
  134. }, [newTerm, parentStudioId, termData.channelId, word]);
  135. const onModalClose = () => {
  136. if (document.getElementsByTagName("body")[0].hasAttribute("style")) {
  137. document.getElementsByTagName("body")[0].removeAttribute("style");
  138. }
  139. };
  140. const onPopoverOpen = (visible: boolean) => {
  141. setOpenPopover(visible);
  142. if (visible && isInit && typeof id !== "undefined") {
  143. const term = cache?.find((value) => value.guid === id);
  144. if (term) {
  145. setTermData(dataMap(term));
  146. setIsInit(false);
  147. return;
  148. } else {
  149. const url = `/v2/terms/${id}?community_summary=1`;
  150. console.info("api request", url);
  151. setLoading(true);
  152. get<ITermResponse>(url)
  153. .then((json) => {
  154. if (json.ok) {
  155. setTermData(dataMap(json.data));
  156. setIsInit(false);
  157. store.dispatch(upgrade(json.data));
  158. }
  159. })
  160. .finally(() => setLoading(false));
  161. }
  162. }
  163. };
  164. if (typeof termData?.id === "string") {
  165. return (
  166. <>
  167. <span className="term"></span>
  168. <Popover
  169. title={
  170. <Space style={{ justifyContent: "space-between", width: "100%" }}>
  171. <span>
  172. <Text strong>{termData.meaning}</Text>{" "}
  173. {community ? <Tag>{"社区"}</Tag> : undefined}
  174. </span>
  175. <Space>
  176. <Button
  177. onClick={() => {
  178. window.open(
  179. fullUrl(`/term/list/${termData.word}`),
  180. "_blank"
  181. );
  182. }}
  183. type="link"
  184. size="small"
  185. icon={<SearchOutlined />}
  186. />
  187. <TermModal
  188. onUpdate={(value: ITermDataResponse) => {
  189. onModalClose();
  190. sessionStorage.removeItem(`term/summary/${value.guid}`);
  191. store.dispatch(refresh(value));
  192. }}
  193. onClose={() => {
  194. onModalClose();
  195. }}
  196. trigger={
  197. <Button
  198. onClick={(_event: React.MouseEvent<any, MouseEvent>) => {
  199. setOpenPopover(false);
  200. //event.stopPropagation();
  201. }}
  202. type="link"
  203. size="small"
  204. icon={<EditOutlined />}
  205. />
  206. }
  207. id={termData.id}
  208. word={termData.word}
  209. channelId={termData.channelId}
  210. parentChannelId={parentChannelId}
  211. parentStudioId={parentStudioId}
  212. community={community}
  213. />
  214. </Space>
  215. </Space>
  216. }
  217. open={openPopover}
  218. onOpenChange={onPopoverOpen}
  219. content={
  220. <div style={{ maxWidth: 500, minWidth: 300 }}>
  221. <Title level={5}>
  222. <Link to={`/term/list/${termData.word}`} target="_blank">
  223. {word}
  224. </Link>
  225. </Title>
  226. {loading ? (
  227. <Skeleton
  228. title={{ width: 200 }}
  229. paragraph={{ rows: 4 }}
  230. active
  231. />
  232. ) : (
  233. <>
  234. <div>{termData.summary}</div>
  235. <div style={{ textAlign: "right" }}>
  236. {termData.summary_is_community ? "社区解释" : ""}
  237. </div>
  238. </>
  239. )}
  240. </div>
  241. }
  242. placement="bottom"
  243. >
  244. <Typography.Link
  245. style={{
  246. color: community ? "green" : undefined,
  247. wordBreak: "keep-all",
  248. }}
  249. onClick={() => {
  250. console.debug("term send redux");
  251. store.dispatch(click(termData));
  252. }}
  253. >
  254. {termData?.meaning ?? termData?.word ?? "unknown"}
  255. </Typography.Link>
  256. </Popover>
  257. {isFirst && !compact ? (
  258. <TermExtra pali={word} meaning2={termData?.meaning2} />
  259. ) : undefined}
  260. </>
  261. );
  262. } else {
  263. //术语未创建
  264. return (
  265. <TermModal
  266. onUpdate={(value: ITermDataResponse) => {
  267. onModalClose();
  268. store.dispatch(refresh(value));
  269. }}
  270. onClose={() => {
  271. onModalClose();
  272. }}
  273. trigger={
  274. <Typography.Link>
  275. <Text type="danger" style={{ wordBreak: "keep-all" }}>
  276. {termData?.word}
  277. </Text>
  278. </Typography.Link>
  279. }
  280. word={termData?.word}
  281. parentChannelId={parentChannelId}
  282. parentStudioId={parentStudioId}
  283. community={community}
  284. />
  285. );
  286. }
  287. };
  288. interface IWidgetTerm {
  289. props: string;
  290. }
  291. const Widget = ({ props }: IWidgetTerm) => {
  292. const prop = JSON.parse(atob(props)) as IWidgetTermCtl;
  293. return (
  294. <>
  295. <TermCtl {...prop} />
  296. </>
  297. );
  298. };
  299. export default Widget;