From 6ff13544ce4d59b864740d4ce4813173bbf0008e Mon Sep 17 00:00:00 2001 From: Yan <75355375+yancat160@users.noreply.github.com> Date: Fri, 8 May 2026 11:04:04 -0400 Subject: [PATCH] let users toggle DataGrid between table and card view DataGrid was already injecting displayMode='cards' on small viewports via useMobile() and leaving it undefined (default 'table') everywhere else. The user had no way to override either side: someone on a wide screen who preferred a denser card list could not get there, and someone on a tablet with a tall narrow window could not force the table view to keep their layout consistent. Add a small icon toggle in the DataGrid header row that flips between table and cards, and persist the choice in localStorage under umami.datagrid.displayMode. The user choice wins; if there is none, the existing useMobile-driven default applies. Every DataGrid consumer (sessions, websites, links, pixels, boards, team admin, etc.) gets the toggle automatically with no caller-side change. Verified in playwright on the sessions page: at 1400 viewport the default is table; clicking the toggle switches to cards and a reload keeps cards. At 800 viewport the default is cards; clicking the toggle switches to table even though useMobile would otherwise force cards. Round trip in both directions works and the choice survives navigation away and back. --- src/components/common/DataGrid.tsx | 52 +++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 5 deletions(-) diff --git a/src/components/common/DataGrid.tsx b/src/components/common/DataGrid.tsx index 993929fd2..4ac8e72af 100644 --- a/src/components/common/DataGrid.tsx +++ b/src/components/common/DataGrid.tsx @@ -1,5 +1,15 @@ import type { UseQueryResult } from '@tanstack/react-query'; -import { Column, Row, SearchField } from '@umami/react-zen'; +import { + Button, + Column, + Icon, + Row, + SearchField, + Text, + Tooltip, + TooltipTrigger, +} from '@umami/react-zen'; +import { LayoutGrid, Table2 } from 'lucide-react'; import { cloneElement, isValidElement, @@ -12,9 +22,13 @@ import { Empty } from '@/components/common/Empty'; import { LoadingPanel } from '@/components/common/LoadingPanel'; import { Pager } from '@/components/common/Pager'; import { useMessages, useMobile, useNavigation } from '@/components/hooks'; +import { getItem, setItem } from '@/lib/storage'; import type { PageResult } from '@/lib/types'; const DEFAULT_SEARCH_DELAY = 600; +const DISPLAY_MODE_STORAGE_KEY = 'umami.datagrid.displayMode'; + +type DisplayMode = 'table' | 'cards'; export interface DataGridProps { query: UseQueryResult, any>; @@ -43,7 +57,19 @@ export function DataGrid({ const [search, setSearch] = useState(queryParams?.search || data?.search || ''); const showPager = allowPaging && data && data.count > data.pageSize; const { isMobile } = useMobile(); - const displayMode = isMobile ? 'cards' : undefined; + const [userDisplayMode, setUserDisplayMode] = useState( + () => getItem(DISPLAY_MODE_STORAGE_KEY) ?? null, + ); + + // Effective mode: explicit user choice wins, otherwise fall back to the + // mobile-driven default (cards on small viewports, table elsewhere). + const displayMode: DisplayMode | undefined = userDisplayMode ?? (isMobile ? 'cards' : undefined); + + const handleToggleDisplayMode = () => { + const next: DisplayMode = displayMode === 'cards' ? 'table' : 'cards'; + setItem(DISPLAY_MODE_STORAGE_KEY, next); + setUserDisplayMode(next); + }; const handleSearch = (value: string) => { if (value !== search) { @@ -61,10 +87,21 @@ export function DataGrid({ const child = data ? (typeof children === 'function' ? children(data) : children) : null; + const viewToggleButton = ( + + + + {displayMode === 'cards' ? 'Switch to table view' : 'Switch to card view'} + + + ); + return ( - {allowSearch && ( - + + {allowSearch ? ( + ) : ( + + )} + {renderActions?.()} + {viewToggleButton} - )} +