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',