Skip to content

Commit 9d4ac4b

Browse files
authored
Merge pull request #42 from Amira1502/convert-js-to-ts
chore: convert hello-near-examples frontend to ts
2 parents 9d93241 + 59d2191 commit 9d4ac4b

File tree

14 files changed

+219
-102
lines changed

14 files changed

+219
-102
lines changed

frontend/.eslintrc.json

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,33 @@
11
{
2+
"root": true,
3+
"parser": "@typescript-eslint/parser",
4+
"parserOptions": {
5+
"project": "./tsconfig.json",
6+
"tsconfigRootDir": ".",
7+
"ecmaVersion": 2022,
8+
"sourceType": "module",
9+
"ecmaFeatures": {
10+
"jsx": true
11+
}
12+
},
13+
"plugins": ["@typescript-eslint"],
214
"extends": [
3-
"next/babel",
4-
"next/core-web-vitals"
5-
]
6-
}
15+
"next/core-web-vitals",
16+
"plugin:@typescript-eslint/recommended",
17+
"plugin:@typescript-eslint/recommended-requiring-type-checking"
18+
],
19+
"rules": {
20+
"@typescript-eslint/no-explicit-any": "off",
21+
"@typescript-eslint/explicit-function-return-type": "off",
22+
"@typescript-eslint/no-unused-vars": ["warn", { "argsIgnorePattern": "^_" }],
23+
"@typescript-eslint/no-unsafe-assignment": "warn",
24+
"@typescript-eslint/no-unsafe-call": "warn",
25+
"@typescript-eslint/no-unsafe-member-access": "warn"
26+
},
27+
"ignorePatterns": ["node_modules/", ".next/", "out/"],
28+
"settings": {
29+
"react": {
30+
"version": "detect"
31+
}
32+
}
33+
}

frontend/jsconfig.json

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

frontend/next-env.d.ts

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

frontend/src/components/cards.js renamed to frontend/src/components/cards.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import Link from 'next/link';
2-
3-
import styles from '@/styles/app.module.css';
1+
import Link from "next/link";
2+
import styles from "@/styles/app.module.css";
43

54
export const Cards = () => {
65
return (

frontend/src/components/navigation.js renamed to frontend/src/components/navigation.tsx

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import Image from 'next/image';
22
import Link from 'next/link';
3-
43
import { useEffect, useState } from 'react';
54
import { useWalletSelector } from '@near-wallet-selector/react-hook';
65

7-
import NearLogo from '/public/near-logo.svg';
6+
import NearLogo from '../../public/near-logo.svg';
87

98
export const Navigation = () => {
10-
const [action, setAction] = useState(() => { });
11-
const [label, setLabel] = useState('Loading...');
9+
// Type the action as a function that returns void
10+
const [action, setAction] = useState<() => void>(() => () => {});
11+
const [label, setLabel] = useState<string>('Loading...');
1212
const { signedAccountId, signIn, signOut } = useWalletSelector();
1313

1414
useEffect(() => {
@@ -19,13 +19,20 @@ export const Navigation = () => {
1919
setAction(() => signIn);
2020
setLabel('Login');
2121
}
22-
}, [signedAccountId]);
22+
}, [signedAccountId, signIn, signOut]);
2323

2424
return (
2525
<nav className="navbar navbar-expand-lg">
2626
<div className="container-fluid">
2727
<Link href="/" passHref legacyBehavior>
28-
<Image priority src={NearLogo} alt="NEAR" width="30" height="24" className="d-inline-block align-text-top" />
28+
<Image
29+
priority
30+
src={NearLogo}
31+
alt="NEAR"
32+
width={30}
33+
height={24}
34+
className="d-inline-block align-text-top"
35+
/>
2936
</Link>
3037
<div className="navbar-nav pt-1">
3138
<button className="btn btn-secondary" onClick={action}>

frontend/src/config.js

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

frontend/src/config.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Type definitions
2+
interface ContractPerNetwork {
3+
mainnet: string;
4+
testnet: string;
5+
}
6+
7+
interface EVMWalletChain {
8+
chainId: number;
9+
name: string;
10+
explorer: string;
11+
rpc: string;
12+
}
13+
14+
// Contract addresses per network
15+
const contractPerNetwork: ContractPerNetwork = {
16+
mainnet: 'hello.near-examples.near',
17+
testnet: 'hello.near-examples.testnet',
18+
};
19+
20+
// Chains for EVM Wallets
21+
const evmWalletChains: Record<'mainnet' | 'testnet', EVMWalletChain> = {
22+
mainnet: {
23+
chainId: 397,
24+
name: 'Near Mainnet',
25+
explorer: 'https://eth-explorer.near.org',
26+
rpc: 'https://eth-rpc.mainnet.near.org',
27+
},
28+
testnet: {
29+
chainId: 398,
30+
name: 'Near Testnet',
31+
explorer: 'https://eth-explorer-testnet.near.org',
32+
rpc: 'https://eth-rpc.testnet.near.org',
33+
},
34+
};
35+
36+
// Selected network
37+
export const NetworkId: 'mainnet' | 'testnet' = 'testnet';
38+
39+
// Contract & EVM chain for the selected network
40+
export const HelloNearContract: string = contractPerNetwork[NetworkId];
41+
export const EVMWalletChain: EVMWalletChain = evmWalletChains[NetworkId];

frontend/src/global.d.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// SVG modules
2+
declare module '*.svg' {
3+
import * as React from 'react';
4+
const ReactComponent: React.FunctionComponent<React.SVGProps<SVGSVGElement>>;
5+
const src: string;
6+
export default src;
7+
export { ReactComponent };
8+
}
9+
10+
// CSS modules
11+
declare module "*.module.css" {
12+
const classes: { [key: string]: string };
13+
export default classes;
14+
}
15+
16+
// Regular CSS
17+
declare module "*.css";
18+
19+
// Untyped local modules (if needed)
20+
declare module "@/config";
21+
declare module "@/wallets/web3modal";
22+
declare module "@/components/navigation";

frontend/src/pages/_app.js

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

frontend/src/pages/_app.tsx

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import "@/styles/globals.css";
2+
import "@near-wallet-selector/modal-ui/styles.css";
3+
4+
import type { AppProps } from "next/app";
5+
import { WalletSelectorProvider } from "@near-wallet-selector/react-hook";
6+
import { Navigation } from "@/components/navigation";
7+
import { HelloNearContract, NetworkId } from "@/config";
8+
9+
// Wallet setups
10+
import { setupMyNearWallet } from "@near-wallet-selector/my-near-wallet";
11+
import { setupMeteorWallet } from "@near-wallet-selector/meteor-wallet";
12+
import { setupMeteorWalletApp } from "@near-wallet-selector/meteor-wallet-app";
13+
import { setupBitteWallet } from "@near-wallet-selector/bitte-wallet";
14+
import { setupEthereumWallets } from "@near-wallet-selector/ethereum-wallets";
15+
import { setupHotWallet } from "@near-wallet-selector/hot-wallet";
16+
import { setupLedger } from "@near-wallet-selector/ledger";
17+
import { setupSender } from "@near-wallet-selector/sender";
18+
import { setupHereWallet } from "@near-wallet-selector/here-wallet";
19+
import { setupNearMobileWallet } from "@near-wallet-selector/near-mobile-wallet";
20+
import { setupWelldoneWallet } from "@near-wallet-selector/welldone-wallet";
21+
22+
// Ethereum adapters
23+
import { wagmiAdapter, web3Modal } from "@/wallets/web3modal";
24+
25+
// Types
26+
import type { WalletModuleFactory } from "@near-wallet-selector/core";
27+
28+
const walletSelectorConfig = {
29+
network: NetworkId,
30+
modules: [
31+
setupEthereumWallets({ wagmiConfig: wagmiAdapter.wagmiConfig, web3Modal }),
32+
setupBitteWallet(),
33+
setupMeteorWallet(),
34+
setupMeteorWalletApp({ contractId: HelloNearContract }),
35+
setupHotWallet(),
36+
setupLedger(),
37+
setupSender(),
38+
setupHereWallet(),
39+
setupNearMobileWallet(),
40+
setupWelldoneWallet(),
41+
setupMyNearWallet(),
42+
] as WalletModuleFactory[], // ✅ force correct typing
43+
};
44+
45+
export default function App({ Component, pageProps }: AppProps) {
46+
return (
47+
<WalletSelectorProvider config={walletSelectorConfig}>
48+
<Navigation />
49+
<Component {...pageProps} />
50+
</WalletSelectorProvider>
51+
);
52+
}

0 commit comments

Comments
 (0)