Skip to content
Open
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
47 changes: 47 additions & 0 deletions src/components/DebugInfo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { memo } from "react";
import css from "./css/DebugInfo.module.css";
import sharedStyles from "./css/shared.module.css";
import type { World } from "../api/worlds.ts";

type Snapshot = {
selectedWorldId: string;
hello: { worlds: World[] | undefined };
};

type Props = {
/**
* Snapshot data to display.
*/
snapshot: Snapshot | null;
/**
* Optional title for the debug panel. Defaults to "Store snapshot".
*/
title?: string;
};

export const DebugInfo = memo(({ snapshot, title }: Props) => {
const displayTitle = title || "Store snapshot";

const pretty =
snapshot != null
? JSON.stringify(
{
selectedWorldId: snapshot.selectedWorldId,
hello: {
worlds: snapshot.hello.worlds?.map((world) => ({ ...world })),
},
},
null,
2,
)
: "Snapshot pending…";

return (
<div className={`${sharedStyles.card} ${css.snapshot}`}>
<div className={css.snapshotTitle}>{displayTitle}</div>
<pre className={css.pre}>{pretty}</pre>
</div>
);
});

DebugInfo.displayName = "DebugInfo";
4 changes: 2 additions & 2 deletions src/examples/propDrilling/PropDrillingRenderDemo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useState } from "react";
import { useLoaderData, useRevalidator } from "react-router-dom";
import css from "../../components/css/DemoLayout.module.css";
import { WorldApp } from "./exampleComponents/WorldApp.tsx";
import { PropDrillingDebugInfo } from "./demoControls/PropDrillingDebugInfo.tsx";
import { DebugInfo } from "../../components/DebugInfo.tsx";
import type { World, WorldsResponse } from "../../api/worlds.ts";

type Snapshot = {
Expand Down Expand Up @@ -32,7 +32,7 @@ export function PropDrillingRenderDemo() {
onSnapshotChange={setSnapshot}
revalidate={revalidator.revalidate}
/>
<PropDrillingDebugInfo snapshot={snapshot} />
<DebugInfo snapshot={snapshot} title="Top level state snapshot" />
</div>
);
}
32 changes: 0 additions & 32 deletions src/examples/propDrilling/demoControls/PropDrillingDebugInfo.tsx

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState, useEffect } from "react";
import css from "../../components/css/DemoLayout.module.css";
import { PropDrillingWorldApp } from "./exampleComponents/WorldApp.tsx";
import { PropDrillingDebugInfo } from "./demoControls/PropDrillingDebugInfo.tsx";
import { DebugInfo } from "../../components/DebugInfo.tsx";
import { fetchWorlds, type World } from "../../api/worlds.ts";

type Snapshot = {
Expand Down Expand Up @@ -34,7 +34,7 @@ export function PropDrillingNaiveRenderDemo() {
setWorlds={setWorlds}
onSnapshotChange={setSnapshot}
/>
<PropDrillingDebugInfo snapshot={snapshot} />
<DebugInfo snapshot={snapshot} title="Top level state snapshot" />
</div>
);
}

This file was deleted.

17 changes: 15 additions & 2 deletions src/examples/zustand-query/ZustandQuery.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
import { QueryClientProvider } from "@tanstack/react-query";
import css from "../../components/css/DemoLayout.module.css";
import { DebugInfo } from "./demoControls/DebugInfo";
import { DebugInfo } from "../../components/DebugInfo.tsx";
import { WorldApp } from "./components/WorldApp";
import { memo } from "react";
import { queryClient } from "./data/WorldData.ts";
import { useGetWorlds } from "./data/WorldData.ts";
import { useWorldStore } from "../zustand/data/WorldStore.tsx";

function ZustandQueryDebugInfo() {
const selectedWorldId = useWorldStore((s) => s.selectedWorldId);
const { data } = useGetWorlds();

return (
<DebugInfo
snapshot={{ selectedWorldId, hello: { worlds: data?.worlds } }}
/>
);
}

export const ZustandQuery = memo(() => (
<QueryClientProvider client={queryClient}>
Expand All @@ -15,7 +28,7 @@ export const ZustandQuery = memo(() => (
more fully featured for handling data specific needs.
</p>
<WorldApp />
<DebugInfo />
<ZustandQueryDebugInfo />
</div>
</QueryClientProvider>
));
21 changes: 0 additions & 21 deletions src/examples/zustand-query/demoControls/DebugInfo.tsx

This file was deleted.

11 changes: 9 additions & 2 deletions src/examples/zustand/ZustandRenderDemo.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { useEffect } from "react";
import { useLoaderData, useRevalidator } from "react-router-dom";
import css from "../../components/css/DemoLayout.module.css";
import { DebugInfo } from "./demoControls/DebugInfo.tsx";
import { DebugInfo } from "../../components/DebugInfo.tsx";
import { WorldApp } from "./exampleComponents/WorldApp.tsx";
import { useWorldStore } from "./data/WorldStore.tsx";
import type { WorldsResponse } from "../../api/worlds.ts";

function ZustandDebugInfo() {
const selectedWorldId = useWorldStore((s) => s.selectedWorldId);
const worlds = useWorldStore((s) => s.hello.worlds);

return <DebugInfo snapshot={{ selectedWorldId, hello: { worlds } }} />;
}

export function ZustandRenderDemo() {
const data = useLoaderData() as WorldsResponse;
const revalidator = useRevalidator();
Expand All @@ -28,7 +35,7 @@ export function ZustandRenderDemo() {
</p>

<WorldApp revalidate={revalidator.revalidate} />
<DebugInfo />
<ZustandDebugInfo />
</div>
);
}
17 changes: 0 additions & 17 deletions src/examples/zustand/demoControls/DebugInfo.tsx

This file was deleted.