MainSleepActiveCard.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import { View, Text } from "@tarojs/components";
  2. import './MainCard.scss'
  3. import { useEffect, useState } from "react";
  4. import { rpxToPx } from "@/utils/tools";
  5. import Rings, { RingCommon, BgRing, TargetRing, CurrentDot } from "@/features/trackTimeDuration/components/Rings";
  6. import { MainColorType } from "@/context/themes/color";
  7. import { useDispatch, useSelector } from "react-redux";
  8. import { WindowStatusType, WindowType } from "@/utils/types";
  9. import { durationArc, isCurrentTimeInRange, startArc } from "./util";
  10. import { setMode } from "@/store/health";
  11. import { getScenario, getWindowStatus } from "./hooks/health_hooks";
  12. import { useTranslation } from "react-i18next";
  13. export default function MainSleepActiveCard(props: {
  14. count: any,
  15. typeChanged: Function,
  16. id: number,
  17. onClick: Function,
  18. scale: number
  19. }) {
  20. const [isSleepMode, setIsSleepMode] = useState(true)
  21. const user = useSelector((state: any) => state.user);
  22. const health = useSelector((state: any) => state.health);
  23. const dispatch = useDispatch()
  24. const {t} = useTranslation()
  25. useEffect(() => {
  26. if (health.mode == 'SLEEP') {
  27. setIsSleepMode(true)
  28. }
  29. else if (health.mode == 'ACTIVE') {
  30. setIsSleepMode(false)
  31. }
  32. }, [health.mode])
  33. useEffect(() => {
  34. const { sleep } = health.windows.sleep_active
  35. if (isCurrentTimeInRange(sleep.period.start_time, sleep.period.end_time)) {
  36. setIsSleepMode(true)
  37. }
  38. else {
  39. setIsSleepMode(false)
  40. }
  41. }, [])
  42. useEffect(() => {
  43. }, [user.isLogin])
  44. useEffect(() => {
  45. if (health.selTab == 2) {
  46. dispatch(setMode(isSleepMode ? 'SLEEP' : 'ACTIVE'))
  47. }
  48. }, [health.selTab, isSleepMode])
  49. const common: RingCommon = {
  50. useCase: 'ChooseScenario',
  51. radius: 23.5,
  52. lineWidth: 13,
  53. isFast: true,
  54. status: 'WAIT_FOR_START'
  55. }
  56. const bgRing: BgRing = {
  57. color: MainColorType.ringBg
  58. }
  59. function getArc() {
  60. const { sleep, active } = health.windows.sleep_active
  61. if (isSleepMode) {
  62. return startArc(sleep.target.start_timestamp)
  63. }
  64. return startArc(active.target.start_timestamp)
  65. }
  66. function getTargetArc() {
  67. const { sleep, active } = health.windows.sleep_active
  68. if (isSleepMode) {
  69. return durationArc(sleep.target.start_timestamp, sleep.target.end_timestamp)
  70. }
  71. return durationArc(active.target.start_timestamp, active.target.end_timestamp)
  72. }
  73. function targetRing() {
  74. const color = isSleepMode ? '#D0C0FB' : '#FF498366'
  75. const startArc = getArc()
  76. const durationArc = getTargetArc()
  77. return {
  78. color,
  79. startArc,
  80. durationArc
  81. }
  82. }
  83. function getRealArc(time) {
  84. var date = new Date(time);
  85. var hour = date.getHours();
  86. var minute = date.getMinutes();
  87. var second = date.getSeconds();
  88. return (hour * 3600 + minute * 60 + second) / (24 * 3600) * 2 * Math.PI - Math.PI / 2.0;
  89. }
  90. function getRealDurationArc(start, end) {
  91. var duration = (end - start) / 1000;
  92. return duration / (24 * 3600) * 2 * Math.PI;
  93. }
  94. function realRing() {
  95. const status = getWindowStatus(health.windows, isSleepMode ? 'SLEEP' : 'ACTIVE')
  96. const scenario = getScenario(health.windows, isSleepMode ? 'SLEEP' : 'ACTIVE')
  97. if (status == WindowStatusType.upcoming) {
  98. return null;
  99. // return {
  100. // color: '#cccccc',
  101. // startArc: getRealArc(new Date().getTime()),
  102. // durationArc: getRealDurationArc(new Date().getTime(), scenario.target.start_timestamp),
  103. // }
  104. }
  105. return {
  106. color: isSleepMode ? MainColorType.sleep : MainColorType.active,
  107. startArc: getRealArc(scenario.target.start_timestamp),
  108. durationArc: getRealDurationArc(scenario.target.start_timestamp, new Date().getTime())
  109. }
  110. }
  111. function currentDot() {
  112. if (health.mode == 'SLEEP' || health.mode == 'ACTIVE') {
  113. const status = getWindowStatus(health.windows, isSleepMode ? 'SLEEP' : 'ACTIVE')
  114. return {
  115. color: status == WindowStatusType.upcoming?'#B2B2B2':isSleepMode ? MainColorType.sleep : MainColorType.active,
  116. lineWidth: 4,
  117. borderColor: '#F5F5F5',
  118. offset: 0
  119. }
  120. }
  121. return null;
  122. }
  123. function showDotAnimation(){
  124. if (health.mode == 'SLEEP' || health.mode == 'ACTIVE') {
  125. const status = getWindowStatus(health.windows, isSleepMode ? 'SLEEP' : 'ACTIVE')
  126. if (status == WindowStatusType.process) return true;
  127. }
  128. return false
  129. }
  130. function ring() {
  131. return <Rings
  132. common={common}
  133. bgRing={bgRing}
  134. targetRing={targetRing()}
  135. realRing={realRing()}
  136. canvasId={'smal11lee' + props.id}
  137. currentDot={currentDot()}
  138. scale={props.scale ?? 1.0}
  139. showCurrentDotAnimation={showDotAnimation()}
  140. />
  141. }
  142. return <View style={{ width: rpxToPx(634 / 3), display: 'flex', flexShrink: 0, flexDirection: 'column', alignItems: 'center' }} onClick={() => props.onClick()}>
  143. <View style={{ position: 'relative', alignItems: 'center', display: 'flex', flexDirection: 'column' }}>
  144. <View className="demo_scale">
  145. {
  146. ring()
  147. }
  148. </View>
  149. <View className={health.selTab == 2 ? 'window_name window_name_sel' : 'window_name'}>{isSleepMode ? t('health.sleep') : t('health.active')}</View>
  150. </View>
  151. </View>
  152. }