| 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 max-w-80 flex-col gap-2 py-1"> |
| 8 | <n-upload |
| 9 | v-model:file-list="fileList" |
| 10 | :max="1" |
| 11 | accept="application/x-yaml, .yaml, .YAML, .yml, .YML" |
| 12 | :disabled="loading" |
| 13 | > |
| 14 | <n-upload-dragger> |
| 15 | <div> |
| 16 | <Icon :name="UploadIcon" :size="28" :depth="3" /> |
| 17 | </div> |
| 18 | <div class="font-semibold">Click or drag a file to this area to upload</div> |
| 19 | <p class="mt-2">Only .yaml files are accepted</p> |
| 20 | </n-upload-dragger> |
| 21 | </n-upload> |
| 22 | |
| 23 | <div class="flex justify-between gap-2"> |
| 24 | <n-button quaternary size="small" @click="closePopup()">Close</n-button> |
| 25 | <n-button :disabled="!isValid" :loading type="primary" size="small" @click="uploadQueries()"> |
| 26 | Upload |
| 27 | </n-button> |
| 28 | </div> |
| 29 | </div> |
| 30 | </n-popover> |
| 31 | </template> |
| 32 | |
| 33 | <script setup lang="ts"> |
| 34 | import type { UploadFileInfo } from "naive-ui" |
| 35 | import { NButton, NPopover, NUpload, NUploadDragger, useMessage } from "naive-ui" |
| 36 | import { computed, ref } from "vue" |
| 37 | import Api from "@/api" |
| 38 | import Icon from "@/components/common/Icon.vue" |
| 39 | |
| 40 | const emit = defineEmits<{ |
| 41 | (e: "updated"): void |
| 42 | }>() |
| 43 | |
| 44 | const loading = defineModel<boolean | undefined>("loading", { default: false }) |
| 45 | |
| 46 | const UploadIcon = "carbon:document-add" |
| 47 | |
| 48 | const show = ref(false) |
| 49 | const lastShow = ref(Date.now()) |
| 50 | const message = useMessage() |
| 51 | const fileList = ref<UploadFileInfo[]>([]) |
| 52 | const yamlFile = computed<File | null>(() => fileList.value?.[0]?.file || null) |
| 53 | const isValid = computed(() => fileList.value.length) |
| 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 uploadQueries() { |
| 67 | loading.value = true |
| 68 | |
| 69 | if (yamlFile.value) { |
| 70 | Api.sigma |
| 71 | .uploadRulesFile(yamlFile.value) |
| 72 | .then(res => { |
| 73 | if (res.data.success) { |
| 74 | emit("updated") |
| 75 | fileList.value = [] |
| 76 | message.success(res.data?.message || "Successfully uploaded the Sigma queries to the database") |
| 77 | } else { |
| 78 | message.warning(res.data?.message || "An error occurred. Please try again later.") |
| 79 | } |
| 80 | }) |
| 81 | .catch(err => { |
| 82 | message.error(err.response?.data?.message || "An error occurred. Please try again later.") |
| 83 | }) |
| 84 | .finally(() => { |
| 85 | loading.value = false |
| 86 | }) |
| 87 | } |
| 88 | } |
| 89 | </script> |