import { View, Text, PickerView } from "@tarojs/components"; import { useEffect, useRef, useState } from "react"; import { TimeFormatter } from "@/utils/time_format"; import { useDispatch, useSelector } from "react-redux"; import Taro from "@tarojs/taro"; import LimitPickers from '@/components/input/LimitPickers'; import { endFast, endSleep, startFast, startSleep } from "../actions/TrackTimeActions"; import { durationDatas, durationIndex, getColor, getDurationTitle, getTimePickerTitle, getTitle, textAlpha, textNextStepAlpha } from "../hooks/Console"; import PickerViews from "@/components/input/PickerViews"; import Modal from "@/components/layout/Modal"; import Stepper from "@/components/input/Stepper"; import { EndFastBtn, EndSleepBtn, StartFastBtn, StartSleepBtn } from "@/features/common/SpecBtns"; import Box from "@/components/layout/Box"; import './Console.scss' import { ConsoleType, changeConsoleStatus } from "@/store/console"; import { updateScenario } from "@/store/time"; import { useTranslation } from "react-i18next"; import { ColorType } from "@/context/themes/color"; export default function Component(props: { isNextStep?: boolean }) { const scenario = useSelector((state: any) => state.scenario); const time = useSelector((state: any) => state.time); const user = useSelector((state: any) => state.user); const common = useSelector((state: any) => state.common); const [isFast, setIsFast] = useState(true); const [fastDuration, setFastDuration] = useState(0); const [sleepDuration, setSleepDuration] = useState(0); const [fastPickerValue, setFastPickerValue] = useState([0, 0]) const [sleepPickerValue, setSleepPickerValue] = useState([0, 0]) const limitPickerRef = useRef(null) const durationPickerRef = useRef(null) const { t } = useTranslation() const dispatch = useDispatch(); useEffect(() => { getStateDetail(); }, [time.fast, time.sleep, time.status, time.scenario]); function getStateDetail() { if (time.fast) { var fastCount = time.fast.target_end_time - time.fast.target_start_time setFastDuration(fastCount) setFastPickerValue(durationIndex(time.fast.target_start_time, time.fast.target_end_time, common)) } if (time.sleep) { var sleepCount = time.sleep.target_end_time - time.sleep.target_start_time setSleepDuration(sleepCount) setSleepPickerValue(durationIndex(time.sleep.target_start_time, time.sleep.target_end_time, common)) } if (time.scenario == 'FAST') { setIsFast(true) } else if (time.scenario == 'SLEEP') { setIsFast(false) } else { if (time.status == 'WAIT_FOR_START' || time.status == 'ONGOING3') { setIsFast(true) } else { setIsFast(false) } } } function showPicker() { if (global.testInfotimer) { return } global.set_time = new Date().getTime() updateNodeInfo() global.testInfotimer = setInterval(() => { updateNodeInfo() }, 1000) } function updateNodeInfo() { var node = ( { global.showClockModal(false, null); hidePicker() }} confirm={() => { hidePicker() var picker = limitPickerRef.current; pickerConfirm((picker as any).getConfirmData()); global.showClockModal(false, null); }} />); global.showClockModal(true, node); } function hidePicker() { global.showClockModal(false, null); if (global.testInfotimer) { clearInterval(global.testInfotimer) global.testInfotimer = null } else { console.log('global test info timer') } } function testLayout() { if (!user.test_user) { return } var isStart = false; if (time.status == 'WAIT_FOR_START' || time.status == 'ONGOING1') { isStart = true } var isFast = false; switch (time.status) { case 'WAIT_FOR_START': case 'ONGOING': { isFast = (scenario.name == 'FAST' || scenario.name == 'FAST_SLEEP') } break; case 'ONGOING1': case 'ONGOING2': { isFast = false; } break case 'ONGOING3': { isFast = true; } break; } if (props.isNextStep) { isFast = true isStart = false } return check scenario:{isFast ? 'fast' : 'sleep'} check type:{isStart ? 'start' : 'end'} picker restriction min:{TimeFormatter.formatTimestamp(global.limit)} pick:{TimeFormatter.formatTimestamp(global.picker_time)} max{TimeFormatter.formatTimestamp(global.set_time)} now:{TimeFormatter.formatTimestamp(new Date().getTime())} elapsed:{TimeFormatter.calculateTimeDifference(global.picker_time, new Date().getTime())} real start:{isStart ? '-' : TimeFormatter.formatTimestamp(isFast ? time.fast.real_start_time : time.sleep.real_start_time)} real duration:{isStart ? '-' : TimeFormatter.calculateTimeDifference(isFast ? time.fast.real_start_time : time.sleep.real_start_time, new Date().getTime())} } function layoutContent() { var limit = global.set_time - 7 * 3600 * 1000 * 24; global.limit = limit if (time.last_real_check_time) { limit = time.last_real_check_time global.limit = limit //当set_time秒数<=latest_record_time秒数时,最小限制时间戳需+1分钟 if (new Date(global.set_time).getSeconds() <= new Date(time.last_real_check_time).getSeconds()) { limit = limit + 60 * 1000 } } var title = getTimePickerTitle(time, t) var color = getColor(time) if (props.isNextStep) { title = t('feature.track_time_duration.console.fast_end') color = global.fastColor?global.fastColor:ColorType.fast } return { pickerConfirm(e) hidePicker() }} /> } function pickerConfirm(t: number) { var date = new Date(t) var setDate = new Date(global.set_time); date.setMilliseconds(setDate.getMilliseconds()); date.setSeconds(setDate.getSeconds()); t = date.getTime(); if (props.isNextStep) { endFast(t) return; } global.postBtnUpdateStatus('normal'); if (isFast) { if (time.status == 'WAIT_FOR_START') { startFast(t, fastDuration).then(res => { if ((res as any).current_record.scenario == 'FAST_SLEEP' && (res as any).current_record.status == 'ONGOING1') { global.consoleType = 'going' global.showSingleFastEnd() dispatch(changeConsoleStatus({ status: ConsoleType.going })); } dispatch(updateScenario((res as any).current_record)); global.postBtnUpdateStatus('idle'); }); } else { endFast(t).then(res => { dispatch(updateScenario((res as any).current_record)); global.postBtnUpdateStatus('idle'); }) } } else { if (time.status == 'WAIT_FOR_START' || time.status == 'ONGOING1') { startSleep(t, sleepDuration).then(res => { dispatch(updateScenario((res as any).current_record)); global.postBtnUpdateStatus('idle'); }); } else { endSleep(t).then(res => { dispatch(updateScenario((res as any).current_record)); global.postBtnUpdateStatus('idle'); }) } } } function targetView() { if (props.isNextStep && (time.status == 'ONGOING1' || time.status == 'ONGOING2')) { var opacity = textNextStepAlpha(time) if (opacity == 1) { return 超出目标结束 } return 距离目标结束 } if (time.status == 'ONGOING' || time.status == 'ONGOING2' || time.status == 'ONGOING3') { var opacity = textAlpha(time) if (opacity == 1) { return 超出目标结束 } return 距离目标结束 } return } function mixedBtns() { return { (time.status == 'WAIT_FOR_START' || time.status == 'DONE') && } { (time.status == 'ONGOING' || time.status == 'ONGOING3') && new Date().getTime()} /> } { time.status == 'ONGOING1' && } { time.status == 'ONGOING2' && new Date().getTime()} /> } } function nextStepBtns() { return { (time.status == 'WAIT_FOR_START') && } { time.status == 'ONGOING1' && new Date().getTime()} /> } { time.status == 'ONGOING2' && new Date().getTime()} /> } } function fastBtns() { return { time.status == 'ONGOING' ? : } } function sleepBtns() { return { time.status == 'ONGOING' ? : } } function durationChange(e) { var count = (e[0] + common.duration.min) * 60 + e[1] * common.duration.step isFast ? setFastDuration(count * 60000) : setSleepDuration(count * 60000); isFast ? setFastPickerValue(e) : setSleepPickerValue(e) global.showClockModal2(false, null) } function login() { Taro.navigateTo({ url: '/pages/account/ChooseAuth' }) } function durationFormate() { var t = isFast ? fastDuration / 60000 : sleepDuration / 60000 if (props.isNextStep) t = sleepDuration / 60000 var hour = Math.floor(t / 60) var minute = Math.floor(t % 60) return `${hour > 0 ? hour + '小时' : ''}${minute > 0 ? minute + '分钟' : ''}` } function showDurationPicker(e) { e.stopPropagation() var node = global.showClockModal2(false, null)} confirm={() => { var picker = durationPickerRef.current; durationChange((picker as any).getConfirmData()); global.showClockModal2(false, null); }} /> global.showClockModal2(true, node); } function durationPickerContent() { var color = getColor(time) var title = getDurationTitle(time, t) return { global.showClockModal2(false, null); }} /> } function minus() { if (isFast) { var count = fastDuration - common.duration.step * 60 * 1000 count = count<3600000?3600000:count setFastDuration(count) var hour = count / 60000 / 60 var minute = count / 60000 % 60 setFastPickerValue(durationIndex('00:00', `${hour > 10 ? hour : '0' + hour}:${minute > 10 ? minute : '0' + minute}`, common)) } else { var count = sleepDuration - common.duration.step * 60 * 1000 count = count<3600000?3600000:count setSleepDuration(count) var hour = count / 60000 / 60 var minute = count / 60000 % 60 setSleepPickerValue(durationIndex('00:00', `${hour > 10 ? hour : '0' + hour}:${minute > 10 ? minute : '0' + minute}`, common)) } } function plus() { if (isFast) { var count = fastDuration + common.duration.step * 60 * 1000 count = count>23*3600000?23*3600000:count setFastDuration(count) var hour = count / 60000 / 60 var minute = count / 60000 % 60 setFastPickerValue(durationIndex('00:00', `${hour > 10 ? hour : '0' + hour}:${minute > 10 ? minute : '0' + minute}`, common)) } else { var count = sleepDuration + common.duration.step * 60 * 1000 count = count>23*3600000?23*3600000:count setSleepDuration(count) var hour = count / 60000 / 60 var minute = count / 60000 % 60 setSleepPickerValue(durationIndex('00:00', `${hour > 10 ? hour : '0' + hour}:${minute > 10 ? minute : '0' + minute}`, common)) } } function disableMinus() { if (isFast) { if (fastDuration <= 60 * 60 * 1000) { return true; } } else { if (sleepDuration <= 60 * 60 * 1000) { return true; } } return false; } function disablePlus() { if (isFast) { if (fastDuration >= 23 * 60 * 60 * 1000) { return true; } } else { if (sleepDuration >= 23 * 60 * 60 * 1000) { return true; } } return false; } var textColor = getColor(time) if (time.status != 'ONGOING1' && time.status != 'WAIT_FOR_START') { if (textAlpha(time) == 1) { textColor = '#FA5151'; } } if (!user.isLogin) { return 16:00 Start Fast } if (props.isNextStep) { return { (time.status == 'WAIT_FOR_START') && {durationFormate()} } minus={minus} plus={plus} themeColor={global.sleepColor?global.sleepColor:ColorType.sleep} disableMinus={disableMinus()} disablePlus={disablePlus()} /> } { (time.status == 'ONGOING1' || time.status == 'ONGOING2') && { TimeFormatter.calculateTimeDifference(time.fast.target_start_time, time.fast.target_end_time, true)} {t('feature.track_time_duration.console.total_duration')} {TimeFormatter.countdown(time.fast.target_end_time)} {textNextStepAlpha(time) == 1 ? t('feature.track_time_duration.console.timeout') : t('feature.track_time_duration.console.countdown_not_due')} } { nextStepBtns() } } function ongoing() { if (time.status == 'ONGOING' || time.status == 'ONGOING2' || time.status == 'ONGOING3') { var isFastData = time.scenario == 'FAST' || time.status == 'ONGOING3' var duration = TimeFormatter.calculateTimeDifference(isFastData ? time.fast.target_start_time : time.sleep.target_start_time, isFast ? time.fast.target_end_time : time.sleep.target_end_time, true); return {duration} {t('feature.track_time_duration.console.total_duration')} {isFastData ? TimeFormatter.countdown(time.fast.target_end_time) : TimeFormatter.countdown(time.sleep.target_end_time)} {textAlpha(time) == 1 ? t('feature.track_time_duration.console.timeout') : t('feature.track_time_duration.console.countdown_not_due')} } return } return ( { ongoing() } { (time.status == 'ONGOING1' || time.status == 'WAIT_FOR_START') && {durationFormate()} } minus={minus} plus={plus} themeColor={isFast ? global.fastColor?global.fastColor:ColorType.fast : global.sleepColor?global.sleepColor:ColorType.sleep} disableMinus={disableMinus()} disablePlus={disablePlus()} /> } {/* { targetView() } */} { time.scenario === 'FAST_SLEEP' && mixedBtns() } { time.scenario === 'FAST' && fastBtns() } { time.scenario === 'SLEEP' && sleepBtns() } ) }