-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename PriorityFeesModal to AppSettingsModal
- Loading branch information
Showing
12 changed files
with
175 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/components/modals/AppSettingsModal/AppSettingsModal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { FC } from 'react' | ||
|
||
import classNames from 'classnames' | ||
|
||
import { Button } from '@banx/components/Buttons' | ||
import { useTabs } from '@banx/components/Tabs' | ||
import { Modal } from '@banx/components/modals/BaseModal' | ||
|
||
import { PriorityLevel, usePriorityFees, useSlippage } from '@banx/store/common' | ||
import { enqueueSnackbar } from '@banx/utils' | ||
|
||
import { PRIORITY_TYPED_TABS, SLIPPAGE_TABS } from './constants' | ||
|
||
import styles from './AppSettingsModal.module.less' | ||
|
||
type AppSettingsModalProps = { | ||
onCancel: () => void | ||
} | ||
|
||
export const AppSettingsModal: FC<AppSettingsModalProps> = ({ onCancel }) => { | ||
const { priorityLevel, setPriorityLevel } = usePriorityFees() | ||
const priorityTabsValues = useTabs({ | ||
tabs: PRIORITY_TYPED_TABS, | ||
defaultValue: | ||
PRIORITY_TYPED_TABS.find(({ value }) => value === priorityLevel)?.value ?? | ||
PRIORITY_TYPED_TABS[0].value, | ||
}) | ||
|
||
const { slippage, setSlippage } = useSlippage() | ||
const slippageTabsValues = useTabs({ | ||
tabs: SLIPPAGE_TABS, | ||
defaultValue: | ||
SLIPPAGE_TABS.find(({ value }) => parseFloat(value) === slippage)?.value ?? | ||
SLIPPAGE_TABS[0].value, | ||
}) | ||
|
||
const onSaveChanges = () => { | ||
setPriorityLevel(priorityTabsValues.value as PriorityLevel) | ||
setSlippage(parseFloat(slippageTabsValues.value)) | ||
enqueueSnackbar({ | ||
message: 'Changes applied', | ||
type: 'success', | ||
autoHideDuration: 1.5, | ||
}) | ||
onCancel() | ||
} | ||
|
||
return ( | ||
<Modal open onCancel={onCancel} maskClosable={false} width={500}> | ||
<h2 className={styles.title}>Settings</h2> | ||
<TabsContent {...slippageTabsValues} title="Slippage" /> | ||
<TabsContent {...priorityTabsValues} title="Priority mode" /> | ||
|
||
<Button className={styles.saveButton} onClick={onSaveChanges}> | ||
Save changes | ||
</Button> | ||
</Modal> | ||
) | ||
} | ||
|
||
type TabsContentProps = { title: string } & ReturnType<typeof useTabs> | ||
|
||
const TabsContent: FC<TabsContentProps> = ({ title, value, tabs, setValue }) => { | ||
return ( | ||
<> | ||
<h5 className={styles.subtitle}>{title}</h5> | ||
<div className={styles.tabs}> | ||
{tabs.map(({ label, value: tabValue, disabled }) => { | ||
const isActive = tabValue === value | ||
|
||
return ( | ||
<button | ||
key={tabValue} | ||
className={classNames(styles.tab, { [styles.tabActive]: isActive })} | ||
name={tabValue} | ||
onClick={() => { | ||
setValue(tabValue) | ||
}} | ||
disabled={disabled} | ||
> | ||
{label} | ||
</button> | ||
) | ||
})} | ||
</div> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Tab } from '@banx/components/Tabs' | ||
|
||
import { PriorityLevel, SLIPPAGE_VALUES, getHumanReadablePriorityLevel } from '@banx/store/common' | ||
|
||
export const PRIORITY_TYPED_TABS: Tab[] = Object.values(PriorityLevel).map((priorityLevel) => ({ | ||
label: getHumanReadablePriorityLevel(priorityLevel), | ||
value: priorityLevel, | ||
})) | ||
|
||
export const SLIPPAGE_TABS: Tab[] = SLIPPAGE_VALUES.map((slippage) => ({ | ||
label: `${slippage}%`, | ||
value: slippage.toString(), | ||
})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './AppSettingsModal' |
70 changes: 0 additions & 70 deletions
70
src/components/modals/PriorityFeesModal/PriorityFeesModal.tsx
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { z } from 'zod' | ||
import { create } from 'zustand' | ||
|
||
const BANX_SLIPPAGE_STATE_LS_KEY = '@banx.slippageState' | ||
|
||
export const SLIPPAGE_VALUES = [1, 2, 3, 5] as const //% | ||
|
||
const DEFAULT_SLIPPAGE = SLIPPAGE_VALUES[2] | ||
|
||
type SlippageState = { | ||
slippage: number | ||
setSlippage: (slippage: number) => void | ||
} | ||
|
||
export const useSlippageState = create<SlippageState>((set) => ({ | ||
slippage: DEFAULT_SLIPPAGE, | ||
setSlippage: (slippage: number) => set((state) => ({ ...state, slippage })), | ||
})) | ||
|
||
export const useSlippage = () => { | ||
const { slippage, setSlippage: setSlippageState } = useSlippageState((state) => { | ||
const slippage = getSlippage() | ||
|
||
return { | ||
...state, | ||
slippage, | ||
} | ||
}) | ||
|
||
const setSlippage = (slippage: number) => { | ||
try { | ||
setSlippageState(slippage) | ||
localStorage.setItem(BANX_SLIPPAGE_STATE_LS_KEY, JSON.stringify(slippage)) | ||
} catch (error) { | ||
console.error(error) | ||
} | ||
} | ||
|
||
return { slippage, setSlippage } | ||
} | ||
|
||
export const getSlippage = () => { | ||
try { | ||
const slippageJSON = localStorage.getItem(BANX_SLIPPAGE_STATE_LS_KEY) | ||
|
||
//? If slippage isn't saved in LS --> return default | ||
if (!slippageJSON) { | ||
return DEFAULT_SLIPPAGE | ||
} | ||
|
||
const slippage: number = JSON.parse(slippageJSON) | ||
|
||
//? Check LS data validity | ||
z.number().parse(slippage) | ||
|
||
return slippage | ||
} catch (error) { | ||
console.error('Invalid slippage value in LS. Value was removed') | ||
localStorage.removeItem(BANX_SLIPPAGE_STATE_LS_KEY) | ||
return DEFAULT_SLIPPAGE | ||
} | ||
} |