mirror of
https://github.com/apotdevin/thunderhub.git
synced 2025-02-22 22:25:21 +01:00
chore: refactor settings context
This commit is contained in:
parent
222cd75c7b
commit
6e56cf1665
4 changed files with 17 additions and 59 deletions
|
@ -9,9 +9,17 @@ export const Price = ({
|
||||||
amount: number;
|
amount: number;
|
||||||
breakNumber?: boolean;
|
breakNumber?: boolean;
|
||||||
}) => {
|
}) => {
|
||||||
const { price, symbol, currency } = useSettings();
|
const { currency, prices } = useSettings();
|
||||||
|
|
||||||
const priceProps = { price, symbol, currency };
|
const current: { last: number; symbol: string } = prices[currency] ?? {
|
||||||
|
last: 0,
|
||||||
|
symbol: '',
|
||||||
|
};
|
||||||
|
const priceProps = {
|
||||||
|
price: current.last,
|
||||||
|
symbol: current.symbol,
|
||||||
|
currency,
|
||||||
|
};
|
||||||
|
|
||||||
const getFormat = (amount: number) =>
|
const getFormat = (amount: number) =>
|
||||||
getValue({ amount, ...priceProps, breakNumber });
|
getValue({ amount, ...priceProps, breakNumber });
|
||||||
|
|
|
@ -5,36 +5,28 @@ interface PriceProps {
|
||||||
last: number;
|
last: number;
|
||||||
symbol: string;
|
symbol: string;
|
||||||
}
|
}
|
||||||
interface CurrencyChangeProps {
|
|
||||||
currency: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface ChangeProps {
|
interface ChangeProps {
|
||||||
prices?: { [key: string]: PriceProps };
|
prices?: { [key: string]: PriceProps };
|
||||||
theme?: string;
|
theme?: string;
|
||||||
sidebar?: boolean;
|
sidebar?: boolean;
|
||||||
|
currency?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface SettingsProps {
|
interface SettingsProps {
|
||||||
prices: { [key: string]: PriceProps };
|
prices: { [key: string]: PriceProps };
|
||||||
price: number;
|
|
||||||
symbol: string;
|
|
||||||
currency: string;
|
currency: string;
|
||||||
theme: string;
|
theme: string;
|
||||||
sidebar: boolean;
|
sidebar: boolean;
|
||||||
setSettings: (newProps: ChangeProps) => void;
|
setSettings: (newProps: ChangeProps) => void;
|
||||||
updateCurrency: (newProps: CurrencyChangeProps) => void;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const SettingsContext = createContext<SettingsProps>({
|
export const SettingsContext = createContext<SettingsProps>({
|
||||||
prices: {},
|
prices: {},
|
||||||
price: 0,
|
|
||||||
symbol: '',
|
|
||||||
currency: '',
|
currency: '',
|
||||||
theme: '',
|
theme: '',
|
||||||
sidebar: true,
|
sidebar: true,
|
||||||
setSettings: () => {},
|
setSettings: () => {},
|
||||||
updateCurrency: () => {},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const SettingsProvider = ({ children }: any) => {
|
const SettingsProvider = ({ children }: any) => {
|
||||||
|
@ -43,59 +35,18 @@ const SettingsProvider = ({ children }: any) => {
|
||||||
localStorage.getItem('sidebar') === 'false' ? false : true;
|
localStorage.getItem('sidebar') === 'false' ? false : true;
|
||||||
const savedCurrency = localStorage.getItem('currency') || 'sat';
|
const savedCurrency = localStorage.getItem('currency') || 'sat';
|
||||||
|
|
||||||
const setSettings = ({ prices, theme, sidebar }: ChangeProps) => {
|
const setSettings = ({ prices, currency, theme, sidebar }: ChangeProps) => {
|
||||||
let price = 0;
|
|
||||||
let symbol = '';
|
|
||||||
|
|
||||||
const lookupCurrency =
|
|
||||||
savedCurrency === 'sat' || savedCurrency === 'btc'
|
|
||||||
? 'EUR'
|
|
||||||
: savedCurrency;
|
|
||||||
|
|
||||||
if (prices && prices[lookupCurrency]) {
|
|
||||||
price = prices[lookupCurrency].last;
|
|
||||||
symbol = prices[lookupCurrency].symbol;
|
|
||||||
}
|
|
||||||
|
|
||||||
updateSettings((prevState: any) => {
|
updateSettings((prevState: any) => {
|
||||||
const newState = { ...prevState };
|
const newState = { ...prevState };
|
||||||
return merge(newState, {
|
return merge(newState, {
|
||||||
prices,
|
prices,
|
||||||
...(prices && { price, symbol }),
|
currency,
|
||||||
theme,
|
theme,
|
||||||
sidebar,
|
sidebar,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const updateCurrency = ({ currency }: CurrencyChangeProps) => {
|
|
||||||
const prices: { [key: string]: PriceProps } = settings?.prices;
|
|
||||||
let price = 0;
|
|
||||||
let symbol = '';
|
|
||||||
let isFiat = false;
|
|
||||||
|
|
||||||
if (
|
|
||||||
prices &&
|
|
||||||
currency !== 'sat' &&
|
|
||||||
currency !== 'btc' &&
|
|
||||||
prices[currency]
|
|
||||||
) {
|
|
||||||
price = prices[currency].last;
|
|
||||||
symbol = prices[currency].symbol;
|
|
||||||
isFiat = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log(price, symbol, isFiat);
|
|
||||||
|
|
||||||
updateSettings((prevState: any) => {
|
|
||||||
const newState = { ...prevState };
|
|
||||||
return merge(newState, {
|
|
||||||
...(isFiat && { price, symbol }),
|
|
||||||
currency,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const settingsState = {
|
const settingsState = {
|
||||||
prices: { EUR: { last: 0, symbol: '€' } },
|
prices: { EUR: { last: 0, symbol: '€' } },
|
||||||
price: 0,
|
price: 0,
|
||||||
|
@ -104,7 +55,6 @@ const SettingsProvider = ({ children }: any) => {
|
||||||
theme: savedTheme,
|
theme: savedTheme,
|
||||||
sidebar: savedSidebar,
|
sidebar: savedSidebar,
|
||||||
setSettings,
|
setSettings,
|
||||||
updateCurrency,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const [settings, updateSettings] = useState(settingsState);
|
const [settings, updateSettings] = useState(settingsState);
|
||||||
|
|
|
@ -86,7 +86,7 @@ export const SideSettings = ({
|
||||||
isBurger,
|
isBurger,
|
||||||
setIsOpen,
|
setIsOpen,
|
||||||
}: SideSettingsProps) => {
|
}: SideSettingsProps) => {
|
||||||
const { theme, currency, updateCurrency, setSettings } = useSettings();
|
const { theme, currency, setSettings } = useSettings();
|
||||||
|
|
||||||
const renderIcon = (
|
const renderIcon = (
|
||||||
type: string,
|
type: string,
|
||||||
|
@ -103,7 +103,7 @@ export const SideSettings = ({
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
localStorage.setItem(type, value);
|
localStorage.setItem(type, value);
|
||||||
type === 'currency' &&
|
type === 'currency' &&
|
||||||
updateCurrency({
|
setSettings({
|
||||||
currency:
|
currency:
|
||||||
isOpen || isBurger
|
isOpen || isBurger
|
||||||
? value
|
? value
|
||||||
|
|
|
@ -14,7 +14,7 @@ import {
|
||||||
} from 'components/buttons/multiButton/MultiButton';
|
} from 'components/buttons/multiButton/MultiButton';
|
||||||
|
|
||||||
export const InterfaceSettings = () => {
|
export const InterfaceSettings = () => {
|
||||||
const { setSettings, updateCurrency } = useSettings();
|
const { setSettings } = useSettings();
|
||||||
const cTheme = localStorage.getItem('theme') || 'light';
|
const cTheme = localStorage.getItem('theme') || 'light';
|
||||||
const cCurrency = localStorage.getItem('currency') || 'sat';
|
const cCurrency = localStorage.getItem('currency') || 'sat';
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ export const InterfaceSettings = () => {
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
localStorage.setItem(type, value);
|
localStorage.setItem(type, value);
|
||||||
type === 'theme' && setSettings({ theme: value });
|
type === 'theme' && setSettings({ theme: value });
|
||||||
type === 'currency' && updateCurrency({ currency: value });
|
type === 'currency' && setSettings({ currency: value });
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{title}
|
{title}
|
||||||
|
|
Loading…
Add table
Reference in a new issue