DayNightDetailPopup.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. import { View, Text } from '@tarojs/components'
  2. import './CircadianDetailPopup.scss'
  3. import { useTranslation } from 'react-i18next'
  4. import { useState } from 'react'
  5. import { rpxToPx } from '@/utils/tools'
  6. import { TimeFormatter } from '@/utils/time_format'
  7. import { useSelector } from 'react-redux'
  8. import { ColorType } from '@/context/themes/color'
  9. import Timeline from '@/components/view/Timeline'
  10. export default function DayNightDetailPopup(props: {
  11. isNight: boolean,
  12. authInfo: any,
  13. sunsetDate: any,
  14. sunriseDate: any,
  15. sunriseTmrDate: any,
  16. sunsetTime: any,
  17. sunriseTime: any,
  18. sunriseTmrTime: any,
  19. nightDate:any,
  20. dayDate:any,
  21. onClose: Function
  22. }) {
  23. const dayNight = useSelector((state: any) => state.night);
  24. const day = useSelector((state: any) => state.day);
  25. const [tabIndex, setTabIndex] = useState(0)
  26. const { t } = useTranslation()
  27. function getTitle() {
  28. return props.isNight ? t('feature.day_night.night_popover') : t('feature.day_night.day_popover')
  29. }
  30. function nightDuration() {
  31. var sunRiseObj = dayNight.nightRingSunrise
  32. var sunSetObj = dayNight.nightRingSunset
  33. var sunRise = 24 * 60 + parseInt(sunRiseObj.split(':')[0]) * 60 + parseInt(sunRiseObj.split(':')[1])
  34. var sunSet = parseInt(sunSetObj.split(':')[0]) * 60 + parseInt(sunSetObj.split(':')[1])
  35. if (sunSetObj.indexOf('PM') != -1) {
  36. sunSet += 12 * 60
  37. }
  38. var duration = (sunRise - sunSet) * 60 * 1000
  39. return TimeFormatter.calculateTimeDifference(new Date().getTime(), new Date().getTime() + duration);
  40. }
  41. function dayDuration() {
  42. var sunRiseObj = day.dayRingSunrise
  43. var sunSetObj = day.dayRingSunset
  44. var sunRise = parseInt(sunRiseObj.split(':')[0]) * 60 + parseInt(sunRiseObj.split(':')[1])
  45. var sunSet = parseInt(sunSetObj.split(':')[0]) * 60 + parseInt(sunSetObj.split(':')[1])
  46. if (sunSetObj.indexOf('PM') != -1) {
  47. sunSet += 12 * 60
  48. }
  49. var duration = (sunSet - sunRise) * 60 * 1000
  50. return TimeFormatter.calculateTimeDifference(new Date().getTime(), new Date().getTime() + duration);
  51. }
  52. function showExtraData() {
  53. var now = new Date()
  54. if (props.isNight) {
  55. if (props.sunsetDate.getTime() > now.getTime()) {
  56. return false
  57. }
  58. return true
  59. }
  60. if (props.sunriseDate.getTime() < now.getTime() && now.getTime() < props.sunsetDate.getTime()) {
  61. return true;
  62. }
  63. return false
  64. }
  65. function timeCount() {
  66. var now = new Date()
  67. if (props.isNight) {
  68. if (now.getTime() < props.sunriseTmrDate.getTime()) {
  69. return TimeFormatter.countdown(props.sunsetDate.getTime())
  70. }
  71. return TimeFormatter.countdown(props.sunsetDate.getTime())
  72. } else {
  73. if (now.getTime() < props.sunsetDate.getTime()) {
  74. return TimeFormatter.countdown(props.sunriseDate.getTime())
  75. }
  76. return TimeFormatter.countdown(props.sunriseTmrDate.getTime())
  77. }
  78. }
  79. function timeCount2() {
  80. var now = new Date()
  81. if (props.isNight) {
  82. if (now.getTime() < props.sunsetDate.getTime()) {
  83. return TimeFormatter.countdown(props.sunriseTmrDate.getTime())
  84. }
  85. return TimeFormatter.countdown(props.sunriseTmrDate.getTime())
  86. } else {
  87. return TimeFormatter.countdown(props.sunsetDate.getTime())
  88. }
  89. }
  90. function timeDesc() {
  91. var now = new Date()
  92. if (props.isNight) {
  93. var list = props.sunsetTime.split(':')
  94. var hour = parseInt(list[0])
  95. var min = parseInt(list[1])
  96. var second = list.length == 3 ? parseInt(list[2]) : 0
  97. now.setHours(hour)
  98. now.setMinutes(min)
  99. now.setSeconds(second)
  100. var sunriseDate = new Date()
  101. var list2 = props.sunriseTmrTime.split(':')
  102. var hour2 = parseInt(list2[0])
  103. var min2 = parseInt(list2[1])
  104. var second2 = list2.length == 3 ? parseInt(list2[2]) : 0
  105. sunriseDate.setHours(hour2)
  106. sunriseDate.setMinutes(min2)
  107. sunriseDate.setSeconds(second2)
  108. if (sunriseDate.getTime() > new Date().getTime()) {
  109. return t('feature.day_night.time_past_sunset')//'Time past Sunset'
  110. }
  111. if (now.getTime() < new Date().getTime()) {
  112. return t('feature.day_night.time_past_sunset')//'Time past Sunset'
  113. }
  114. return t('feature.day_night.time_to_sunset')//'Time to Sunset'
  115. }
  116. else {
  117. var list = props.sunriseTime.split(':')
  118. var hour = parseInt(list[0])
  119. var min = parseInt(list[1])
  120. var second = list.length == 3 ? parseInt(list[2]) : 0
  121. now.setHours(hour)
  122. now.setMinutes(min)
  123. now.setSeconds(second)
  124. var sunsetDate = new Date()
  125. var list2 = props.sunsetTime.split(':')
  126. var hour2 = parseInt(list2[0])
  127. var min2 = parseInt(list2[1])
  128. var second2 = list2.length == 3 ? parseInt(list2[2]) : 0
  129. sunsetDate.setHours(hour2)
  130. sunsetDate.setMinutes(min2)
  131. sunsetDate.setSeconds(second2)
  132. if (new Date().getTime() > sunsetDate.getTime()) {
  133. return t('feature.day_night.time_to_sunrise')//'Time to Sunrise'
  134. }
  135. if (now.getTime() < new Date().getTime()) {
  136. return t('feature.day_night.time_past_sunrise')//'Time past Sunrise'
  137. }
  138. return t('feature.day_night.time_to_sunrise')//'Time to Sunrise'
  139. }
  140. }
  141. function timeDesc2() {
  142. if (props.isNight) {
  143. return t('feature.day_night.time_to_sunrise')//'Time to Sunrise'
  144. }
  145. return t('feature.day_night.time_to_sunset')//'Time to Sunset'
  146. }
  147. function overview() {
  148. return <View className='pop_ring_bg pop_overview_bg'>
  149. <Text className='pop_duration_title'>{props.isNight ? t('feature.day_night.night_duration') : t('feature.day_night.day_duration')}</Text>
  150. <View style={{ flexDirection: 'row', alignItems: 'center', marginTop: rpxToPx(8), display: 'flex', width: '100%' }}>
  151. <Text className='pop_duration_txt'>{props.isNight ? nightDuration() : dayDuration()}</Text>
  152. </View>
  153. <View style={{ marginTop: rpxToPx(20), display: 'flex', flexDirection: 'row', alignItems: 'center' }}>
  154. <View className='countdown_time_bg'>
  155. <Text className='title'>{timeDesc()}</Text>
  156. <Text className='value' style={{ color: props.isNight ? ColorType.night : ColorType.day }}>{timeCount()}</Text>
  157. </View>
  158. {
  159. showExtraData() && <View className='countdown_time_bg'>
  160. <Text className='title'>{timeDesc2()}</Text>
  161. <Text className='value' style={{ opacity: 0.4, color: props.isNight ? ColorType.night : ColorType.day }}>{timeCount2()}</Text>
  162. </View>
  163. }
  164. </View>
  165. </View>
  166. }
  167. function nightDurationDesc() {
  168. if (!props.authInfo || !(props.authInfo as any).lat) {
  169. if (new Date().getHours() >= 6) {
  170. return [t('feature.day_night.tonight'),t('feature.day_night.tomorrow_morning')]
  171. }
  172. return [t('feature.day_night.last_night'),t('feature.day_night.this_morning')]
  173. // return `Yesterday ${sunsetTime} - Today ${sunriseTmrTime}`
  174. }
  175. if (props.nightDate.getDate() == new Date().getDate()) {
  176. return [t('feature.day_night.tonight'),t('feature.day_night.tomorrow_morning')]
  177. }
  178. return [t('feature.day_night.last_night'),t('feature.day_night.this_morning')]
  179. //`Yesterday ${sunsetTime} - Today ${sunriseTmrTime}`
  180. }
  181. function dayDurationDesc() {
  182. if (!props.authInfo || !(props.authInfo as any).lat) {
  183. if (new Date().getHours() >= 18) {
  184. return `${t('feature.day_night.tomorrow')}`
  185. // return `Tomorrow ${sunriseTime} - ${sunsetTime}`
  186. }
  187. return `${t('feature.day_night.today')}`
  188. // return `Today ${sunriseTime} - ${sunsetTime}`
  189. }
  190. if (props.dayDate.getDate() == new Date().getDate()) {
  191. return `${t('feature.day_night.today')}`
  192. // return `Today ${sunriseTime} - ${sunsetTime}`
  193. }
  194. return `${t('feature.day_night.tomorrow')}`
  195. // return `Tomorrow ${sunriseTime} - ${sunsetTime}`
  196. }
  197. function events() {
  198. let timelineItems: any = []
  199. if (props.isNight) {
  200. var list = nightDurationDesc()
  201. timelineItems.push(
  202. {
  203. status: showExtraData() ? 'done' : 'padding',
  204. title: '日落',//list[0],
  205. content: list[0]+' '+props.sunsetTime,
  206. date: '',
  207. color: ColorType.night
  208. }
  209. )
  210. timelineItems.push(
  211. {
  212. status: 'padding',
  213. title: '日出',//list[1],
  214. content: list[1]+' '+props.sunriseTmrTime,
  215. date: '',
  216. color: ColorType.night
  217. }
  218. )
  219. }
  220. else {
  221. timelineItems.push(
  222. {
  223. status: showExtraData() ? 'done' : 'padding',
  224. title: '日出',//dayDurationDesc(),
  225. content: dayDurationDesc()+' '+props.sunriseTime,
  226. date: '',
  227. color: ColorType.day
  228. }
  229. )
  230. timelineItems.push(
  231. {
  232. status: 'padding',
  233. title: '日落',//dayDurationDesc(),
  234. content: dayDurationDesc()+' '+props.sunsetTime,
  235. date: '',
  236. color: ColorType.day
  237. }
  238. )
  239. }
  240. return <View style={{ display: 'flex', flexDirection: 'row' }}>
  241. <Timeline items={timelineItems} title='' width={468} />
  242. </View>
  243. }
  244. return <View className='detail_container'>
  245. <Text className='detail_popup_title'>{getTitle()}</Text>
  246. <View className='detail_tabbar'>
  247. <View onClick={() => { setTabIndex(0) }} className={tabIndex == 0 ? 'detail_tabitem_sel' : 'detail_tabitem_nor'}>{t('feature.day_night.overview')}</View>
  248. <View onClick={() => { setTabIndex(1) }} className={tabIndex == 1 ? 'detail_tabitem_sel' : 'detail_tabitem_nor'}>{t('feature.day_night.events')}</View>
  249. </View>
  250. <View className='detail_content'>
  251. {
  252. tabIndex == 0 ? overview() : events()
  253. }
  254. </View>
  255. <View className='detail_bottom'>
  256. <View className='detail_bottom_btn' onClick={(e) => {
  257. if (process.env.TARO_ENV == 'weapp') {
  258. e.stopPropagation()
  259. }
  260. props.onClose();
  261. }}>{t('feature.track_time_duration.common.okay')}</View>
  262. </View>
  263. </View>
  264. }