Skip to content

Dynamically populate the Environment dropdown #14

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

Open
wants to merge 2 commits 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
15 changes: 6 additions & 9 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({ envOption, envOptions, setEnv }: { envOption: EnvOption; envOptions: EnvOption[], setEnv: (env: Env) => void }) {
return (
<Container size="xl" pt={12}>
<div className={classes.inner}>
Expand All @@ -36,18 +36,15 @@ export function Header({ env, setEnv }: { env: string; setEnv: (env: Env) => voi
onClick={(event) => event.preventDefault()}
>
<Center>
<span className={classes.linkLabel}>{env}</span>
<span className={classes.linkLabel}>{envOption.env}</span>
<IconChevronDown size="0.9rem" stroke={1.5} />
</Center>
</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
38 changes: 14 additions & 24 deletions providers/Providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,29 @@ 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, EnvOption, 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 [envOption, setEnvOption] = useState<EnvOption>(() => {
const found = envOptions.find(({ env: e }) => e === queryEnv);

return found || envOptions[0];
});
const endpoint = useMemo(() => envOption.endpoint, [envOption]);

const doSetEnv = (e: Env) => {
const params = new URLSearchParams(window.location.search);
params.set('env', e);

setEnv(e);
const found = envOptions.find((option) => option.env === e);
if (!found) {
return;
}
setEnvOption(found);
router.push(`${pathname}?${params.toString()}`);
};

Expand All @@ -35,26 +43,8 @@ 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]);

return (
<EnvProvider env={env!}>
<EnvProvider env={envOption.env!}>
<ConnectionProvider endpoint={endpoint!}>
<WalletProvider wallets={[]} autoConnect>
<WalletModalProvider>
Expand All @@ -69,7 +59,7 @@ export function Providers({ children }: { children: ReactNode }) {
}}
>
<AppShell.Header bg="black" withBorder={false}>
<Header env={env} setEnv={doSetEnv} />
<Header envOption={envOption} 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