diff --git a/.changeset/shareable-fullscreen-tile.md b/.changeset/shareable-fullscreen-tile.md new file mode 100644 index 000000000..778eb57d3 --- /dev/null +++ b/.changeset/shareable-fullscreen-tile.md @@ -0,0 +1,10 @@ +--- +'@hyperdx/app': minor +--- + +feat: keep the fullscreen dashboard tile in the URL, so a single tile can be +shared by link. `fullscreenTileId` holds the tile, and `fullscreenFrom`, +`fullscreenTo` and `fullscreenGranularity` hold the range and granularity +picked inside the fullscreen view, separate from the dashboard's own. Opening +the link lands on that tile in fullscreen, browser back/forward steps through +the view, and the other dashboard URL params are kept. diff --git a/packages/app/src/DBDashboardPage.tsx b/packages/app/src/DBDashboardPage.tsx index 1ba6a11b5..be3d612a8 100644 --- a/packages/app/src/DBDashboardPage.tsx +++ b/packages/app/src/DBDashboardPage.tsx @@ -16,8 +16,11 @@ import { pick } from 'lodash'; import { parseAsArrayOf, parseAsBoolean, + parseAsFloat, parseAsString, + parseAsStringEnum, useQueryState, + useQueryStates, } from 'nuqs'; import { ErrorBoundary } from 'react-error-boundary'; import RGL from 'react-grid-layout'; @@ -30,6 +33,7 @@ import { import { convertToDashboardTemplate, Granularity, + isDateRangeEqual, isTimeSeriesDisplayType, savedChartConfigSupportsAlerts, } from '@hyperdx/common-utils/dist/core/utils'; @@ -484,6 +488,22 @@ const TilePlaceholder = ({ ); +type FullscreenTileView = { + dateRange?: [Date, Date]; + granularity?: Granularity | 'auto'; +}; + +// Module-level so useQueryStates gets a stable key map and setter. +const fullscreenQueryStateMap = { + fullscreenTileId: parseAsString, + fullscreenFrom: parseAsFloat, + fullscreenTo: parseAsFloat, + fullscreenGranularity: parseAsStringEnum([ + ...Object.values(Granularity), + 'auto', + ]), +}; + const Tile = ({ chart, dateRange, @@ -503,6 +523,7 @@ const Tile = ({ showAlertAnnotations, showReleaseAnnotations, isLive, + isLiveMode, readOnly, onFilter, @@ -514,6 +535,10 @@ const Tile = ({ onTouchEnd, children, isHighlighted, + isFullscreen, + onFullscreenChange, + fullscreenView, + onFullscreenViewChange, isSelected, onSelect, ref, @@ -545,6 +570,11 @@ const Tile = ({ // When true, draw release markers on this tile's chart. showReleaseAnnotations?: boolean; isLive?: boolean; + /** + * The dashboard is in live mode. Unlike `isLive`, stays true while the + * browser tab is hidden and refreshes pause. + */ + isLiveMode?: boolean; readOnly?: boolean; /** Set when an ad hoc filter broadcasts to this tile; clicked values update it. */ onFilter?: TileFilterHandler; @@ -557,11 +587,15 @@ const Tile = ({ onTouchEnd?: (e: React.TouchEvent) => void; children?: React.ReactNode; // Resizer tooltip isHighlighted?: boolean; + isFullscreen: boolean; + onFullscreenChange: (isFullscreen: boolean) => void; + /** The fullscreen view's own picker values, where they differ from the dashboard's. */ + fullscreenView?: FullscreenTileView; + onFullscreenViewChange: (view: FullscreenTileView) => void; isSelected?: boolean; onSelect?: (tileId: string) => void; ref?: ForwardedRef; }) => { - const [isFullscreen, setIsFullscreen] = useState(false); const [isFocused, setIsFocused] = useState(false); // A live dashboard's granularity is the coarse refresh interval; heatmaps @@ -593,35 +627,78 @@ const Tile = ({ const timeZone = useDisplayTimeZone(); - // Date range and granularity state local to the fullscreen view so that - // changing them does not propagate up to the dashboard. - const [fullscreenDateRange, setFullscreenDateRange] = - useState<[Date, Date]>(dateRange); + // The fullscreen view starts from the dashboard's range and granularity. + // Its own picker changes go to `fullscreenView` (URL params read only by + // this view), so they never refetch the dashboard. `fullscreenSeed` holds + // the dashboard values the view starts from, or null while closed. It + // follows the dashboard when those values change underneath (back/forward, + // a shared link whose range resolves after mount), except on a live + // dashboard, whose range moves every refresh. + const [fullscreenSeed, setFullscreenSeed] = useState<{ + dateRange: [Date, Date]; + granularity: SQLInterval | undefined; + } | null>(null); + const isOpeningFullscreen = isFullscreen && fullscreenSeed == null; + if (!isFullscreen && fullscreenSeed != null) { + setFullscreenSeed(null); + } else if ( + isOpeningFullscreen || + (isFullscreen && + fullscreenSeed != null && + !isLiveMode && + (fullscreenSeed.dateRange !== dateRange || + fullscreenSeed.granularity !== granularity)) + ) { + setFullscreenSeed({ dateRange, granularity }); + } + const fullscreenDateRange = + fullscreenView?.dateRange ?? fullscreenSeed?.dateRange ?? dateRange; + const fullscreenGranularity: Granularity | 'auto' = + fullscreenView?.granularity ?? + ((fullscreenSeed ? fullscreenSeed.granularity : granularity) as + | Granularity + | undefined) ?? + 'auto'; + + // Rewrite the picker text on open and when the range changes from outside + // it (back/forward, chart brush), but keep what the user typed or picked + // ("Last 15 minutes") for the range their own search produced. const [fullscreenInputValue, setFullscreenInputValue] = useTimeRangeInputText(fullscreenDateRange); - const [fullscreenGranularity, setFullscreenGranularity] = useState< - Granularity | 'auto' | undefined - >(() => (granularity as Granularity | undefined) ?? 'auto'); - - const openFullscreen = useCallback(() => { - // Reinitialize to the dashboard's current date range and granularity - // each time the fullscreen view is opened. - setFullscreenDateRange(dateRange); - setFullscreenInputValue(dateRangeToString(dateRange, timeZone)); - setFullscreenGranularity( - (granularity as Granularity | undefined) ?? 'auto', - ); - setIsFullscreen(true); - }, [dateRange, granularity, timeZone, setFullscreenInputValue]); + const [fullscreenInputRange, setFullscreenInputRange] = + useState(fullscreenDateRange); + const [fullscreenSearchedRange, setFullscreenSearchedRange] = useState< + [Date, Date] | null + >(null); + if ( + isOpeningFullscreen || + !isDateRangeEqual(fullscreenInputRange, fullscreenDateRange) + ) { + setFullscreenInputRange(fullscreenDateRange); + setFullscreenSearchedRange(null); + if ( + isOpeningFullscreen || + fullscreenSearchedRange == null || + !isDateRangeEqual(fullscreenSearchedRange, fullscreenDateRange) + ) { + setFullscreenInputValue(dateRangeToString(fullscreenDateRange, timeZone)); + } + } + + const openFullscreen = useCallback( + () => onFullscreenChange(true), + [onFullscreenChange], + ); const handleFullscreenSearch = useCallback( (value: string) => { const [start, end] = parseTimeRangeInput(value, timeZone); if (start != null && end != null) { - setFullscreenDateRange([start, end]); + setFullscreenSearchedRange([start, end]); + onFullscreenViewChange({ dateRange: [start, end] }); } }, - [timeZone], + [timeZone, onFullscreenViewChange], ); useEffect(() => { @@ -638,11 +715,7 @@ const Tile = ({ 'f', () => { if (!isFocused) return; - if (isFullscreen) { - setIsFullscreen(false); - } else { - openFullscreen(); - } + onFullscreenChange(!isFullscreen); }, ], ]); @@ -1368,7 +1441,10 @@ const Tile = ({ readOnly ? undefined : isFullscreenView - ? (start, end) => setFullscreenDateRange([start, end]) + ? (start, end) => + onFullscreenViewChange({ + dateRange: [start, end], + }) : onTimeRangeSelect } setDisplayType={ @@ -1580,6 +1656,7 @@ const Tile = ({ dateRange, fullscreenDateRange, fullscreenGranularity, + onFullscreenViewChange, filterWarning, isSourceMissing, isSourceUnset, @@ -1674,7 +1751,7 @@ const Tile = ({ {/* Fullscreen Modal */} setIsFullscreen(false)} + onClose={() => onFullscreenChange(false)} > {isFullscreen && ( @@ -1686,7 +1763,9 @@ const Tile = ({ /> + onFullscreenViewChange({ granularity: value ?? 'auto' }) + } /> @@ -1987,6 +2066,50 @@ function DBDashboardPage({ const { data: allDashboards } = useDashboards(); const [highlightedTileId] = useQueryState('highlightedTileId'); + // The fullscreen tile and the range and granularity picked inside its view, + // so a link reopens exactly that view. Kept apart from `from`/`to` and + // `granularity` so fullscreen changes don't refetch every dashboard tile. + // Pushed (not replaced) so back/forward steps through them. + const [fullscreenState, setFullscreenState] = useQueryStates( + fullscreenQueryStateMap, + { history: 'push' }, + ); + const { fullscreenTileId, fullscreenFrom, fullscreenTo } = fullscreenState; + const fullscreenView = useMemo((): FullscreenTileView => { + const hasRange = + fullscreenFrom != null && + fullscreenTo != null && + Number.isFinite(fullscreenFrom) && + Number.isFinite(fullscreenTo) && + fullscreenFrom < fullscreenTo; + return { + dateRange: hasRange + ? [new Date(fullscreenFrom), new Date(fullscreenTo)] + : undefined, + granularity: fullscreenState.fullscreenGranularity ?? undefined, + }; + }, [fullscreenFrom, fullscreenTo, fullscreenState.fullscreenGranularity]); + const setFullscreenTileId = useCallback( + (tileId: string | null) => + setFullscreenState({ + fullscreenTileId: tileId, + fullscreenFrom: null, + fullscreenTo: null, + fullscreenGranularity: null, + }), + [setFullscreenState], + ); + const setFullscreenView = useCallback( + ({ dateRange, granularity }: FullscreenTileView) => + setFullscreenState({ + ...(dateRange && { + fullscreenFrom: dateRange[0].getTime(), + fullscreenTo: dateRange[1].getTime(), + }), + ...(granularity && { fullscreenGranularity: granularity }), + }), + [setFullscreenState], + ); const [granularity, setGranularity] = useQueryState( 'granularity', @@ -2437,32 +2560,6 @@ function DBDashboardPage({ [setUrlActiveTabs], ); - // When arriving via ?highlightedTileId, switch to the tile's tab if it - // isn't already active so the tile is in the DOM for the Tile-level - // scroll/highlight effect to take effect. Guard with a ref keyed on the - // highlighted id so a user manually switching tabs afterwards doesn't - // get reverted on the next render. - const handledHighlightRef = useRef(null); - useEffect(() => { - if (!highlightedTileId || !dashboard) return; - if (handledHighlightRef.current === highlightedTileId) return; - handledHighlightRef.current = highlightedTileId; - const tile = dashboard.tiles.find(t => t.id === highlightedTileId); - if (!tile?.containerId || !tile.tabId) return; - const container = containers.find(c => c.id === tile.containerId); - if (!container || getActiveTabId(container) === tile.tabId) return; - setUrlActiveTabs(prev => ({ - ...(prev ?? {}), - [container.id]: tile.tabId!, - })); - }, [ - highlightedTileId, - dashboard, - containers, - getActiveTabId, - setUrlActiveTabs, - ]); - const moveTargetContainers = useMemo( () => buildMoveTargets(containers), [containers], @@ -2576,6 +2673,7 @@ function DBDashboardPage({ }} readOnly={isKioskMode} isLive={isRefreshEnabled} + isLiveMode={isLive || isKioskMode} granularity={ isRefreshEnabled ? granularityOverride : (granularity ?? undefined) } @@ -2587,6 +2685,14 @@ function DBDashboardPage({ showAlertAnnotations={showAlertAnnotations} showReleaseAnnotations={showReleaseAnnotations} isHighlighted={highlightedTileId === chart.id} + isFullscreen={fullscreenTileId === chart.id} + onFullscreenChange={isFullscreen => + setFullscreenTileId(isFullscreen ? chart.id : null) + } + fullscreenView={ + fullscreenTileId === chart.id ? fullscreenView : undefined + } + onFullscreenViewChange={setFullscreenView} onUpdateChart={ isKioskMode ? undefined @@ -2676,9 +2782,14 @@ function DBDashboardPage({ dashboard, searchedTimeRange, isRefreshEnabled, + isLive, granularityOverride, granularity, highlightedTileId, + fullscreenTileId, + setFullscreenTileId, + fullscreenView, + setFullscreenView, confirm, setDashboard, onTimeRangeSelect, @@ -2773,6 +2884,58 @@ function DBDashboardPage({ ], ); + // Activate the tile's tab, and with `expand` its collapsed group, so a + // deep-linked tile mounts. A tile ID that matches no tile does nothing. + const revealTile = useCallback( + (tileId: string, { expand }: { expand: boolean }) => { + const tile = dashboard?.tiles.find(t => t.id === tileId); + const container = tile?.containerId + ? containers.find(c => c.id === tile.containerId) + : undefined; + if (!tile || !container) return; + const { tabId } = tile; + // A stale tabId never becomes active, so setting it would loop. + const isKnownTab = container.tabs?.some(t => t.id === tabId) ?? false; + if (tabId && isKnownTab && getActiveTabId(container) !== tabId) { + setUrlActiveTabs(prev => ({ ...(prev ?? {}), [container.id]: tabId })); + } + if (expand && isContainerCollapsed(container)) { + addToUrlSet(setUrlExpandedIds, container.id); + removeFromUrlSet(setUrlCollapsedIds, container.id); + } + }, + [ + dashboard, + containers, + getActiveTabId, + isContainerCollapsed, + setUrlActiveTabs, + addToUrlSet, + removeFromUrlSet, + setUrlCollapsedIds, + setUrlExpandedIds, + ], + ); + + // When arriving via ?highlightedTileId, switch to the tile's tab so the + // tile is in the DOM for the Tile-level scroll/highlight effect. Guard + // with a ref keyed on the highlighted id so a user manually switching tabs + // afterwards doesn't get reverted on the next render. + const handledHighlightRef = useRef(null); + useEffect(() => { + if (!highlightedTileId || !dashboard) return; + if (handledHighlightRef.current === highlightedTileId) return; + handledHighlightRef.current = highlightedTileId; + revealTile(highlightedTileId, { expand: false }); + }, [highlightedTileId, dashboard, revealTile]); + + // The fullscreen view renders inside its tile, so a ?fullscreenTileId link + // must reveal the tile for it to mount. No ref guard: back/forward onto a + // fullscreen entry must reveal the tile again. + useEffect(() => { + if (fullscreenTileId) revealTile(fullscreenTileId, { expand: true }); + }, [fullscreenTileId, revealTile]); + // Collapsible-only subset, used both for bulk collapse/expand and for // disabling the menu items when nothing can be toggled. const collapsibleContainers = useMemo( diff --git a/packages/app/tests/e2e/features/dashboard-fullscreen-url.spec.ts b/packages/app/tests/e2e/features/dashboard-fullscreen-url.spec.ts new file mode 100644 index 000000000..db578fe62 --- /dev/null +++ b/packages/app/tests/e2e/features/dashboard-fullscreen-url.spec.ts @@ -0,0 +1,462 @@ +/** + * The fullscreen tile view lives in the `?fullscreenTileId` query param so a + * single tile can be shared by link. These tests guard its interaction with + * the other dashboard URL params (time range, granularity, where, group + * state) and with browser history. + */ +import { Page } from '@playwright/test'; + +import { DashboardPage } from '../page-objects/DashboardPage'; +import { getApiUrl, getSources } from '../utils/api-helpers'; +import { expect, test } from '../utils/base-test'; +import { DEFAULT_LOGS_SOURCE_NAME } from '../utils/constants'; + +const markdownTile = ( + sourceId: string, + id: string, + x: number, + placement: { containerId?: string; tabId?: string } = {}, +) => ({ + id, + x, + y: 0, + w: 12, + h: 4, + ...placement, + config: { + name: id, + source: sourceId, + displayType: 'markdown', + select: [], + where: '', + whereLanguage: 'lucene', + markdown: tileContent(id), + }, +}); + +const tileContent = (id: string) => `Fullscreen content ${id}`; + +const rangeHours = (params: Record) => + (Number(params.to) - Number(params.from)) / (60 * 60 * 1000); + +async function seedDashboard(page: Page): Promise { + const logSources = await getSources(page, 'log'); + const sourceId: string = logSources.find( + (s: { name: string }) => s.name === DEFAULT_LOGS_SOURCE_NAME, + ).id; + const tile = markdownTile.bind(null, sourceId); + const res = await page.request.post(`${getApiUrl()}/dashboards`, { + data: { + name: `E2E fullscreen URL ${Date.now()}`, + containers: [ + { id: 'collapsed-group', title: 'Collapsed group', collapsed: true }, + { + id: 'tabbed-group', + title: 'Tabbed group', + collapsed: false, + tabs: [ + { id: 'tab-one', title: 'Tab one' }, + { id: 'tab-two', title: 'Tab two' }, + ], + }, + ], + tiles: [ + tile('tile-a', 0), + tile('tile-b', 12), + tile('tile-collapsed', 0, { containerId: 'collapsed-group' }), + tile('tile-tab-one', 0, { + containerId: 'tabbed-group', + tabId: 'tab-one', + }), + tile('tile-tab-two', 0, { + containerId: 'tabbed-group', + tabId: 'tab-two', + }), + tile('tile-stale-tab', 12, { + containerId: 'tabbed-group', + tabId: 'deleted-tab', + }), + ], + tags: [], + }, + }); + expect(res.ok()).toBeTruthy(); + const body = await res.json(); + return body.id ?? body._id; +} + +test.describe( + 'Dashboard fullscreen tile URL state', + { tag: ['@dashboard', '@full-stack'] }, + () => { + let dashboardPage: DashboardPage; + let dashboardId: string; + + // Fixed past range so the values can be compared exactly. + const to = Date.UTC(2026, 0, 15, 12, 0, 0); + const from = to - 2 * 60 * 60 * 1000; + const baseParams = { + from: String(from), + to: String(to), + granularity: '5 minute', + }; + + test.beforeEach(async ({ page }) => { + test.setTimeout(60000); + dashboardPage = new DashboardPage(page); + dashboardId = await seedDashboard(page); + }); + + async function expectFullscreenShowing(tileId: string) { + await expect(dashboardPage.fullscreenModalBody).toContainText( + tileContent(tileId), + ); + await expect + .poll(() => dashboardPage.getFullscreenTileIdParam()) + .toBe(tileId); + } + + async function expectFullscreenClosed() { + await expect(dashboardPage.fullscreenModalBody).toHaveCount(0); + await expect + .poll(() => dashboardPage.getFullscreenTileIdParam()) + .toBeNull(); + expect(dashboardPage.getFullscreenViewParams()).toEqual({ + from: null, + to: null, + granularity: null, + }); + } + + test('a link with the param opens that tile fullscreen with the URL time range and granularity', async () => { + await dashboardPage.gotoDashboardWithParams(dashboardId, { + ...baseParams, + fullscreenTileId: 'tile-b', + }); + + await test.step('The linked tile opens fullscreen', async () => { + await expectFullscreenShowing('tile-b'); + await expect(dashboardPage.fullscreenModalBody).not.toContainText( + tileContent('tile-a'), + ); + }); + + await test.step('The fullscreen pickers start from the URL values', async () => { + await expect(dashboardPage.fullscreenTimePickerInput).toHaveValue( + await dashboardPage.dashboardTimePickerInput.inputValue(), + ); + await expect(dashboardPage.fullscreenGranularityPicker).toHaveValue( + '5 Minutes Granularity', + ); + }); + + await test.step('Closing removes only the fullscreen param', async () => { + await dashboardPage.closeFullscreen(); + await expectFullscreenClosed(); + expect(dashboardPage.getUrlParamsExceptFullscreen()).toMatchObject( + baseParams, + ); + }); + + await test.step('The fullscreen view survives a reload', async () => { + await dashboardPage.goBack(); + await expectFullscreenShowing('tile-b'); + await dashboardPage.reload(); + await expectFullscreenShowing('tile-b'); + expect(dashboardPage.getUrlParamsExceptFullscreen()).toMatchObject( + baseParams, + ); + }); + }); + + test('entering and exiting fullscreen keeps time range, granularity, and where params', async () => { + await dashboardPage.gotoDashboardWithParams(dashboardId, baseParams); + await expect(dashboardPage.getTileById('tile-a')).toBeVisible(); + + await dashboardPage.setGlobalFilter('ServiceName:fullscreen-test'); + await expect + .poll(() => dashboardPage.getUrlParamsExceptFullscreen().where) + .toBeTruthy(); + const paramsBefore = dashboardPage.getUrlParamsExceptFullscreen(); + expect(paramsBefore).toMatchObject(baseParams); + + await test.step('Open with the toolbar button', async () => { + await dashboardPage.openFullscreenForTileId('tile-a'); + await expectFullscreenShowing('tile-a'); + expect(dashboardPage.getUrlParamsExceptFullscreen()).toMatchObject( + paramsBefore, + ); + }); + + await test.step('Fullscreen picker changes keep the tile open and leave dashboard params alone', async () => { + await dashboardPage.selectFullscreenRelativeTime('Last 15 minutes'); + await dashboardPage.changeFullscreenGranularity('1 Minute Granularity'); + await expect(dashboardPage.fullscreenGranularityPicker).toHaveValue( + '1 Minute Granularity', + ); + await expectFullscreenShowing('tile-a'); + expect(dashboardPage.getUrlParamsExceptFullscreen()).toMatchObject( + paramsBefore, + ); + }); + + await test.step('Close with Escape', async () => { + await dashboardPage.closeFullscreen(); + await expectFullscreenClosed(); + expect(dashboardPage.getUrlParamsExceptFullscreen()).toMatchObject( + paramsBefore, + ); + }); + + await test.step('The f shortcut toggles the param', async () => { + await dashboardPage.pressFullscreenHotkeyOnTile('tile-b'); + await expectFullscreenShowing('tile-b'); + await dashboardPage.pressFullscreenHotkey(); + await expectFullscreenClosed(); + expect(dashboardPage.getUrlParamsExceptFullscreen()).toMatchObject( + paramsBefore, + ); + }); + }); + + test('the range and granularity picked in fullscreen are shared by the link', async () => { + await dashboardPage.gotoDashboardWithParams(dashboardId, baseParams); + await expect(dashboardPage.getTileById('tile-a')).toBeVisible(); + await dashboardPage.openFullscreenForTileId('tile-a'); + const dashboardRangeText = + await dashboardPage.fullscreenTimePickerInput.inputValue(); + + await test.step('Picker changes go to the fullscreen params only', async () => { + await dashboardPage.selectFullscreenRelativeTime('Last 15 minutes'); + await expect + .poll(() => dashboardPage.getFullscreenViewParams().from) + .not.toBeNull(); + await dashboardPage.changeFullscreenGranularity('1 Minute Granularity'); + await expect + .poll(() => dashboardPage.getFullscreenViewParams().granularity) + .toBe('1 minute'); + const { from: fsFrom, to: fsTo } = + dashboardPage.getFullscreenViewParams(); + expect(rangeHours({ from: fsFrom ?? '', to: fsTo ?? '' })).toBeCloseTo( + 0.25, + 2, + ); + expect(dashboardPage.getUrlParamsExceptFullscreen()).toMatchObject( + baseParams, + ); + }); + + const fullscreenParams = dashboardPage.getFullscreenViewParams(); + + await test.step('Opening the link restores the fullscreen pickers', async () => { + await dashboardPage.reload(); + await expectFullscreenShowing('tile-a'); + expect(dashboardPage.getFullscreenViewParams()).toEqual( + fullscreenParams, + ); + await expect(dashboardPage.fullscreenGranularityPicker).toHaveValue( + '1 Minute Granularity', + ); + await expect(dashboardPage.fullscreenTimePickerInput).not.toHaveValue( + dashboardRangeText, + ); + expect(dashboardPage.getUrlParamsExceptFullscreen()).toMatchObject( + baseParams, + ); + }); + + await test.step('Back steps through the picker changes, then closes', async () => { + await dashboardPage.goBack(); + await expectFullscreenShowing('tile-a'); + await expect(dashboardPage.fullscreenGranularityPicker).toHaveValue( + '5 Minutes Granularity', + ); + await dashboardPage.goBack(); + await expectFullscreenShowing('tile-a'); + await expect(dashboardPage.fullscreenTimePickerInput).toHaveValue( + dashboardRangeText, + ); + await dashboardPage.goBack(); + await expectFullscreenClosed(); + }); + }); + + test('reopening fullscreen drops picker text that was typed but not applied', async () => { + await dashboardPage.gotoDashboardWithParams(dashboardId, baseParams); + await expect(dashboardPage.getTileById('tile-a')).toBeVisible(); + await dashboardPage.openFullscreenForTileId('tile-a'); + const dashboardRangeText = + await dashboardPage.fullscreenTimePickerInput.inputValue(); + + await dashboardPage.typeFullscreenTimeRange('Last 3 days'); + await dashboardPage.closeFullscreen(); + await expectFullscreenClosed(); + + await dashboardPage.openFullscreenForTileId('tile-a'); + await expect(dashboardPage.fullscreenTimePickerInput).toHaveValue( + dashboardRangeText, + ); + }); + + test('rapid time range and fullscreen changes end in the combined URL state', async () => { + await dashboardPage.gotoDashboardWithParams(dashboardId, baseParams); + await expect(dashboardPage.getTileById('tile-a')).toBeVisible(); + + await test.step('Change the time range, then open fullscreen right away', async () => { + await dashboardPage.timePicker.selectRelativeTime('Last 6 hours'); + await dashboardPage.openFullscreenForTileId('tile-a'); + await expectFullscreenShowing('tile-a'); + await expect + .poll(() => dashboardPage.getUrlParamsExceptFullscreen().from) + .not.toBe(baseParams.from); + const params = dashboardPage.getUrlParamsExceptFullscreen(); + expect(rangeHours(params)).toBeCloseTo(6, 2); + expect(params.granularity).toBe(baseParams.granularity); + }); + + await test.step('Close fullscreen, then change the time range right away', async () => { + await dashboardPage.closeFullscreen(); + await dashboardPage.timePicker.selectRelativeTime('Last 1 hour'); + await expectFullscreenClosed(); + await expect + .poll(() => + Math.round( + rangeHours(dashboardPage.getUrlParamsExceptFullscreen()), + ), + ) + .toBe(1); + expect(dashboardPage.getUrlParamsExceptFullscreen().granularity).toBe( + baseParams.granularity, + ); + }); + }); + + test('back and forward toggle the fullscreen view', async () => { + await dashboardPage.gotoDashboardWithParams(dashboardId, baseParams); + await expect(dashboardPage.getTileById('tile-a')).toBeVisible(); + + await dashboardPage.openFullscreenForTileId('tile-a'); + await expectFullscreenShowing('tile-a'); + await dashboardPage.closeFullscreen(); + await expectFullscreenClosed(); + + await test.step('Back reopens the tile', async () => { + await dashboardPage.goBack(); + await expectFullscreenShowing('tile-a'); + }); + + await test.step('Back again returns to the dashboard', async () => { + await dashboardPage.goBack(); + await expectFullscreenClosed(); + await expect(dashboardPage.getTileById('tile-a')).toBeVisible(); + }); + + await test.step('Forward reopens the tile', async () => { + await dashboardPage.goForward(); + await expectFullscreenShowing('tile-a'); + expect(dashboardPage.getUrlParamsExceptFullscreen()).toMatchObject( + baseParams, + ); + }); + + await test.step('Forward again closes it', async () => { + await dashboardPage.goForward(); + await expectFullscreenClosed(); + expect(dashboardPage.getUrlParamsExceptFullscreen()).toMatchObject( + baseParams, + ); + }); + }); + + test('a live dashboard keeps the fullscreen picker values while the tab is hidden', async () => { + // Without a URL granularity, hiding the tab swaps the tiles from the + // live auto granularity back to none. + await dashboardPage.gotoDashboardWithParams(dashboardId, { + from: baseParams.from, + to: baseParams.to, + }); + await expect(dashboardPage.getTileById('tile-a')).toBeVisible(); + await dashboardPage.toggleLiveMode(); + + await dashboardPage.openFullscreenForTileId('tile-a'); + await dashboardPage.selectFullscreenRelativeTime('Last 15 minutes'); + await dashboardPage.changeFullscreenGranularity('1 Minute Granularity'); + await expect(dashboardPage.fullscreenGranularityPicker).toHaveValue( + '1 Minute Granularity', + ); + const fullscreenRange = + await dashboardPage.fullscreenTimePickerInput.inputValue(); + + await dashboardPage.setTabHidden(true); + await dashboardPage.setTabHidden(false); + + await expectFullscreenShowing('tile-a'); + await expect(dashboardPage.fullscreenGranularityPicker).toHaveValue( + '1 Minute Granularity', + ); + await expect(dashboardPage.fullscreenTimePickerInput).toHaveValue( + fullscreenRange, + ); + }); + + test('an unknown tile ID shows the normal dashboard', async () => { + await dashboardPage.gotoDashboardWithParams(dashboardId, { + ...baseParams, + fullscreenTileId: 'no-such-tile', + }); + + await expect(dashboardPage.getTileById('tile-a')).toBeVisible(); + await expect(dashboardPage.fullscreenModalBody).toHaveCount(0); + + await test.step('A tile whose tab no longer exists leaves the tabs alone', async () => { + await dashboardPage.gotoDashboardWithParams(dashboardId, { + ...baseParams, + fullscreenTileId: 'tile-stale-tab', + }); + await expect(dashboardPage.getTileById('tile-tab-one')).toBeVisible(); + await expect(dashboardPage.fullscreenModalBody).toHaveCount(0); + expect(dashboardPage.getActiveTabsParam()).toEqual({}); + }); + + await test.step('Opening a real tile replaces the unknown ID', async () => { + await dashboardPage.openFullscreenForTileId('tile-a'); + await expectFullscreenShowing('tile-a'); + expect(dashboardPage.getUrlParamsExceptFullscreen()).toMatchObject( + baseParams, + ); + }); + }); + + test('a link to a tile in a collapsed group or inactive tab reveals it', async () => { + await test.step('Collapsed group', async () => { + await dashboardPage.gotoDashboardWithParams(dashboardId, { + ...baseParams, + fullscreenTileId: 'tile-collapsed', + }); + await expectFullscreenShowing('tile-collapsed'); + await expect + .poll(() => dashboardPage.getExpandedParam()) + .toContain('collapsed-group'); + await dashboardPage.closeFullscreen(); + await expect(dashboardPage.getTileById('tile-collapsed')).toBeVisible(); + }); + + await test.step('Inactive tab', async () => { + await dashboardPage.gotoDashboardWithParams(dashboardId, { + ...baseParams, + fullscreenTileId: 'tile-tab-two', + }); + await expectFullscreenShowing('tile-tab-two'); + await expect + .poll(() => dashboardPage.getActiveTabsParam()['tabbed-group']) + .toBe('tab-two'); + await dashboardPage.closeFullscreen(); + await expect(dashboardPage.getTileById('tile-tab-two')).toBeVisible(); + await expect(dashboardPage.getTileById('tile-tab-one')).toHaveCount(0); + expect(dashboardPage.getUrlParamsExceptFullscreen()).toMatchObject( + baseParams, + ); + }); + }); + }, +); diff --git a/packages/app/tests/e2e/page-objects/DashboardPage.ts b/packages/app/tests/e2e/page-objects/DashboardPage.ts index dd16b5144..2c11f324a 100644 --- a/packages/app/tests/e2e/page-objects/DashboardPage.ts +++ b/packages/app/tests/e2e/page-objects/DashboardPage.ts @@ -675,7 +675,7 @@ export class DashboardPage { * toolbar for a single collapsed menu button. */ async openTileActionsMenuById(tileId: string, { narrow = false } = {}) { - const tile = this.page.getByTestId(`dashboard-tile-${tileId}`); + const tile = this.getTileById(tileId); await tile.scrollIntoViewIfNeeded(); await tile.hover(); const trigger = narrow @@ -2581,14 +2581,132 @@ export class DashboardPage { await intervalButton.click({ timeout: 10000 }); } + /** Type into the fullscreen time picker without applying the range. */ + async typeFullscreenTimeRange(text: string) { + await this.fullscreenTimePickerInput.fill(text); + } + /** - * Close the fullscreen modal by pressing Escape and wait for it to disappear. + * Close the fullscreen modal with Escape and wait for it to disappear. + * Retries because with a picker popover open the first Escape only closes + * the popover. */ async closeFullscreen() { - await this.page.keyboard.press('Escape'); + await expect(async () => { + await this.page.keyboard.press('Escape'); + await expect(this.fullscreenTimePickerInput).toBeHidden({ + timeout: 1000, + }); + }).toPass({ timeout: 10000 }); + } + + get fullscreenGranularityPicker() { + return this.fullscreenModalBody.getByTestId('granularity-picker'); + } + + async changeFullscreenGranularity(granularity: string) { + await this.fullscreenGranularityPicker.click(); + // The dashboard's own granularity dropdown keeps its options mounted. + await this.page + .locator('[role="option"]', { hasText: granularity }) + .filter({ visible: true }) + .click(); + } + + /** Open the dashboard with the given query params already in the URL. */ + async gotoDashboardWithParams( + dashboardId: string, + params: Record, + ) { + const query = new URLSearchParams(params).toString(); + await this.page.goto(`/dashboards/${dashboardId}?${query}`); + } + + getTileById(tileId: string): Locator { + return this.page.getByTestId(`dashboard-tile-${tileId}`); + } + + /** Click the toolbar fullscreen button of the tile with this id. */ + async openFullscreenForTileId(tileId: string) { + const tile = this.getTileById(tileId); + await tile.scrollIntoViewIfNeeded(); + await tile.hover(); + await this.page.getByTestId(`tile-fullscreen-button-${tileId}`).click(); await this.fullscreenTimePickerInput.waitFor({ - state: 'hidden', - timeout: 5000, + state: 'visible', + timeout: 10000, }); } + + /** + * The dashboard's own time picker input. The fullscreen modal portals its + * picker after the page content, so the first match is the dashboard's. + */ + get dashboardTimePickerInput() { + return this.page.getByTestId('time-picker-input').first(); + } + + /** Press the `f` shortcut while hovering the tile with this id. */ + async pressFullscreenHotkeyOnTile(tileId: string) { + await this.getTileById(tileId).hover(); + await this.pressFullscreenHotkey(); + } + + async pressFullscreenHotkey() { + await this.page.keyboard.press('f'); + } + + /** + * Fire `visibilitychange` as if the browser tab were hidden or shown again. + * Headless Chromium keeps every page visible, so override the property. + */ + async setTabHidden(hidden: boolean) { + await this.page.evaluate(isHidden => { + Object.defineProperty(document, 'visibilityState', { + configurable: true, + get: () => (isHidden ? 'hidden' : 'visible'), + }); + Object.defineProperty(document, 'hidden', { + configurable: true, + get: () => isHidden, + }); + document.dispatchEvent(new Event('visibilitychange')); + }, hidden); + } + + async goBack() { + await this.page.goBack(); + } + + async goForward() { + await this.page.goForward(); + } + + getFullscreenTileIdParam(): string | null { + return new URL(this.page.url()).searchParams.get('fullscreenTileId'); + } + + /** The range and granularity picked inside the fullscreen view. */ + getFullscreenViewParams() { + const params = new URL(this.page.url()).searchParams; + return { + from: params.get('fullscreenFrom'), + to: params.get('fullscreenTo'), + granularity: params.get('fullscreenGranularity'), + }; + } + + /** All query params except the fullscreen ones, for comparing URL state. */ + getUrlParamsExceptFullscreen(): Record { + const params = new URL(this.page.url()).searchParams; + for (const key of [ + 'fullscreenTileId', + 'fullscreenFrom', + 'fullscreenTo', + 'fullscreenGranularity', + ]) { + params.delete(key); + } + return Object.fromEntries(params.entries()); + } }