feat(dashboard): merge rearrange into the Customize modal
Two days, two modals, both touching widget layout - collapsed into
one. The separate "Rearrange" button + RearrangeWidgetsDialog from
54c5d0f are gone; the Customize modal now does both jobs:
- Two sections in the body: "On dashboard (N)" and "Hidden (N)"
- Visible rows are sortable (drag handle on the left, position number,
switch on the right). Single SortableContext, vertical strategy.
- Hidden rows are toggle-only (no drag handle - order doesn't matter
for off-dashboard widgets). Flipping the switch on appends to the
bottom of the visible section.
- Both visibility toggles and reorder commits optimistically via
useDashboardWidgets so the dashboard reflows in the background.
dashboard-shell: removes the Rearrange button + RearrangeWidgetsDialog
import + setOrder destructure. rearrange-widgets-dialog.tsx deleted.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,7 +1,24 @@
|
||||
'use client';
|
||||
|
||||
import { useState } from 'react';
|
||||
import { LayoutGrid } from 'lucide-react';
|
||||
import {
|
||||
DndContext,
|
||||
KeyboardSensor,
|
||||
PointerSensor,
|
||||
closestCenter,
|
||||
useSensor,
|
||||
useSensors,
|
||||
type DragEndEvent,
|
||||
} from '@dnd-kit/core';
|
||||
import {
|
||||
SortableContext,
|
||||
arrayMove,
|
||||
sortableKeyboardCoordinates,
|
||||
useSortable,
|
||||
verticalListSortingStrategy,
|
||||
} from '@dnd-kit/sortable';
|
||||
import { CSS } from '@dnd-kit/utilities';
|
||||
import { GripVertical, LayoutGrid } from 'lucide-react';
|
||||
|
||||
import { Button } from '@/components/ui/button';
|
||||
import {
|
||||
@@ -14,29 +31,64 @@ import {
|
||||
DialogTrigger,
|
||||
} from '@/components/ui/dialog';
|
||||
import { Switch } from '@/components/ui/switch';
|
||||
import { cn } from '@/lib/utils';
|
||||
import { useDashboardWidgets } from '@/hooks/use-dashboard-widgets';
|
||||
import type { DashboardWidget } from './widget-registry';
|
||||
|
||||
/**
|
||||
* Modal widget picker for the dashboard header. Replaced the original
|
||||
* dropdown menu because 13 widgets + 3 footer buttons made the dropdown
|
||||
* cramped and hid the descriptions reps need to know what each card
|
||||
* actually shows.
|
||||
* Combined visibility + reorder picker for the dashboard header. Two
|
||||
* sections in one modal:
|
||||
*
|
||||
* Backed by the same `useDashboardWidgets` hook that drives the
|
||||
* Settings card — toggles update both surfaces optimistically.
|
||||
* 1. "On dashboard" — visible widgets, each row with a drag handle
|
||||
* (reorder via dnd-kit single SortableContext, no buckets); flipping
|
||||
* a switch off moves the row to section 2.
|
||||
* 2. "Hidden" — widgets currently off; flipping a switch on appends to
|
||||
* the bottom of section 1.
|
||||
*
|
||||
* Both visibility toggles and order changes commit optimistically via
|
||||
* `useDashboardWidgets` so the dashboard reflows in the background and
|
||||
* the rep can keep editing. The "Rearrange" button on the header is
|
||||
* gone — order lives here too now, keeping all dashboard layout
|
||||
* controls in one place.
|
||||
*/
|
||||
export function CustomizeWidgetsMenu() {
|
||||
const [open, setOpen] = useState(false);
|
||||
const { allWidgets, visibility, setVisible, setAll, resetToDefaults, isSaving } =
|
||||
useDashboardWidgets();
|
||||
const {
|
||||
allWidgets,
|
||||
visibleWidgets,
|
||||
visibility,
|
||||
setVisible,
|
||||
setAll,
|
||||
setOrder,
|
||||
resetToDefaults,
|
||||
isSaving,
|
||||
} = useDashboardWidgets();
|
||||
|
||||
const visibleCount = Object.values(visibility).filter(Boolean).length;
|
||||
const allVisible = visibleCount === allWidgets.length;
|
||||
const allHidden = visibleCount === 0;
|
||||
// Reset is a no-op when state already matches the registry defaults —
|
||||
// disable in that case to avoid pointless API round-trips.
|
||||
const matchesDefaults = allWidgets.every((w) => (visibility[w.id] ?? false) === w.defaultVisible);
|
||||
|
||||
// Hidden = everything not currently rendered. Sorted by registry order
|
||||
// so it reads predictably (newly-added widgets appear at the bottom
|
||||
// until the rep explicitly enables them).
|
||||
const hidden = allWidgets.filter((w) => !visibility[w.id]);
|
||||
|
||||
const sensors = useSensors(
|
||||
useSensor(PointerSensor, { activationConstraint: { distance: 5 } }),
|
||||
useSensor(KeyboardSensor, { coordinateGetter: sortableKeyboardCoordinates }),
|
||||
);
|
||||
|
||||
function onDragEnd(event: DragEndEvent) {
|
||||
const { active, over } = event;
|
||||
if (!over || active.id === over.id) return;
|
||||
const ids = visibleWidgets.map((w) => w.id);
|
||||
const oldIndex = ids.indexOf(String(active.id));
|
||||
const newIndex = ids.indexOf(String(over.id));
|
||||
if (oldIndex === -1 || newIndex === -1) return;
|
||||
setOrder(arrayMove(ids, oldIndex, newIndex));
|
||||
}
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={setOpen}>
|
||||
<DialogTrigger asChild>
|
||||
@@ -49,40 +101,57 @@ export function CustomizeWidgetsMenu() {
|
||||
<DialogHeader>
|
||||
<DialogTitle>Customize dashboard</DialogTitle>
|
||||
<DialogDescription>
|
||||
Pick which analytics cards appear on your dashboard. Hidden cards leave no empty space -
|
||||
the layout reflows to fill the available width.
|
||||
Drag a visible widget to change its position. Toggle the switch to show or hide. Hidden
|
||||
widgets leave no empty space - the layout reflows to fill the available width.
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
{/* Toggle list. Capped at ~60vh with internal scroll so the modal
|
||||
doesn't push the action footer off-screen on shorter viewports. */}
|
||||
{/* Toggle + reorder list. Capped at ~60vh with internal scroll so
|
||||
the modal doesn't push the action footer off-screen. */}
|
||||
<div className="max-h-[60vh] -mx-2 overflow-y-auto px-2">
|
||||
<div className="space-y-1 py-1">
|
||||
{allWidgets.map((w) => (
|
||||
<label
|
||||
key={w.id}
|
||||
className="flex cursor-pointer items-start justify-between gap-4 rounded-md px-3 py-2.5 hover:bg-accent/40"
|
||||
{visibleWidgets.length > 0 ? (
|
||||
<Section title={`On dashboard (${visibleWidgets.length})`}>
|
||||
<DndContext
|
||||
sensors={sensors}
|
||||
collisionDetection={closestCenter}
|
||||
onDragEnd={onDragEnd}
|
||||
>
|
||||
<div className="min-w-0 flex-1">
|
||||
<div className="text-sm font-medium text-foreground">{w.label}</div>
|
||||
<p className="text-xs text-muted-foreground">{w.description}</p>
|
||||
</div>
|
||||
<Switch
|
||||
aria-label={`Show ${w.label}`}
|
||||
checked={visibility[w.id] ?? false}
|
||||
disabled={isSaving}
|
||||
onCheckedChange={(checked) => setVisible(w.id, checked)}
|
||||
className="mt-0.5 shrink-0"
|
||||
/>
|
||||
</label>
|
||||
))}
|
||||
</div>
|
||||
<SortableContext
|
||||
items={visibleWidgets.map((w) => w.id)}
|
||||
strategy={verticalListSortingStrategy}
|
||||
>
|
||||
<ul className="space-y-1">
|
||||
{visibleWidgets.map((w, idx) => (
|
||||
<SortableVisibleRow
|
||||
key={w.id}
|
||||
widget={w}
|
||||
position={idx + 1}
|
||||
disabled={isSaving}
|
||||
onToggle={(checked) => setVisible(w.id, checked)}
|
||||
/>
|
||||
))}
|
||||
</ul>
|
||||
</SortableContext>
|
||||
</DndContext>
|
||||
</Section>
|
||||
) : null}
|
||||
|
||||
{hidden.length > 0 ? (
|
||||
<Section title={`Hidden (${hidden.length})`}>
|
||||
<ul className="space-y-1">
|
||||
{hidden.map((w) => (
|
||||
<HiddenRow
|
||||
key={w.id}
|
||||
widget={w}
|
||||
disabled={isSaving}
|
||||
onToggle={(checked) => setVisible(w.id, checked)}
|
||||
/>
|
||||
))}
|
||||
</ul>
|
||||
</Section>
|
||||
) : null}
|
||||
</div>
|
||||
|
||||
{/* Footer: stacks vertically on mobile (counter row, secondary
|
||||
buttons row, full-width primary "Done") so no button gets
|
||||
orphaned beneath the others. Reverts to single inline row at
|
||||
sm+ where there's space. */}
|
||||
<DialogFooter className="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between sm:gap-2">
|
||||
<span className="text-xs text-muted-foreground sm:order-first">
|
||||
{visibleCount} of {allWidgets.length} visible
|
||||
@@ -121,3 +190,90 @@ export function CustomizeWidgetsMenu() {
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
|
||||
function Section({ title, children }: { title: string; children: React.ReactNode }) {
|
||||
return (
|
||||
<div className="py-2 first:pt-1">
|
||||
<div className="px-1 pb-1.5 text-[11px] font-semibold uppercase tracking-wide text-muted-foreground">
|
||||
{title}
|
||||
</div>
|
||||
{children}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function SortableVisibleRow({
|
||||
widget,
|
||||
position,
|
||||
disabled,
|
||||
onToggle,
|
||||
}: {
|
||||
widget: DashboardWidget;
|
||||
position: number;
|
||||
disabled: boolean;
|
||||
onToggle: (checked: boolean) => void;
|
||||
}) {
|
||||
const { attributes, listeners, setNodeRef, transform, transition, isDragging } = useSortable({
|
||||
id: widget.id,
|
||||
});
|
||||
return (
|
||||
<li
|
||||
ref={setNodeRef}
|
||||
style={{ transform: CSS.Transform.toString(transform), transition }}
|
||||
className={cn(
|
||||
'flex items-start gap-2 rounded-md border bg-background px-2 py-2.5',
|
||||
isDragging && 'opacity-60 shadow-md',
|
||||
)}
|
||||
>
|
||||
<button
|
||||
type="button"
|
||||
className="mt-0.5 inline-flex h-7 w-7 shrink-0 cursor-grab items-center justify-center rounded text-muted-foreground hover:bg-accent active:cursor-grabbing"
|
||||
aria-label={`Drag handle for ${widget.label}`}
|
||||
{...attributes}
|
||||
{...listeners}
|
||||
>
|
||||
<GripVertical className="h-4 w-4" aria-hidden />
|
||||
</button>
|
||||
<span className="mt-1 w-5 shrink-0 text-right text-xs tabular-nums text-muted-foreground">
|
||||
{position}
|
||||
</span>
|
||||
<div className="min-w-0 flex-1">
|
||||
<div className="text-sm font-medium text-foreground">{widget.label}</div>
|
||||
<p className="text-xs text-muted-foreground">{widget.description}</p>
|
||||
</div>
|
||||
<Switch
|
||||
aria-label={`Show ${widget.label}`}
|
||||
checked
|
||||
disabled={disabled}
|
||||
onCheckedChange={onToggle}
|
||||
className="mt-0.5 shrink-0"
|
||||
/>
|
||||
</li>
|
||||
);
|
||||
}
|
||||
|
||||
function HiddenRow({
|
||||
widget,
|
||||
disabled,
|
||||
onToggle,
|
||||
}: {
|
||||
widget: DashboardWidget;
|
||||
disabled: boolean;
|
||||
onToggle: (checked: boolean) => void;
|
||||
}) {
|
||||
return (
|
||||
<li className="flex items-start gap-3 rounded-md px-3 py-2.5 hover:bg-accent/40">
|
||||
<div className="min-w-0 flex-1">
|
||||
<div className="text-sm font-medium text-foreground">{widget.label}</div>
|
||||
<p className="text-xs text-muted-foreground">{widget.description}</p>
|
||||
</div>
|
||||
<Switch
|
||||
aria-label={`Show ${widget.label}`}
|
||||
checked={false}
|
||||
disabled={disabled}
|
||||
onCheckedChange={onToggle}
|
||||
className="mt-0.5 shrink-0"
|
||||
/>
|
||||
</li>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user