import JournalCover from "@/features/journal/components/journal_cover"; import { getJournals } from "@/services/health"; import { View, Text, Image, ScrollView } from "@tarojs/components"; import { useEffect, useState } from "react"; import './Journal.scss' import dayjs from "dayjs"; import { rpxToPx } from "@/utils/tools"; import { jumpPage } from "@/features/trackTimeDuration/hooks/Common"; import NewHeader, { NewHeaderType } from "@/_health/components/new_header"; import Taro, { useRouter } from "@tarojs/taro"; import StickyDateList from "@/_health/components/sticky_date_list"; import { MainColorType } from "@/context/themes/color"; import ListFooter from "@/_health/components/list_footer"; import TimeTitleDesc from "@/_health/components/time_title_desc"; import { TimeFormatter } from "@/utils/time_format"; import { useTranslation } from "react-i18next"; import NewButton, { NewButtonType } from "@/_health/base/new_button"; import { getScenario, getThemeColor } from "@/features/health/hooks/health_hooks"; import { useSelector } from "react-redux"; import { IconClose } from "@/components/basic/Icons"; import TargetProgress from "@/_health/components/target_progress"; import NoRecord from "@/_health/components/no_record"; import TimelineDate from "@/_health/components/timeline_date"; let myScrollTop = 0 let useRoute; let useNavigation; let scenario = ''; if (process.env.TARO_ENV == 'rn') { useRoute = require("@react-navigation/native").useRoute useNavigation = require("@react-navigation/native").useNavigation } export default function Journal() { const health = useSelector((state: any) => state.health); const [journals, setJournals] = useState([]) const [isPulling, setIsPulling] = useState(false) const [itemLayouts, setItemLayouts] = useState([]) const [itemHeights, setItemHeights] = useState([]) const [showDate, setShowDate] = useState(false) const [date, setDate] = useState('') const [loaded, setLoaded] = useState(false) const [page, setPage] = useState(1) const [total, setTotal] = useState(0) const [loading, setLoading] = useState(false) const { t } = useTranslation() const [pageTop, setPageTop] = useState(0) let router let navigation; if (useNavigation) { navigation = useNavigation() } if (process.env.TARO_ENV == 'rn') { router = useRoute() } else { router = useRouter() } const [window, setWindow] = useState(router.params.type ?? '') const items = ['FAST', 'SLEEP', 'EAT', 'ACTIVE'] const [showTip, setShowTip] = useState(router.params.show_tip == '1') const [showBadge, setShowBadge] = useState(router.params.show_badge == '1') // useEffect(() => { // }, []) useEffect(() => { getJounalsData(1) }, [window]) useEffect(() => { console.log('last show status', showDate) }, [showDate]) useEffect(() => { if (journals.length > 0) { setTimeout(() => { measureItemLayouts() }, 500) } }, [journals]) function more() { if (loading) return; if (total == journals.length) return; var index = page; index++; setPage(index) getJounalsData(index) } function getJounalsData(index = 1) { setLoading(true) setPage(index) var params: any = { page: index, limit: 10, } params.window = window getJournals(params).then(res => { setLoaded(true) let list = (res as any).data // list.forEach(element => { // let array: any = [] // element.windows.map(item => { // item.events.map(event => { // event.moments && event.moments.map(moment => { // if (moment.media && moment.media.length > 0) { // moment.media.map(media => { // array.push(media.url) // }) // } // }) // }) // }) // element.imgs = array // }); setLoading(false) if (index == 1) { setTotal((res as any).total) setJournals(list) setIsPulling(false) } else { setJournals([...journals, ...list]) } }).catch(e => { setLoading(false) }) } function measureItemLayouts() { const query = Taro.createSelectorQuery() journals.forEach((item, index) => { query.select(`#history-${index}`).boundingClientRect() }); query.exec((res) => { var layouts: any = [] var heights: any = [] res.forEach((rect, index) => { if (rect) { layouts[index] = rect.top + myScrollTop heights[index] = rect.height } }); setItemLayouts(layouts) setItemHeights(heights) }) } function onScroll(e) { var top = e.detail.scrollTop - e.detail.deltaY myScrollTop = e.detail.scrollTop setPageTop(top) if (top > 60) { Taro.setNavigationBarTitle({ title: pageTitle() }) } else { Taro.setNavigationBarTitle({ title: '' }) } if (itemLayouts.length > 0) { var i = -1 var dt = '' journals.forEach((item, index) => { if (top >= itemLayouts[index] - 50) { i = index dt = global.language=='en'?dayjs(item.timestamp).format('MMMM YYYY'):dayjs(item.timestamp).format('YYYY年M月') } }) setShowDate(i != -1) setDate(dt) } else { setShowDate(false) setDate('') } // if (itemLayouts.length > 0 && itemLayouts[0] > top) { // setShowDate(false) // setDate('') // console.log(itemLayouts[0], showDate, date, top, e.detail.deltaY) // } // else { // console.log(itemLayouts[0], showDate, date, top, e.detail.deltaY) // } } function getTitle(item) { if (item.title) { return item.title; } if (item.moments) { return item.moments[0].title } return '' } function journalItem(window, index) { var array: any = []; window.events.map(event => { event.moments && event.moments.map(moment => { if (moment.media && moment.media.length > 0) { moment.media.map(media => { array.push(media.url) }) } }) }) return { array.length > 0 && } { window.events.map((item2, index2) => { if (item2.moments && item2.moments[0].type == 'PIC') return return 0 ? rpxToPx(350) : rpxToPx(512) }} timeObj={item2.time} time={dayjs(item2.time.timestamp).format('HH:mm')} title={getTitle(item2)} desc={item2.moments && item2.moments.length > 0 ? item2.moments[0].description : ''} /> }) } } function historyYear(index) { var showDate = false; var dateStr2: any = '' if (index == 0) { var currentDate = global.language == 'en' ? dayjs(journals[index].timestamp).format('YYYY') : dayjs(journals[index].timestamp).format('YYYY年') var now = global.language == 'en' ? dayjs().format('YYYY') : dayjs().format('YYYY年') if (currentDate != now) { showDate = true dateStr2 = currentDate } } else { var currentDate = global.language == 'en' ? dayjs(journals[index].timestamp).format('YYYY') : dayjs(journals[index].timestamp).format('YYYY年') var now = global.language == 'en' ? dayjs(journals[index - 1].timestamp).format('YYYY') : dayjs(journals[index - 1].timestamp).format('YYYY年') if (currentDate != now) { showDate = true dateStr2 = currentDate } } if (showDate) { return {dateStr2} } return } function historyDate(item, index) { if (index == 0) { if (global.language == 'zh' && TimeFormatter.isToday(item.timestamp)) { return '今天' } if (global.language == 'zh' && TimeFormatter.isYesterday(item.timestamp)) { return '昨天' } return dayjs(item.timestamp).format('DD') } if (dayjs(item.timestamp).format('MM-DD') == dayjs(journals[index - 1].timestamp).format('MM-DD')) { return '' } if (global.language == 'zh' && TimeFormatter.isToday(item.timestamp)) { return '今天' } if (global.language == 'zh' && TimeFormatter.isYesterday(item.timestamp)) { return '昨天' } return dayjs(item.timestamp).format('DD') } function historyMonth(item, index) { if (index == 0) { if (global.language == 'zh' && TimeFormatter.isToday(item.timestamp)) { return '' } if (global.language == 'zh' && TimeFormatter.isYesterday(item.timestamp)) { return '' } return dayjs(item.timestamp).format('MMM') } if (dayjs(item.timestamp).format('MM-DD') == dayjs(journals[index - 1].timestamp).format('MM-DD')) { return '' } if (global.language == 'zh' && TimeFormatter.isToday(item.timestamp)) { return '' } if (global.language == 'zh' && TimeFormatter.isYesterday(item.timestamp)) { return '' } return dayjs(item.timestamp).format('MMM') } function journalCellText(index) { var windows = journals[index].windows var array: any = [] windows.map(window => { window.events.map(event => { event.moments && event.moments.map(moment => { array.push({ title: moment.title, description: moment.description }) }) }) }) return array if (array.length > 0) { return { array.map((item, index) => { if (index >= 3) return return }) } } return } function journalCell(item, index) { // if (item.show_ring) { // if (item.windows) { // return { // item.windows.map((temp, i) => { // return // }) // } // } // return // } return { (item.pics.length > 0 || item.texts.length > 0) && { item.pics.length > 0 && { if (process.env.TARO_ENV == 'weapp') { e.stopPropagation() } Taro.previewImage({ current: item.pics[0], urls: item.pics }) }}> } { item.texts.map((item2, index2) => { return 0 ? rpxToPx(350) : rpxToPx(512) }} time='' title={item2.title} desc={item2.description} /> }) } {/* ... */} } { item.show_ring && item.windows && { item.windows.map((temp, i) => { return }) } } } function pageTitle() { if (router.params.type == 'EAT') { return t('health.title_food_journal') } else if (router.params.type == 'ACTIVE') { return t('health.title_active_journal') } return t('health.title_journal') } function pageYear(){ if (!loaded) return '' if (journals.length==0) return global.language == 'en'?dayjs().format('YYYY'):dayjs().format('YYYY年') if (journals[0].timestamp) return global.language == 'en'?dayjs(journals[0].timestamp).format('YYYY'):dayjs(journals[0].timestamp).format('YYYY年') return '' } function markDoneTip() { if (health.mode != 'EAT' && health.mode != 'ACTIVE') return var scenario = getScenario(health.windows, health.mode) if (scenario.status != 'OG') return if (!showTip) return var strTitle = '' if (scenario.archive_timestamp) { var today = new Date().getDate() var date = new Date(scenario.archive_timestamp).getDate() if (today == date) { strTitle = t('health.tonight_at', { time: dayjs(scenario.archive_timestamp).format('HH:mm') }) } else { strTitle = t('health.tomorrow_at', { time: dayjs(scenario.archive_timestamp).format('HH:mm') }) } } return {strTitle} {t('health.detail_complete_tip_head')}{t('health.detail_complete_tip_end')} { setShowTip(false) if (health.mode == 'EAT') { global.hideEatArchiveTip = true } else { global.hideActiveArchiveTip = true } }}> } if (!loaded) return // return { // setIsPulling(true) // getJounalsData(1) // }} // refresherTriggered={isPulling} // onScroll={onScroll} // onScrollToLower={() => { // more() // }} // > // // // { // markDoneTip() // } // // { // journals.map((item, index) => { // return // { // historyYear(index) // } // { // jumpPage('/pages/account/JournalDetail?date=' + item.date + '&window=' + window) //JSON.stringify(item)) // }}> // // {historyDate(item, index)} // {historyMonth(item, index)} // // // { // journalCell(item, index) // } // // // // }) // } // 长列表后面的内容 // function itemData(index,item) { if (itemLayouts.length >= index + 1 && index > 5) { if (Math.abs(itemLayouts[index] - pageTop) > 2500) { return {/* {index} */} } } return { jumpPage('/pages/account/JournalDetail?date=' + item.date + '&window=' + window) //JSON.stringify(item)) }}> 0?journals[index - 1].timestamp:null} isJournal /> {/* {historyDate(item, index)} {historyMonth(item, index)} */} { journalCell(item, index) } } return { setIsPulling(true) getJounalsData(1) }} isPulling={isPulling} onScroll={onScroll} showDate={showDate} date={date} loadMore={() => { more() }} > { markDoneTip() } { journals.map((item, index) => { return { historyYear(index) } { itemData(index,item) } }) } { loaded && journals.length == 0 && } 0) && (total == journals.length)} loading={loading} /> }