Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamically populate the Environment dropdown #14

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions components/Header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { IconChevronDown } from '@tabler/icons-react';
import { WalletMultiButton } from '@solana/wallet-adapter-react-ui';

import classes from './Header.module.css';
import { Env } from '@/providers/useEnv';
import { Env, EnvOption } from '@/providers/useEnv';
import RetainQueryLink from '../RetainQueryLink';

const HeaderLink = ({ label, link, disabled }: { label: string, link: string, disabled?: boolean }) => {
Expand All @@ -15,7 +15,7 @@ const HeaderLink = ({ label, link, disabled }: { label: string, link: string, di
);
};

export function Header({ env, setEnv }: { env: string; setEnv: (env: Env) => void }) {
export function Header({ env, envOptions, setEnv }: { env: string; envOptions: EnvOption[], setEnv: (env: Env) => void }) {
return (
<Container
size="xl"
Expand Down Expand Up @@ -46,12 +46,9 @@ export function Header({ env, setEnv }: { env: string; setEnv: (env: Env) => voi
</a>
</Menu.Target>
<Menu.Dropdown>
<Menu.Item onClick={() => setEnv('mainnet')}>Solana Mainnet</Menu.Item>
<Menu.Item onClick={() => setEnv('devnet')}>Solana Devnet</Menu.Item>
<Menu.Item onClick={() => setEnv('eclipse-mainnet')}>Eclipse Mainnet</Menu.Item>
<Menu.Item onClick={() => setEnv('eclipse-devnet')}>Eclipse Devnet</Menu.Item>
<Menu.Item onClick={() => setEnv('sonic-devnet')}>Sonic Devnet</Menu.Item>
<Menu.Item onClick={() => setEnv('localhost')}>Localhost</Menu.Item>
{envOptions.map(({ env: e, label }) => (
<Menu.Item key={e} onClick={() => setEnv(e)}>{label}</Menu.Item>
))}
</Menu.Dropdown>
</Menu>
</Group>
Expand Down
24 changes: 4 additions & 20 deletions providers/Providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import { useSearchParams, useRouter, usePathname } from 'next/navigation';
import { Header } from '@/components/Header/Header';
import { UmiProvider } from './UmiProvider';
import { EnvProvider } from './EnvProvider';
import { Env } from './useEnv';
import { Env, envOptions } from './useEnv';

export function Providers({ children }: { children: ReactNode }) {
const router = useRouter();
const searchParams = useSearchParams();
const pathname = usePathname();
const queryEnv = searchParams.get('env');
const [client] = useState(new QueryClient());
const [env, setEnv] = useState<Env>((queryEnv === 'mainnet' || queryEnv === 'devnet') ? queryEnv : 'mainnet');
const [env, setEnv] = useState<Env>((queryEnv === 'mainnet' || queryEnv === 'devnet') ? queryEnv : envOptions[0]?.env);
const wallets = useMemo(
() => [
new PhantomWalletAdapter(),
Expand All @@ -43,23 +43,7 @@ export function Providers({ children }: { children: ReactNode }) {
// }
// }, []);

const endpoint = useMemo(() => {
switch (env) {
case 'mainnet':
return process.env.NEXT_PUBLIC_MAINNET_RPC_URL;
case 'eclipse-mainnet':
return process.env.NEXT_PUBLIC_ECLIPSE_MAINNET_RPC_URL;
case 'sonic-devnet':
return process.env.NEXT_PUBLIC_SONIC_DEVNET_RPC_URL;
case 'eclipse-devnet':
return process.env.NEXT_PUBLIC_ECLIPSE_DEVNET_RPC_URL;
case 'localhost':
return 'http://localhost:8899';
case 'devnet':
default:
return process.env.NEXT_PUBLIC_DEVNET_RPC_URL;
}
}, [env]);
const endpoint = useMemo(() => envOptions.find(({ env: e }) => e === env)?.endpoint, [env]);

return (
<EnvProvider env={env!}>
Expand All @@ -77,7 +61,7 @@ export function Providers({ children }: { children: ReactNode }) {
}}
>
<AppShell.Header bg="black" withBorder={false}>
<Header env={env} setEnv={doSetEnv} />
<Header env={env} envOptions={envOptions} setEnv={doSetEnv} />
</AppShell.Header>
<AppShell.Main>
{children}
Expand Down
32 changes: 31 additions & 1 deletion providers/useEnv.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
import { createContext, useContext } from 'react';

export type Env = 'devnet' | 'testnet' | 'mainnet' | 'localhost' | 'eclipse-devnet' | 'eclipse-mainnet' | 'sonic-devnet';
export type Env = 'devnet' | 'mainnet' | 'localhost' | 'eclipse-devnet' | 'eclipse-mainnet' | 'sonic-devnet';
export type EnvOption = { env: Env, endpoint: string, label: string };

const endpoints: Record<Env, string | undefined> = {
mainnet: process.env.NEXT_PUBLIC_MAINNET_RPC_URL,
devnet: process.env.NEXT_PUBLIC_DEVNET_RPC_URL,
'eclipse-mainnet': process.env.NEXT_PUBLIC_ECLIPSE_MAINNET_RPC_URL,
'eclipse-devnet': process.env.NEXT_PUBLIC_ECLIPSE_DEVNET_RPC_URL,
'sonic-devnet': process.env.NEXT_PUBLIC_SONIC_DEVNET_RPC_URL,
localhost: 'http://localhost:8899',
};

const labels: Record<Env, string> = {
mainnet: 'Solana Mainnet',
devnet: 'Solana Devnet',
'eclipse-mainnet': 'Eclipse Mainnet',
'eclipse-devnet': 'Eclipse Devnet',
'sonic-devnet': 'Sonic Devnet',
localhost: 'Localhost',
};

const envs: Env[] = Object
.entries(endpoints)
.map(([env, endpoint]) => endpoint ? env as Env : null)
.filter(Boolean) as Env[];

export const envOptions: EnvOption[] = envs.map((env) => ({
env,
endpoint: endpoints[env] as string,
label: labels[env] as string,
}));

type EnvContext = {
env: Env;
Expand Down