Jelajahi Sumber

feat: remember the logs calendar month in the URL (#1162)

James Read 3 hari lalu
induk
melakukan
57cd908cd2

+ 2 - 0
docs/modules/ROOT/pages/logs/calendar.adoc

@@ -26,6 +26,8 @@ This gives you a quick visual overview of when actions were executed throughout
 
 Use the navigation controls at the top of the calendar to move between months. The calendar will display all logged executions for the visible month.
 
+The month you are viewing is kept in the page address (for example `/logs/calendar?month=2026-08`). This means that after opening an execution, your browser's back button returns you to the same month, and you can bookmark or share a link to a particular month.
+
 === Click on an Event
 
 Click on any event (execution) on the calendar to view the full execution details, including:

+ 6 - 2
frontend/resources/vue/App.vue

@@ -49,7 +49,7 @@
       <ConnectionBanner />
     </template>
 
-    <router-view :key="$route.fullPath" />
+    <router-view :key="routerViewKey" />
 
     <template
       v-if="showFooter"
@@ -178,7 +178,7 @@
 
 <script setup>
 import { ref, onMounted, computed } from 'vue'
-import { useRouter } from 'vue-router'
+import { useRoute, useRouter } from 'vue-router'
 import Sidebar from 'picocrank/vue/components/Sidebar.vue'
 import Navigation from 'picocrank/vue/components/Navigation.vue'
 import Header from 'picocrank/vue/components/Header.vue'
@@ -196,6 +196,10 @@ import { applyThemeStyles } from './utils/themeLoader.js'
 const { t } = useI18n()
 
 const router = useRouter()
+const route = useRoute()
+
+// Views that keep their own state in the query (e.g. the calendar month) opt out of remounting when only the query changes.
+const routerViewKey = computed(() => route.meta.keepOnQueryChange ? route.path : route.fullPath)
 
 const sidebar = ref(null)
 const navigation = ref(null)

+ 1 - 0
frontend/resources/vue/router.js

@@ -40,6 +40,7 @@ const routes = [
     component: () => import('./views/LogsCalendarView.vue'),
     meta: {
       title: 'Logs Calendar',
+      keepOnQueryChange: true,
       breadcrumbs: () => [
         { name: 'Logs', to: { name: 'Logs' } },
         { name: 'Calendar' }

+ 26 - 0
frontend/resources/vue/utils/calendarMonthQuery.js

@@ -0,0 +1,26 @@
+const MONTH_QUERY_PATTERN = /^(\d{4})-(\d{2})$/
+
+export function formatCalendarMonthQuery (monthIndex, year) {
+  return `${String(year).padStart(4, '0')}-${String(monthIndex + 1).padStart(2, '0')}`
+}
+
+// Falls back to the month of `today` so that a missing or hand-edited bad value still shows a calendar.
+export function parseCalendarMonthQuery (value, today = new Date()) {
+  const fallback = { monthIndex: today.getMonth(), year: today.getFullYear() }
+
+  if (typeof value !== 'string') {
+    return fallback
+  }
+
+  const match = MONTH_QUERY_PATTERN.exec(value)
+  if (!match) {
+    return fallback
+  }
+
+  const monthIndex = Number(match[2]) - 1
+  if (monthIndex < 0 || monthIndex > 11) {
+    return fallback
+  }
+
+  return { monthIndex, year: Number(match[1]) }
+}

+ 26 - 0
frontend/resources/vue/utils/calendarMonthQuery.test.mjs

@@ -0,0 +1,26 @@
+import assert from 'node:assert/strict'
+import test from 'node:test'
+
+import { formatCalendarMonthQuery, parseCalendarMonthQuery } from './calendarMonthQuery.js'
+
+const today = new Date(2026, 10, 13)
+
+test('formatCalendarMonthQuery pads the month and uses a 1-based month', () => {
+  assert.equal(formatCalendarMonthQuery(0, 2026), '2026-01')
+  assert.equal(formatCalendarMonthQuery(11, 2025), '2025-12')
+})
+
+test('parseCalendarMonthQuery reads a valid month', () => {
+  assert.deepEqual(parseCalendarMonthQuery('2026-08', today), { monthIndex: 7, year: 2026 })
+})
+
+test('parseCalendarMonthQuery round-trips with formatCalendarMonthQuery', () => {
+  const { monthIndex, year } = parseCalendarMonthQuery(formatCalendarMonthQuery(2, 2024), today)
+  assert.deepEqual({ monthIndex, year }, { monthIndex: 2, year: 2024 })
+})
+
+for (const value of [undefined, null, '', ['2026-08'], '2026-8', '2026-13', '2026-00', 'august', '2026-08-01']) {
+  test(`parseCalendarMonthQuery falls back to the current month for ${JSON.stringify(value)}`, () => {
+    assert.deepEqual(parseCalendarMonthQuery(value, today), { monthIndex: 10, year: 2026 })
+  })
+}

+ 8 - 7
frontend/resources/vue/views/LogsCalendarView.vue

@@ -41,20 +41,24 @@
 
 <script setup>
 import { ref, computed, onMounted } from 'vue'
-import { useRouter } from 'vue-router'
+import { useRoute, useRouter } from 'vue-router'
 import { useI18n } from 'vue-i18n'
 import { Calendar01Icon } from '@hugeicons/core-free-icons'
 import Calendar from 'picocrank/vue/components/Calendar.vue'
 import Section from 'picocrank/vue/components/Section.vue'
 import { loadStoredLogsFilter } from '../utils/logsFilterStorage.js'
+import { formatCalendarMonthQuery, parseCalendarMonthQuery } from '../utils/calendarMonthQuery.js'
 const router = useRouter()
+const route = useRoute()
 const { t } = useI18n()
 
 const logs = ref([])
 const loading = ref(false)
 const error = ref(null)
-const currentMonthIndex = ref(new Date().getMonth())
-const currentYear = ref(new Date().getFullYear())
+// The month lives in the URL so that browser Back from an execution returns to the month being browsed.
+const displayedMonth = computed(() => parseCalendarMonthQuery(route.query.month))
+const currentMonthIndex = computed(() => displayedMonth.value.monthIndex)
+const currentYear = computed(() => displayedMonth.value.year)
 
 const logsListLocation = computed(() => {
   const filter = loadStoredLogsFilter()
@@ -164,10 +168,7 @@ function handleDayClick (date) {
 }
 
 function handleMonthChange (month, year) {
-  currentMonthIndex.value = month
-  currentYear.value = year
-  // Optionally fetch logs for the new month if needed
-  // For now, we'll keep all logs loaded
+  router.replace({ query: { ...route.query, month: formatCalendarMonthQuery(month, year) } })
 }
 
 onMounted(() => {

+ 52 - 0
integration-tests/tests/calendarMonthHistory/calendarMonthHistory.mjs

@@ -0,0 +1,52 @@
+import { By, until } from 'selenium-webdriver'
+import { expect } from 'chai'
+import {
+  getRootAndWait,
+  takeScreenshotOnFailure,
+  waitForCurrentUrl,
+} from '../../lib/elements.js'
+
+async function getCalendarTitle () {
+  const title = await webdriver.wait(until.elementLocated(By.css('.calendar-title')), 5000)
+  return title.getText()
+}
+
+describe('logs calendar: month is kept in the URL', function () {
+  before(async function () {
+    await runner.start('calendarMonthHistory')
+  })
+
+  after(async () => {
+    await runner.stop()
+  })
+
+  afterEach(function () {
+    takeScreenshotOnFailure(this.currentTest, webdriver)
+  })
+
+  it('returns to the browsed month when going back', async () => {
+    await getRootAndWait()
+    await webdriver.get(new URL('logs/calendar', runner.baseUrl()).toString())
+
+    const currentMonthTitle = await getCalendarTitle()
+
+    const previousButton = await webdriver.findElement(By.xpath("//div[contains(@class, 'calendar-nav-buttons')]//button[normalize-space()='‹']"))
+    await previousButton.click()
+    await waitForCurrentUrl(url => url.includes('month='))
+
+    const previousMonthTitle = await getCalendarTitle()
+    expect(previousMonthTitle).to.not.equal(currentMonthTitle)
+
+    const monthUrl = await webdriver.getCurrentUrl()
+
+    const backToListLink = await webdriver.findElement(By.css('a.button.inline-icon[href$="/logs"]'))
+    await backToListLink.click()
+    await waitForCurrentUrl(url => !url.includes('calendar'))
+
+    await webdriver.navigate().back()
+    await waitForCurrentUrl(url => url === monthUrl)
+
+    await webdriver.wait(async () => (await getCalendarTitle()) === previousMonthTitle, 5000)
+    expect(await getCalendarTitle()).to.equal(previousMonthTitle)
+  })
+})

+ 12 - 0
integration-tests/tests/calendarMonthHistory/config.yaml

@@ -0,0 +1,12 @@
+#
+# Integration Test Config: Calendar month history
+#
+
+listenAddressSingleHTTPFrontend: 0.0.0.0:1337
+
+checkForUpdates: false
+
+actions:
+  - title: Ping example.com
+    shell: ping example.com -c 1
+    icon: ping