fix: dashboard edit mode renders live widgets
This commit is contained in:
@@ -1,11 +1,12 @@
|
|||||||
import { notFound } from "next/navigation";
|
import { notFound } from "next/navigation";
|
||||||
import { Suspense } from "react";
|
import { Suspense } from "react";
|
||||||
import { getCurrentSession } from "@/lib/session";
|
import { getCurrentSession } from "@/lib/session";
|
||||||
import { parseDashboardLayout } from "@/lib/dashboard";
|
import { parseDashboardLayout, widgetContentKey } from "@/lib/dashboard";
|
||||||
import { computeDefaultLayout } from "@/lib/dashboard.server";
|
import { computeDefaultLayout } from "@/lib/dashboard.server";
|
||||||
import { getWidget, getWidgetMetas } from "@/modules/_core";
|
import { getWidget, getWidgetMetas } from "@/modules/_core";
|
||||||
import { getDashboardBySlug } from "@/app/d/actions";
|
import { getDashboardBySlug } from "@/app/d/actions";
|
||||||
import { DashboardEditor } from "@/components/dashboard-editor";
|
import { DashboardEditor } from "@/components/dashboard-editor";
|
||||||
|
import { DashboardWidgetContent } from "@/components/dashboard-widget-content";
|
||||||
import { EditDashboardButton } from "@/components/edit-dashboard-button";
|
import { EditDashboardButton } from "@/components/edit-dashboard-button";
|
||||||
import { DashboardSwitcher } from "@/components/dashboard-switcher";
|
import { DashboardSwitcher } from "@/components/dashboard-switcher";
|
||||||
import { DashboardTab } from "@/components/dashboard-tab";
|
import { DashboardTab } from "@/components/dashboard-tab";
|
||||||
@@ -52,11 +53,24 @@ export default async function DashboardPage({
|
|||||||
const widgetMetas = getWidgetMetas();
|
const widgetMetas = getWidgetMetas();
|
||||||
|
|
||||||
if (isEditing) {
|
if (isEditing) {
|
||||||
|
const ctx = { userId: user.id, householdId: household.id };
|
||||||
|
const widgetContents = Object.fromEntries(
|
||||||
|
layout.widgets.map((placement) => [
|
||||||
|
widgetContentKey(placement),
|
||||||
|
<DashboardWidgetContent
|
||||||
|
key={widgetContentKey(placement)}
|
||||||
|
placement={placement}
|
||||||
|
ctx={ctx}
|
||||||
|
/>,
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<DashboardEditor
|
<DashboardEditor
|
||||||
dashboard={{ id: dashboard.id, name: dashboard.name, slug: dashboard.slug }}
|
dashboard={{ id: dashboard.id, name: dashboard.name, slug: dashboard.slug }}
|
||||||
layout={layout}
|
layout={layout}
|
||||||
widgetMetas={widgetMetas}
|
widgetMetas={widgetMetas}
|
||||||
|
widgetContents={widgetContents}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,13 +3,13 @@
|
|||||||
import "react-grid-layout/css/styles.css";
|
import "react-grid-layout/css/styles.css";
|
||||||
import "react-resizable/css/styles.css";
|
import "react-resizable/css/styles.css";
|
||||||
|
|
||||||
import { useEffect, useState, useTransition } from "react";
|
import { useEffect, useState, useTransition, type ReactNode } from "react";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
import { GridLayout } from "react-grid-layout";
|
import { GridLayout } from "react-grid-layout";
|
||||||
import type { Layout } from "react-grid-layout";
|
import type { Layout } from "react-grid-layout";
|
||||||
import { GripVertical, Settings2, Trash2, RotateCcw, Plus, LayoutGrid } from "lucide-react";
|
import { GripVertical, Settings2, Trash2, RotateCcw, Plus, LayoutGrid } from "lucide-react";
|
||||||
import type { DashboardLayout, WidgetPlacement, PresetId } from "@/lib/dashboard";
|
import type { DashboardLayout, WidgetPlacement, PresetId } from "@/lib/dashboard";
|
||||||
import { computePresetLayoutFromMetas } from "@/lib/dashboard";
|
import { computePresetLayoutFromMetas, widgetContentKey } from "@/lib/dashboard";
|
||||||
import type { SerializedWidgetMeta } from "@/modules/_core/registry";
|
import type { SerializedWidgetMeta } from "@/modules/_core/registry";
|
||||||
import { saveDashboardLayout, resetDashboardLayout } from "@/app/d/actions";
|
import { saveDashboardLayout, resetDashboardLayout } from "@/app/d/actions";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
@@ -23,10 +23,12 @@ export function DashboardEditor({
|
|||||||
dashboard,
|
dashboard,
|
||||||
layout: initialLayout,
|
layout: initialLayout,
|
||||||
widgetMetas,
|
widgetMetas,
|
||||||
|
widgetContents,
|
||||||
}: {
|
}: {
|
||||||
dashboard: { id: string; name: string; slug: string };
|
dashboard: { id: string; name: string; slug: string };
|
||||||
layout: DashboardLayout;
|
layout: DashboardLayout;
|
||||||
widgetMetas: SerializedWidgetMeta[];
|
widgetMetas: SerializedWidgetMeta[];
|
||||||
|
widgetContents: Record<string, ReactNode>;
|
||||||
}) {
|
}) {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const [isPending, startTransition] = useTransition();
|
const [isPending, startTransition] = useTransition();
|
||||||
@@ -213,6 +215,7 @@ export function DashboardEditor({
|
|||||||
>
|
>
|
||||||
{placements.map((placement, i) => {
|
{placements.map((placement, i) => {
|
||||||
const meta = widgetMetas.find((m) => m.id === placement.widgetId);
|
const meta = widgetMetas.find((m) => m.id === placement.widgetId);
|
||||||
|
const content = widgetContents[widgetContentKey(placement)];
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
key={placementKey(placement, i)}
|
key={placementKey(placement, i)}
|
||||||
@@ -240,8 +243,12 @@ export function DashboardEditor({
|
|||||||
<Trash2 className="size-4" />
|
<Trash2 className="size-4" />
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex-1 flex items-center justify-center p-4">
|
<div className="flex-1 min-h-0 overflow-auto p-4">
|
||||||
<p className="text-xs text-muted-foreground">{meta?.description}</p>
|
{content ?? (
|
||||||
|
<p className="text-xs text-muted-foreground text-center">
|
||||||
|
{meta?.description ?? "Preview available after save"}
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
import { Suspense } from "react";
|
||||||
|
import type { WidgetPlacement } from "@/lib/dashboard";
|
||||||
|
import { getWidget } from "@/modules/_core";
|
||||||
|
import type { WidgetContext } from "@/modules/_core/module";
|
||||||
|
import { Skeleton } from "@/components/ui/skeleton";
|
||||||
|
|
||||||
|
export function DashboardWidgetContent({
|
||||||
|
placement,
|
||||||
|
ctx,
|
||||||
|
}: {
|
||||||
|
placement: WidgetPlacement;
|
||||||
|
ctx: WidgetContext;
|
||||||
|
}) {
|
||||||
|
const widget = getWidget(placement.widgetId);
|
||||||
|
if (!widget) {
|
||||||
|
return <p className="text-xs text-muted-foreground">Unknown widget</p>;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Suspense
|
||||||
|
fallback={
|
||||||
|
<div className="space-y-3">
|
||||||
|
<Skeleton className="h-4 w-3/4" />
|
||||||
|
<Skeleton className="h-4 w-1/2" />
|
||||||
|
<Skeleton className="h-4 w-2/3" />
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
>
|
||||||
|
{widget.render({ config: placement.config, ctx })}
|
||||||
|
</Suspense>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -10,6 +10,10 @@ export type WidgetPlacement = {
|
|||||||
h: number;
|
h: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export function widgetContentKey(placement: Pick<WidgetPlacement, "widgetId" | "config">): string {
|
||||||
|
return `${placement.widgetId}::${JSON.stringify(placement.config)}`;
|
||||||
|
}
|
||||||
|
|
||||||
export type DashboardLayout = {
|
export type DashboardLayout = {
|
||||||
version: 1;
|
version: 1;
|
||||||
widgets: WidgetPlacement[];
|
widgets: WidgetPlacement[];
|
||||||
|
|||||||
@@ -1,7 +1,17 @@
|
|||||||
import { expect, test } from "@playwright/test";
|
import { expect, test, type Page } from "@playwright/test";
|
||||||
|
|
||||||
|
async function ensureSignedIn(page: Page) {
|
||||||
|
await page.goto("/");
|
||||||
|
const devLogin = page.getByRole("button", { name: "Dev login" });
|
||||||
|
if (await devLogin.isVisible().catch(() => false)) {
|
||||||
|
await devLogin.click();
|
||||||
|
await page.waitForURL((url) => !url.pathname.startsWith("/login"));
|
||||||
|
}
|
||||||
|
await expect(page.getByText("Upcoming events", { exact: true })).toBeVisible();
|
||||||
|
}
|
||||||
|
|
||||||
test("dashboard happy path", async ({ page }) => {
|
test("dashboard happy path", async ({ page }) => {
|
||||||
await page.goto("/");
|
await ensureSignedIn(page);
|
||||||
|
|
||||||
// Page title is present
|
// Page title is present
|
||||||
await expect(page.getByRole("heading", { name: "Dashboard" })).toBeVisible();
|
await expect(page.getByRole("heading", { name: "Dashboard" })).toBeVisible();
|
||||||
@@ -24,3 +34,20 @@ test("dashboard happy path", async ({ page }) => {
|
|||||||
const viewportWidth = await page.evaluate(() => window.innerWidth);
|
const viewportWidth = await page.evaluate(() => window.innerWidth);
|
||||||
expect(bodyWidth).toBeLessThanOrEqual(viewportWidth + 2); // 2px tolerance for sub-pixel rendering
|
expect(bodyWidth).toBeLessThanOrEqual(viewportWidth + 2); // 2px tolerance for sub-pixel rendering
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("dashboard edit mode shows live widget content", async ({ page }) => {
|
||||||
|
await ensureSignedIn(page);
|
||||||
|
await page.goto("/d/home?edit=1");
|
||||||
|
|
||||||
|
await expect(page.getByText("Drag to reorder")).toBeVisible();
|
||||||
|
await expect(page.getByRole("button", { name: "Save" })).toBeVisible();
|
||||||
|
|
||||||
|
const main = page.getByRole("main");
|
||||||
|
await expect(main.getByText("Recent activity", { exact: true })).toBeVisible();
|
||||||
|
|
||||||
|
// Server-rendered widget body (empty-state copy), not the meta description placeholder
|
||||||
|
await expect(
|
||||||
|
main.getByText(/No recent activity|No events in the next|No pinned notes|No notes/i).first(),
|
||||||
|
).toBeVisible();
|
||||||
|
await expect(main.getByText("Latest changes across your household.")).not.toBeVisible();
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user