import { ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { isNil } from 'lodash'

// project 页面及其子页面获取项目 ID
export default function useProjectID() {
  const route = useRoute()
  const router = useRouter()

  // 是否已经获取到了 projectID
  const shouldRender = ref(false)

  // 项目 ID
  let projectID = ''
  watch(() => route.query.projectID, (val) => {
    if (isNil(val) || val === '') {
      router.replace('/project-list')
    } else {
      shouldRender.value = true
      projectID = val as string
    }
  }, {
    immediate: true
  })

  return {
    // 是否已经获取到了 projectID, Ref 类型
    shouldRender,
    projectID
  }
}


