web/front/composables/useKompasActions.ts

56 lines
1.9 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/useKompasActions.ts
const availableActions = ref<Record<string, { label: string; allowedTypes: number[] }>>({})
export function useKompasActions() {
const { sendCommandToPython, isReady } = usePythonBridge()
const error = ref<string | null>(null)
/**
* Загружает список действий с бэкенда
*/
async function loadActions() {
if (!isReady.value) {
console.warn('Bridge не готов')
return
}
error.value = null
try {
// Отправляем команду и ожидаем ответ в новом формате
const response = await sendCommandToPython<{
status: 'success' | 'error'
data: Record<string, { label: string; allowed_types: number[] }> | null
error: string | null
}>('get_available_actions')
if (response.status === 'success' && response.data) {
// Преобразуем данные
const convertedActions: Record<string, { label: string; allowedTypes: number[] }> = {}
for (const key in response.data) {
const { label, allowed_types } = response.data[key]
convertedActions[key] = {
label,
allowedTypes: allowed_types
}
}
availableActions.value = convertedActions
} else {
throw new Error(response.error || 'Неизвестная ошибка')
}
} catch (err: any) {
console.error('Ошибка при получении действий:', err)
error.value = 'Не удалось загрузить список действий'
}
}
return {
availableActions,
error,
loadActions,
}
}