Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions examples/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,70 @@ func main() {
},
},
},
{
Type: admin.CustomPageSectionCharts,
Title: "Audience",
Children: []admin.Chart{
{
Type: admin.ChartTypeLine,
Title: "Subscribers",
Data: []map[string]any{
{"day": "Mon", "subscribers": 42},
{"day": "Tue", "subscribers": 58},
{"day": "Wed", "subscribers": 73},
{"day": "Thu", "subscribers": 81},
{"day": "Fri", "subscribers": 96},
},
XKey: "day",
YKey: "subscribers",
},
},
},
{
Type: admin.CustomPageSectionCharts,
Title: "Publishing pipeline",
Children: []admin.Chart{
{
Type: admin.ChartTypeLine,
Title: "Drafts created",
Data: []map[string]any{
{"day": "Mon", "drafts": 4},
{"day": "Tue", "drafts": 7},
{"day": "Wed", "drafts": 5},
{"day": "Thu", "drafts": 9},
{"day": "Fri", "drafts": 6},
},
XKey: "day",
YKey: "drafts",
},
{
Type: admin.ChartTypeLine,
Title: "Reviews completed",
Data: []map[string]any{
{"day": "Mon", "reviews": 3},
{"day": "Tue", "reviews": 4},
{"day": "Wed", "reviews": 6},
{"day": "Thu", "reviews": 5},
{"day": "Fri", "reviews": 8},
},
XKey: "day",
YKey: "reviews",
},
{
Type: admin.ChartTypeLine,
Title: "Posts scheduled",
Data: []map[string]any{
{"day": "Mon", "scheduled": 2},
{"day": "Tue", "scheduled": 3},
{"day": "Wed", "scheduled": 4},
{"day": "Thu", "scheduled": 7},
{"day": "Fri", "scheduled": 5},
},
XKey: "day",
YKey: "scheduled",
},
},
},
{
Type: admin.CustomPageSectionText,
Title: "Editorial note",
Expand Down
8 changes: 7 additions & 1 deletion examples/web/e2e/crud.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,13 @@ test("custom dashboard page renders from the backend schema", async ({ page }) =
await expect(main.getByText("Published posts")).toBeVisible();
await expect(main.getByText("Monthly views")).toBeVisible();
await expect(main.getByText("Views by day")).toBeVisible();
await expect(main.getByText("views", { exact: true })).toBeVisible();
await expect(main.getByText("Engagement")).toBeVisible();
await expect(main.getByText("Audience")).toBeVisible();
const audienceSection = main.locator("section").filter({ hasText: "Audience" });
await expect(audienceSection.getByText(/^Subscribers$/)).toBeVisible();
await expect(
audienceSection.locator('[data-chart-type="line"]'),
).toHaveCount(1);
await expect(main.getByText("Editorial note")).toBeVisible();
await expect(main.getByText("Review draft posts weekly")).toBeVisible();

Expand All @@ -66,6 +71,7 @@ test("custom dashboard page renders from the backend schema", async ({ page }) =

await main
.locator('[data-chart-type="line"] svg[role="application"]')
.first()
.hover({ position: { x: 80, y: 120 } });
await expect(tooltip).toContainText("Tue");
await expect(tooltip).toContainText("Reads");
Expand Down
194 changes: 104 additions & 90 deletions packages/admin-next/src/components/custom-resource-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
BarChart,
CartesianGrid,
Legend,
ResponsiveContainer,
Tooltip,
XAxis,
YAxis,
Expand Down Expand Up @@ -113,21 +114,34 @@ function ChartSection({
}: {
section: Extract<CustomPageSection, { type: "charts" }>;
}): React.JSX.Element {
const charts = section.children ?? [];
const gridClassName =
charts.length === 1 ? "grid gap-3" : "grid gap-3 @2xl/main:grid-cols-2";

return (
<section>
<SectionHeading title={section.title} description={section.description} />
<div className="grid gap-3 @2xl/main:grid-cols-2">
{(section.children ?? []).map((chart, index) => (
<div
key={`${chart.title ?? chart.type}:${index}`}
className="rounded-lg border border-border p-4"
>
{chart.title && (
<div className="mb-3 text-sm font-medium">{chart.title}</div>
)}
<SimpleChart chart={chart} />
</div>
))}
<div className={gridClassName}>
{charts.map((chart, index) => {
const isOddLastChart =
charts.length > 1 &&
charts.length % 2 === 1 &&
index === charts.length - 1;

return (
<div
key={`${chart.title ?? chart.type}:${index}`}
className={`rounded-lg border border-border p-4${
isOddLastChart ? " @2xl/main:col-span-2" : ""
}`}
>
{chart.title && (
<div className="mb-3 text-sm font-medium">{chart.title}</div>
)}
<SimpleChart chart={chart} />
</div>
);
})}
</div>
</section>
);
Expand Down Expand Up @@ -201,88 +215,88 @@ function SimpleChart({ chart }: { chart: Chart }): React.JSX.Element {
}

return (
<div className="h-64 w-full" data-chart-type={kind}>
{kind === "line" ? (
<AreaChart
width={480}
height={250}
data={data}
margin={{ top: 8, right: 16, bottom: 0, left: 0 }}
style={{ width: "100%", height: "100%" }}
accessibilityLayer
>
<CartesianGrid stroke="var(--border)" vertical={false} />
<XAxis
dataKey={chart.xKey}
axisLine={false}
tickLine={false}
tick={{ fill: "var(--muted-foreground)", fontSize: 12 }}
/>
<YAxis hide />
<Tooltip
content={<ChartTooltip />}
cursor={{ stroke: "var(--border)" }}
offset={0}
/>
<Legend
iconType="circle"
wrapperStyle={{ color: "var(--muted-foreground)", fontSize: 12 }}
/>
{series.map((s, index) => {
const color = s.color ?? paletteColor(index);
return (
<Area
<div className="w-full" data-chart-type={kind}>
<ResponsiveContainer
width="100%"
height={256}
minWidth={0}
>
{kind === "line" ? (
<AreaChart
data={data}
margin={{ top: 8, right: 16, bottom: 0, left: 0 }}
accessibilityLayer
>
<CartesianGrid stroke="var(--border)" vertical={false} />
<XAxis
dataKey={chart.xKey}
axisLine={false}
tickLine={false}
tick={{ fill: "var(--muted-foreground)", fontSize: 12 }}
/>
<YAxis hide />
<Tooltip
content={<ChartTooltip />}
cursor={{ stroke: "var(--border)" }}
offset={0}
/>
<Legend
iconType="circle"
wrapperStyle={{ color: "var(--muted-foreground)", fontSize: 12 }}
/>
{series.map((s, index) => {
const color = s.color ?? paletteColor(index);
return (
<Area
key={s.key}
type="monotone"
dataKey={s.key}
name={s.label ?? s.key}
stroke={color}
fill={color}
fillOpacity={0.12}
strokeWidth={3}
dot={{ r: 4, fill: color, strokeWidth: 0 }}
activeDot={{ r: 5 }}
/>
);
})}
</AreaChart>
) : (
<BarChart
data={data}
margin={{ top: 8, right: 16, bottom: 0, left: 0 }}
accessibilityLayer
>
<CartesianGrid stroke="var(--border)" vertical={false} />
<XAxis
dataKey={chart.xKey}
axisLine={false}
tickLine={false}
tick={{ fill: "var(--muted-foreground)", fontSize: 12 }}
/>
<YAxis hide />
<Tooltip
content={<ChartTooltip />}
cursor={{ fill: "var(--muted)" }}
offset={0}
/>
<Legend
iconType="circle"
wrapperStyle={{ color: "var(--muted-foreground)", fontSize: 12 }}
/>
{series.map((s, index) => (
<Bar
key={s.key}
type="monotone"
dataKey={s.key}
name={s.label ?? s.key}
stroke={color}
fill={color}
fillOpacity={0.12}
strokeWidth={3}
dot={{ r: 4, fill: color, strokeWidth: 0 }}
activeDot={{ r: 5 }}
fill={s.color ?? paletteColor(index)}
radius={[4, 4, 0, 0]}
/>
);
})}
</AreaChart>
) : (
<BarChart
width={480}
height={250}
data={data}
margin={{ top: 8, right: 16, bottom: 0, left: 0 }}
style={{ width: "100%", height: "100%" }}
accessibilityLayer
>
<CartesianGrid stroke="var(--border)" vertical={false} />
<XAxis
dataKey={chart.xKey}
axisLine={false}
tickLine={false}
tick={{ fill: "var(--muted-foreground)", fontSize: 12 }}
/>
<YAxis hide />
<Tooltip
content={<ChartTooltip />}
cursor={{ fill: "var(--muted)" }}
offset={0}
/>
<Legend
iconType="circle"
wrapperStyle={{ color: "var(--muted-foreground)", fontSize: 12 }}
/>
{series.map((s, index) => (
<Bar
key={s.key}
dataKey={s.key}
name={s.label ?? s.key}
fill={s.color ?? paletteColor(index)}
radius={[4, 4, 0, 0]}
/>
))}
</BarChart>
)}
))}
</BarChart>
)}
</ResponsiveContainer>
</div>
);
}
Expand Down
47 changes: 42 additions & 5 deletions packages/admin-next/src/components/resource-view.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
import type { CustomResourcePage, ResourceInfo, TableSchema } from "../types.js";
import type { AdminActions } from "../server/actions.js";
Expand All @@ -24,10 +24,45 @@ const noopActions: AdminActions = {
submitAction: vi.fn(),
};

class TestResizeObserver implements ResizeObserver {
private callback: ResizeObserverCallback;

constructor(callback: ResizeObserverCallback) {
this.callback = callback;
}

observe(target: Element): void {
this.callback(
[
{
target,
contentRect: DOMRectReadOnly.fromRect({
width: 640,
height: 256,
}),
} as ResizeObserverEntry,
],
this,
);
}

unobserve(): void {}

disconnect(): void {}
}

beforeEach(() => {
vi.stubGlobal("ResizeObserver", TestResizeObserver);
vi.spyOn(Element.prototype, "getBoundingClientRect").mockReturnValue(
DOMRect.fromRect({ width: 640, height: 256 }),
);
});

afterEach(() => {
routerPush.mockReset();
window.history.replaceState(null, "", "/");
vi.restoreAllMocks();
vi.unstubAllGlobals();
});

describe("ResourceView", () => {
Expand Down Expand Up @@ -233,7 +268,7 @@ describe("ResourceView", () => {
expect(routerPush).not.toHaveBeenCalled();
});

it("renders a custom resource page with actions and sections", () => {
it("renders a custom resource page with actions and sections", async () => {
const customSchema: CustomResourcePage = {
uiType: "custom",
type: "view",
Expand Down Expand Up @@ -310,9 +345,11 @@ describe("ResourceView", () => {
expect(screen.getByText("Posts")).toBeTruthy();
expect(screen.getByText("25")).toBeTruthy();
expect(screen.getByText("Views")).toBeTruthy();
expect(screen.getByText("views")).toBeTruthy();
expect(screen.getByText("Reads")).toBeTruthy();
expect(screen.getByText("Shares")).toBeTruthy();
await waitFor(() => {
expect(screen.getByText("views")).toBeTruthy();
expect(screen.getByText("Reads")).toBeTruthy();
expect(screen.getByText("Shares")).toBeTruthy();
});
expect(screen.getByText("Review pending posts before publishing.")).toBeTruthy();
});
});