Log.vue 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <template>
  2. <div class="flex flex-col">
  3. <div
  4. class="bg-primary border-b border-dividerLight flex flex-1 pl-4 top-0 z-10 sticky items-center justify-between"
  5. >
  6. <label for="log" class="font-semibold text-secondaryLight py-2">
  7. {{ title }}
  8. </label>
  9. </div>
  10. <div ref="logsRef" name="log" class="realtime-log">
  11. <span v-if="log" class="space-y-2">
  12. <span
  13. v-for="(entry, index) in log"
  14. :key="`entry-${index}`"
  15. :style="{ color: entry.color }"
  16. >{{ entry.ts }}{{ source(entry.source) }}{{ entry.payload }}</span
  17. >
  18. </span>
  19. <span v-else>{{ t("response.waiting_for_connection") }}</span>
  20. </div>
  21. </div>
  22. </template>
  23. <script setup lang="ts">
  24. import { nextTick, ref, watch } from "@nuxtjs/composition-api"
  25. import { getSourcePrefix as source } from "~/helpers/utils/string"
  26. import { useI18n } from "~/helpers/utils/composables"
  27. const t = useI18n()
  28. const props = defineProps({
  29. log: { type: Array, default: () => [] },
  30. title: {
  31. type: String,
  32. default: "",
  33. },
  34. })
  35. const logsRef = ref<any | null>(null)
  36. const BOTTOM_SCROLL_DIST_INNACURACY = 5
  37. watch(
  38. () => props.log,
  39. () => {
  40. if (!logsRef.value) return
  41. const distToBottom =
  42. logsRef.value.scrollHeight -
  43. logsRef.value.scrollTop -
  44. logsRef.value.clientHeight
  45. if (distToBottom < BOTTOM_SCROLL_DIST_INNACURACY) {
  46. nextTick(() => (logsRef.value.scrollTop = logsRef.value.scrollHeight))
  47. }
  48. }
  49. )
  50. </script>
  51. <style scoped lang="scss">
  52. .realtime-log {
  53. @apply p-4;
  54. @apply bg-transparent;
  55. @apply text-secondary;
  56. @apply overflow-auto;
  57. height: 256px;
  58. &,
  59. span {
  60. @apply select-text;
  61. }
  62. span {
  63. @apply block;
  64. @apply break-words break-all;
  65. }
  66. }
  67. </style>