Skip to content

Commit

Permalink
feat: added vite template (#2162)
Browse files Browse the repository at this point in the history
* feat: added vite template

* feat: added vite template

* feat: added vite template

* minor changes

* feat: change routing to react router

* chore: handle dependencies

---------

Co-authored-by: Guillermo Alejandro Gallardo Diez <[email protected]>
Co-authored-by: Guillermo Alejandro Gallardo Diez <[email protected]>
  • Loading branch information
3 people authored Jan 4, 2025
1 parent 01723a6 commit e3982e3
Show file tree
Hide file tree
Showing 25 changed files with 1,029 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ export const successContractToText = (contract: Contract) =>

const frontendTemplates: FrontendMessage = {
'next-page': 'NextJS (Classic)',
'next-app': 'NextJS (App Router)'
'next-app': 'NextJS (App Router)',
'vite-react': 'Vite React'
};

export const successFrontendToText = (frontend: Frontend) =>
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export type Contract = 'ts' | 'rs' | 'none';
export const CONTRACTS: Contract[] = ['ts', 'rs', 'none'];

export type Frontend = 'next-app' | 'next-page' | 'none';
export const FRONTENDS: Frontend[] = ['next-app' , 'next-page', 'none'];
export type Frontend = 'next-app' | 'next-page' | 'vite-react' | 'none';
export const FRONTENDS: Frontend[] = ['next-app', 'next-page', 'vite-react', 'none'];

export type App = 'contract' | 'gateway';
export const APPS: App[] = ['contract', 'gateway'];
Expand Down
1 change: 1 addition & 0 deletions src/user-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const contractChoices: Choices<Contract> = [
const frontendChoices: Choices<Frontend> = [
{ title: 'NextJs (Classic)', description: 'A web-app built using Next.js Page Router', value: 'next-page' },
{ title: 'NextJS (App Router)', description: 'A web-app built using Next.js new App Router', value: 'next-app' },
{ title: 'Vite (React)', description: 'A web-app built using Vite with React', value: 'vite-react' },
];

const appPrompt: PromptObject = {
Expand Down
5 changes: 0 additions & 5 deletions templates/frontend/next-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@
"react-dom": "^18",
"wagmi": "^2.12.16"
},
"overrides": {
"@near-wallet-selector/ethereum-wallets": {
"near-api-js": "4.0.3"
}
},
"devDependencies": {
"encoding": "^0.1.13",
"eslint": "^8",
Expand Down
5 changes: 0 additions & 5 deletions templates/frontend/next-page/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@
"react-dom": "^18",
"wagmi": "^2.12.16"
},
"overrides": {
"@near-wallet-selector/ethereum-wallets": {
"near-api-js": "4.0.3"
}
},
"devDependencies": {
"encoding": "^0.1.13",
"eslint": "^8",
Expand Down
24 changes: 24 additions & 0 deletions templates/frontend/vite-react/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
38 changes: 38 additions & 0 deletions templates/frontend/vite-react/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import js from '@eslint/js'
import globals from 'globals'
import react from 'eslint-plugin-react'
import reactHooks from 'eslint-plugin-react-hooks'
import reactRefresh from 'eslint-plugin-react-refresh'

export default [
{ ignores: ['dist'] },
{
files: ['**/*.{js,jsx}'],
languageOptions: {
ecmaVersion: 2020,
globals: globals.browser,
parserOptions: {
ecmaVersion: 'latest',
ecmaFeatures: { jsx: true },
sourceType: 'module',
},
},
settings: { react: { version: '18.3' } },
plugins: {
react,
'react-hooks': reactHooks,
'react-refresh': reactRefresh,
},
rules: {
...js.configs.recommended.rules,
...react.configs.recommended.rules,
...react.configs['jsx-runtime'].rules,
...reactHooks.configs.recommended.rules,
'react/jsx-no-target-blank': 'off',
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
},
},
]
13 changes: 13 additions & 0 deletions templates/frontend/vite-react/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title> Near App</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.jsx"></script>
</body>
</html>
50 changes: 50 additions & 0 deletions templates/frontend/vite-react/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"name": "hello-near",
"private": true,
"version": "1.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"lint": "eslint .",
"preview": "vite preview"
},
"dependencies": {
"@esbuild-plugins/node-globals-polyfill": "^0.2.3",
"@near-js/providers": "^1.0.1",
"@near-wallet-selector/bitte-wallet": "^8.9.14",
"@near-wallet-selector/core": "^8.9.14",
"@near-wallet-selector/ethereum-wallets": "^8.9.14",
"@near-wallet-selector/here-wallet": "^8.9.14",
"@near-wallet-selector/ledger": "^8.9.14",
"@near-wallet-selector/meteor-wallet": "^8.9.14",
"@near-wallet-selector/modal-ui": "^8.9.14",
"@near-wallet-selector/my-near-wallet": "^8.9.14",
"@near-wallet-selector/near-mobile-wallet": "^8.9.14",
"@near-wallet-selector/sender": "^8.9.14",
"@near-wallet-selector/welldone-wallet": "^8.9.14",
"@wagmi/connectors": "^5.7.1",
"@wagmi/core": "^2.16.1",
"@web3modal/wagmi": "^5.1.11",
"bootstrap": "^5",
"bootstrap-icons": "^1.11.3",
"buffer": "^6.0.3",
"near-api-js": "^4.0.3",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-router": "^7.1.1",
"vite-plugin-eslint": "^1.8.1"
},
"devDependencies": {
"@eslint/js": "^9.17.0",
"@types/react": "^18.3.18",
"@types/react-dom": "^18.3.5",
"@vitejs/plugin-react": "^4.3.4",
"eslint": "^9.17.0",
"eslint-plugin-react": "^7.37.2",
"eslint-plugin-react-hooks": "^5.0.0",
"eslint-plugin-react-refresh": "^0.4.16",
"globals": "^15.14.0",
"vite": "^6.0.5"
}
}
Binary file added templates/frontend/vite-react/public/favicon.ico
Binary file not shown.
36 changes: 36 additions & 0 deletions templates/frontend/vite-react/src/App.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Navigation } from './components/navigation';
import Home from './pages/home';

import HelloNear from './pages/hello_near';
import { useEffect, useState } from 'react';
import { NetworkId } from './config.js';
import { NearContext, Wallet } from '@/wallets/near';
import { BrowserRouter, Routes, Route } from "react-router";

// Wallet instance
const wallet = new Wallet({ NetworkId: NetworkId });

// Optional: Create an access key so the user does not need to sign transactions. Read more about access keys here: https://docs.near.org/concepts/protocol/access-keys
// const wallet = new Wallet({ networkId: NetworkId, createAccessKeyFor: HelloNearContract });

function App() {
const [signedAccountId, setSignedAccountId] = useState(null);

useEffect(() => {
wallet.startUp(setSignedAccountId);
}, []);

return (
<NearContext.Provider value={{ wallet, signedAccountId }}>
<BrowserRouter>
<Navigation />
<Routes>
<Route path="/" element={<Home />} />
<Route path="/hello-near" element={<HelloNear />} />
</Routes>
</BrowserRouter>
</NearContext.Provider>
)
}

export default App
43 changes: 43 additions & 0 deletions templates/frontend/vite-react/src/assets/near-logo.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 templates/frontend/vite-react/src/assets/near.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 templates/frontend/vite-react/src/assets/react.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions templates/frontend/vite-react/src/components/cards.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import styles from '@/styles/app.module.css';
import { Link } from 'react-router';

export const Cards = () => {
return (
<div className={styles.grid}>
<a
href="https://docs.near.org/build/web3-apps/quickstart"
className={styles.card}
target="_blank"
rel="noopener noreferrer"
>
<h2>
Near Docs <span>-&gt;</span>
</h2>
<p>Learn how this application works, and what you can build on Near.</p>
</a>

<Link to="/hello-near" className={styles.card} rel="noopener noreferrer">
<h2>
Near Integration <span>-&gt;</span>
</h2>
<p>Discover how simple it is to interact with a Near smart contract.</p>
</Link>
</div>
);
};
39 changes: 39 additions & 0 deletions templates/frontend/vite-react/src/components/navigation.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { useContext,useEffect, useState } from 'react';

import NearLogo from '@/assets/near-logo.svg';
import { NearContext } from '@/wallets/near';
import { Link } from "react-router";
import styles from '@/styles/app.module.css';

export const Navigation = () => {
const { signedAccountId, wallet } = useContext(NearContext);
const [action, setAction] = useState(() => {});
const [label, setLabel] = useState('Loading...');

useEffect(() => {
if (!wallet) return;

if (signedAccountId) {
setAction(() => wallet.signOut);
setLabel(`Logout ${signedAccountId}`);
} else {
setAction(() => wallet.signIn);
setLabel('Login');
}
}, [signedAccountId, wallet]);

return (
<nav className="navbar navbar-expand-lg">
<div className="container-fluid">
<Link to="/">
<img src={NearLogo} alt="NEAR" width="30" height="24" className={styles.logo} />
</Link>
<div className="navbar-nav pt-1">
<button className="btn btn-secondary" onClick={action}>
{label}
</button>
</div>
</div>
</nav>
);
};
24 changes: 24 additions & 0 deletions templates/frontend/vite-react/src/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const contractPerNetwork = {
mainnet: 'hello.near-examples.near',
testnet: 'hello.near-examples.testnet',
};

// Chains for EVM Wallets
const evmWalletChains = {
mainnet: {
chainId: 397,
name: 'Near Mainnet',
explorer: 'https://eth-explorer.near.org',
rpc: 'https://eth-rpc.mainnet.near.org',
},
testnet: {
chainId: 398,
name: 'Near Testnet',
explorer: 'https://eth-explorer-testnet.near.org',
rpc: 'https://eth-rpc.testnet.near.org',
},
};

export const NetworkId = 'testnet';
export const HelloNearContract = contractPerNetwork[NetworkId];
export const EVMWalletChain = evmWalletChains[NetworkId];
11 changes: 11 additions & 0 deletions templates/frontend/vite-react/src/main.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import './styles/globals.css';
import App from './App.jsx';


createRoot(document.getElementById('root')).render(
<StrictMode>
<App />
</StrictMode>,
);
Loading

0 comments on commit e3982e3

Please sign in to comment.