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

refactor: moved theme to config #21

Merged
merged 3 commits into from
Oct 3, 2024
Merged
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
3 changes: 0 additions & 3 deletions src/components/Theme/index.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './Theme';
export * from './Disclaimer';
14 changes: 14 additions & 0 deletions src/config/Theme/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { getMuiThemeConfig } from './muiThemeConfig';
import { customTheme as rainbowTheme } from './rainbowTheme';
import { lightTheme, darkTheme } from './theme';

export const getCustomThemes = () => {
return {
main: {
light: lightTheme,
dark: darkTheme,
},
getMui: getMuiThemeConfig,
rainbow: rainbowTheme,
};
};
File renamed without changes.
File renamed without changes.
16 changes: 7 additions & 9 deletions src/config/env.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { Env } from '~/types';

export const getEnv = (): Env => {
const NEXT_PUBLIC_RPC_URL = process.env.NEXT_PUBLIC_RPC_URL;
const NEXT_PUBLIC_PROJECT_ID = process.env.NEXT_PUBLIC_PROJECT_ID;
const NEXT_PUBLIC_ALCHEMY_KEY = process.env.NEXT_PUBLIC_ALCHEMY_KEY;
const env: Env = {
RPC_URL: process.env.NEXT_PUBLIC_RPC_URL as string,
PROJECT_ID: process.env.NEXT_PUBLIC_PROJECT_ID as string,
ALCHEMY_KEY: process.env.NEXT_PUBLIC_ALCHEMY_KEY as string,
};

return {
RPC_URL: NEXT_PUBLIC_RPC_URL as string,
PROJECT_ID: NEXT_PUBLIC_PROJECT_ID as string,
ALCHEMY_KEY: NEXT_PUBLIC_ALCHEMY_KEY as string,
};
export const getEnv = (): Env => {
return env;
};
6 changes: 4 additions & 2 deletions src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createPublicClient, http } from 'viem';
import { mainnet } from 'viem/chains';
import { Config } from '~/types';
import { getCustomThemes } from './Theme';
import { getConstants } from './constants';
import { getEnv } from './env';

Expand All @@ -11,6 +12,7 @@ export const publicClient = createPublicClient({
});

export const getConfig = (): Config => ({
...getEnv(),
...getConstants(),
env: getEnv(),
constants: getConstants(),
customThemes: getCustomThemes(),
});
6 changes: 4 additions & 2 deletions src/providers/ThemeProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createContext, useEffect, useMemo, useState } from 'react';
import { ThemeProvider as MuiThemeProvider } from '@mui/material/styles';
import { getMuiThemeConfig } from '~/components';
import { getConfig } from '~/config';
import { Theme, ThemeName } from '~/types';
import { THEME_KEY, getTheme } from '~/utils';

Expand All @@ -17,6 +17,8 @@ interface StateProps {
export const ThemeContext = createContext({} as ContextType);

export const ThemeProvider = ({ children }: StateProps) => {
const { getMui: getMuiThemeConfig } = getConfig().customThemes;

const defaultTheme = 'dark';

const [theme, setTheme] = useState<ThemeName>(defaultTheme);
Expand All @@ -27,7 +29,7 @@ export const ThemeProvider = ({ children }: StateProps) => {
localStorage.setItem(THEME_KEY, newTheme);
setTheme(newTheme);
};
const muiTheme = useMemo(() => getMuiThemeConfig(currentTheme, theme), [currentTheme, theme]);
const muiTheme = useMemo(() => getMuiThemeConfig(currentTheme, theme), [currentTheme, theme, getMuiThemeConfig]);

// Load theme from local storage on load
useEffect(() => {
Expand Down
8 changes: 7 additions & 1 deletion src/types/Config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { CustomThemes } from '~/types';

export interface Env {
RPC_URL: string;
PROJECT_ID: string;
Expand All @@ -8,4 +10,8 @@ export interface Constants {
//...
}

export interface Config extends Env, Constants {}
export interface Config {
env: Env;
constants: Constants;
customThemes: CustomThemes;
}
12 changes: 12 additions & 0 deletions src/types/Theme.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { Theme as MuiTheme } from '@mui/material';
import { Theme as RainbowTheme } from '@rainbow-me/rainbowkit';

export type ThemeName = 'light' | 'dark';

export interface Theme {
Expand All @@ -17,3 +20,12 @@ export interface Theme {
export interface PropTheme {
theme: Theme;
}

export interface CustomThemes {
main: {
light: Theme;
dark: Theme;
};
getMui: (currentTheme: Theme, themeName: ThemeName) => MuiTheme;
rainbow: RainbowTheme;
}
4 changes: 2 additions & 2 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { connectorsForWallets } from '@rainbow-me/rainbowkit';
import { rainbowWallet, walletConnectWallet, injectedWallet } from '@rainbow-me/rainbowkit/wallets';
import { createConfig, http, cookieStorage, createStorage } from 'wagmi';
import { localhost, sepolia } from 'wagmi/chains';
import { getConfig } from '../config';
import { getConfig } from '~/config';

const { PROJECT_ID } = getConfig();
const { PROJECT_ID } = getConfig().env;

const getWallets = () => {
if (PROJECT_ID) {
Expand Down
10 changes: 6 additions & 4 deletions src/utils/getTheme.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { darkTheme, lightTheme } from '~/components/Theme';
import { getConfig } from '~/config';
import { Theme, ThemeName } from '~/types';

export const getTheme = (theme?: ThemeName): Theme => {
const { dark, light } = getConfig().customThemes.main;

switch (theme) {
case 'light':
return lightTheme;
return light;
case 'dark':
return darkTheme;
return dark;
default:
return lightTheme;
return light;
}
};
Loading