ExpTime.tsx 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { useEffect, useState } from "react";
  2. import { get } from "../../request";
  3. import type { IUserStatisticResponse } from "../../api/Exp";
  4. import { Skeleton } from "antd";
  5. interface IWidget {
  6. userName?: string;
  7. }
  8. const ExpTime = ({ userName }: IWidget) => {
  9. const [expSum, setExpSum] = useState<number>();
  10. const [loading, setLoading] = useState(false);
  11. useEffect(() => {
  12. if (typeof userName === "undefined") {
  13. return;
  14. }
  15. const url = `/v2/user-statistic/${userName}?view=exp-sum`;
  16. console.info("api request", url);
  17. setLoading(true);
  18. get<IUserStatisticResponse>(url)
  19. .then((json) => {
  20. console.debug("api response", json);
  21. if (json.ok) {
  22. setExpSum(Math.ceil(json.data.exp.sum / 1000 / 60 / 60));
  23. }
  24. })
  25. .finally(() => {
  26. setLoading(false);
  27. })
  28. .catch((e) => console.error(e));
  29. }, [userName]);
  30. return loading ? (
  31. <Skeleton.Button active={true} size={"small"} shape={"default"} />
  32. ) : (
  33. <>
  34. {expSum}
  35. {"小时"}
  36. </>
  37. );
  38. };
  39. export default ExpTime;