IndexItem.tsx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. import { View, Text, Image, PageContainer } from "@tarojs/components";
  2. import { bigRingRadius, getBgRing, getCommon, getDot, getReal, getSchedule, getTarget, ringWidth, smallRingRadius, thirdRingRadius, timeTotimestamp } from "../hooks/RingData";
  3. import { RealRing, CurrentDot } from "@/features/trackTimeDuration/components/Rings";
  4. import Rings from "./Rings";
  5. import './IndexItem.scss'
  6. import { useTranslation } from "react-i18next";
  7. import { ColorType } from "@/context/themes/color";
  8. import { jumpPage } from "@/features/trackTimeDuration/hooks/Common";
  9. import { TimeFormatter } from "@/utils/time_format";
  10. import '@/utils/ring_card.scss';
  11. import { useSelector } from "react-redux";
  12. import { useEffect, useState } from "react";
  13. import Taro from "@tarojs/taro";
  14. import Modal from "@/components/layout/Modal.weapp";
  15. import { ModalType } from "@/utils/types";
  16. import TimelineStage from "./TimelineStage";
  17. import CircadianDetailPopup from "@/features/trackTimeDuration/components/CircadianDetailPopup";
  18. import { rpxToPx } from "@/utils/tools";
  19. import CircadianStatus from "./CircadianStatus";
  20. let useNavigation;
  21. if (process.env.TARO_ENV == 'rn') {
  22. useNavigation = require("@react-navigation/native").useNavigation
  23. }
  24. export default function Component(props: { type: string, data: any, time: any, showStage?: boolean }) {
  25. const { t } = useTranslation()
  26. const [record, setRecord] = useState(props.data.current_record);
  27. const user = useSelector((state: any) => state.user);
  28. const dayNight = useSelector((state: any) => state.night);
  29. const day = useSelector((state: any) => state.day);
  30. const ring = useSelector((state: any) => state.ring);
  31. const [stageList, setStageList] = useState([true, false, false])
  32. const [isStageMode, setIsStageMode] = useState(false)
  33. const [showDetailModal, setShowDetailModal] = useState(false)
  34. const [count, setCount] = useState(0)
  35. let navigation;
  36. if (useNavigation) {
  37. navigation = useNavigation()
  38. }
  39. useEffect(() => {
  40. setRecord(props.data.current_record)
  41. }, [props.data])
  42. useEffect(() => {
  43. setCount((pre) => pre + 1)
  44. }, [dayNight.nightRingSunrise, dayNight.nightRingSunset])
  45. useEffect(() => {
  46. if (props.type == 'FAST_SLEEP') {
  47. global.updateMixItem = (data) => {
  48. setStageList(data)
  49. }
  50. global.changeMixIndex = (index) => {
  51. setIsStageMode(index == 1)
  52. }
  53. }
  54. }, [])
  55. function durationArc(start_time: number, end_time: number) {
  56. var duration = (end_time - start_time) / 1000;
  57. return duration / (24 * 3600) * 2 * Math.PI;
  58. }
  59. const startArc = (time: number) => {
  60. var date = new Date(time);
  61. var hour = date.getHours();
  62. var minute = date.getMinutes();
  63. var second = date.getSeconds();
  64. return (hour * 3600 + minute * 60 + second) / (24 * 3600) * 2 * Math.PI - Math.PI / 2.0;
  65. }
  66. function bigRing() {
  67. var common = getCommon(null, true)
  68. common.radius = bigRingRadius;
  69. common.lineWidth = ringWidth;
  70. var bgRing = getBgRing()
  71. var currentDot1 = getDot(record, true)
  72. var targetBigRing1 = getTarget(record, true)
  73. targetBigRing1.color = record.scenario == 'SLEEP' ? ColorType.sleep + '66' : ColorType.fast + '66'
  74. if (record.status == 'ONGOING') {
  75. var realRing1 = getReal(record, true, false)
  76. // debugger
  77. return <Rings common={common} bgRing={bgRing} currentDot={currentDot1} realRing={realRing1} targetRing={targetBigRing1} canvasId={props.type + 'index_big'} />
  78. }
  79. if (record.status == 'WAIT_FOR_START') {
  80. var scenario = JSON.parse(JSON.stringify(props.data.scenario))
  81. if (scenario.name == 'SLEEP') {
  82. var countduration = (props.data.current_record.sleep.target_end_time - props.data.current_record.sleep.target_start_time) / 60000
  83. var min1 = TimeFormatter.timestringToSeconds(scenario.schedule.sleep.start_time) / 60
  84. var min2 = TimeFormatter.timestringToSeconds(scenario.schedule.sleep.end_time) / 60
  85. var leftMinutes = min1 < min2 ? min2 - min1 : min2 + 24 * 60 - min1
  86. if (leftMinutes != countduration) {
  87. min2 = min1 + countduration
  88. if (min2 > 1440) {
  89. min2 -= 1440
  90. }
  91. var hour = Math.floor(min2 / 60)
  92. var minute = min2 % 60
  93. scenario.schedule.sleep.end_time = `${hour}:${minute}`
  94. }
  95. }
  96. else {
  97. var countduration = (props.data.current_record.fast.target_end_time - props.data.current_record.fast.target_start_time) / 60000
  98. var min1 = TimeFormatter.timestringToSeconds(scenario.schedule.fast.start_time) / 60
  99. var min2 = TimeFormatter.timestringToSeconds(scenario.schedule.fast.end_time) / 60
  100. var leftMinutes = min1 < min2 ? min2 - min1 : min2 + 24 * 60 - min1
  101. if (leftMinutes != countduration) {
  102. min2 = min1 + countduration
  103. if (min2 > 1440) {
  104. min2 -= 1440
  105. }
  106. var hour = Math.floor(min2 / 60)
  107. var minute = min2 % 60
  108. scenario.schedule.fast.end_time = `${hour}:${minute}`
  109. }
  110. }
  111. var realRing1 = getSchedule(scenario, scenario.name != 'SLEEP', true, true)//getSchedule(record, props.type != 'SLEEP', true)
  112. var list: any = []
  113. if (scenario.name == 'FAST_SLEEP') {
  114. realRing1.color = ColorType.fast + '66'
  115. var detail = timeTotimestamp(scenario)
  116. if (stageList[0]) {
  117. const realRingBig: RealRing = {
  118. color: global.fastColor ? global.fastColor : ColorType.fast,
  119. startArc: startArc(detail.fast.target_start_time),
  120. durationArc: durationArc(detail.fast.target_start_time, detail.sleep.target_start_time)
  121. }
  122. list.push(realRingBig)
  123. }
  124. if (stageList[1]) {
  125. const realRingBig: RealRing = {
  126. color: global.fastColor ? global.fastColor : ColorType.fast,
  127. startArc: startArc(detail.sleep.target_start_time),
  128. durationArc: durationArc(detail.sleep.target_start_time, detail.sleep.target_end_time)
  129. }
  130. list.push(realRingBig)
  131. }
  132. if (stageList[2]) {
  133. const realRingBig: RealRing = {
  134. color: global.fastColor ? global.fastColor : ColorType.fast,
  135. startArc: startArc(detail.sleep.target_end_time),
  136. durationArc: durationArc(detail.sleep.target_end_time, detail.fast.target_end_time)
  137. }
  138. list.push(realRingBig)
  139. }
  140. }
  141. else if (scenario.name == 'SLEEP') {
  142. realRing1.color = ColorType.sleep + '66'
  143. }
  144. else {
  145. realRing1.color = ColorType.fast + '66'
  146. }
  147. if (user.isLogin) {
  148. list = []
  149. }
  150. if (!user.isLogin) {
  151. currentDot1 = null
  152. // realRing1 = null
  153. }
  154. return <Rings common={common} bgRing={bgRing} currentDot={isStageMode ? null : currentDot1} stageList={list} realRing={realRing1} canvasId={props.type + 'index_big'} />
  155. }
  156. var realRing1 = getReal(record, true, false)
  157. return <Rings common={common} bgRing={bgRing} realRing={realRing1} currentDot={currentDot1} targetRing={targetBigRing1} canvasId={props.type + 'index_big'} />
  158. }
  159. function smallRing() {
  160. var common = getCommon(null, false)
  161. common.radius = smallRingRadius;
  162. common.lineWidth = ringWidth;
  163. var bgRing = getBgRing()
  164. var realRing = getReal(record, false, false)
  165. if (props.type == 'SLEEP' || props.data.scenario.name == 'FAST_SLEEP') {
  166. if (!record.sleep) {
  167. return null
  168. }
  169. if (record.sleep.status == 'WAIT_FOR_END') {
  170. var targetBigRing1 = getTarget(record, false)
  171. targetBigRing1.color = ColorType.sleep + '66'
  172. var currentDot = getDot(record, false)
  173. realRing.durationArc = durationArc(record.sleep.target_start_time, (new Date()).getTime())
  174. return <Rings common={common} bgRing={bgRing} currentDot={currentDot} canvasId={props.type + 'index_small'} targetRing={targetBigRing1} realRing={realRing} />
  175. }
  176. else if (record.status == 'WAIT_FOR_START' || record.status == 'ONGOING1') {
  177. var scenario = JSON.parse(JSON.stringify(props.data.scenario))
  178. // if (record.status == 'ONGOING1') {
  179. // var countduration = (props.data.current_record.sleep.target_end_time - props.data.current_record.sleep.target_start_time) / 60000
  180. // var min1 = TimeFormatter.timestringToSeconds(scenario.schedule.sleep.start_time) / 60
  181. // var min2 = TimeFormatter.timestringToSeconds(scenario.schedule.sleep.end_time) / 60
  182. // var leftMinutes = min1 < min2 ? min2 - min1 : min2 + 24 * 60 - min1
  183. // if (leftMinutes != countduration) {
  184. // min2 = min1 + countduration
  185. // if (min2 > 1440) {
  186. // min2 -= 1440
  187. // }
  188. // var hour = Math.floor(min2 / 60)
  189. // var minute = min2 % 60
  190. // scenario.schedule.sleep.end_time = `${hour}:${minute}`
  191. // }
  192. // }
  193. if (record.status == 'WAIT_FOR_START') {
  194. realRing = getSchedule(scenario, false, true, record.status == 'WAIT_FOR_START')//getSchedule(record, false, true)
  195. }
  196. else {
  197. realRing.startArc = startArc(record.sleep.target_start_time)
  198. realRing.durationArc = durationArc(record.sleep.target_start_time, record.sleep.target_end_time)
  199. }
  200. realRing.color = ColorType.sleep + '66'
  201. var currentDot = getDot(record, false)
  202. if (!user.isLogin) {
  203. currentDot = null
  204. }
  205. return <Rings common={common} bgRing={bgRing} currentDot={isStageMode ? null : currentDot} realRing={realRing} canvasId={props.type + 'index_small'} />
  206. }
  207. else if (record.sleep.status == 'NOT_COMPLETED') {
  208. realRing.durationArc = 0.01
  209. var currentDot = getDot(record, false)
  210. return <Rings common={common} bgRing={bgRing} currentDot={currentDot} canvasId={props.type + 'index_small'} realRing={realRing} />
  211. }
  212. else if (record.sleep.status == 'COMPLETED') {
  213. realRing = getReal(record, false, true)
  214. var currentDot = getDot(record, false)
  215. if (record.status == 'ONGOING3') {
  216. currentDot = null
  217. // currentDot.color = '#ffffffff'
  218. }
  219. return <Rings common={common} bgRing={bgRing} currentDot={currentDot} canvasId={props.type + 'index_small'} realRing={realRing} />
  220. }
  221. else if (record.sleep.status == 'ONGOING2') {
  222. var currentDot = getDot(record, false)
  223. return <Rings common={common} bgRing={bgRing} currentDot={currentDot} canvasId={props.type + 'index_small'} />
  224. }
  225. return <Rings common={common} bgRing={bgRing} canvasId={props.type + 'index_small'} />
  226. }
  227. else {
  228. var currentDot = getDot(record, false)
  229. var targetRing = getTarget(record, false)
  230. if (record.status == 'ONGOING2') {
  231. var realRing = getReal(record, false, false)
  232. return <Rings common={common} bgRing={bgRing} realRing={realRing} currentDot={currentDot} targetRing={targetRing} canvasId={props.type + 'index_small'} />
  233. }
  234. if (record.status == 'ONGOING3') {
  235. currentDot.color = 'rgba(0, 255, 255, 0.5)'
  236. }
  237. return <Rings common={common} bgRing={bgRing} currentDot={currentDot} canvasId={props.type + 'index_small'} />
  238. }
  239. }
  240. function dayRing() {
  241. var common = getCommon(null, true)
  242. common.radius = thirdRingRadius;
  243. common.lineWidth = ringWidth;
  244. var bgRing = getBgRing()
  245. let realRingBig: RealRing = {
  246. color: ColorType.night + '66',
  247. startArc: 0,
  248. durationArc: 2
  249. }
  250. var sunRise = 24 * 60 + 6 * 60
  251. var sunSet = 18 * 60
  252. var sunRiseObj = dayNight.nightRingSunrise
  253. var sunSetObj = dayNight.nightRingSunset
  254. sunRise = 24 * 60 + parseInt(sunRiseObj.split(':')[0]) * 60 + parseInt(sunRiseObj.split(':')[1])
  255. sunSet = parseInt(sunSetObj.split(':')[0]) * 60 + parseInt(sunSetObj.split(':')[1])
  256. if (sunSetObj.indexOf('PM') != -1) {
  257. sunSet += 12 * 60
  258. }
  259. // }
  260. var duration = sunRise - sunSet
  261. realRingBig.startArc = (sunSet * 60) / (24 * 3600) * 2 * Math.PI - Math.PI / 2.0;
  262. realRingBig.durationArc = (duration * 60) / (24 * 3600) * 2 * Math.PI;
  263. var currentDot = getDot(record, false)
  264. currentDot.color = ColorType.night
  265. var now = new Date()
  266. var t = now.getHours() * 60 + now.getMinutes()
  267. var duration2 = t - sunSet
  268. if (duration2 < 0) {
  269. duration2 += 24 * 60
  270. }
  271. let realRing: RealRing = {
  272. color: ColorType.night,
  273. startArc: (sunSet * 60) / (24 * 3600) * 2 * Math.PI - Math.PI / 2.0,
  274. durationArc: (duration2 * 60) / (24 * 3600) * 2 * Math.PI
  275. }
  276. if (dayNight.nightRingDate) {
  277. if (new Date(dayNight.nightRingDate).getDate() == new Date().getDate() && new Date().getHours() < 12) {
  278. realRing = null;
  279. }
  280. }
  281. if (!user.isLogin) {
  282. currentDot = null;
  283. realRing = null;
  284. }
  285. return <Rings common={common} bgRing={bgRing} targetRing={realRingBig} realRing={duration2 > duration ? null : realRing} currentDot={currentDot} canvasId={props.type + 'index_day'} />
  286. }
  287. function rings() {
  288. return <View style={{
  289. position: 'relative', zIndex: 1,
  290. marginLeft: !props.showStage && global.showNightRing === true && false ? -6 : 0,
  291. }}>
  292. {
  293. bigRing()
  294. }
  295. {
  296. props.data.scenario.name == 'FAST_SLEEP' && <View style={{ display: 'flex', position: 'absolute', left: 0, right: 0, top: 0, bottom: 0, alignItems: 'center', justifyContent: 'center' }}>
  297. {
  298. smallRing()
  299. }
  300. </View>
  301. }
  302. {
  303. !props.showStage && day.showDayRing === true && false && <View style={{ display: 'flex', position: 'absolute', left: -14, top: -14, right: -14, bottom: -14 }}>
  304. {
  305. dayRing()
  306. }
  307. </View>
  308. }
  309. </View>
  310. }
  311. function getDuration(obj) {
  312. if (!obj) {
  313. }
  314. if (obj.status == 'NOT_STARTED' || obj.status == 'NOT_COMPLETED') {
  315. return ''
  316. }
  317. var start = obj.real_start_time
  318. var end = obj.real_end_time
  319. if (!end) {
  320. end = (new Date()).getTime()
  321. }
  322. if (obj.status == 'WAIT_FOR_START') {
  323. start = obj.target_start_time
  324. end = obj.target_end_time
  325. }
  326. return TimeFormatter.durationFormate(start, end)
  327. // return TimeFormatter.calculateTimeDifference(start, end)
  328. }
  329. function fastDuration() {
  330. if (!record.fast) {
  331. return ''
  332. }
  333. if (!user.isLogin && record.sleep) {
  334. var count = 0
  335. if (stageList[0]) {
  336. count = record.sleep.target_start_time - record.fast.target_start_time
  337. }
  338. if (stageList[1]) {
  339. count += record.sleep.target_end_time - record.sleep.target_start_time
  340. }
  341. if (stageList[2]) {
  342. count += record.fast.target_end_time - record.sleep.target_end_time
  343. }
  344. if (count == 0) {
  345. return t('feature.track_time_duration.stage.no_stage_selected')
  346. }
  347. global.fastDuration = TimeFormatter.durationFormate(new Date().getTime(), new Date().getTime() + count)//`${count}小时`
  348. return TimeFormatter.durationFormate(new Date().getTime(), new Date().getTime() + count)//`${count}小时`
  349. }
  350. if (record.fast.status == 'WAIT_FOR_END') {
  351. global.fastDuration = TimeFormatter.countdown(record.fast.real_start_time)
  352. return TimeFormatter.countdown(record.fast.real_start_time)
  353. // return TimeFormatter.formateTimeDifference(record.fast.real_start_time, new Date().getTime(), false)
  354. }
  355. if (isStageMode && record.scenario == 'FAST_SLEEP') {
  356. if (stageList[0]) {
  357. return TimeFormatter.durationFormate(record.fast.target_start_time, record.sleep.target_start_time)
  358. }
  359. if (stageList[1]) {
  360. return TimeFormatter.durationFormate(record.sleep.target_start_time, record.sleep.target_end_time)
  361. }
  362. if (stageList[2]) {
  363. return TimeFormatter.durationFormate(record.sleep.target_end_time, record.fast.target_end_time)
  364. }
  365. }
  366. global.fastDuration = getDuration(record.fast)
  367. return getDuration(record.fast)
  368. }
  369. function sleepDuration() {
  370. if (!record.sleep) {
  371. return ''
  372. }
  373. if (record.sleep.status == 'WAIT_FOR_END') {
  374. global.sleepDuration = TimeFormatter.formateTimeDifference(record.sleep.real_start_time, new Date().getTime(), false)
  375. return TimeFormatter.formateTimeDifference(record.sleep.real_start_time, new Date().getTime(), false)
  376. }
  377. global.sleepDuration = getDuration(record.sleep)
  378. return getDuration(record.sleep)
  379. }
  380. function nightDuration() {
  381. var sunRiseObj = dayNight.nightRingSunrise
  382. var sunSetObj = dayNight.nightRingSunset
  383. var sunRise = 24 * 60 + parseInt(sunRiseObj.split(':')[0]) * 60 + parseInt(sunRiseObj.split(':')[1])
  384. var sunSet = parseInt(sunSetObj.split(':')[0]) * 60 + parseInt(sunSetObj.split(':')[1])
  385. if (sunSetObj.indexOf('PM') != -1) {
  386. sunSet += 12 * 60
  387. }
  388. var duration = (sunRise - sunSet) * 60 * 1000
  389. return TimeFormatter.calculateTimeDifference(new Date().getTime(), new Date().getTime() + duration);
  390. }
  391. function goDetail(e) {
  392. if (process.env.TARO_ENV == 'weapp') {
  393. e.stopPropagation()
  394. }
  395. if (!user.isLogin) {
  396. jumpPage('/pages/account/ChooseAuth', 'ChooseAuth', navigation)
  397. return
  398. }
  399. setShowDetailModal(true)
  400. }
  401. function modalContent() {
  402. return <Modal
  403. testInfo={null}
  404. dismiss={() => {
  405. global.pauseIndexTimer = false
  406. setShowDetailModal(false)
  407. }}
  408. confirm={() => { }}>
  409. {
  410. <CircadianDetailPopup
  411. record={record}
  412. schedule={props.data.scenario.schedule}
  413. onClose={() => { setShowDetailModal(false); }} />
  414. }
  415. </Modal>
  416. }
  417. return <View className="ring_full_container">
  418. <View className="time_operate_item1" onClick={goDetail}>
  419. <View className={!props.showStage && day.showDayRing === true && false ?
  420. 'fast_sleep_item three_ring_card_detail' : 'fast_sleep_item ring_card_detail'}>
  421. {
  422. rings()
  423. }
  424. <View className='top_tag_bg'>
  425. <CircadianStatus data={props.data} />
  426. </View>
  427. <View className="duration_bg2" style={{ marginLeft: (!props.showStage && day.showDayRing === true && false) ? rpxToPx(68) : rpxToPx(52), height: bigRingRadius * 2, overflow: 'visible' }}>
  428. {
  429. !props.showStage && day.showDayRing === true && false &&
  430. <Text className="duration_title2">{t('feature.common.overnight')}</Text>
  431. }
  432. {
  433. !props.showStage && day.showDayRing === true && false &&
  434. <Text className="duration_value2" style={{ color: ColorType.night }}>{nightDuration()}</Text>
  435. }
  436. {
  437. (props.data.scenario.name == 'FAST' || props.data.scenario.name == 'FAST_SLEEP') && <Text className="duration_title2">{t('feature.track_time_duration.record_fast_sleep.item.fast')}</Text>
  438. }
  439. {
  440. (props.data.scenario.name == 'FAST' || props.data.scenario.name == 'FAST_SLEEP') && <Text className="duration_value2" style={{ color: global.fastColor ? global.fastColor : ColorType.fast }}>{fastDuration()}</Text>
  441. }
  442. {
  443. (props.data.scenario.name == 'SLEEP' || props.data.scenario.name == 'FAST_SLEEP') && <Text className="duration_title2" >{t('feature.track_time_duration.record_fast_sleep.item.sleep')}</Text>
  444. }
  445. {
  446. (props.data.scenario.name == 'SLEEP' || props.data.scenario.name == 'FAST_SLEEP') && <Text className="duration_value2" style={{ color: global.sleepColor ? global.sleepColor : ColorType.sleep, marginBottom: 0 }}>{sleepDuration()}</Text>
  447. }
  448. </View>
  449. <Image className="arrow2" src={require('@/assets/images/arrow3.png')} />
  450. </View>
  451. {
  452. showDetailModal && modalContent()
  453. }
  454. </View>
  455. </View>
  456. }