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 5bb4b4ee9..4f9fee0ab 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'; @@ -29,6 +32,7 @@ import { import { convertToDashboardTemplate, Granularity, + isDateRangeEqual, isTimeSeriesDisplayType, savedChartConfigSupportsAlerts, } from '@hyperdx/common-utils/dist/core/utils'; @@ -483,6 +487,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, @@ -501,6 +521,7 @@ const Tile = ({ showAlertAnnotations, showReleaseAnnotations, isLive, + isLiveMode, readOnly, onFilter, @@ -512,6 +533,10 @@ const Tile = ({ onTouchEnd, children, isHighlighted, + isFullscreen, + onFullscreenChange, + fullscreenView, + onFullscreenViewChange, isSelected, onSelect, ref, @@ -543,6 +568,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; @@ -555,11 +585,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 @@ -591,35 +625,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(() => { @@ -636,11 +713,7 @@ const Tile = ({ 'f', () => { if (!isFocused) return; - if (isFullscreen) { - setIsFullscreen(false); - } else { - openFullscreen(); - } + onFullscreenChange(!isFullscreen); }, ], ]); @@ -1361,7 +1434,10 @@ const Tile = ({ readOnly ? undefined : isFullscreenView - ? (start, end) => setFullscreenDateRange([start, end]) + ? (start, end) => + onFullscreenViewChange({ + dateRange: [start, end], + }) : onTimeRangeSelect } setDisplayType={ @@ -1572,6 +1648,7 @@ const Tile = ({ dateRange, fullscreenDateRange, fullscreenGranularity, + onFullscreenViewChange, filterWarning, isSourceMissing, isSourceUnset, @@ -1666,7 +1743,7 @@ const Tile = ({ {/* Fullscreen Modal */} setIsFullscreen(false)} + onClose={() => onFullscreenChange(false)} > {isFullscreen && ( @@ -1678,7 +1755,9 @@ const Tile = ({ /> + onFullscreenViewChange({ granularity: value ?? 'auto' }) + } /> @@ -1976,6 +2055,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', @@ -2544,6 +2667,7 @@ function DBDashboardPage({ onEditClick={() => setEditedTile(chart)} readOnly={isKioskMode} isLive={isRefreshEnabled} + isLiveMode={isLive || isKioskMode} granularity={ isRefreshEnabled ? granularityOverride : (granularity ?? undefined) } @@ -2555,6 +2679,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 @@ -2644,9 +2776,14 @@ function DBDashboardPage({ dashboard, searchedTimeRange, isRefreshEnabled, + isLive, granularityOverride, granularity, highlightedTileId, + fullscreenTileId, + setFullscreenTileId, + fullscreenView, + setFullscreenView, confirm, setDashboard, onTimeRangeSelect, @@ -2741,6 +2878,39 @@ function DBDashboardPage({ ], ); + // The fullscreen view renders inside its tile, so a ?fullscreenTileId link + // must activate the tile's tab and expand its group for the tile to mount. + // A tile ID that matches no tile leaves the dashboard as it is. + useEffect(() => { + if (!fullscreenTileId || !dashboard) return; + const tile = dashboard.tiles.find(t => t.id === fullscreenTileId); + 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 (isContainerCollapsed(container)) { + addToUrlSet(setUrlExpandedIds, container.id); + removeFromUrlSet(setUrlCollapsedIds, container.id); + } + }, [ + fullscreenTileId, + dashboard, + containers, + getActiveTabId, + isContainerCollapsed, + setUrlActiveTabs, + addToUrlSet, + removeFromUrlSet, + setUrlCollapsedIds, + setUrlExpandedIds, + ]); + // 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..3b2420ced --- /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.closeFullscreenFromPicker(); + 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 54a87a038..e7cec8678 100644 --- a/packages/app/tests/e2e/page-objects/DashboardPage.ts +++ b/packages/app/tests/e2e/page-objects/DashboardPage.ts @@ -2539,4 +2539,132 @@ export class DashboardPage { timeout: 5000, }); } + + /** Type into the fullscreen time picker without applying the range. */ + async typeFullscreenTimeRange(text: string) { + await this.fullscreenTimePickerInput.fill(text); + } + + /** + * Close fullscreen with Escape even when a picker popover is open, since + * the first Escape then only closes the popover. + */ + async closeFullscreenFromPicker() { + 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: '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()); + } }