main
vue 99 lines 2.44 KB
Raw
1 <template>
2 <n-popover v-model:show="show" trigger="manual" to="body" content-class="px-0" @clickoutside="closePopup()">
3 <template #trigger>
4 <slot :loading :toggle-popup />
5 </template>
6
7 <div class="flex min-w-52 flex-col justify-center gap-4 py-1">
8 <div class="flex items-center justify-center gap-2 py-3">
9 <span>Active</span>
10 <n-switch v-model:value="model.active" :disabled="loading" />
11 </div>
12
13 <div class="flex justify-between gap-2">
14 <n-button quaternary size="small" @click="closePopup()">Close</n-button>
15 <n-button :disabled="!dirty" :loading type="primary" size="small" @click="updateActive()">
16 Save
17 </n-button>
18 </div>
19 </div>
20 </n-popover>
21 </template>
22
23 <script setup lang="ts">
24 // TODO-FE: refactor
25 import type { SigmaQuery } from "@/types/sigma.d"
26 import { NButton, NPopover, NSwitch, useMessage } from "naive-ui"
27 import { computed, onBeforeMount, ref, toRefs, watch } from "vue"
28 import Api from "@/api"
29
30 const props = defineProps<{
31 query: SigmaQuery
32 }>()
33
34 const emit = defineEmits<{
35 (e: "updated", value: SigmaQuery): void
36 }>()
37
38 const { query } = toRefs(props)
39
40 const loading = defineModel<boolean | undefined>("loading", { default: false })
41
42 const show = ref(false)
43 const lastShow = ref(Date.now())
44 const message = useMessage()
45 const model = ref<{ active: boolean }>({ active: false })
46 const active = ref<boolean>(false)
47 const dirty = computed(() => active.value !== model.value.active)
48
49 watch(show, val => {
50 if (val && !loading.value) {
51 setModel()
52 }
53 })
54
55 function togglePopup() {
56 if (Date.now() - lastShow.value > 500) {
57 show.value = !show.value
58 }
59 }
60
61 function closePopup() {
62 lastShow.value = Date.now()
63 show.value = false
64 }
65
66 function setModel() {
67 active.value = !!query.value.active
68 model.value.active = active.value
69 }
70
71 function updateActive() {
72 if (query.value.rule_name) {
73 loading.value = true
74
75 Api.sigma
76 .setQueryActive(query.value.rule_name, model.value.active)
77 .then(res => {
78 if (res.data.success) {
79 if (res.data.sigma_queries[0]) {
80 emit("updated", res.data.sigma_queries[0])
81 }
82 message.success(res.data?.message || "Sigma query updated successfully")
83 } else {
84 message.warning(res.data?.message || "An error occurred. Please try again later.")
85 }
86 })
87 .catch(err => {
88 message.error(err.response?.data?.message || "An error occurred. Please try again later.")
89 })
90 .finally(() => {
91 loading.value = false
92 })
93 }
94 }
95
96 onBeforeMount(() => {
97 setModel()
98 })
99 </script>