From 5d3ec1aa16378c3ab61246049801faed73bcf65c Mon Sep 17 00:00:00 2001 From: Yakun Wu <195412331+yakwu@users.noreply.github.com> Date: Thu, 12 Nov 2026 14:01:20 +0800 Subject: [PATCH] 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 --- .../src/components/admin/usage/UsageTable.vue | 35 ++++++++++++++++--- .../admin/usage/__tests__/UsageTable.spec.ts | 30 ++++++++++++++++ 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/frontend/src/components/admin/usage/UsageTable.vue b/frontend/src/components/admin/usage/UsageTable.vue index 59a8e68ad2..2a9ead8856 100644 --- a/frontend/src/components/admin/usage/UsageTable.vue +++ b/frontend/src/components/admin/usage/UsageTable.vue @@ -330,6 +330,7 @@
-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(null) const tooltipData = ref(null) // Tooltip state - token const tokenTooltipVisible = ref(false) const tokenTooltipPosition = ref({ x: 0, y: 0 }) +const tokenTooltipEl = ref(null) const tokenTooltipData = ref(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, + 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 = () => { diff --git a/frontend/src/components/admin/usage/__tests__/UsageTable.spec.ts b/frontend/src/components/admin/usage/__tests__/UsageTable.spec.ts index 4c55953454..c62c3cfa77 100644 --- a/frontend/src/components/admin/usage/__tests__/UsageTable.spec.ts +++ b/frontend/src/components/admin/usage/__tests__/UsageTable.spec.ts @@ -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',