HeaderCircadian.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. import { View, Image, Text } from '@tarojs/components'
  2. import './HeaderCircadian.scss'
  3. import { useDispatch, useSelector } from 'react-redux';
  4. import { useEffect, useState } from 'react';
  5. import { setShowActionTip } from '@/store/health';
  6. import { getScenario } from './hooks/health_hooks';
  7. import dayjs from 'dayjs';
  8. import { useTranslation } from 'react-i18next';
  9. import { rpxToPx } from '@/utils/tools';
  10. import { IconActive, IconDay, IconEat, IconFast, IconNight, IconSleep } from '@/components/basic/Icons';
  11. import { MainColorType } from '@/context/themes/color';
  12. let lauchShow = false
  13. let timer;
  14. export default function HeaderCircadian() {
  15. const health = useSelector((state: any) => state.health);
  16. const dispatch = useDispatch()
  17. const [showTip, setShowTip] = useState(false)
  18. const [icon, setIcon] = useState('DAY')
  19. const [title, setTitle] = useState('')
  20. const [desc, setDesc] = useState('')
  21. const [isOG, setIsOG] = useState(false)
  22. const { t } = useTranslation()
  23. const kWidth = global.language == 'en' ? rpxToPx(236) : rpxToPx(176)
  24. useEffect(() => {
  25. if (health && health.windows && !lauchShow) {
  26. lauchShow = true
  27. checkTip()
  28. }
  29. }, [health])
  30. useEffect(() => {
  31. checkTip()
  32. }, [health.mode])
  33. function checkTip() {
  34. //启动时toast
  35. if (health && health.windows) {
  36. setShowTip(true)
  37. setIcon(health.mode)
  38. if (timer) {
  39. clearTimeout(timer)
  40. timer = null
  41. }
  42. timer = setTimeout(() => {
  43. setShowTip(false)
  44. dispatch(setShowActionTip({
  45. isShow: false,
  46. isCompleted: false
  47. }))
  48. }, 3000)
  49. // lauchShow = true
  50. const scenario = getScenario(health.windows, health.mode)
  51. if (scenario.real || scenario.status == 'OG') {
  52. setIsOG(true)
  53. switch (health.mode) {
  54. case 'FAST':
  55. setTitle(t('health.fasting_log'))
  56. break
  57. case 'EAT':
  58. setTitle(t('health.meal_log'))
  59. break
  60. case 'SLEEP':
  61. setTitle(t('health.sleep_log'))
  62. break
  63. case 'ACTIVE':
  64. setTitle(t('health.active_log'))
  65. break
  66. }
  67. setDesc(t('health.in_progress'))
  68. return;
  69. }
  70. else {
  71. if (new Date().getTime() >= scenario.target.start_timestamp && new Date().getTime() < scenario.target.end_timestamp) {
  72. setDesc(t('health.live_now'))
  73. }
  74. else {
  75. setDesc(t('health.next_up'))
  76. }
  77. }
  78. switch (health.mode) {
  79. case 'FAST':
  80. {
  81. setTitle(t('health.fasting_schedule'))
  82. }
  83. break;
  84. case 'EAT':
  85. {
  86. setTitle(t('health.eating_schedule'))
  87. }
  88. break;
  89. case 'SLEEP':
  90. {
  91. setTitle(t('health.sleep_schedule'))
  92. }
  93. break;
  94. case 'ACTIVE':
  95. {
  96. setTitle(t('health.active_schedule'))
  97. }
  98. break;
  99. case 'DAY':
  100. {
  101. setTitle(t('health.daytime'))
  102. }
  103. break;
  104. case 'NIGHT':
  105. {
  106. setTitle(t('health.nighttime'))
  107. }
  108. break;
  109. }
  110. setIsOG(false)
  111. }
  112. }
  113. useEffect(() => {
  114. if (health.showActionCircadian) {
  115. checkTip()
  116. // const scenario = getScenario(health.windows, health.mode)
  117. // setIsOG(scenario.status == 'OG')
  118. // var isLiveNow = new Date().getTime() >= scenario.target.start_timestamp && new Date().getTime() < scenario.target.end_timestamp
  119. // setDesc(scenario.status == 'OG' ? t('health.in_progress') : isLiveNow ? t('health.live_now') : t('health.next_up'))
  120. // switch (health.mode) {
  121. // case 'FAST':
  122. // {
  123. // setIcon('FAST')
  124. // setTitle(scenario.status == 'OG' ? t('health.fasting_log') : t('health.fasting_schedule'))
  125. // }
  126. // break;
  127. // case 'EAT':
  128. // {
  129. // setIcon('EAT')
  130. // setTitle(scenario.status == 'OG' ? t('health.meal_log') : t('health.eating_schedule'))
  131. // // setDesc(health.isCompleted ? 'COMPLETED' : t('health.in_progress'))
  132. // }
  133. // break;
  134. // case 'SLEEP':
  135. // {
  136. // setIcon('SLEEP')
  137. // setTitle(scenario.status == 'OG' ? t('health.sleep_log') : t('health.sleep_schedule'))
  138. // // setDesc(health.isCompleted ? 'COMPLETED' : t('health.in_progress'))
  139. // }
  140. // break;
  141. // case 'ACTIVE':
  142. // {
  143. // setIcon('ACTIVE')
  144. // setTitle(scenario.status == 'OG' ? t('health.active_log') : t('health.active_schedule'))
  145. // // setDesc(health.isCompleted ? 'COMPLETED' : t('health.in_progress'))
  146. // }
  147. // break;
  148. // }
  149. // setShowTip(true)
  150. // if (timer) {
  151. // clearTimeout(timer)
  152. // timer = null
  153. // }
  154. // timer = setTimeout(() => {
  155. // setShowTip(false)
  156. // dispatch(setShowActionTip({
  157. // isShow: false,
  158. // isCompleted: false
  159. // }))
  160. // }, 3000)
  161. }
  162. }, [health.showActionCircadian])
  163. useEffect(() => {
  164. setInterval(() => {
  165. if (health.windows) {
  166. const now = dayjs().format('YYYY-MM-DD HH:mm:ss')
  167. const day = getScenario(health.windows, 'DAY')
  168. const night = getScenario(health.windows, 'NIGHT')
  169. const fast = getScenario(health.windows, 'FAST')
  170. const eat = getScenario(health.windows, 'EAT')
  171. const sleep = getScenario(health.windows, 'SLEEP')
  172. const active = getScenario(health.windows, 'ACTIVE')
  173. var isShow = false
  174. if (dayjs(day.target.start_timestamp).format('YYYY-MM-DD HH:mm:ss') == now) {
  175. isShow = true;
  176. setIcon('DAY')
  177. setTitle(t('health.daytime'))
  178. }
  179. else if (dayjs(night.target.start_timestamp).format('YYYY-MM-DD HH:mm:ss') == now) {
  180. isShow = true;
  181. setIcon('NIGHT')
  182. setTitle(t('health.nighttime'))
  183. }
  184. else if (dayjs(fast.target.start_timestamp).format('YYYY-MM-DD HH:mm:ss') == now) {
  185. isShow = true;
  186. setIcon('FAST')
  187. setTitle(t('health.fasting_schedule'))
  188. }
  189. else if (dayjs(eat.target.start_timestamp).format('YYYY-MM-DD HH:mm:ss') == now) {
  190. isShow = true;
  191. setIcon('EAT')
  192. setTitle(t('health.eating_schedule'))
  193. }
  194. else if (dayjs(sleep.target.start_timestamp).format('YYYY-MM-DD HH:mm:ss') == now) {
  195. isShow = true;
  196. setIcon('SLEEP')
  197. setTitle(t('health.sleep_schedule'))
  198. }
  199. else if (dayjs(active.target.start_timestamp).format('YYYY-MM-DD HH:mm:ss') == now) {
  200. isShow = true;
  201. setIcon('ACTIVE')
  202. setTitle(t('health.active_schedule'))
  203. }
  204. if (isShow) {
  205. setDesc(t('health.live_now'))
  206. setShowTip(true)
  207. if (timer) {
  208. clearTimeout(timer)
  209. timer = null
  210. }
  211. timer = setTimeout(() => {
  212. setShowTip(false)
  213. dispatch(setShowActionTip({
  214. isShow: false,
  215. isCompleted: false
  216. }))
  217. }, 3000)
  218. }
  219. }
  220. }, 1000)
  221. }, [])
  222. function leftIcon() {
  223. var bgColor = 'transparent'
  224. switch (icon) {
  225. case 'FAST':
  226. if (isOG) bgColor = MainColorType.fast
  227. break
  228. case 'EAT':
  229. if (isOG) bgColor = MainColorType.eat
  230. break
  231. case 'SLEEP':
  232. if (isOG) bgColor = MainColorType.sleep
  233. break
  234. case 'ACTIVE':
  235. if (isOG) bgColor = MainColorType.active
  236. break
  237. }
  238. return <View className='circadian_icon' style={{ backgroundColor: bgColor }}>
  239. {
  240. icon == 'DAY' && <IconDay width={rpxToPx(36)} color={MainColorType.day} />
  241. }
  242. {
  243. icon == 'NIGHT' && <IconNight width={rpxToPx(36)} color={MainColorType.night} />
  244. }
  245. {
  246. icon == 'FAST' && <IconFast width={rpxToPx(36)} color={isOG ? MainColorType.black : MainColorType.fast} />
  247. }
  248. {
  249. icon == 'SLEEP' && <IconSleep width={rpxToPx(36)} color={isOG ? MainColorType.black : MainColorType.sleep} />
  250. }
  251. {
  252. icon == 'EAT' && <IconEat width={rpxToPx(36)} color={isOG ? MainColorType.black : MainColorType.eat} />
  253. }
  254. {
  255. icon == 'ACTIVE' && <IconActive width={rpxToPx(36)} color={isOG ? MainColorType.black : MainColorType.active} />
  256. }
  257. </View>
  258. }
  259. function contentColor() {
  260. switch (icon) {
  261. case 'FAST':
  262. if (isOG) return MainColorType.fast
  263. break
  264. case 'EAT':
  265. if (isOG) return MainColorType.eat
  266. break
  267. case 'SLEEP':
  268. if (isOG) return MainColorType.sleep
  269. break
  270. case 'ACTIVE':
  271. if (isOG) return MainColorType.active
  272. break
  273. }
  274. return MainColorType.g02
  275. }
  276. return <View className='circadian_bg' style={{ opacity: showTip ? 1 : 0 }}>
  277. {
  278. leftIcon()
  279. }
  280. {/* <Image src={icon} className='circadian_icon' style={{ opacity: lowLight ? 0.7 : 1.0 }} /> */}
  281. <View className='circadian_content' style={{ width: kWidth }}>
  282. <View className='circadian_name'>{title}</View>
  283. <View className='circadian_status' style={{ color: contentColor() }}>{desc}</View>
  284. </View>
  285. </View>
  286. }