Skip to content
This repository has been archived by the owner on Jan 7, 2025. It is now read-only.

Commit

Permalink
Merge pull request #204 from HaresMahmood/network-switcher-component
Browse files Browse the repository at this point in the history
Add Network Switcher component.
  • Loading branch information
DonnySolana authored Jun 23, 2022
2 parents 067d255 + a19242e commit 5431031
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 8 deletions.
8 changes: 6 additions & 2 deletions src/components/AppBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Link from "next/link";

import { WalletMultiButton } from "@solana/wallet-adapter-react-ui";
import { useAutoConnect } from '../contexts/AutoConnectProvider';
import NetworkSwitcher from './NetworkSwitcher';

export const AppBar: FC = props => {
const { autoConnect, setAutoConnect } = useAutoConnect();
Expand Down Expand Up @@ -62,7 +63,9 @@ export const AppBar: FC = props => {

{/* Wallet & Settings */}
<div className="navbar-end">
<div className="dropdown">
<WalletMultiButton className="btn btn-ghost mr-4" />

<div className="dropdown dropdown-end">
<div tabIndex={0} className="btn btn-square btn-ghost text-right">
<svg className="w-6 h-6" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z" />
Expand All @@ -76,11 +79,12 @@ export const AppBar: FC = props => {
<a>Autoconnect</a>
<input type="checkbox" checked={autoConnect} onChange={(e) => setAutoConnect(e.target.checked)} className="toggle" />
</label>

<NetworkSwitcher />
</div>
</li>
</ul>
</div>
<WalletMultiButton className="btn btn-ghost mr-4" />
</div>
</div>
{props.children}
Expand Down
28 changes: 28 additions & 0 deletions src/components/NetworkSwitcher.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { FC } from 'react';
import dynamic from 'next/dynamic';
import { useNetworkConfiguration } from '../contexts/NetworkConfigurationProvider';

const NetworkSwitcher: FC = () => {
const { networkConfiguration, setNetworkConfiguration } = useNetworkConfiguration();

console.log(networkConfiguration);

return (
<label className="cursor-pointer label">
<a>Network</a>
<select
value={networkConfiguration}
onChange={(e) => setNetworkConfiguration(e.target.value)}
className="select max-w-xs"
>
<option value="mainnet-beta">main</option>
<option value="devnet">dev</option>
<option value="testnet">test</option>
</select>
</label>
);
};

export default dynamic(() => Promise.resolve(NetworkSwitcher), {
ssr: false
})
4 changes: 3 additions & 1 deletion src/components/Notification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { XIcon } from '@heroicons/react/solid'
import useNotificationStore from '../stores/useNotificationStore'
import { useConnection } from '@solana/wallet-adapter-react';
import { getExplorerUrl } from '../utils/explorer'
import { useNetworkConfiguration } from 'contexts/NetworkConfigurationProvider';

const NotificationList = () => {
const { notifications, set: setNotificationStore } = useNotificationStore(
Expand Down Expand Up @@ -46,6 +47,7 @@ const NotificationList = () => {

const Notification = ({ type, message, description, txid, onHide }) => {
const { connection } = useConnection();
const { networkConfiguration } = useNetworkConfiguration();

// TODO: we dont have access to the network or endpoint here..
// getExplorerUrl(connection., txid, 'tx')
Expand Down Expand Up @@ -86,7 +88,7 @@ const Notification = ({ type, message, description, txid, onHide }) => {
<div className="flex flex-row">

<a
href={'https://explorer.solana.com/tx/' + txid + `?cluster=devnet`}
href={'https://explorer.solana.com/tx/' + txid + `?cluster=${networkConfiguration}`}
target="_blank"
rel="noreferrer"
className="flex flex-row link link-accent"
Expand Down
1 change: 1 addition & 0 deletions src/components/SendTransaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const SendTransaction: FC = () => {
signature = await sendTransaction(transaction, connection);

await connection.confirmTransaction(signature, 'confirmed');
console.log(signature);
notify({ type: 'success', message: 'Transaction successful!', txid: signature });
} catch (error: any) {
notify({ type: 'error', message: `Transaction failed!`, description: error?.message, txid: signature });
Expand Down
18 changes: 13 additions & 5 deletions src/contexts/ContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@ import {
// LedgerWalletAdapter,
// SlopeWalletAdapter,
} from '@solana/wallet-adapter-wallets';
import { clusterApiUrl } from '@solana/web3.js';
import { Cluster, clusterApiUrl } from '@solana/web3.js';
import { FC, ReactNode, useCallback, useMemo } from 'react';
import { AutoConnectProvider, useAutoConnect } from './AutoConnectProvider';
import { notify } from "../utils/notifications";
import { NetworkConfigurationProvider, useNetworkConfiguration } from './NetworkConfigurationProvider';

const WalletContextProvider: FC<{ children: ReactNode }> = ({ children }) => {
const { autoConnect } = useAutoConnect();
const network = WalletAdapterNetwork.Devnet;
const { networkConfiguration } = useNetworkConfiguration();
const network = networkConfiguration as WalletAdapterNetwork;
const endpoint = useMemo(() => clusterApiUrl(network), [network]);

console.log(network);

const wallets = useMemo(
() => [
new PhantomWalletAdapter(),
Expand Down Expand Up @@ -53,8 +57,12 @@ const WalletContextProvider: FC<{ children: ReactNode }> = ({ children }) => {

export const ContextProvider: FC<{ children: ReactNode }> = ({ children }) => {
return (
<AutoConnectProvider>
<WalletContextProvider>{children}</WalletContextProvider>
</AutoConnectProvider>
<>
<NetworkConfigurationProvider>
<AutoConnectProvider>
<WalletContextProvider>{children}</WalletContextProvider>
</AutoConnectProvider>
</NetworkConfigurationProvider>
</>
);
};
22 changes: 22 additions & 0 deletions src/contexts/NetworkConfigurationProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { useLocalStorage } from '@solana/wallet-adapter-react';
import { createContext, FC, ReactNode, useContext } from 'react';


export interface NetworkConfigurationState {
networkConfiguration: string;
setNetworkConfiguration(networkConfiguration: string): void;
}

export const NetworkConfigurationContext = createContext<NetworkConfigurationState>({} as NetworkConfigurationState);

export function useNetworkConfiguration(): NetworkConfigurationState {
return useContext(NetworkConfigurationContext);
}

export const NetworkConfigurationProvider: FC<{ children: ReactNode }> = ({ children }) => {
const [networkConfiguration, setNetworkConfiguration] = useLocalStorage("network", "devnet");

return (
<NetworkConfigurationContext.Provider value={{ networkConfiguration, setNetworkConfiguration }}>{children}</NetworkConfigurationContext.Provider>
);
};

0 comments on commit 5431031

Please sign in to comment.