Log.vue 1.8 KB

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