Skip to content

Commit 28e36f7

Browse files
authored
Merge pull request Wei-Shaw#1096 from Ethan0x0000/pr/fix-idle-usage-windows
fix(ui): 会话窗口空闲时显示“现在”,避免重置时间缺失
2 parents 6c02076 + 2005fc9 commit 28e36f7

3 files changed

Lines changed: 84 additions & 1 deletion

File tree

frontend/src/components/account/AccountUsageCell.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
:utilization="usageInfo.five_hour.utilization"
8383
:resets-at="usageInfo.five_hour.resets_at"
8484
:window-stats="usageInfo.five_hour.window_stats"
85+
:show-now-when-idle="true"
8586
color="indigo"
8687
/>
8788
<UsageProgressBar
@@ -90,6 +91,7 @@
9091
:utilization="usageInfo.seven_day.utilization"
9192
:resets-at="usageInfo.seven_day.resets_at"
9293
:window-stats="usageInfo.seven_day.window_stats"
94+
:show-now-when-idle="true"
9395
color="emerald"
9496
/>
9597
</div>

frontend/src/components/account/UsageProgressBar.vue

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
</span>
4949

5050
<!-- Reset time -->
51-
<span v-if="resetsAt" class="shrink-0 text-[10px] text-gray-400">
51+
<span v-if="shouldShowResetTime" class="shrink-0 text-[10px] text-gray-400">
5252
{{ formatResetTime }}
5353
</span>
5454
</div>
@@ -68,6 +68,7 @@ const props = defineProps<{
6868
resetsAt?: string | null
6969
color: 'indigo' | 'emerald' | 'purple' | 'amber'
7070
windowStats?: WindowStats | null
71+
showNowWhenIdle?: boolean
7172
}>()
7273
7374
const { t } = useI18n()
@@ -139,9 +140,20 @@ const displayPercent = computed(() => {
139140
return percent > 999 ? '>999%' : `${percent}%`
140141
})
141142
143+
const shouldShowResetTime = computed(() => {
144+
if (props.resetsAt) return true
145+
return Boolean(props.showNowWhenIdle && props.utilization <= 0)
146+
})
147+
142148
// Format reset time
143149
const formatResetTime = computed(() => {
150+
// For rolling windows, when utilization is 0%, treat as immediately available.
151+
if (props.showNowWhenIdle && props.utilization <= 0) {
152+
return '现在'
153+
}
154+
144155
if (!props.resetsAt) return '-'
156+
145157
const date = new Date(props.resetsAt)
146158
const diffMs = date.getTime() - now.value.getTime()
147159
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2+
import { mount } from '@vue/test-utils'
3+
import UsageProgressBar from '../UsageProgressBar.vue'
4+
5+
vi.mock('vue-i18n', async () => {
6+
const actual = await vi.importActual<typeof import('vue-i18n')>('vue-i18n')
7+
return {
8+
...actual,
9+
useI18n: () => ({
10+
t: (key: string) => key
11+
})
12+
}
13+
})
14+
15+
describe('UsageProgressBar', () => {
16+
beforeEach(() => {
17+
vi.useFakeTimers()
18+
vi.setSystemTime(new Date('2026-03-17T00:00:00Z'))
19+
})
20+
21+
afterEach(() => {
22+
vi.useRealTimers()
23+
})
24+
25+
it('showNowWhenIdle=true 且利用率为 0 时显示“现在”', () => {
26+
const wrapper = mount(UsageProgressBar, {
27+
props: {
28+
label: '5h',
29+
utilization: 0,
30+
resetsAt: '2026-03-17T02:30:00Z',
31+
showNowWhenIdle: true,
32+
color: 'indigo'
33+
}
34+
})
35+
36+
expect(wrapper.text()).toContain('现在')
37+
expect(wrapper.text()).not.toContain('2h 30m')
38+
})
39+
40+
it('showNowWhenIdle=true 但利用率大于 0 时显示倒计时', () => {
41+
const wrapper = mount(UsageProgressBar, {
42+
props: {
43+
label: '7d',
44+
utilization: 12,
45+
resetsAt: '2026-03-17T02:30:00Z',
46+
showNowWhenIdle: true,
47+
color: 'emerald'
48+
}
49+
})
50+
51+
expect(wrapper.text()).toContain('2h 30m')
52+
expect(wrapper.text()).not.toContain('现在')
53+
})
54+
55+
it('showNowWhenIdle=false 时保持原有倒计时行为', () => {
56+
const wrapper = mount(UsageProgressBar, {
57+
props: {
58+
label: '1d',
59+
utilization: 0,
60+
resetsAt: '2026-03-17T02:30:00Z',
61+
showNowWhenIdle: false,
62+
color: 'indigo'
63+
}
64+
})
65+
66+
expect(wrapper.text()).toContain('2h 30m')
67+
expect(wrapper.text()).not.toContain('现在')
68+
})
69+
})

0 commit comments

Comments
 (0)