Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Examples: next js app router #11

Closed
Closed
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
3 changes: 3 additions & 0 deletions examples/nextjs-app/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "next/core-web-vitals"
}
36 changes: 36 additions & 0 deletions examples/nextjs-app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js
.yarn/install-state.gz

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts
36 changes: 36 additions & 0 deletions examples/nextjs-app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).

## Getting Started

First, run the development server:

```bash
npm run dev
# or
yarn dev
# or
pnpm dev
# or
bun dev
```

Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.

You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.

This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font.

## Learn More

To learn more about Next.js, take a look at the following resources:

- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.

You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!

## Deploy on Vercel

The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.

Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
47 changes: 47 additions & 0 deletions examples/nextjs-app/next.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/** @type {import("next").NextConfig} */
const nextConfig = {
transpilePackages: [
"@nillion/client-core",
"@nillion/client-react-hooks",
"@nillion/client-vms",
],
webpack: (
config,
{ buildId, dev, isServer, defaultLoaders, nextRuntime, webpack },
) => {
config.resolve.fallback = {
crypto: false,
stream: false,
buffer: false,
vm: false,
};

config.module.rules.push({
test: /\.wasm$/,
type: "asset/resource",
});

return config;
},
async headers() {
return [
{
source: "/:path*",
headers: [
{ key: "Cross-Origin-Embedder-Policy", value: "require-corp" },
{ key: "Cross-Origin-Opener-Policy", value: "same-origin" },
],
},
];
},
async rewrites() {
return [
{
source: "/nilchain",
destination: "http://127.0.0.1:48102/",
},
];
},
};

export default nextConfig;
29 changes: 29 additions & 0 deletions examples/nextjs-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "nextjs-app",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev -p 8080",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"next": "14.2.5",
"react": "^18",
"react-dom": "^18"
},
"devDependencies": {
"@nillion/client-core": "^0.1.0-rc.9",
"@nillion/client-react-hooks": "^0.1.0-rc.9",
"@nillion/client-vms": "^0.1.0-rc.9",
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
"eslint": "^8",
"eslint-config-next": "14.2.5",
"postcss": "^8",
"tailwindcss": "^3.4.1",
"typescript": "^5"
}
}
8 changes: 8 additions & 0 deletions examples/nextjs-app/postcss.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/** @type {import('postcss-load-config').Config} */
const config = {
plugins: {
tailwindcss: {},
},
};

export default config;
1 change: 1 addition & 0 deletions examples/nextjs-app/public/next.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions examples/nextjs-app/public/vercel.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/nextjs-app/src/app/favicon.ico
Binary file not shown.
33 changes: 33 additions & 0 deletions examples/nextjs-app/src/app/globals.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
@tailwind base;
@tailwind components;
@tailwind utilities;

:root {
--foreground-rgb: 0, 0, 0;
--background-start-rgb: 214, 219, 220;
--background-end-rgb: 255, 255, 255;
}

@media (prefers-color-scheme: dark) {
:root {
--foreground-rgb: 255, 255, 255;
--background-start-rgb: 0, 0, 0;
--background-end-rgb: 0, 0, 0;
}
}

body {
color: rgb(var(--foreground-rgb));
background: linear-gradient(
to bottom,
transparent,
rgb(var(--background-end-rgb))
)
rgb(var(--background-start-rgb));
}

@layer utilities {
.text-balance {
text-wrap: balance;
}
}
25 changes: 25 additions & 0 deletions examples/nextjs-app/src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { Metadata } from "next";
import { Inter } from "next/font/google";
import "./globals.css";
import { NillionWrappedProvider } from "../app/providers/NillionWrappedProvider";

const inter = Inter({ subsets: ["latin"] });

export const metadata: Metadata = {
title: "Create Next App",
description: "Generated by create next app",
};

export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return (
<html lang="en">
<NillionWrappedProvider>
<body className={inter.className}>{children}</body>
</NillionWrappedProvider>
</html>
);
}
87 changes: 87 additions & 0 deletions examples/nextjs-app/src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
"use client";
import * as React from "react";
import {
useFetchValue,
useNillion,
useStoreValue,
} from "@nillion/client-react-hooks";
import { useState } from "react";

export default function Home() {
const [id, setId] = useState("");
const storeValue = useStoreValue();

const client = useNillion();
console.log("clientNillion", client);
console.log("storeValue", storeValue);

const fetchValue = useFetchValue(
{
id,
name: "data",
type: "SecretInteger",
},
{
staleTime: 1000 * 30, // data stale after 30 seconds
},
);

const data = 42;

if (storeValue.data && !id) {
setId(storeValue.data);
}

const handleStoreClick = () => {
storeValue.mutate({
values: { data },
ttl: 1,
});
};

const handleFetchClick = async () => {
await fetchValue.refetch();
};

const updatedAtTs =
fetchValue.dataUpdatedAt === 0
? ""
: new Date(fetchValue.dataUpdatedAt).toLocaleString("en-GB", {
timeZone: "UTC",
});

return (
<main className="flex min-h-screen flex-col items-center justify-between p-24">
<div>
<h2>Hello from @nillion/client-* 👋</h2>
<p>Original data: {JSON.stringify(data)}</p>
<p>1. Store data</p>
<button onClick={handleStoreClick} disabled={storeValue.isPending}>
Store
</button>
<ul>
<li>Status: {storeValue.status}</li>
{id && <li>Id: {id}</li>}
</ul>
<p>2. Read data</p>
<button
onClick={handleFetchClick}
disabled={!Boolean(id) || fetchValue.isPending}
>
Force refresh
</button>
<ul>
<li>Status: {fetchValue.status}</li>
<li>Updated at: {updatedAtTs}</li>
<li>
From cache:{" "}
{Boolean(
fetchValue.isFetched && !fetchValue.isFetchedAfterMount,
).toString()}
</li>
{fetchValue.data && <li>Data: {JSON.stringify(fetchValue.data)}</li>}
</ul>
</div>
</main>
);
}
42 changes: 42 additions & 0 deletions examples/nextjs-app/src/app/providers/NillionWrappedProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"use client";

import * as React from "react";
import { NamedNetwork } from "@nillion/client-core";
import { createSignerFromKey } from "@nillion/client-payments";
import { NillionClientProvider } from "@nillion/client-react-hooks";
import { NillionClient } from "@nillion/client-vms";
import { useState } from "react";

const createNillionClient = () =>
NillionClient.create({
network: NamedNetwork.enum.Devnet,
overrides: async () => {
// first account when running `nillion-devnet` with default seed
const signer = await createSignerFromKey(
"9a975f567428d054f2bf3092812e6c42f901ce07d9711bc77ee2cd81101f42c5"
);
return {
endpoint: "http://localhost:8080/nilchain",
signer,
userSeed: "nillion-devnet",
nodeSeed: Math.random().toString(),
bootnodes: [
"/ip4/127.0.0.1/tcp/54936/ws/p2p/12D3KooWMvw1hEqm7EWSDEyqTb6pNetUVkepahKY6hixuAuMZfJS",
],
cluster: "9e68173f-9c23-4acc-ba81-4f079b639964",
chain: "nillion-chain-devnet",
};
},
});

export function NillionWrappedProvider({
children,
}: {
children: React.ReactNode;
}) {
const [client] = useState(createNillionClient);

return (
<NillionClientProvider client={client}>{children}</NillionClientProvider>
);
}
20 changes: 20 additions & 0 deletions examples/nextjs-app/tailwind.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { Config } from "tailwindcss";

const config: Config = {
content: [
"./src/pages/**/*.{js,ts,jsx,tsx,mdx}",
"./src/components/**/*.{js,ts,jsx,tsx,mdx}",
"./src/app/**/*.{js,ts,jsx,tsx,mdx}",
],
theme: {
extend: {
backgroundImage: {
"gradient-radial": "radial-gradient(var(--tw-gradient-stops))",
"gradient-conic":
"conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))",
},
},
},
plugins: [],
};
export default config;
Loading