web/front/composables/useKompasDocuments.ts

70 lines
2.3 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// composables/useKompasDocuments.ts
const documentTypeLabels = {
0: 'Неизвестно',
1: 'Чертёж',
2: 'Фрагмент',
3: 'Спецификация',
4: 'Деталь',
5: 'Сборка',
6: 'Текстовый документ',
7: 'Технологическая сборка',
} as const
type DocumentTypeLabel = typeof documentTypeLabels
export function getDocumentLabel(typeCode: number): string {
return documentTypeLabels[typeCode as keyof DocumentTypeLabel] || 'Неизвестный тип'
}
export function useKompasDocuments() {
// Список документов из КОМПАС
const documents = ref<KompasDocument[]>([])
// Фильтр по типам
const selectedTypes = ref<number[]>([])
// Доступные действия (динамически с бэкенда)
const availableActions = ref<Record<string, { label: string; allowedTypes: number[] }>>({})
// Вычисляем количество документов по типам
const documentCounts = computed(() => {
const counts: Record<number, number> = {}
Object.keys(documentTypeLabels).forEach(key => {
const code = Number(key)
counts[code] = documents.value.filter(doc => doc.type === code).length
})
return counts
})
// Отфильтрованный список
const filteredDocuments = computed(() => {
if (selectedTypes.value.length === 0) return documents.value
return documents.value.filter(doc => selectedTypes.value.includes(Number(doc.type)))
})
// Какие действия сейчас применимы к документам
const applicableActions = computed(() => {
const result: Record<string, { label: string }> = {}
for (const key in availableActions.value) {
const action = availableActions.value[key]
const canApply = documents.value.some(doc => action.allowedTypes.includes(doc.type))
if (canApply) {
result[key] = { label: action.label }
}
}
return result
})
return {
documents,
selectedTypes,
documentCounts,
filteredDocuments,
documentTypeLabels,
getDocumentLabel,
applicableActions,
}
}