33 lines
1.5 KiB
TypeScript
33 lines
1.5 KiB
TypeScript
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<string>) {
|
|
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<ProjectDetail | null>(() => query.data.value?.project ?? null)
|
|
const checkpoints = computed<Checkpoint[]>(() => query.data.value?.checkpoints ?? [])
|
|
const stageReadiness = computed(() => query.data.value?.stageReadiness ?? null)
|
|
return { ...query, project, checkpoints, stageReadiness }
|
|
}
|
|
|
|
/** 项目布局向子页面提供的类型安全上下文。 */
|
|
export const projectContextKey: InjectionKey<ReturnType<typeof useProjectData>> = Symbol('project-context')
|
|
|
|
/** 读取项目上下文,错误布局在开发阶段立即暴露。 */
|
|
export function useProjectContext() {
|
|
const context = inject(projectContextKey)
|
|
if (!context) throw new Error('项目页面必须位于 ProjectLayout 中')
|
|
return context
|
|
}
|