| 1 | <template> |
| 2 | <n-spin :show="loadingDetails"> |
| 3 | <div class="@container min-h-50"> |
| 4 | <n-alert v-if="detailsError" title="Error" type="error" :description="detailsError" /> |
| 5 | <AgentOverview v-else-if="agent" :agent @critical-asset-updated="handleCriticalAssetUpdated" /> |
| 6 | </div> |
| 7 | </n-spin> |
| 8 | </template> |
| 9 | |
| 10 | <script setup lang="ts"> |
| 11 | import type { AgentCriticalUpdateSuccessPayload } from "../AgentCriticalSelect.vue" |
| 12 | import type { Agent } from "@/types/agents" |
| 13 | import type { ApiError } from "@/types/common" |
| 14 | import { NAlert, NSpin } from "naive-ui" |
| 15 | import { ref, watch } from "vue" |
| 16 | import Api from "@/api" |
| 17 | import { getApiErrorMessage } from "@/utils" |
| 18 | import AgentOverview from "./AgentOverview.vue" |
| 19 | |
| 20 | const props = defineProps<{ |
| 21 | agentId: string | number | null |
| 22 | }>() |
| 23 | |
| 24 | const emit = defineEmits<{ |
| 25 | (e: "criticalAssetUpdated", value: AgentCriticalUpdateSuccessPayload): void |
| 26 | }>() |
| 27 | |
| 28 | const agent = ref<Agent | null>(null) |
| 29 | const detailsError = ref<string | null>(null) |
| 30 | const loadingDetails = ref(false) |
| 31 | |
| 32 | async function loadAgentDetails() { |
| 33 | if (props.agentId === null) return |
| 34 | |
| 35 | loadingDetails.value = true |
| 36 | detailsError.value = null |
| 37 | |
| 38 | try { |
| 39 | const response = await Api.agents.getAgentById(props.agentId.toString()) |
| 40 | agent.value = response.data.agents?.[0] || null |
| 41 | if (!agent.value) { |
| 42 | detailsError.value = "Agent not found." |
| 43 | } |
| 44 | } catch (err) { |
| 45 | detailsError.value = getApiErrorMessage(err as ApiError) |
| 46 | } finally { |
| 47 | loadingDetails.value = false |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | function handleCriticalAssetUpdated(payload: AgentCriticalUpdateSuccessPayload) { |
| 52 | if (!agent.value) return |
| 53 | agent.value.critical_asset = payload.critical |
| 54 | emit("criticalAssetUpdated", payload) |
| 55 | } |
| 56 | |
| 57 | watch( |
| 58 | () => props.agentId, |
| 59 | async newAgentId => { |
| 60 | agent.value = null |
| 61 | detailsError.value = null |
| 62 | |
| 63 | if (newAgentId !== null) { |
| 64 | await loadAgentDetails() |
| 65 | } |
| 66 | }, |
| 67 | { immediate: true } |
| 68 | ) |
| 69 | </script> |