Skip to content

Commit

Permalink
Next.js template (#1096)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xp3gasus authored Mar 26, 2024
1 parent 502ceca commit 2a18a05
Show file tree
Hide file tree
Showing 12 changed files with 287 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/frameworks/next.js-example/.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/frameworks/next.js-example/.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/frameworks/next.js-example/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.
74 changes: 74 additions & 0 deletions examples/frameworks/next.js-example/components/ClientComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
'use client'
import { Balance } from '@xchainjs/xchain-client'
import { generatePhrase } from '@xchainjs/xchain-crypto'
import { Client as DashClient, defaultDashParams } from '@xchainjs/xchain-dash'
import { assetToString, baseToAsset } from '@xchainjs/xchain-util'
import { useRequest } from 'ahooks'
import { useRef, useState } from 'react'

export function ClientComponent() {
const dashClient = useRef<DashClient>()
const [balance, setBalance] = useState<Balance[]>()
const { run: getBalance, loading } = useRequest(
async () => {
if (!dashClient.current) return undefined
return dashClient.current.getBalance(await dashClient.current.getAddressAsync())
},
{
pollingInterval: 60 * 1000,
manual: true,
onSuccess: setBalance,
},
)

return (
<div className="flex flex-col items-center gap-y-5">
<p className="text-xl">Client component</p>
{!dashClient.current && (
<button
className="text-2xl bg-slate-400 p-2 rounded-xl"
onClick={() => {
dashClient.current = new DashClient({
...defaultDashParams,
phrase: process.env.NEXT_PUBLIC_PHRASE || generatePhrase(),
})
getBalance()
}}
>
Init DASH client with {process.env.NEXT_PUBLIC_PHRASE ? 'your phrase' : 'random phrase'}
</button>
)}
{dashClient.current && (
<div className="flex flex-col items-center">
<p>Your address</p>
<p>{dashClient.current.getAddress()}</p>
</div>
)}
{loading && <p>Loading balance...</p>}
{balance && (
<div className="flex flex-col items-center">
<p>Your balance</p>
{balance.map((balance, index) => {
return (
<div key={index}>
{assetToString(balance.asset)} {baseToAsset(balance.amount).amount().toString()}
</div>
)
})}
</div>
)}
{dashClient.current && (
<button
className="text-2xl bg-slate-400 p-2 rounded-xl"
onClick={() => {
dashClient.current?.purgeClient()
dashClient.current = undefined
setBalance(undefined)
}}
>
Disconnect client
</button>
)}
</div>
)
}
9 changes: 9 additions & 0 deletions examples/frameworks/next.js-example/next.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
webpack: (config) => {
config.resolve.fallback = { fs: false }
return config
}
};

export default nextConfig;
32 changes: 32 additions & 0 deletions examples/frameworks/next.js-example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "next.js-example",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"react": "18.2.0",
"react-dom": "18.2.0",
"next": "14.1.4",
"@xchainjs/xchain-dash": "*",
"@xchainjs/xchain-crypto": "*",
"@xchainjs/xchain-util": "*",
"@xchainjs/xchain-client": "*",
"ahooks": "3.7.10"
},
"devDependencies": {
"typescript": "5.4.3",
"@types/node": "20.11.30",
"@types/react": "18.2.69",
"@types/react-dom": "18.2.22",
"autoprefixer": "10.0.1",
"postcss": "8.4.38",
"tailwindcss": "3.3.0",
"eslint": "8.57.0",
"eslint-config-next": "14.1.4"
}
}
6 changes: 6 additions & 0 deletions examples/frameworks/next.js-example/postcss.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
};
3 changes: 3 additions & 0 deletions examples/frameworks/next.js-example/src/app/globals.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@tailwind base;
@tailwind components;
@tailwind utilities;
23 changes: 23 additions & 0 deletions examples/frameworks/next.js-example/src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { Metadata } from 'next'
import { Inter } from 'next/font/google'

import './globals.css'

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

export const metadata: Metadata = {
title: 'Next.js Template',
description: 'Generated by create next app',
}

export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode
}>) {
return (
<html lang="en">
<body className={inter.className}>{children}</body>
</html>
)
}
19 changes: 19 additions & 0 deletions examples/frameworks/next.js-example/src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { generatePhrase } from '@xchainjs/xchain-crypto'
import { Client, defaultDashParams } from '@xchainjs/xchain-dash'

import { ClientComponent } from '../../components/ClientComponent'

export default async function Home() {
const client = new Client({
...defaultDashParams,
phrase: generatePhrase(),
})

return (
<main className="flex min-h-screen flex-col items-center p-24 gap-y-5">
<h1 className="text-2xl">Next.js template</h1>
<p>Get random DASH address from server component: {await client.getAddressAsync()}</p>
<ClientComponent />
</main>
)
}
20 changes: 20 additions & 0 deletions examples/frameworks/next.js-example/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}',
'./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
26 changes: 26 additions & 0 deletions examples/frameworks/next.js-example/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"compilerOptions": {
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"plugins": [
{
"name": "next"
}
],
"paths": {
"@/*": ["./src/*"]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
}

0 comments on commit 2a18a05

Please sign in to comment.