Skip to content

Commit

Permalink
Add CounterDisplay child component
Browse files Browse the repository at this point in the history
- Refactor BuildersCheckInCount to render a child server component. This bend the pattern composition rule but works thanks to the fact that all the child component are serializable. (This would be explained more in the PR)
- Add CounterDisplay server component wich is rendered in the server, sent to the client and the rehydrated with the current values when the become available in the client component.
  • Loading branch information
All-Khwarizmi committed Jan 11, 2025
1 parent 295ef19 commit 9a7e113
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
16 changes: 2 additions & 14 deletions packages/nextjs/app/_components/BuildersCheckInCount.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
"use client";

import React from "react";
import { displayTxResult } from "../debug/_components/contract";
import { ArrowPathIcon } from "@heroicons/react/24/outline";
import { CounterDisplay } from "./CounterDisplay";
import { useScaffoldReadContract } from "~~/hooks/scaffold-eth";

const CONTRACT_NAME = "BatchRegistry";
Expand All @@ -19,18 +18,7 @@ function BuildersCheckInCount() {
functionName: FUNCTION_NAME,
});

return (
<div className="text-lg flex gap-2 justify-center items-center">
<span className="font-bold">Checked in builders count:</span>
{isLoading || (!error && !counter) ? (
<ArrowPathIcon className="size-5 animate-spin" />
) : error ? (
<span className="text-error">Error!</span>
) : (
<span>{displayTxResult(counter)}</span>
)}
</div>
);
return <CounterDisplay data={counter} isLoading={isLoading} error={error} />;
}

export default BuildersCheckInCount;
25 changes: 25 additions & 0 deletions packages/nextjs/app/_components/CounterDisplay.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { displayTxResult } from "../debug/_components/contract";
import { ArrowPathIcon } from "@heroicons/react/16/solid";

export async function CounterDisplay({
data: counter,
isLoading,
error,
}: {
data?: bigint;
isLoading: boolean;
error: Error | null;
}) {
return (
<div className="text-lg flex gap-2 justify-center items-center">
<span className="font-bold">Checked in builders count:</span>
{isLoading || (!error && !counter) ? (
<ArrowPathIcon className="size-5 animate-spin" />
) : error ? (
<span className="text-error">Error!</span>
) : (
<span>{displayTxResult(counter)}</span>
)}
</div>
);
}

0 comments on commit 9a7e113

Please sign in to comment.