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

feat: user profile #589

Merged
merged 19 commits into from
Jan 15, 2025
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: 2 additions & 1 deletion apps/dao/src/components/ConnectWallet.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { isLoading } from '@/ui/utils'
import useStore from '@/store/useStore'
import { useUserProfileStore } from '@ui-kit/features/user-profile'

import ConnectWalletPrompt from '@/ui/ConnectWalletPrompt'

Expand All @@ -12,7 +13,7 @@ type ConnectWalletProps = {
const ConnectWallet: React.FC<ConnectWalletProps> = ({ description, connectText, loadingText }) => {
const updateConnectWalletStateKeys = useStore((state) => state.wallet.updateConnectWalletStateKeys)
const connectState = useStore((state) => state.connectState)
const theme = useStore((state) => state.themeType)
const theme = useUserProfileStore((state) => state.theme)

const loading = isLoading(connectState)

Expand Down
6 changes: 5 additions & 1 deletion apps/dao/src/hooks/usePageOnMount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { helpers } from '@/lib/curvejs'
import { isFailure, isLoading, isSuccess } from '@/ui/utils'
import networks from '@/networks'
import useStore from '@/store/useStore'
import { useUserProfileStore } from '@ui-kit/features/user-profile'

function usePageOnMount(params: Params, location: Location, navigate: NavigateFunction, chainIdNotRequired?: boolean) {
const [{ wallet }, connect, disconnect] = useConnectWallet()
Expand All @@ -26,6 +27,8 @@ function usePageOnMount(params: Params, location: Location, navigate: NavigateFu
const updateCurveJs = useStore((state) => state.updateCurveJs)
const updateGlobalStoreByKey = useStore((state) => state.updateGlobalStoreByKey)

const setLocale = useUserProfileStore((state) => state.setLocale)

const walletChainId = getWalletChainId(wallet)
const walletSignerAddress = getWalletSignerAddress(wallet)
const parsedParams = parseParams(params, chainIdNotRequired)
Expand Down Expand Up @@ -219,7 +222,8 @@ function usePageOnMount(params: Params, location: Location, navigate: NavigateFu
let data = await import(`@/locales/${rLocale}/messages`)
dynamicActivate(rLocale, data)
})()
updateAppLocale(rLocale, updateGlobalStoreByKey)
setLocale(rLocale)
updateAppLocale(rLocale)
updateWalletLocale(rLocale)
} else if (walletChainId && curve && curve.chainId === walletChainId && parsedParams.rChainId !== walletChainId) {
// switch network if url network is not same as wallet
Expand Down
18 changes: 3 additions & 15 deletions apps/dao/src/layout/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import useLayoutHeight from '@/hooks/useLayoutHeight'
import useStore from '@/store/useStore'
import { Header as NewHeader, useHeaderHeight } from '@/common/widgets/Header'
import { NavigationSection } from '@/common/widgets/Header/types'
import { ThemeKey } from 'curve-ui-kit/src/themes/basic-theme'
import { APP_LINK } from '@ui-kit/shared/routes'
import { GlobalBannerProps } from '@/ui/Banner/GlobalBanner'
import { useUserProfileStore } from '@ui-kit/features/user-profile'

type HeaderProps = { sections: NavigationSection[]; BannerProps: GlobalBannerProps }

Expand All @@ -27,41 +27,29 @@ export const Header = ({ sections, BannerProps }: HeaderProps) => {
const connectState = useStore((state) => state.connectState)
const isMdUp = useStore((state) => state.layout.isMdUp)
const bannerHeight = useStore((state) => state.layoutHeight.globalAlert)
const locale = useStore((state) => state.locale)
const routerProps = useStore((state) => state.routerProps)
const themeType = useStore((state) => state.themeType)
const setAppCache = useStore((state) => state.setAppCache)
const updateConnectState = useStore((state) => state.updateConnectState)

const locale = useUserProfileStore((state) => state.locale)

const location = useLocation()
const { rLocalePathname } = getLocaleFromUrl()
const { params: routerParams } = routerProps ?? {}

const routerNetwork = routerParams?.network ?? 'ethereum'
const routerPathname = location?.pathname ?? ''

const theme = themeType == 'default' ? 'light' : (themeType as ThemeKey)
return (
<NewHeader<ChainId>
networkName={rNetwork}
mainNavRef={mainNavRef}
locale={locale}
isMdUp={isMdUp}
currentApp="dao"
pages={useMemo(
() => _parseRouteAndIsActive(APP_LINK.dao.pages, rLocalePathname, routerPathname, routerNetwork),
[rLocalePathname, routerNetwork, routerPathname],
)}
themes={[
theme,
useCallback(
(selectedThemeType: ThemeKey) =>
setAppCache('themeType', selectedThemeType == 'light' ? 'default' : selectedThemeType),
[setAppCache],
),
]}
ChainProps={{
theme,
options: visibleNetworksList,
disabled: isLoading(connectState, CONNECT_STAGE.SWITCH_NETWORK),
chainId: rChainId,
Expand Down
4 changes: 3 additions & 1 deletion apps/dao/src/layout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import useStore from '@/store/useStore'

import Header from '@/layout/Header'
import { Footer } from 'curve-ui-kit/src/widgets/Footer'
import { useUserProfileStore } from '@ui-kit/features/user-profile'

const BaseLayout = ({ children }: { children: React.ReactNode }) => {
const [{ wallet }] = useConnectWallet()
Expand All @@ -23,7 +24,8 @@ const BaseLayout = ({ children }: { children: React.ReactNode }) => {
const layoutHeight = useStore((state) => state.layoutHeight)
const updateConnectState = useStore((state) => state.updateConnectState)
const updateLayoutHeight = useStore((state) => state.updateLayoutHeight)
const locale = useStore((state) => state.locale)

const locale = useUserProfileStore((state) => state.locale)

useEffect(() => {
updateLayoutHeight('globalAlert', globalAlertHeight)
Expand Down
28 changes: 13 additions & 15 deletions apps/dao/src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { ThemeProvider } from 'curve-ui-kit/src/shared/ui/ThemeProvider'

import { dynamicActivate, initTranslation, updateAppLocale } from '@ui-kit/lib/i18n'
import { connectWalletLocales, initOnboard } from '@ui-kit/features/connect-wallet'
import { getLocaleFromUrl, getStorageValue } from '@/utils'
import { getLocaleFromUrl } from '@/utils'
import { getIsMobile, getPageWidthClassName, isSuccess } from '@/ui/utils'
import { messages as messagesEn } from '@/locales/en/messages.js'
import networks from '@/networks'
Expand All @@ -25,15 +25,14 @@ import usePageVisibleInterval from '@/hooks/usePageVisibleInterval'
import Page from '@/layout'
import GlobalStyle from '@/globalStyle'
import { ChadCssProperties } from '@ui-kit/themes/typography'
import { useUserProfileStore } from '@ui-kit/features/user-profile'

i18n.load({ en: messagesEn })
i18n.activate('en')

function CurveApp({ Component }: AppProps) {
const connectState = useStore((state) => state.connectState)
const locale = useStore((state) => state.locale)
const pageWidth = useStore((state) => state.layout.pageWidth)
const themeType = useStore((state) => state.themeType)
const setPageWidth = useStore((state) => state.layout.setLayoutWidth)
const updateShowScrollButton = useStore((state) => state.updateShowScrollButton)
const updateGlobalStoreByKey = useStore((state) => state.updateGlobalStoreByKey)
Expand All @@ -47,6 +46,10 @@ function CurveApp({ Component }: AppProps) {
const onboard = useStore((state) => state.wallet.onboard)
const isPageVisible = useStore((state) => state.isPageVisible)

const theme = useUserProfileStore((state) => state.theme)
const locale = useUserProfileStore((state) => state.locale)
const setLocale = useUserProfileStore((state) => state.setLocale)

const [appLoaded, setAppLoaded] = useState(false)

const handleResizeListener = useCallback(() => {
Expand All @@ -57,8 +60,8 @@ function CurveApp({ Component }: AppProps) {
useEffect(() => {
if (!pageWidth) return

document.body.className = `theme-${themeType} ${pageWidth} ${getIsMobile() ? '' : 'scrollSmooth'}`
document.body.setAttribute('data-theme', themeType || '')
document.body.className = `theme-${theme} ${pageWidth} ${getIsMobile() ? '' : 'scrollSmooth'}`
document.body.setAttribute('data-theme', theme)
document.documentElement.lang = locale
})

Expand All @@ -67,12 +70,6 @@ function CurveApp({ Component }: AppProps) {
updateShowScrollButton(window.scrollY)
}

const { themeType } = getStorageValue('APP_CACHE') ?? {}

// init theme
const darkModeQuery = window.matchMedia('(prefers-color-scheme: dark)')
updateGlobalStoreByKey('themeType', themeType ? themeType : darkModeQuery.matches ? 'dark' : 'default')

// init locale
const { rLocale } = getLocaleFromUrl()
const parsedLocale = rLocale?.value ?? 'en'
Expand All @@ -81,10 +78,11 @@ function CurveApp({ Component }: AppProps) {
let data = await import(`@/locales/${parsedLocale}/messages`)
dynamicActivate(parsedLocale, data)
})()
updateAppLocale(parsedLocale, updateGlobalStoreByKey)
setLocale(parsedLocale)
updateAppLocale(parsedLocale)

// init onboard
const onboardInstance = initOnboard(connectWalletLocales, locale, themeType, networks)
const onboardInstance = initOnboard(connectWalletLocales, locale, theme, networks)
updateWalletStoreByKey('onboard', onboardInstance)

const handleVisibilityChange = () => {
Expand Down Expand Up @@ -148,8 +146,8 @@ function CurveApp({ Component }: AppProps) {
)

return (
<div suppressHydrationWarning style={{ ...(themeType === 'chad' && ChadCssProperties) }}>
<ThemeProvider theme={themeType === 'default' ? 'light' : themeType}>
<div suppressHydrationWarning style={{ ...(theme === 'chad' && ChadCssProperties) }}>
<ThemeProvider theme={theme}>
{typeof window === 'undefined' || !appLoaded ? null : (
<HashRouter>
<I18nProvider i18n={i18n}>
Expand Down
27 changes: 0 additions & 27 deletions apps/dao/src/store/createAppSlice.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
import type { GetState, SetState } from 'zustand'
import type { State } from '@/store/useStore'
import type { ConnectState } from '@/ui/utils'
import type { Locale } from '@ui-kit/lib/i18n'

import isEqual from 'lodash/isEqual'
import produce from 'immer'

import { log } from '@ui-kit/lib'
import { setStorageValue } from '@/utils'

export type DefaultStateKeys = keyof typeof DEFAULT_STATE
export type SliceKey = keyof State | ''
export type StateKey = string
export type Theme = 'dark' | 'default' | 'chad'
export type LayoutHeight = {
globalAlert: number
mainNav: number
secondaryNav: number
footer: number
}

type AppCacheKeys = 'themeType'

type SliceState = {
connectState: ConnectState
curve: CurveApi | null
Expand All @@ -31,21 +26,16 @@ type SliceState = {
isPageVisible: boolean
layoutHeight: LayoutHeight
loaded: boolean
locale: Locale['value']
maxSlippage: string
routerProps: RouterProps | null
showScrollButton: boolean
themeType: Theme
}

// prettier-ignore
export interface AppSlice extends SliceState {
setThemeType: (themeType: Theme) => void
updateConnectState(status: ConnectState['status'], stage: ConnectState['stage'], options?: ConnectState['options']): void
updateCurveJs(curveApi: CurveApi, prevCurveApi: CurveApi | null, wallet: Wallet | null): Promise<void>
updateLayoutHeight: (key: keyof LayoutHeight, value: number | null) => void
updateShowScrollButton(scrollY: number): void
setAppCache<T>(key: AppCacheKeys, value: T): void
updateGlobalStoreByKey: <T>(key: DefaultStateKeys, value: T) => void

setAppStateByActiveKey<T>(sliceKey: SliceKey, key: StateKey, activeKey: string, value: T, showLog?: boolean): void
Expand All @@ -62,31 +52,20 @@ const DEFAULT_STATE = {
isLoadingCurve: true,
isPageVisible: true,
loaded: false,
locale: 'en' as const,
pageWidth: null,
layoutHeight: {
globalAlert: 0,
mainNav: 0,
secondaryNav: 0,
footer: 0,
},
maxSlippage: '',
routerProps: null,
showScrollButton: false,
themeType: 'default' as const,
}

const createAppSlice = (set: SetState<State>, get: GetState<State>): AppSlice => ({
...DEFAULT_STATE,

setThemeType: (themeType: Theme) => {
set(
produce((state: State) => {
state.themeType = themeType
}),
)
setStorageValue('APP_CACHE', { themeType })
},
updateConnectState: (
status: ConnectState['status'],
stage: ConnectState['stage'],
Expand Down Expand Up @@ -146,12 +125,6 @@ const createAppSlice = (set: SetState<State>, get: GetState<State>): AppSlice =>
)
}
},
setAppCache: <T>(key: AppCacheKeys, value: T) => {
get().updateGlobalStoreByKey(key, value)
setStorageValue('APP_CACHE', {
themeType: key === 'themeType' ? value : get().themeType || 'default',
})
},
updateGlobalStoreByKey: <T>(key: DefaultStateKeys, value: T) => {
set(
produce((state) => {
Expand Down
10 changes: 0 additions & 10 deletions apps/dao/src/utils/utilsStorage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type { Theme } from '@/store/createAppSlice'

import merge from 'lodash/merge'

import dayjs from '@ui-kit/lib/dayjs'
Expand All @@ -24,20 +22,12 @@ export function getStorageValue(key: Key) {

if (key === 'APP_CACHE') {
return {
themeType: getTheme(parsedStoredValue.themeType),
timestamp: parsedStoredValue.timestamp ?? '',
walletName: getWalletName(parsedStoredValue.walletName, parsedStoredValue.timestamp),
}
}
}

function getTheme(svThemeType: string | undefined) {
if (svThemeType) {
const foundThemeType = ['default', 'dark', 'chad'].find((t) => t === svThemeType) as Theme
return (foundThemeType || 'default') as Theme
}
}

function getWalletName(walletName: string | undefined, timestamp: string | undefined) {
const isStaled = walletName && timestamp && dayjs().diff(+timestamp, 'days') > 5
return isStaled || !walletName ? '' : walletName
Expand Down
6 changes: 4 additions & 2 deletions apps/lend/src/components/AdvancedSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Icon from '@/ui/Icon'
import IconTooltip from '@/ui/Tooltip/TooltipIcon'
import ModalDialog, { OpenDialogIconButton } from '@/ui/Dialog'
import InputProvider, { InputField } from '@/ui/InputComp'
import { useUserProfileStore } from '@ui-kit/features/user-profile'

type FormValues = {
selected: string
Expand Down Expand Up @@ -57,13 +58,14 @@ function getDefaultFormValuesState(formValues: FormValues, propsMaxSlippage: str
export const AdvancedSettings = ({ className, buttonIcon, maxSlippage }: React.PropsWithChildren<Props>) => {
const overlayTriggerState = useOverlayTriggerState({})
const isMobile = useStore((state) => state.isMobile)
const updateGlobalStoreByKey = useStore((state) => state.updateGlobalStoreByKey)

const setMaxSlippage = useUserProfileStore((state) => state.setMaxSlippage)

const [formValues, setFormValues] = useState(DEFAULT_FORM_VALUES)

const handleSave = () => {
const updatedCustomSlippage = formValues.selected === 'custom' ? formValues.customValue : formValues.selected
updateGlobalStoreByKey('maxSlippage', updatedCustomSlippage)
setMaxSlippage(updatedCustomSlippage)
if (isMobile) {
delayAction(overlayTriggerState.close)
} else {
Expand Down
12 changes: 7 additions & 5 deletions apps/lend/src/components/ChartOhlcWrapper/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ import PoolActivity from '@/components/ChartOhlcWrapper/PoolActivity'
import TextCaption from '@/ui/TextCaption'
import AlertBox from '@/ui/AlertBox'
import { useOneWayMarket } from '@/entities/chain'
import { useUserProfileStore } from '@ui-kit/features/user-profile'

const ChartOhlcWrapper: React.FC<ChartOhlcWrapperProps> = ({ rChainId, userActiveKey, rOwmId }) => {
const market = useOneWayMarket(rChainId, rOwmId).data
const isAdvanceMode = useStore((state) => state.isAdvanceMode)
const themeType = useStore((state) => state.themeType)
const borrowMoreActiveKey = useStore((state) => state.loanBorrowMore.activeKey)
const loanRepayActiveKey = useStore((state) => state.loanRepay.activeKey)
const loanCollateralAddActiveKey = useStore((state) => state.loanCollateralAdd.activeKey)
Expand All @@ -40,6 +39,9 @@ const ChartOhlcWrapper: React.FC<ChartOhlcWrapperProps> = ({ rChainId, userActiv
(state) => state.loanCollateralRemove.detailInfo[loanCollateralRemoveActiveKey]?.prices ?? null,
)

const theme = useUserProfileStore((state) => state.theme)
const isAdvancedMode = useUserProfileStore((state) => state.isAdvancedMode)

const isMdUp = useStore((state) => state.layout.isMdUp)
const {
chartLlammaOhlc,
Expand Down Expand Up @@ -377,7 +379,7 @@ const ChartOhlcWrapper: React.FC<ChartOhlcWrapperProps> = ({ rChainId, userActiv
chartStatus={currentChart.fetchStatus}
chartHeight={chartHeight}
chartExpanded={chartExpanded}
themeType={themeType}
themeType={theme}
ohlcData={currentChart.data}
volumeData={chartLlammaOhlc.volumeData}
oraclePriceData={oraclePriceData}
Expand Down Expand Up @@ -405,7 +407,7 @@ const ChartOhlcWrapper: React.FC<ChartOhlcWrapperProps> = ({ rChainId, userActiv
</LpEventsWrapperExpanded>
</ExpandedWrapper>
) : (
<Wrapper className={!isAdvanceMode ? 'normal-mode' : ''} chartExpanded={chartExpanded}>
<Wrapper className={isAdvancedMode ? '' : 'normal-mode'} chartExpanded={chartExpanded}>
<SelectorRow>
<SelectorButton
variant={'text'}
Expand Down Expand Up @@ -437,7 +439,7 @@ const ChartOhlcWrapper: React.FC<ChartOhlcWrapperProps> = ({ rChainId, userActiv
chartStatus={currentChart.fetchStatus}
chartHeight={chartHeight}
chartExpanded={false}
themeType={themeType}
themeType={theme}
ohlcData={currentChart.data}
volumeData={chartLlammaOhlc.volumeData}
oraclePriceData={oraclePriceData}
Expand Down
Loading
Loading