| 1 | <template> |
| 2 | <CardEntity hoverable size="small"> |
| 3 | <template #headerMain> |
| 4 | {{ template.title }} |
| 5 | </template> |
| 6 | <template #default> |
| 7 | <p class="text-xs"> |
| 8 | {{ template.description }} |
| 9 | </p> |
| 10 | </template> |
| 11 | |
| 12 | <template #footerMain> |
| 13 | <Badge type="splitted"> |
| 14 | <template #label>Panels</template> |
| 15 | <template #value>{{ template.panels.length }}</template> |
| 16 | </Badge> |
| 17 | </template> |
| 18 | |
| 19 | <template #footerExtra> |
| 20 | <n-button v-if="isEnabled" size="small" type="error" quaternary :loading="toggling" @click="onDisable"> |
| 21 | <template #icon> |
| 22 | <Icon :name="DisableIcon" /> |
| 23 | </template> |
| 24 | Disable |
| 25 | </n-button> |
| 26 | <n-button |
| 27 | v-else |
| 28 | size="small" |
| 29 | type="primary" |
| 30 | :disabled="!canEnable || toggling" |
| 31 | :loading="toggling" |
| 32 | @click="onEnable" |
| 33 | > |
| 34 | <template #icon> |
| 35 | <Icon :name="selectedEventSourceId ? EnableIcon : LockedIcon" /> |
| 36 | </template> |
| 37 | Enable |
| 38 | </n-button> |
| 39 | </template> |
| 40 | </CardEntity> |
| 41 | </template> |
| 42 | |
| 43 | <script setup lang="ts"> |
| 44 | import type { DashboardTemplate, EnabledDashboard } from "@/types/dashboards.d" |
| 45 | import { NButton, useDialog, useMessage } from "naive-ui" |
| 46 | import { computed, ref } from "vue" |
| 47 | import Api from "@/api" |
| 48 | import Badge from "@/components/common/Badge.vue" |
| 49 | import CardEntity from "@/components/common/cards/CardEntity.vue" |
| 50 | import Icon from "@/components/common/Icon.vue" |
| 51 | |
| 52 | const props = defineProps<{ |
| 53 | template: DashboardTemplate |
| 54 | selectedCustomerCode: string | null |
| 55 | selectedEventSourceId: number | null |
| 56 | selectedCategoryId: string | null |
| 57 | enabledDashboards: EnabledDashboard[] |
| 58 | }>() |
| 59 | |
| 60 | const emit = defineEmits<{ |
| 61 | refreshEnabledDashboards: [] |
| 62 | }>() |
| 63 | |
| 64 | const message = useMessage() |
| 65 | const dialog = useDialog() |
| 66 | |
| 67 | const EnableIcon = "carbon:add-alt" |
| 68 | const DisableIcon = "carbon:subtract-alt" |
| 69 | const LockedIcon = "carbon:locked" |
| 70 | |
| 71 | const toggling = ref(false) |
| 72 | |
| 73 | const canEnable = computed(() => !!props.selectedCustomerCode && !!props.selectedEventSourceId) |
| 74 | |
| 75 | const isEnabled = computed(() => { |
| 76 | if (!props.selectedCategoryId || !props.selectedEventSourceId) return false |
| 77 | |
| 78 | return props.enabledDashboards.some( |
| 79 | d => |
| 80 | d.library_card === props.selectedCategoryId && |
| 81 | d.template_id === props.template.id && |
| 82 | d.event_source_id === props.selectedEventSourceId |
| 83 | ) |
| 84 | }) |
| 85 | |
| 86 | function onEnable() { |
| 87 | if (!props.selectedCustomerCode || !props.selectedEventSourceId || !props.selectedCategoryId) { |
| 88 | message.warning("Please select a customer and event source first") |
| 89 | return |
| 90 | } |
| 91 | |
| 92 | toggling.value = true |
| 93 | |
| 94 | Api.siem |
| 95 | .enableDashboard({ |
| 96 | customer_code: props.selectedCustomerCode, |
| 97 | event_source_id: props.selectedEventSourceId, |
| 98 | library_card: props.selectedCategoryId, |
| 99 | template_id: props.template.id, |
| 100 | display_name: props.template.title |
| 101 | }) |
| 102 | .then(res => { |
| 103 | if (res.data.success) { |
| 104 | message.success(res.data?.message || "Dashboard enabled successfully") |
| 105 | emit("refreshEnabledDashboards") |
| 106 | } else { |
| 107 | message.warning(res.data?.message || "An error occurred. Please try again later.") |
| 108 | } |
| 109 | }) |
| 110 | .catch(err => { |
| 111 | message.error(err.response?.data?.message || "An error occurred. Please try again later.") |
| 112 | }) |
| 113 | .finally(() => { |
| 114 | toggling.value = false |
| 115 | }) |
| 116 | } |
| 117 | |
| 118 | function onDisable() { |
| 119 | const match = props.enabledDashboards.find( |
| 120 | d => |
| 121 | d.library_card === props.selectedCategoryId && |
| 122 | d.template_id === props.template.id && |
| 123 | d.event_source_id === props.selectedEventSourceId |
| 124 | ) |
| 125 | |
| 126 | if (!match) return |
| 127 | |
| 128 | dialog.warning({ |
| 129 | title: "Disable Dashboard", |
| 130 | content: `Are you sure you want to disable "${props.template.title}"?`, |
| 131 | positiveText: "Disable", |
| 132 | negativeText: "Cancel", |
| 133 | onPositiveClick: () => { |
| 134 | toggling.value = true |
| 135 | |
| 136 | return Api.siem |
| 137 | .disableDashboard(match.id) |
| 138 | .then(res => { |
| 139 | if (res.data.success) { |
| 140 | message.success(res.data?.message || "Dashboard disabled successfully") |
| 141 | emit("refreshEnabledDashboards") |
| 142 | } else { |
| 143 | message.warning(res.data?.message || "An error occurred. Please try again later.") |
| 144 | } |
| 145 | }) |
| 146 | .catch(err => { |
| 147 | message.error(err.response?.data?.message || "An error occurred. Please try again later.") |
| 148 | }) |
| 149 | .finally(() => { |
| 150 | toggling.value = false |
| 151 | }) |
| 152 | } |
| 153 | }) |
| 154 | } |
| 155 | </script> |