-
-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathhmr.vue
More file actions
358 lines (333 loc) · 10.7 KB
/
Copy pathhmr.vue
File metadata and controls
358 lines (333 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
<script setup lang="ts">
import type { HmrUpdate } from '../../shared/types'
import { useAsyncState, useIntervalFn } from '@vueuse/core'
import { computed, nextTick, ref, watch } from 'vue'
import { useRpc } from '#imports'
const rpc = useRpc()
const filter = ref('')
const liveMode = ref(true)
const listRef = ref<HTMLElement>()
const { state: updates, isLoading, execute: refresh } = useAsyncState(
() => rpc.value.call('vite:hmr-updates') as Promise<HmrUpdate[]>,
[] as HmrUpdate[],
{ resetOnExecute: false },
)
useIntervalFn(refresh, 1000)
// Auto-scroll to top only when new updates arrive AND user is already near the top
const lastSeenId = ref<string | null>(null)
const missedUpdates = ref(0)
watch(() => updates.value?.[0]?.id, async (newestId) => {
if (!newestId || newestId === lastSeenId.value)
return
lastSeenId.value = newestId
if (!liveMode.value || !listRef.value) {
missedUpdates.value++
return
}
// Only auto-scroll if user is already near the top (within 100px)
if (listRef.value.scrollTop <= 100) {
await nextTick()
listRef.value!.scrollTop = 0
}
else {
missedUpdates.value++
}
})
function scrollToTop() {
if (listRef.value) {
listRef.value.scrollTop = 0
missedUpdates.value = 0
}
}
function onListScroll() {
if (listRef.value && listRef.value.scrollTop <= 100) {
missedUpdates.value = 0
}
}
interface GroupedUpdate {
key: string
file: string
updates: typeof updates.value
count: number
latestTimestamp: number
}
const filteredUpdates = computed(() => {
if (!filter.value)
return updates.value ?? []
const q = filter.value.toLowerCase()
return (updates.value ?? []).filter(u =>
u.files.some(f => f.toLowerCase().includes(q))
|| u.modules.some(m => m.toLowerCase().includes(q)),
)
})
const groupedByFile = computed<GroupedUpdate[]>(() => {
const groups = new Map<string, GroupedUpdate>()
for (const update of filteredUpdates.value) {
const file = update.files[0] ?? 'unknown'
const existing = groups.get(file)
if (existing) {
existing.updates.push(update)
existing.count++
if (update.timestamp > existing.latestTimestamp) {
existing.latestTimestamp = update.timestamp
}
}
else {
groups.set(file, {
key: file,
file,
updates: [update],
count: 1,
latestTimestamp: update.timestamp,
})
}
}
return Array.from(groups.values()).sort((a, b) => b.latestTimestamp - a.latestTimestamp)
})
const viewMode = ref<'timeline' | 'grouped'>('timeline')
async function clearHistory() {
await rpc.value.call('vite:hmr-clear')
await refresh()
}
function openInEditor(file: string) {
rpc.value.call('vite:core:open-in-editor', file)
}
function formatTime(timestamp: number) {
return new Date(timestamp).toLocaleTimeString()
}
function timeAgo(timestamp: number) {
const diff = Date.now() - timestamp
if (diff < 1000)
return 'just now'
if (diff < 60000)
return `${Math.floor(diff / 1000)}s ago`
if (diff < 3600000)
return `${Math.floor(diff / 60000)}m ago`
return formatTime(timestamp)
}
function getRelativePath(file: string) {
return file.replace(/^.*\//, '')
}
</script>
<template>
<div p4 flex="~ col gap-4" h-full of-hidden>
<!-- Header -->
<div flex="~ items-center gap-2 sm:gap-3 flex-wrap">
<h1 text-base sm:text-lg font-semibold flex="~ items-center gap-2">
<div class="i-ph-lightning-duotone" text-xl />
<span>HMR Inspector</span>
</h1>
<div flex-1 />
<!-- View mode toggle -->
<div flex="~ items-center" border="~ base rounded" text-sm>
<button
v-tooltip="'Timeline view'"
px2 py1
:class="viewMode === 'timeline' ? 'bg-active' : 'hover:bg-active/50'"
@click="viewMode = 'timeline'"
>
<div class="i-ph-list" />
</button>
<button
v-tooltip="'Grouped by file'"
px2 py1
:class="viewMode === 'grouped' ? 'bg-active' : 'hover:bg-active/50'"
@click="viewMode = 'grouped'"
>
<div class="i-ph-stack" />
</button>
</div>
<input
v-model="filter"
placeholder="Filter..."
border="~ base rounded"
px2 py1 text-sm
class="w-32 sm:w-60"
bg-transparent
outline-none
focus:border-primary
>
<!-- Live mode toggle -->
<button
v-tooltip="liveMode ? 'Disable live mode' : 'Enable live mode'"
border="~ base rounded"
px2 py1 text-sm flex="~ items-center gap-1"
:class="liveMode ? 'text-green border-green/50' : 'hover:bg-active'"
@click="liveMode = !liveMode"
>
<div class="i-ph-broadcast" :class="liveMode ? 'animate-pulse' : ''" />
Live
</button>
<button
v-tooltip="'Clear history'"
border="~ base rounded"
px2 py1 text-sm
hover:bg-active
@click="clearHistory"
>
<div class="i-ph-trash" />
</button>
<button
v-tooltip="'Refresh'"
border="~ base rounded"
px2 py1 text-sm
hover:bg-active
@click="refresh()"
>
<div class="i-ph-arrow-clockwise" />
</button>
</div>
<!-- Empty states -->
<div v-if="isLoading && !updates?.length" flex-1 flex items-center justify-center>
<div op50>
Loading HMR history...
</div>
</div>
<div v-else-if="!filteredUpdates.length" flex-1 flex items-center justify-center>
<div op50 flex="~ col items-center gap-2">
<div class="i-ph-lightning-duotone" text-4xl />
<span>No HMR updates yet</span>
<span text-xs>Edit a file to see updates appear here</span>
</div>
</div>
<!-- Timeline view -->
<div v-else-if="viewMode === 'timeline'" class="flex-1 of-hidden relative">
<!-- New updates indicator (positioned over the scroll container) -->
<Transition name="slide-down">
<button
v-if="missedUpdates > 0"
absolute top-2 left="50%" z-10
class="-translate-x-50%"
flex="~ items-center gap-1.5"
bg-primary text-white
px3 py1.5 rounded-full
text-xs font-medium
shadow-lg
hover:opacity-90
transition-opacity
@click="scrollToTop"
>
<div class="i-ph-arrow-up" />
{{ missedUpdates }} new update{{ missedUpdates > 1 ? 's' : '' }}
</button>
</Transition>
<div ref="listRef" h-full of-auto @scroll="onListScroll">
<div flex="~ col gap-1">
<div
v-for="update in filteredUpdates"
:key="update.id"
border="~ base rounded"
p2 sm:p3 flex="~ col gap-1"
hover:bg-active
transition-colors
>
<div flex="~ items-center gap-2 flex-wrap">
<div
:class="update.type === 'full-reload' ? 'i-ph-arrow-counter-clockwise text-orange' : 'i-ph-lightning text-green'"
/>
<span text-xs op50 font-mono>
{{ formatTime(update.timestamp) }}
</span>
<span
text-xs px1 rounded
:class="update.type === 'full-reload' ? 'bg-orange/20 text-orange' : 'bg-green/20 text-green'"
>
{{ update.type === 'full-reload' ? 'Full Reload' : 'HMR Update' }}
</span>
<div flex-1 />
<span text-xs op30 hidden sm:inline>{{ timeAgo(update.timestamp) }}</span>
</div>
<div pl5 flex="~ col gap-0.5" of-hidden>
<div
v-for="file in update.files"
:key="file"
flex="~ items-center gap-1"
text-sm group
of-hidden
>
<div class="i-ph-file-duotone shrink-0" text-xs op50 />
<span font-mono truncate :title="file">{{ getRelativePath(file) }}</span>
<button
v-tooltip="'Open in editor'"
class="i-ph-pencil-simple shrink-0 op40 hover:op100"
text-xs
@click.stop="openInEditor(file)"
/>
</div>
<div
v-for="mod in update.modules"
:key="mod"
flex="~ items-center gap-1"
text-xs op40
of-hidden
>
<div class="i-ph-cube-duotone shrink-0" text-xs />
<span font-mono truncate :title="mod">{{ mod }}</span>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Grouped view -->
<div v-else ref="listRef" flex-1 of-auto>
<div flex="~ col gap-2">
<div
v-for="group in groupedByFile"
:key="group.key"
border="~ base rounded"
p2 sm:p3 flex="~ col gap-2"
hover:bg-active
transition-colors
>
<div flex="~ items-center gap-2" of-hidden>
<div class="i-ph-file-duotone shrink-0" op60 />
<span font-mono text-sm truncate :title="group.file">{{ getRelativePath(group.file) }}</span>
<button
v-tooltip="'Open in editor'"
class="i-ph-pencil-simple shrink-0 op40 hover:op100"
text-xs
@click.stop="openInEditor(group.file)"
/>
<div flex-1 />
<span
text-xs px1.5 py0.5 rounded shrink-0
class="bg-primary:15 text-primary"
font-mono
>
{{ group.count }}x
</span>
<span text-xs op40 shrink-0 hidden sm:inline>{{ timeAgo(group.latestTimestamp) }}</span>
</div>
<div pl5 flex="~ gap-1 flex-wrap">
<span
v-for="update in group.updates.slice(0, 10)"
:key="update.id"
text-xs op40 font-mono
border="~ base rounded"
px1
>
{{ formatTime(update.timestamp) }}
</span>
<span v-if="group.count > 10" text-xs op30>
+{{ group.count - 10 }} more
</span>
</div>
</div>
</div>
</div>
<!-- Footer -->
<div border="t base" pt2 text-xs op50 flex="~ items-center gap-2">
<div class="i-ph-info" />
<span>{{ filteredUpdates.length }} update(s)</span>
<span v-if="viewMode === 'grouped'" op30>
· {{ groupedByFile.length }} file(s)
</span>
<div flex-1 />
<span v-if="liveMode" class="text-green" flex="~ items-center gap-1">
<div class="i-ph-circle-fill animate-pulse" text-xs />
<span hidden sm:inline>Live</span>
</span>
</div>
</div>
</template>