
import { useRef } from 'react'
import { useDidShow, useDidHide, nextTick } from '@tarojs/taro'
import { trackPage, updateTrackPage } from '@/services/track'

export default (payload) => {
  const payloadRef = useRef()
  const resRef = useRef()
  payloadRef.current = payload

  useDidShow(() => {
    trackPage(payloadRef.current).then((res) => {
      resRef.current = res;
    }).catch((err) => {
      console.error(err);
    })
  })

  useDidHide(() => {
    if (resRef.current) {
      updateTrackPage(resRef.current.trackId)
      resRef.current = undefined
    }
  })
}
