TitleView.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { View, Text, Image } from '@tarojs/components'
  2. import './TitleView.scss'
  3. import { useDispatch, useSelector } from 'react-redux';
  4. import Taro from '@tarojs/taro';
  5. import { setStep } from '@/store/scenario';
  6. import { useTranslation } from 'react-i18next';
  7. import { jumpPage } from '../hooks/Common';
  8. import { useEffect, useState } from 'react';
  9. let useNavigation;
  10. if (process.env.TARO_ENV == 'rn') {
  11. useNavigation = require("@react-navigation/native").useNavigation
  12. }
  13. export default function Component(props: {
  14. children?: any,
  15. subTitle?: string,
  16. title: string,
  17. titleColor?: string,
  18. secondPage?: boolean,
  19. showAddBtn?: boolean,
  20. onClick?: Function,
  21. titleSize?: any
  22. }) {
  23. const time = useSelector((state: any) => state.time);
  24. const user = useSelector((state: any) => state.user);
  25. const [count, setCount] = useState(0)
  26. const { t } = useTranslation()
  27. const dispatch = useDispatch();
  28. const isFastFirst = true
  29. let navigation;
  30. if (useNavigation) {
  31. navigation = useNavigation()
  32. }
  33. useEffect(() => {
  34. setCount(count + 1)
  35. }, [global.isDebug])
  36. function more() {
  37. if (props.onClick) {
  38. props.onClick()
  39. return
  40. }
  41. Taro.showActionSheet({
  42. itemList: [
  43. t('feature.track_time_duration.action_sheet.switch_scenario'),
  44. t('feature.track_time_duration.action_sheet.change_schedule')
  45. ]
  46. }).then(res => {
  47. switch (res.tapIndex) {
  48. case 1:
  49. if (time.status != 'WAIT_FOR_START') {
  50. Taro.showToast({
  51. title: t('feature.common.toast.ongoing'),
  52. icon: 'none'
  53. })
  54. return;
  55. }
  56. if (time.scenario == 'FAST_SLEEP') {
  57. dispatch(setStep(isFastFirst ? 'fast' : 'sleep'))
  58. }
  59. else if (time.scenario == 'SLEEP') {
  60. dispatch(setStep('sleep'))
  61. }
  62. else {
  63. dispatch(setStep('fast'))
  64. }
  65. jumpPage('/pages/clock/SetSchedule', 'SetSchedule', navigation)
  66. break;
  67. case 0:
  68. if (time.status != 'WAIT_FOR_START') {
  69. Taro.showToast({
  70. title: t('feature.common.toast.ongoing'),
  71. icon: 'none'
  72. })
  73. return;
  74. }
  75. jumpPage('/pages/clock/ChooseScenario', 'ChooseScenario', navigation)
  76. break;
  77. }
  78. })
  79. .catch(err => {
  80. console.log(err.errMsg)
  81. })
  82. }
  83. var showAddIcon = user.isLogin && props.showAddBtn
  84. return <View className={(props.children || props.subTitle) ? 'title_view' : 'title_view no_sub_element'}
  85. style={{ backgroundColor: global.isDebug ? 'red' : 'transparent' }}>
  86. <View className='title_bg'>
  87. <Text className='title' style={{
  88. color: props.titleColor ? props.titleColor : '#fff',
  89. fontSize: props.titleSize?props.titleSize:(props.secondPage ? 28 : 36)
  90. }}>{props.title}</Text>
  91. {
  92. showAddIcon ? <View className='iconAddBg' onClick={more}>
  93. <Image src={require('@/assets/images/add.png')} className='iconAdd' />
  94. </View> : <View className='iconAddBg' style={{ opacity: 0 }}>
  95. <Image src={require('@/assets/images/add.png')} className='iconAdd' />
  96. </View>
  97. }
  98. </View>
  99. {
  100. props.children ? props.children : <View />
  101. }
  102. {
  103. props.subTitle && <Text className='subTitle'>{props.subTitle}</Text>
  104. }
  105. </View>
  106. }