ExpStatisticCard.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { StatisticCard } from "@ant-design/pro-components";
  2. import { message } from "antd";
  3. import { useEffect, useState } from "react";
  4. import { useIntl } from "react-intl";
  5. import { get } from "../../request";
  6. import type { IUserStatisticResponse } from "../../api/Exp";
  7. import ExpPie, { type IPieData } from "./ExpPie";
  8. const { Divider } = StatisticCard;
  9. interface IWidget {
  10. studioName?: string;
  11. }
  12. const ExpStatisticCardWidget = ({ studioName }: IWidget) => {
  13. const intl = useIntl();
  14. const [expSum, setExpSum] = useState<number>();
  15. const [wbwCount, setWbwCount] = useState<number>();
  16. const [lookupCount, setLookupCount] = useState<number>();
  17. const [translationCount, setTranslationCount] = useState<number>();
  18. const [translationPieData, setTranslationPieData] = useState<IPieData[]>();
  19. const [termCount, setTermCount] = useState<number>();
  20. const [termPieData, setTermPieData] = useState<IPieData[]>();
  21. const [dictCount, setDictCount] = useState<number>();
  22. useEffect(() => {
  23. const url = `/v2/user-statistic/${studioName}`;
  24. console.info("api request", url);
  25. get<IUserStatisticResponse>(url).then((json) => {
  26. if (json.ok) {
  27. setExpSum(Math.ceil(json.data.exp.sum / 1000 / 60 / 60));
  28. setWbwCount(json.data.wbw.count);
  29. setLookupCount(json.data.lookup.count);
  30. setTranslationCount(json.data.translation.count);
  31. setTranslationPieData([
  32. { type: "公开", value: json.data.translation.count_pub },
  33. {
  34. type: "未公开",
  35. value:
  36. json.data.translation.count - json.data.translation.count_pub,
  37. },
  38. ]);
  39. setTermCount(json.data.term.count);
  40. setTermPieData([
  41. { type: "百科", value: json.data.term.count_with_note },
  42. {
  43. type: "仅术语",
  44. value: json.data.term.count - json.data.term.count_with_note,
  45. },
  46. ]);
  47. setDictCount(json.data.dict.count);
  48. } else {
  49. message.error(json.message);
  50. }
  51. });
  52. }, [studioName]);
  53. return (
  54. <StatisticCard.Group>
  55. <StatisticCard
  56. statistic={{
  57. title: "总经验",
  58. tip: "帮助文字",
  59. value: expSum,
  60. suffix: "小时",
  61. }}
  62. />
  63. <Divider />
  64. <StatisticCard
  65. statistic={{
  66. title: "逐词解析",
  67. value: wbwCount,
  68. suffix: "词",
  69. }}
  70. />
  71. <StatisticCard
  72. statistic={{
  73. title: intl.formatMessage({
  74. id: "buttons.lookup",
  75. }),
  76. value: lookupCount,
  77. suffix: "次",
  78. }}
  79. />
  80. <StatisticCard
  81. statistic={{
  82. title: "译文",
  83. value: translationCount,
  84. suffix: "句",
  85. }}
  86. chart={<ExpPie data={translationPieData} />}
  87. />
  88. <StatisticCard
  89. statistic={{
  90. title: "术语",
  91. value: termCount,
  92. suffix: "词",
  93. }}
  94. chart={<ExpPie data={termPieData} />}
  95. />
  96. <StatisticCard
  97. statistic={{
  98. title: "单词本",
  99. value: dictCount,
  100. suffix: "词",
  101. }}
  102. />
  103. </StatisticCard.Group>
  104. );
  105. };
  106. export default ExpStatisticCardWidget;