Skip to content

Commit f84b0a7

Browse files
authored
feat: scroll to and select log entry when opened from toast (#284)
1 parent 5d11ef7 commit f84b0a7

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

packages/core/src/client/webcomponents/components/display/ToastOverlay.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script setup lang="ts">
22
import type { DocksContext } from '@vitejs/devtools-kit/client'
3-
import { useLogs } from '../../state/logs'
3+
import { selectLog, useLogs } from '../../state/logs'
44
import { dismissToast, useToasts } from '../../state/toasts'
55
import LogItem from '../log/LogItem.vue'
66
@@ -17,6 +17,7 @@ const toasts = useToasts()
1717
1818
function openLogs(toastId: string) {
1919
dismissToast(toastId)
20+
selectLog(toastId)
2021
props.context?.docks.switchEntry('~logs')
2122
}
2223
</script>

packages/core/src/client/webcomponents/components/views-builtin/ViewBuiltinLogs.vue

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<script setup lang="ts">
22
import type { DevToolsLogEntry, DevToolsLogEntryFrom, DevToolsLogLevel } from '@vitejs/devtools-kit'
33
import type { DocksContext } from '@vitejs/devtools-kit/client'
4-
import { useClipboard, useTimeAgo } from '@vueuse/core'
5-
import { computed, onMounted, ref, watch } from 'vue'
4+
import { useClipboard, useTimeAgo, whenever } from '@vueuse/core'
5+
import { computed, nextTick, onMounted, ref, useTemplateRef, watch } from 'vue'
66
import { markLogsAsRead, useLogs } from '../../state/logs'
77
import FilterToggles from '../display/FilterToggles.vue'
88
import HashBadge from '../display/HashBadge.vue'
@@ -168,6 +168,20 @@ watch(selectedEntry, async (entry) => {
168168
await props.context.rpc.call('devtoolskit:internal:logs:update', entry.id, { autoDelete: 0 })
169169
})
170170
171+
const logListEl = useTemplateRef('logListEl')
172+
173+
whenever(() => logsState.pendingSelectId, async (id) => {
174+
if (!id)
175+
return
176+
177+
logsState.pendingSelectId = null
178+
selectedId.value = id
179+
180+
await nextTick()
181+
logListEl.value?.querySelector<HTMLElement>('[data-selected]')
182+
?.scrollIntoView({ block: 'center', behavior: 'smooth' })
183+
}, { immediate: true })
184+
171185
const selectedTimeAgo = useTimeAgo(computed(() => selectedEntry.value?.timestamp ?? Date.now()))
172186
const { copy: copyStacktrace, copied: stacktraceCopied } = useClipboard()
173187
@@ -306,13 +320,14 @@ onMounted(() => {
306320
<!-- Content -->
307321
<div class="h-full of-hidden" :class="selectedEntry ? 'grid grid-cols-[1fr_1fr]' : ''">
308322
<!-- Log list -->
309-
<div class="h-full of-y-auto">
323+
<div ref="logListEl" class="h-full of-y-auto">
310324
<div v-if="filteredEntries.length === 0" class="flex items-center justify-center h-full op50 text-sm">
311325
No logs
312326
</div>
313327
<div
314328
v-for="entry of filteredEntries"
315329
:key="entry.id"
330+
:data-selected="selectedId === entry.id || undefined"
316331
class="w-full text-left border-b border-base hover:bg-active transition border-l-2 text-sm group cursor-pointer"
317332
:class="[
318333
selectedId === entry.id ? 'bg-active' : '',

packages/core/src/client/webcomponents/state/logs.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { addToast } from './toasts'
77
export interface LogsState {
88
entries: DevToolsLogEntry[]
99
unreadCount: number
10+
pendingSelectId: string | null
1011
}
1112

1213
let _logsState: Reactive<LogsState> | undefined
@@ -18,6 +19,7 @@ export function useLogs(context: DocksContext): Reactive<LogsState> {
1819
const state: Reactive<LogsState> = _logsState = reactive({
1920
entries: [],
2021
unreadCount: 0,
22+
pendingSelectId: null,
2123
})
2224

2325
const entryMap = new Map<string, DevToolsLogEntry>()
@@ -76,3 +78,8 @@ export function markLogsAsRead(): void {
7678
if (_logsState)
7779
_logsState.unreadCount = 0
7880
}
81+
82+
export function selectLog(id: string): void {
83+
if (_logsState)
84+
_logsState.pendingSelectId = id
85+
}

0 commit comments

Comments
 (0)