fix(usage): keep token/cost detail tooltips inside the viewport

The token and cost detail tooltips in the usage table were always placed
8px to the right of the info icon. On narrow (mobile) screens the cost
column sits near the right edge, so the non-wrapping tooltip was cut off.

After rendering, measure the tooltip: flip it to the left of the icon when
it does not fit on the right, pin it to the right edge when neither side
fits, and clamp it vertically to the viewport.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Yakun WuandClaude Opus 5.5 committed 2026-11-27 15:22:26 +08:00
1 parent e38f8ad784
commit 5d3ec1aa16
2 files changed
+60 -5

No files matched your search

@@ -330,6 +330,7 @@
<Teleport to="body">
<div
v-if="tokenTooltipVisible"
ref="tokenTooltipEl"
class="fixed z-[9999] pointer-events-none -translate-y-1/2"
:style="{
left: tokenTooltipPosition.x + 'px',
@@ -423,6 +424,7 @@
<Teleport to="body">
<div
v-if="tooltipVisible"
ref="tooltipEl"
class="fixed z-[9999] pointer-events-none -translate-y-1/2"
:style="{
left: tooltipPosition.x + 'px',
@@ -558,7 +560,7 @@
</template>
<script setup lang="ts">
import { computed, ref } from 'vue'
import { computed, nextTick, ref, type Ref } from 'vue'
import { useI18n } from 'vue-i18n'
import { useAppStore } from '@/stores/app'
import { formatDateTime, formatReasoningEffort, reasoningEffortValuesEqual } from '@/utils/format'
@@ -718,11 +720,13 @@ const copyUpstreamRequestId = (upstreamRequestId: string) =>
// Tooltip state - cost
const tooltipVisible = ref(false)
const tooltipPosition = ref({ x: 0, y: 0 })
const tooltipEl = ref<HTMLElement | null>(null)
const tooltipData = ref<AdminUsageLog | null>(null)
// Tooltip state - token
const tokenTooltipVisible = ref(false)
const tokenTooltipPosition = ref({ x: 0, y: 0 })
const tokenTooltipEl = ref<HTMLElement | null>(null)
const tokenTooltipData = ref<AdminUsageLog | null>(null)
const getRequestTypeLabel = (row: AdminUsageLog): string => {
@@ -761,14 +765,36 @@ const formatDuration = (ms: number | null | undefined): string => {
return `${Math.floor(totalSec / 3600)}h ${Math.floor((totalSec % 3600) / 60)}m`
}
// 浮层默认放在图标右侧;右侧放不下时翻到左侧,两侧都放不下(手机窄屏)就贴视口右边缘;上下也夹在视口内
const TOOLTIP_GAP = 8
const placeTooltip = async (
rect: DOMRect,
tipEl: Ref<HTMLElement | null>,
position: Ref<{ x: number; y: number }>
) => {
position.value = { x: rect.right + TOOLTIP_GAP, y: rect.top + rect.height / 2 }
await nextTick()
if (!tipEl.value) return
const { width, height } = tipEl.value.getBoundingClientRect()
const viewportWidth = document.documentElement.clientWidth || window.innerWidth
const viewportHeight = window.innerHeight
let x = position.value.x
if (x + width > viewportWidth - TOOLTIP_GAP) {
x = rect.left - TOOLTIP_GAP - width
if (x < TOOLTIP_GAP) x = Math.max(TOOLTIP_GAP, viewportWidth - TOOLTIP_GAP - width)
}
const halfHeight = height / 2
const y = Math.max(TOOLTIP_GAP + halfHeight, Math.min(position.value.y, viewportHeight - TOOLTIP_GAP - halfHeight))
position.value = { x, y }
}
// Cost tooltip functions
const showTooltip = (event: MouseEvent, row: AdminUsageLog) => {
const target = event.currentTarget as HTMLElement
const rect = target.getBoundingClientRect()
tooltipData.value = row
tooltipPosition.value.x = rect.right + 8
tooltipPosition.value.y = rect.top + rect.height / 2
tooltipVisible.value = true
placeTooltip(rect, tooltipEl, tooltipPosition)
}
const hideTooltip = () => {
@@ -781,9 +807,8 @@ const showTokenTooltip = (event: MouseEvent, row: AdminUsageLog) => {
const target = event.currentTarget as HTMLElement
const rect = target.getBoundingClientRect()
tokenTooltipData.value = row
tokenTooltipPosition.value.x = rect.right + 8
tokenTooltipPosition.value.y = rect.top + rect.height / 2
tokenTooltipVisible.value = true
placeTooltip(rect, tokenTooltipEl, tokenTooltipPosition)
}
const hideTokenTooltip = () => {
@@ -305,6 +305,36 @@ describe('admin UsageTable tooltip', () => {
wrapper.unmount()
})
it('keeps the cost tooltip inside a narrow mobile viewport', async () => {
const originalWidth = window.innerWidth
const rectSpy = vi.spyOn(HTMLElement.prototype, 'getBoundingClientRect').mockImplementation(function (this: HTMLElement) {
// 浮层宽 260,触发图标贴在 375px 视口右侧
if (this.classList.contains('fixed')) return { left: 0, top: 0, right: 260, bottom: 200, width: 260, height: 200 } as DOMRect
return { left: 340, top: 100, right: 356, bottom: 116, width: 16, height: 16 } as DOMRect
})
Object.defineProperty(window, 'innerWidth', { configurable: true, value: 375 })
const wrapper = mount(UsageTable, {
props: { data: [{ ...baseImageRow, billing_mode: 'token', image_count: 0, input_cost: 0.01, input_tokens: 100 }], loading: false, columns: [] },
global: { stubs: { DataTable: DataTableStub, EmptyState: true, Icon: true, Teleport: true } },
})
try {
const triggers = wrapper.findAll('.group.relative')
await triggers[triggers.length - 1].trigger('mouseenter')
await nextTick()
await nextTick()
const style = wrapper.get('.fixed').attributes('style') ?? ''
const left = Number(style.match(/left:\s*([\d.]+)px/)?.[1])
const top = Number(style.match(/top:\s*([\d.]+)px/)?.[1])
expect(left).toBe(340 - 8 - 260)
expect(left + 260).toBeLessThanOrEqual(375)
expect(top).toBe(108)
} finally {
wrapper.unmount()
rectSpy.mockRestore()
Object.defineProperty(window, 'innerWidth', { configurable: true, value: originalWidth })
}
})
it('shows service tier and billing breakdown in cost tooltip', async () => {
const row = {
request_id: 'req-admin-1',