import { fetchProject } from '@/api'
import { router } from '@/router'
import { useRealtime } from './useRealtime'

import type { Project } from '@/api'

export const useProject = (
  id: Project['projectId'],
  onErr?: (err: any) => void
) => useRealtime(() => fetchProject(id).catch(onErr))[0]

export const useCurrentProject = () => {
  const id = Number(router.currentRoute.value.params.id)
  const back = () => router.back()
  if (!id || id < 0) {
    back()
    throw '错误页面'
  }
  return useProject(id, back)
}
