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.
This commit is contained in:
1 parent
a9508e7aae
commit
6ff13544ce
1 file changed
+47
-5
@@ -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<PageResult<any>, 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<DisplayMode | null>(
|
||||
() => 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 = (
|
||||
<TooltipTrigger delay={0}>
|
||||
<Button variant="zero" onPress={handleToggleDisplayMode}>
|
||||
<Icon>{displayMode === 'cards' ? <Table2 /> : <LayoutGrid />}</Icon>
|
||||
</Button>
|
||||
<Tooltip>
|
||||
<Text>{displayMode === 'cards' ? 'Switch to table view' : 'Switch to card view'}</Text>
|
||||
</Tooltip>
|
||||
</TooltipTrigger>
|
||||
);
|
||||
|
||||
return (
|
||||
<Column gap="4" minHeight="300px">
|
||||
{allowSearch && (
|
||||
<Row alignItems="center" justifyContent="space-between" wrap="wrap" gap>
|
||||
<Row alignItems="center" justifyContent="space-between" wrap="wrap" gap>
|
||||
{allowSearch ? (
|
||||
<SearchField
|
||||
value={search}
|
||||
onSearch={handleSearch}
|
||||
@@ -72,9 +109,14 @@ export function DataGrid({
|
||||
autoFocus={autoFocus}
|
||||
placeholder={t(labels.search)}
|
||||
/>
|
||||
) : (
|
||||
<span />
|
||||
)}
|
||||
<Row alignItems="center" gap>
|
||||
{renderActions?.()}
|
||||
{viewToggleButton}
|
||||
</Row>
|
||||
)}
|
||||
</Row>
|
||||
<LoadingPanel
|
||||
data={data?.data}
|
||||
isLoading={isLoading}
|
||||
|
||||
Reference in new issue
Block a user