Skip to content

Commit e07e163

Browse files
committed
Add Typescript template
1 parent 5fafbb0 commit e07e163

File tree

16 files changed

+271
-25
lines changed

16 files changed

+271
-25
lines changed

templates/default/package.json

Lines changed: 0 additions & 25 deletions
This file was deleted.

templates/typescript/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
NEXT_PUBLIC_INFURA_ID=

templates/typescript/.eslintrc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": ["next/babel", "next/core-web-vitals"]
3+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
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).
2+
3+
## Getting Started
4+
5+
First, run the development server:
6+
7+
```bash
8+
npm run dev
9+
# or
10+
yarn dev
11+
```
12+
13+
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
14+
15+
You can start editing the page by modifying `pages/index.tsx`. The page auto-updates as you edit the file.
16+
17+
[API routes](https://nextjs.org/docs/api-routes/introduction) can be accessed on [http://localhost:3000/api/hello](http://localhost:3000/api/hello). This endpoint can be edited in `pages/api/hello.ts`.
18+
19+
The `pages/api` directory is mapped to `/api/*`. Files in this directory are treated as [API routes](https://nextjs.org/docs/api-routes/introduction) instead of React pages.
20+
21+
## Learn More
22+
23+
To learn more about Next.js, take a look at the following resources:
24+
25+
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
26+
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
27+
28+
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!
29+
30+
## Deploy on Vercel
31+
32+
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.
33+
34+
Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.

templates/typescript/gitignore

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# next.js
12+
/.next/
13+
/out/
14+
15+
# production
16+
/build
17+
18+
# misc
19+
.DS_Store
20+
*.pem
21+
22+
# debug
23+
npm-debug.log*
24+
yarn-debug.log*
25+
yarn-error.log*
26+
27+
# local env files
28+
.env.local
29+
.env.development.local
30+
.env.test.local
31+
.env.production.local
32+
.env
33+
34+
# vercel
35+
.vercel

templates/typescript/next-env.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/// <reference types="next" />
2+
/// <reference types="next/image-types/global" />
3+
4+
// NOTE: This file should not be edited
5+
// see https://nextjs.org/docs/basic-features/typescript for more information.

templates/typescript/next.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/** @type {import('next').NextConfig} */
2+
module.exports = {
3+
reactStrictMode: true,
4+
}

templates/typescript/pages/_app.tsx

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import '../styles/globals.css';
2+
import type { AppProps } from 'next/app';
3+
import { Connector, Provider, chain, defaultChains } from 'wagmi';
4+
import { InjectedConnector } from 'wagmi/connectors/injected';
5+
import { WalletConnectConnector } from 'wagmi/connectors/walletConnect';
6+
import { WalletLinkConnector } from 'wagmi/connectors/walletLink';
7+
import { providers } from 'ethers';
8+
9+
// Get environment variables
10+
const infuraId = process.env.NEXT_PUBLIC_INFURA_ID as string;
11+
12+
// Pick chains
13+
const chains = defaultChains;
14+
const defaultChain = chain.mainnet;
15+
16+
// Set up connectors
17+
type ConnectorsConfig = { chainId?: number };
18+
const connectors = ({ chainId }: ConnectorsConfig) => {
19+
const rpcUrl =
20+
chains.find((x) => x.id === chainId)?.rpcUrls?.[0] ??
21+
defaultChain.rpcUrls[0];
22+
return [
23+
new InjectedConnector({ chains }),
24+
new WalletConnectConnector({
25+
chains,
26+
options: {
27+
infuraId,
28+
qrcode: true,
29+
},
30+
}),
31+
new WalletLinkConnector({
32+
chains,
33+
options: {
34+
appName: 'dForms',
35+
jsonRpcUrl: `${rpcUrl}/${infuraId}`,
36+
},
37+
}),
38+
];
39+
};
40+
41+
// Set up providers
42+
type ProviderConfig = { chainId?: number; connector?: Connector };
43+
const isChainSupported = (chainId?: number) =>
44+
chains.some((x) => x.id === chainId);
45+
46+
const provider = ({ chainId }: ProviderConfig) =>
47+
providers.getDefaultProvider(
48+
isChainSupported(chainId) ? chainId : defaultChain.id,
49+
{
50+
infuraId,
51+
}
52+
);
53+
const webSocketProvider = ({ chainId }: ProviderConfig) =>
54+
isChainSupported(chainId)
55+
? new providers.InfuraWebSocketProvider(chainId, infuraId)
56+
: undefined;
57+
58+
function MyApp({ Component, pageProps }: AppProps) {
59+
return (
60+
<Provider
61+
autoConnect
62+
connectors={connectors}
63+
provider={provider}
64+
webSocketProvider={webSocketProvider}
65+
>
66+
<Component {...pageProps} />
67+
</Provider>
68+
);
69+
}
70+
71+
export default MyApp;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
2+
import type { NextApiRequest, NextApiResponse } from 'next'
3+
4+
type Data = {
5+
name: string
6+
}
7+
8+
export default function handler(
9+
req: NextApiRequest,
10+
res: NextApiResponse<Data>
11+
) {
12+
res.status(200).json({ name: 'John Doe' })
13+
}

templates/typescript/pages/index.tsx

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import type { NextPage } from 'next';
2+
import { useCallback } from 'react';
3+
import { useAccount, useConnect, useNetwork, useSignMessage } from 'wagmi';
4+
5+
const Home: NextPage = () => {
6+
const [{ data, error }, connect] = useConnect();
7+
const [{ data: accountData }, disconnect] = useAccount({
8+
fetchEns: true,
9+
});
10+
const { connected } = data;
11+
12+
if (connected) {
13+
return (
14+
<div className='py-24 text-center'>
15+
<p className='text-2xl font-bold'>
16+
Welcome {accountData?.ens?.name || accountData?.address}
17+
</p>
18+
<button
19+
className='mx-auto mt-10 rounded bg-slate-200 p-2'
20+
onClick={disconnect}
21+
>
22+
Disconnect
23+
</button>
24+
</div>
25+
);
26+
}
27+
28+
return (
29+
<div className='py-24 text-center'>
30+
<h1 className='text-2xl font-bold'>Welcome to create-web3-frontend</h1>
31+
<p className='mt-10'>Connect your wallet:</p>
32+
<div className='mt-5 flex justify-center gap-6'>
33+
{data.connectors.map((x) => (
34+
<button
35+
className='rounded bg-slate-200 p-2'
36+
key={x.id}
37+
onClick={() => connect(x)}
38+
>
39+
{x.name}
40+
{!x.ready && ' (unsupported)'}
41+
</button>
42+
))}
43+
</div>
44+
45+
{error && (
46+
<p className='text-red-500'>{error?.message ?? 'Failed to connect'}</p>
47+
)}
48+
</div>
49+
);
50+
};
51+
52+
export default Home;

0 commit comments

Comments
 (0)