Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions src/main/webui/src/components/DateRangePicker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@ vi.mock('@easepick/bundle', () => {
toJSDate(): Date {
return this.date
}

getTime(): number {
return this.date.getTime()
}
}

class Picker {
private readonly listeners = new Map<string, (event: any) => void>()
setDateRange = vi.fn()
destroy = vi.fn()
ui = {container: document.createElement('div')}

constructor(options: any) {
if (easepick.shouldThrow) throw new Error('Picker unavailable')
Expand All @@ -36,6 +41,21 @@ vi.mock('@easepick/bundle', () => {
select(detail: any): void {
this.listeners.get('select')?.({detail})
}

/** Renders the preset buttons the way PresetPlugin does: timestamps of the range in data attributes. */
renderPresets(): HTMLElement[] {
const presets = easepick.options.at(-1).PresetPlugin.customPreset as Record<string, Array<{getTime: () => number}>>
return Object.entries(presets).map(([label, [start, end]]) => {
const button = document.createElement('button')
button.className = 'preset-button unit'
button.textContent = label
button.dataset.start = String(start.getTime())
button.dataset.end = String(end.getTime())
this.ui.container.appendChild(button)
this.listeners.get('view')?.({detail: {view: 'PresetPluginButton', target: button}})
return button
})
}
}

return {create: Picker, DateTime, LockPlugin: {}, PresetPlugin: {}, RangePlugin: {}}
Expand All @@ -49,12 +69,16 @@ function dateLabel(value: {toJSDate: () => Date}): string {
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`
}

function mountPicker(maxDays = 7) {
function mountPicker(maxDays = 7, from = '2025-01-01T00:00', to = '2025-01-08T00:00') {
return mount(DateRangePicker, {
props: {from: '2025-01-01T00:00', to: '2025-01-08T00:00', maxSpanMs: maxDays * DAY_MS},
props: {from, to, maxSpanMs: maxDays * DAY_MS},
})
}

function activePresets(buttons: HTMLElement[]): string[] {
return buttons.filter(button => button.classList.contains('active')).map(button => button.textContent ?? '')
}

describe('DateRangePicker', () => {
beforeEach(() => {
easepick.shouldThrow = false
Expand Down Expand Up @@ -105,4 +129,18 @@ describe('DateRangePicker', () => {
expect(wrapper.emitted('update:to')).toEqual([['2025-01-31T00:00']])
wrapper.unmount()
})
})

it('highlights the preset matching the current range, and none for a custom one', async () => {
const wrapper = mountPicker(400, '2025-03-25T00:00', '2025-04-01T00:00')
const buttons: HTMLElement[] = easepick.instances[0].renderPresets()
expect(activePresets(buttons)).toEqual(['Last 7 days'])
expect(buttons.find(button => button.textContent === 'Last 7 days')?.getAttribute('aria-pressed')).toBe('true')

await wrapper.setProps({from: '2025-03-31T00:00', to: '2025-04-01T00:00'})
expect(activePresets(buttons)).toEqual(['Today'])

await wrapper.setProps({from: '2025-03-20T00:00', to: '2025-03-27T00:00'})
expect(activePresets(buttons)).toEqual([])
wrapper.unmount()
})
})
21 changes: 20 additions & 1 deletion src/main/webui/src/components/DateRangePicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ function syncPicker(): void {
if (start && end) picker.setDateRange(pickerDate(start), pickerDate(end))
}

/** A preset button is highlighted only when the current range is exactly its own, never for a custom one. */
function highlightPreset(button: HTMLElement): void {
const active = !!props.from && !!props.to
&& Number(button.dataset.start) === pickerDate(props.from.slice(0, 10)).getTime()
&& Number(button.dataset.end) === pickerDate(endDay(props.to)).getTime()
button.classList.toggle('active', active)
button.setAttribute('aria-pressed', String(active))
}

function highlightPresets(): void {
picker?.ui.container.querySelectorAll<HTMLElement>('.preset-button').forEach(highlightPreset)
}

function monthStartBack(months: number, end: Date): Date {
const month = end.getUTCMonth() - months
const lastDay = new Date(Date.UTC(end.getUTCFullYear(), month + 1, 0)).getUTCDate()
Expand Down Expand Up @@ -105,6 +118,9 @@ function initialize(): void {
},
},
setup: (instance) => {
instance.on('view', (event: { detail: { view: string; target: HTMLElement } }) => {
if (event.detail.view === 'PresetPluginButton') highlightPreset(event.detail.target)
})
instance.on('select', (event: { detail: { start?: { toJSDate: () => Date }; end?: { toJSDate: () => Date } } }) => {
const {start, end} = event.detail
if (start && end) emitRange(pickerDay(start.toJSDate()), pickerDay(end.toJSDate()))
Expand All @@ -119,7 +135,10 @@ function initialize(): void {
}

onMounted(initialize)
watch(() => [props.from, props.to], syncPicker)
watch(() => [props.from, props.to], () => {
syncPicker()
highlightPresets()
})
onBeforeUnmount(() => picker?.destroy())
</script>

Expand Down
5 changes: 5 additions & 0 deletions src/main/webui/src/layout/styles/easepick-theme.css
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@
.preset-button:active {
transform: scale(0.97);
}
/* Preset matching the current range; a custom range highlights none */
.preset-button.active {
box-shadow: inset 0 0 0 1.5px var(--color-bg-primary);
font-weight: 600;
}

/* ── Header (month nav arrows) ── */
.header .button-previous-month,
Expand Down
26 changes: 26 additions & 0 deletions src/main/webui/src/pages/impact/components/FilterBar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {afterEach, describe, expect, it} from 'vitest'
import {flushPromises, mount} from '@vue/test-utils'
import {createMemoryHistory, createRouter} from 'vue-router'
import FilterBar from '@/pages/impact/components/FilterBar.vue'
import DateRangePicker from '@/components/DateRangePicker.vue'
import type {AssetInfo, LiveEnvironment} from '@/domain/environment'
import {resetServices, servicesState} from '@/pages/impact/model/useDashboardQuery'

Expand Down Expand Up @@ -51,6 +52,31 @@ describe('FilterBar selection', () => {
wrapper.unmount()
})

it('applies a picked date range immediately, keeping the stored services', async () => {
const {router, wrapper} = await mountFilter(new Map([['a', []]]))
const picker = wrapper.findComponent(DateRangePicker)
picker.vm.$emit('update:from', '2026-11-01T00:00')
picker.vm.$emit('update:to', '2026-11-08T00:00')
await flushPromises()

expect(router.currentRoute.value.path).toBe('/impact/env')
expect(router.currentRoute.value.query).toEqual({from: '2026-11-01T00:00', to: '2026-11-08T00:00'})
expect(servicesState.value.services).toEqual([])
wrapper.unmount()
})

it('does not navigate when the picked range is the current one', async () => {
const {router, wrapper} = await mountFilter()
const before = router.currentRoute.value.fullPath
const picker = wrapper.findComponent(DateRangePicker)
picker.vm.$emit('update:from', from)
picker.vm.$emit('update:to', to)
await flushPromises()

expect(router.currentRoute.value.fullPath).toBe(before)
wrapper.unmount()
})

it('applies checked assets immediately and global service IDs to each selected asset', async () => {
const {router, wrapper} = await mountFilter(new Map([['a', []], ['b', []]]))
const asset = wrapper.get('input[name="assets"][value="b"]')
Expand Down
18 changes: 14 additions & 4 deletions src/main/webui/src/pages/impact/components/FilterBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,23 @@ function selectEnvironment(values: string[]): void {
void router.push({name: 'impact', params: {environment: selected}, query: datesQuery(from.value, to.value)})
}

/** Stores the services and puts the picked dates in the URL, skipping the navigation when they did not change. */
function applySelection(services: string[]): void {
applyServices(environmentId.value, services)
/** Puts the picked dates in the URL, skipping the navigation when they did not change. */
function applyDates(): void {
if (from.value !== props.from || to.value !== props.to)
void router.push({name: 'impact', params: {environment: environmentId.value}, query: datesQuery(from.value, to.value)})
}

/** The picker emits `from` then `to`, so the range is complete, and applied right away, once `to` arrives. */
function selectDates(value: string): void {
to.value = value
applyDates()
}

function applySelection(services: string[]): void {
applyServices(environmentId.value, services)
applyDates()
}

function selectAssets(values: string[]): void {
assetIds.value = values
applySelection(values)
Expand All @@ -79,7 +89,7 @@ function apply(): void {
<form id="filterForm" class="filter-bar" method="get" action="" @submit.prevent="apply">
<div class="filter-group date-range-group">
<span class="filter-label">Date range</span>
<DateRangePicker v-model:from="from" v-model:to="to" :max-span-ms="maxSpanMs" />
<DateRangePicker v-model:from="from" :to="to" :max-span-ms="maxSpanMs" @update:to="selectDates" />
</div>
<div class="filter-group">
<span class="filter-label">Environment</span>
Expand Down
Loading