TimeShow.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import { Space, Tooltip, Typography } from "antd";
  2. import { useIntl } from "react-intl";
  3. import { FieldTimeOutlined } from "@ant-design/icons";
  4. import { useEffect, useState } from "react";
  5. import type { BaseType } from "antd/lib/typography/Base"
  6. const { Text } = Typography;
  7. interface IWidgetTimeShow {
  8. showIcon?: boolean;
  9. showTooltip?: boolean;
  10. showLabel?: boolean;
  11. createdAt?: string;
  12. updatedAt?: string;
  13. title?: string;
  14. type?: BaseType;
  15. }
  16. const TimeShowWidget = ({
  17. showIcon = true,
  18. ___showTooltip = true,
  19. showLabel = true,
  20. createdAt,
  21. updatedAt,
  22. title,
  23. type,
  24. }: IWidgetTimeShow) => {
  25. const intl = useIntl(); //i18n
  26. const [passTime, setPassTime] = useState<string>();
  27. const [mTime, setMTime] = useState(0);
  28. let mTitle: string | undefined;
  29. let showTime: string | undefined;
  30. if (updatedAt && createdAt) {
  31. if (updatedAt === createdAt) {
  32. mTitle = intl.formatMessage({
  33. id: "labels.created-at",
  34. });
  35. showTime = createdAt;
  36. } else {
  37. mTitle = intl.formatMessage({
  38. id: "labels.updated-at",
  39. });
  40. showTime = updatedAt;
  41. }
  42. } else if (createdAt) {
  43. mTitle = intl.formatMessage({
  44. id: "labels.created-at",
  45. });
  46. showTime = createdAt;
  47. } else if (updatedAt) {
  48. mTitle = intl.formatMessage({
  49. id: "labels.updated-at",
  50. });
  51. showTime = updatedAt;
  52. } else {
  53. mTitle = undefined;
  54. showTime = "";
  55. }
  56. if (typeof title !== "undefined") {
  57. mTitle = title;
  58. }
  59. useEffect(() => {
  60. if (typeof createdAt === "undefined" && typeof updatedAt === "undefined") {
  61. return;
  62. }
  63. const timer = setInterval(() => {
  64. setMTime((origin) => origin + 1);
  65. }, 1000 * 60);
  66. return () => {
  67. clearInterval(timer);
  68. };
  69. }, []);
  70. useEffect(() => {
  71. if (typeof showTime !== "undefined" && showTime !== "") {
  72. setPassTime(getPassDataTime(showTime));
  73. }
  74. }, [mTime, showTime]);
  75. if (typeof showTime === "undefined") {
  76. return <></>;
  77. }
  78. const icon = showIcon ? <FieldTimeOutlined /> : <></>;
  79. const tooltip: string = getFullDataTime(showTime);
  80. const color = "lime";
  81. function getPassDataTime(t: string): string {
  82. const currDate = new Date();
  83. const time = new Date(t);
  84. const pass = currDate.getTime() - time.getTime();
  85. let strPassTime = "";
  86. if (pass < 100 * 1000) {
  87. //一分钟内
  88. strPassTime = intl.formatMessage({ id: "utilities.time.now" });
  89. } else if (pass < 3600 * 1000) {
  90. //二小时内
  91. strPassTime =
  92. Math.floor(pass / 1000 / 60) +
  93. intl.formatMessage({ id: "utilities.time.mins_ago" });
  94. } else if (pass < 3600 * 24 * 1000) {
  95. //二天内
  96. strPassTime =
  97. Math.floor(pass / 1000 / 3600) +
  98. intl.formatMessage({ id: "utilities.time.hs_ago" });
  99. } else if (pass < 3600 * 24 * 14 * 1000) {
  100. //二周内
  101. strPassTime =
  102. Math.floor(pass / 1000 / 3600 / 24) +
  103. intl.formatMessage({ id: "utilities.time.days_ago" });
  104. } else if (pass < 3600 * 24 * 30 * 1000) {
  105. //二个月内
  106. strPassTime =
  107. Math.floor(pass / 1000 / 3600 / 24 / 7) +
  108. intl.formatMessage({ id: "utilities.time.weeks_ago" });
  109. } else if (pass < 3600 * 24 * 365 * 1000) {
  110. //一年内
  111. strPassTime =
  112. Math.floor(pass / 1000 / 3600 / 24 / 30) +
  113. intl.formatMessage({ id: "utilities.time.months_ago" });
  114. } else if (pass < 3600 * 24 * 730 * 1000) {
  115. //超过1年小于2年
  116. strPassTime =
  117. Math.floor(pass / 1000 / 3600 / 24 / 365) +
  118. intl.formatMessage({ id: "utilities.time.year_ago" });
  119. } else {
  120. strPassTime =
  121. Math.floor(pass / 1000 / 3600 / 24 / 365) +
  122. intl.formatMessage({ id: "utilities.time.years_ago" });
  123. }
  124. return strPassTime;
  125. }
  126. function getFullDataTime(t: string) {
  127. const inputDate = new Date(t);
  128. return inputDate.toLocaleString();
  129. }
  130. return (
  131. <Tooltip title={tooltip} color={color} key={color}>
  132. <Text type={type}>
  133. <Space>
  134. {icon}
  135. {showLabel ? mTitle : ""}
  136. {passTime}
  137. </Space>
  138. </Text>
  139. </Tooltip>
  140. );
  141. };
  142. export default TimeShowWidget;