| 1 | <template> |
| 2 | <n-spin :show="loading"> |
| 3 | <div class="flex items-center justify-end gap-2"> |
| 4 | <div class="flex grow gap-5"> |
| 5 | <n-popover overlap placement="bottom-start"> |
| 6 | <template #trigger> |
| 7 | <div class="bg-default rounded-lg"> |
| 8 | <n-button size="small" class="cursor-help!"> |
| 9 | <template #icon> |
| 10 | <Icon :name="InfoIcon" /> |
| 11 | </template> |
| 12 | </n-button> |
| 13 | </div> |
| 14 | </template> |
| 15 | <div class="flex flex-col gap-2"> |
| 16 | <div class="box"> |
| 17 | Total: |
| 18 | <code>{{ total }}</code> |
| 19 | </div> |
| 20 | </div> |
| 21 | </n-popover> |
| 22 | </div> |
| 23 | <n-pagination v-model:page="currentPage" :page-size :item-count="total" :page-slot="5" /> |
| 24 | </div> |
| 25 | <div class="my-3 flex min-h-52 flex-col gap-2"> |
| 26 | <template v-if="messages.length"> |
| 27 | <MessageItem v-for="msg of messages" :key="msg.id" :message="msg" /> |
| 28 | </template> |
| 29 | <template v-else> |
| 30 | <n-empty v-if="!loading" description="No items found" class="h-48 justify-center" /> |
| 31 | </template> |
| 32 | </div> |
| 33 | <div class="flex justify-end"> |
| 34 | <n-pagination |
| 35 | v-if="messages.length > 3" |
| 36 | v-model:page="currentPage" |
| 37 | :page-size |
| 38 | :item-count="total" |
| 39 | :page-slot="6" |
| 40 | /> |
| 41 | </div> |
| 42 | </n-spin> |
| 43 | </template> |
| 44 | |
| 45 | <script setup lang="ts"> |
| 46 | import type { MessageExtended } from "@/types/graylog/messages.d" |
| 47 | import { NButton, NEmpty, NPagination, NPopover, NSpin, useMessage } from "naive-ui" |
| 48 | import { nanoid } from "nanoid" |
| 49 | import { onBeforeMount, ref, watch } from "vue" |
| 50 | import Api from "@/api" |
| 51 | import Icon from "@/components/common/Icon.vue" |
| 52 | import MessageItem from "./Item.vue" |
| 53 | |
| 54 | const InfoIcon = "carbon:information" |
| 55 | |
| 56 | const message = useMessage() |
| 57 | const loading = ref(false) |
| 58 | const messages = ref<MessageExtended[]>([]) |
| 59 | const total = ref(0) |
| 60 | const pageSize = ref(1) |
| 61 | const currentPage = ref(1) |
| 62 | |
| 63 | function getData(page: number) { |
| 64 | loading.value = true |
| 65 | |
| 66 | Api.graylog |
| 67 | .getMessages(page) |
| 68 | .then(res => { |
| 69 | if (res.data.success) { |
| 70 | const data = (res.data.graylog_messages || []) as MessageExtended[] |
| 71 | messages.value = data.map(o => { |
| 72 | o.id = nanoid() |
| 73 | return o |
| 74 | }) |
| 75 | total.value = res.data.total_messages || 0 |
| 76 | if (pageSize.value <= 1) pageSize.value = messages.value.length |
| 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 | watch(currentPage, val => { |
| 90 | getData(val) |
| 91 | }) |
| 92 | |
| 93 | onBeforeMount(() => { |
| 94 | getData(currentPage.value) |
| 95 | }) |
| 96 | </script> |