import { computed, inject, type InjectionKey } from 'vue' import { useQuery } from '../../composables/useQuery' import { projectsApi } from './api' import type { ProjectDetail } from './types' import type { Checkpoint } from '../workflows/types' import type { Ref } from 'vue' /** 同一项目的各个 graph 共用一份查询,避免每个面板重复请求。 */ export function useProjectData(id: Ref) { const query = useQuery(id, async (projectId, signal) => { const [project, checkpoints, stageReadiness] = await Promise.all([ projectsApi.detail(projectId, signal), projectsApi.checkpoints(projectId, signal), projectsApi.stageReadiness(projectId, signal) ]) return { project, checkpoints, stageReadiness } }) const project = computed(() => query.data.value?.project ?? null) const checkpoints = computed(() => query.data.value?.checkpoints ?? []) const stageReadiness = computed(() => query.data.value?.stageReadiness ?? null) /** 子页面完成当前步骤后调用,只刷新轻量项目状态与导航门禁。 */ async function refreshStageReadiness() { const projectId = id.value if (!projectId || !query.data.value) return const nextReadiness = await projectsApi.stageReadiness(projectId) if (id.value === projectId && query.data.value) query.data.value = { ...query.data.value, stageReadiness: nextReadiness } } return { ...query, project, checkpoints, stageReadiness, refreshStageReadiness } } /** 项目布局向子页面提供的类型安全上下文。 */ export const projectContextKey: InjectionKey> = Symbol('project-context') /** 读取项目上下文,错误布局在开发阶段立即暴露。 */ export function useProjectContext() { const context = inject(projectContextKey) if (!context) throw new Error('项目页面必须位于 ProjectLayout 中') return context }