|
| 1 | +<script setup lang="ts"> |
| 2 | +import { useWebSocketInspectorStore } from '@proj-airi/stage-ui/stores/devtools/websocket-inspector' |
| 3 | +import { Button, FieldCheckbox, Input } from '@proj-airi/ui' |
| 4 | +import { computed, nextTick, ref, watch } from 'vue' |
| 5 | +
|
| 6 | +const store = useWebSocketInspectorStore() |
| 7 | +
|
| 8 | +const filter = ref('') |
| 9 | +const showIncoming = ref(true) |
| 10 | +const showOutgoing = ref(true) |
| 11 | +const showHeartbeats = ref(true) |
| 12 | +const streamContainer = ref<HTMLDivElement>() |
| 13 | +const showingDetails = ref<string>('') |
| 14 | +const filteredHistory = computed(() => { |
| 15 | + return store.history.filter((item) => { |
| 16 | + if (!showIncoming.value && item.direction === 'incoming') |
| 17 | + return false |
| 18 | + if (!showOutgoing.value && item.direction === 'outgoing') |
| 19 | + return false |
| 20 | + if (!showHeartbeats.value && item.event.type === 'transport:connection:heartbeat') |
| 21 | + return false |
| 22 | + if (filter.value && !JSON.stringify(item.event).toLowerCase().includes(filter.value.toLowerCase())) |
| 23 | + return false |
| 24 | + return true |
| 25 | + }) |
| 26 | +}) |
| 27 | +
|
| 28 | +function formatTime(ts: number) { |
| 29 | + return new Date(ts).toLocaleTimeString() |
| 30 | +} |
| 31 | +
|
| 32 | +async function scrollToTop() { |
| 33 | + await nextTick() |
| 34 | + if (streamContainer.value) |
| 35 | + streamContainer.value.scrollTop = 0 |
| 36 | +} |
| 37 | +
|
| 38 | +watch(() => filteredHistory.value.length, scrollToTop) |
| 39 | +
|
| 40 | +const directionBadgeClassMap: Record<'incoming' | 'outgoing', string[]> = { |
| 41 | + incoming: [ |
| 42 | + 'bg-pink-100 dark:bg-pink-900', |
| 43 | + 'text-pink-600', |
| 44 | + 'dark:text-pink-300', |
| 45 | + 'border-pink-300 dark:border-pink-900', |
| 46 | + ], |
| 47 | + outgoing: [ |
| 48 | + 'bg-blue-100 dark:bg-blue-900', |
| 49 | + 'text-blue-600', |
| 50 | + 'dark:text-blue-300', |
| 51 | + 'border-blue-300 dark:border-blue-900', |
| 52 | + ], |
| 53 | +} |
| 54 | +
|
| 55 | +const directionIconClassMap: Record<'incoming' | 'outgoing', string> = { |
| 56 | + incoming: 'i-solar:arrow-down-linear', |
| 57 | + outgoing: 'i-solar:arrow-up-linear', |
| 58 | +} |
| 59 | +
|
| 60 | +function directionBadgeClasses(direction: 'incoming' | 'outgoing') { |
| 61 | + return directionBadgeClassMap[direction] |
| 62 | +} |
| 63 | +
|
| 64 | +function directionIconClass(direction: 'incoming' | 'outgoing') { |
| 65 | + return directionIconClassMap[direction] |
| 66 | +} |
| 67 | +
|
| 68 | +const cardClassMap: Record<'incoming' | 'outgoing', string[]> = { |
| 69 | + incoming: [ |
| 70 | + 'bg-pink-50', |
| 71 | + 'dark:bg-pink-950', |
| 72 | + 'border-pink-300', |
| 73 | + 'text-pink-700', |
| 74 | + 'dark:border-pink-800', |
| 75 | + 'dark:text-pink-100', |
| 76 | + ], |
| 77 | + outgoing: [ |
| 78 | + 'bg-blue-50', |
| 79 | + 'dark:bg-blue-950', |
| 80 | + 'border-blue-300', |
| 81 | + 'text-blue-700', |
| 82 | + 'dark:border-blue-800', |
| 83 | + 'dark:text-blue-100', |
| 84 | + ], |
| 85 | +} |
| 86 | +
|
| 87 | +function cardClasses(direction: 'incoming' | 'outgoing') { |
| 88 | + return cardClassMap[direction] |
| 89 | +} |
| 90 | +
|
| 91 | +const payloadClassMap: Record<'incoming' | 'outgoing', string[]> = { |
| 92 | + incoming: [ |
| 93 | + 'bg-pink-100', |
| 94 | + 'border-pink-300 border-1 border-solid', |
| 95 | + 'dark:bg-pink-900', |
| 96 | + 'dark:border-pink-700', |
| 97 | + ], |
| 98 | + outgoing: [ |
| 99 | + 'bg-blue-100', |
| 100 | + 'border-blue-300 border-1 border-solid', |
| 101 | + 'dark:bg-blue-900', |
| 102 | + 'dark:border-blue-700', |
| 103 | + ], |
| 104 | +} |
| 105 | +
|
| 106 | +function payloadClasses(direction: 'incoming' | 'outgoing') { |
| 107 | + return payloadClassMap[direction] |
| 108 | +} |
| 109 | +</script> |
| 110 | + |
| 111 | +<template> |
| 112 | + <div class="h-full flex flex-col gap-4 overflow-hidden p-4"> |
| 113 | + <!-- Header / Filters --> |
| 114 | + <div class="flex flex-col gap-4 rounded-xl bg-neutral-50 p-4 dark:bg-[rgba(0,0,0,0.3)]"> |
| 115 | + <div class="flex items-center gap-2"> |
| 116 | + <FieldCheckbox v-model="showIncoming" label="Incoming" /> |
| 117 | + <FieldCheckbox v-model="showOutgoing" label="Outgoing" /> |
| 118 | + <FieldCheckbox v-model="showHeartbeats" label="Heartbeats" /> |
| 119 | + </div> |
| 120 | + |
| 121 | + <div class="flex gap-2"> |
| 122 | + <Input |
| 123 | + v-model="filter" |
| 124 | + placeholder="Filter payload..." |
| 125 | + class="w-64" |
| 126 | + /> |
| 127 | + <Button |
| 128 | + label="Clear" |
| 129 | + icon="i-solar:trash-bin-trash-bold-duotone" |
| 130 | + size="sm" |
| 131 | + variant="ghost" |
| 132 | + @click="store.clear()" |
| 133 | + /> |
| 134 | + <div class="flex flex-shrink-0 items-center text-xs"> |
| 135 | + {{ filteredHistory.length }} / {{ store.history.length }} |
| 136 | + </div> |
| 137 | + </div> |
| 138 | + </div> |
| 139 | + |
| 140 | + <!-- Stream --> |
| 141 | + <div |
| 142 | + ref="streamContainer" |
| 143 | + class="flex-1 overflow-y-auto rounded-xl bg-white/70 dark:bg-neutral-950/50" |
| 144 | + > |
| 145 | + <div |
| 146 | + v-if="filteredHistory.length === 0" |
| 147 | + class="h-full w-full flex justify-center p-3 text-sm" |
| 148 | + > |
| 149 | + No messages found. |
| 150 | + </div> |
| 151 | + <div v-else class="grid gap-3"> |
| 152 | + <div |
| 153 | + v-for="item in filteredHistory" |
| 154 | + :key="item.id" |
| 155 | + class="border rounded-xl p-4 transition-colors" |
| 156 | + :class="cardClasses(item.direction)" |
| 157 | + > |
| 158 | + <div class="flex items-start justify-between gap-3"> |
| 159 | + <div class="flex flex-wrap items-center gap-2 text-sm"> |
| 160 | + <span :class="['rounded-full', 'border', 'px-2', 'py-0.5', 'text-xs', 'flex', 'items-center', 'justify-center', ...directionBadgeClasses(item.direction)]"> |
| 161 | + <span :class="['size-3.5', directionIconClass(item.direction)]" :aria-label="item.direction" /> |
| 162 | + <span class="ml-1 font-bold tracking-wider uppercase">{{ item.direction }}</span> |
| 163 | + </span> |
| 164 | + <span class="font-semibold"> |
| 165 | + {{ item.event.type }} |
| 166 | + </span> |
| 167 | + </div> |
| 168 | + <span class="text-sm font-mono"> |
| 169 | + {{ formatTime(item.timestamp) }} |
| 170 | + </span> |
| 171 | + </div> |
| 172 | + |
| 173 | + <details class="group mt-2" :open="showingDetails === item.id"> |
| 174 | + <summary class="cursor-pointer select-none text-sm font-medium" @click="showingDetails = showingDetails === item.id ? '' : item.id"> |
| 175 | + Payload |
| 176 | + </summary> |
| 177 | + <pre |
| 178 | + class="mt-2 w-full overflow-auto whitespace-pre-wrap rounded-lg p-3 text-sm" |
| 179 | + :class="payloadClasses(item.direction)" |
| 180 | + >{{ JSON.stringify(item.event, null, 2) }}</pre> |
| 181 | + </details> |
| 182 | + </div> |
| 183 | + </div> |
| 184 | + </div> |
| 185 | + </div> |
| 186 | +</template> |
| 187 | + |
| 188 | +<route lang="yaml"> |
| 189 | +meta: |
| 190 | + layout: settings |
| 191 | +</route> |
0 commit comments