Skip to content

Commit

Permalink
kit wallet state
Browse files Browse the repository at this point in the history
  • Loading branch information
SamueleA committed May 16, 2024
1 parent 765ef1a commit 8151246
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 195 deletions.
1 change: 1 addition & 0 deletions packages/wallet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"react": ">=17",
"react-dom": ">=17",
"wagmi": ">=2.5.0",
"valtio": ">=1.13.0",
"viem": ">=2.0.0"
},
"devDependencies": {
Expand Down
10 changes: 0 additions & 10 deletions packages/wallet/src/contexts/WalletModal.ts

This file was deleted.

19 changes: 0 additions & 19 deletions packages/wallet/src/contexts/genericContext.ts

This file was deleted.

24 changes: 13 additions & 11 deletions packages/wallet/src/hooks/useNavigation.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { useNavigationContext, Navigation, History } from '../contexts/Navigation'
import { useSnapshot, INTERNAL_Snapshot } from 'valtio'

interface UseNavigation {
setNavigation: (navigation: Navigation) => void
setHistory?: (history: History) => void
history: History
goBack: () => void
}
import { setHistory, navigationState, Navigation, History } from '../states/Navigation'

// interface UseNavigation {
// setNavigation: (navigation: Navigation) => void
// setHistory: (history: History) => void
// history: INTERNAL_Snapshot<History>
// goBack: () => void
// }

export const useNavigation = (): UseNavigation => {
const { setHistory, history } = useNavigationContext()
export const useNavigation = () => {
const { history } = useSnapshot(navigationState)

const setNavigation = (navigation: Navigation) => {
// Scroll to top of page when navigating to a new page
Expand All @@ -17,13 +19,13 @@ export const useNavigation = (): UseNavigation => {
parentElement?.scrollTo(0, 0)

const newHistory = navigation.location === 'home' ? [] : [...history, navigation]
setHistory(newHistory)
setHistory(newHistory as History)
}

const goBack = () => {
const newHistory = [...history]
newHistory.pop()
setHistory(newHistory)
setHistory(newHistory as History)
}

return { setNavigation, history, setHistory, goBack }
Expand Down
6 changes: 4 additions & 2 deletions packages/wallet/src/hooks/useOpenWalletModal.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { useWalletModalContext } from '../contexts/WalletModal'
import { useSnapshot } from 'valtio'

import { setOpenWalletModal, walletModalState } from '../states/WalletModal'

export const useOpenWalletModal = () => {
const { setOpenWalletModal, openWalletModalState } = useWalletModalContext()
const { openWalletModalState } = useSnapshot(walletModalState)

return { setOpenWalletModal, openWalletModalState }
}
82 changes: 39 additions & 43 deletions packages/wallet/src/shared/KitWalletProvider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { AnimatePresence } from 'framer-motion'
import React, { useState, useEffect } from 'react'

import { HEADER_HEIGHT } from '../../constants'
import { History, Navigation, NavigationContextProvider, WalletModalContextProvider } from '../../contexts'
import { useNavigation, useOpenWalletModal } from '../../hooks'
import { History, Navigation } from '../../states'

import { getHeader, getContent } from './utils'

Expand Down Expand Up @@ -60,12 +61,9 @@ export const KitWalletProvider = (props: KitWalletProviderProps) => {

export const KitWalletContent = ({ children }: KitWalletProviderProps) => {
const { theme, position } = useTheme()
const { openWalletModalState, setOpenWalletModal } = useOpenWalletModal()

// Wallet Modal Context
const [openWalletModal, setOpenWalletModal] = useState<boolean>(false)

// Navigation Context
const [history, setHistory] = useState<History>([])
const { history, setHistory, setNavigation } = useNavigation()
const navigation = history.length > 0 ? history[history.length - 1] : DEFAULT_LOCATION

const displayScrollbar =
Expand All @@ -79,46 +77,44 @@ export const KitWalletContent = ({ children }: KitWalletProviderProps) => {
navigation.location === 'settings-currency'

useEffect(() => {
if (openWalletModal) {
if (openWalletModalState) {
setHistory([])
}
}, [openWalletModal])
}, [openWalletModalState])

return (
<WalletModalContextProvider value={{ setOpenWalletModal, openWalletModalState: openWalletModal }}>
<NavigationContextProvider value={{ setHistory, history }}>
<div id="kit-provider">
<ThemeProvider root="#kit-provider" scope="kit" theme={theme}>
<AnimatePresence>
{openWalletModal && (
<Modal
contentProps={{
style: {
maxWidth: '400px',
height: 'fit-content',
...getModalPositionCss(position)
}
}}
scroll={false}
backdropColor="backgroundBackdrop"
onClose={() => setOpenWalletModal(false)}
>
<Box id="sequence-kit-wallet-content">
{getHeader(navigation)}

{displayScrollbar ? (
<Scroll style={{ paddingTop: HEADER_HEIGHT, height: 'min(800px, 80vh)' }}>{getContent(navigation)}</Scroll>
) : (
getContent(navigation)
)}
</Box>
</Modal>
)}
</AnimatePresence>
</ThemeProvider>
</div>
{children}
</NavigationContextProvider>
</WalletModalContextProvider>
<>
<div id="kit-provider">
<ThemeProvider root="#kit-provider" scope="kit" theme={theme}>
<AnimatePresence>
{openWalletModalState && (
<Modal
contentProps={{
style: {
maxWidth: '400px',
height: 'fit-content',
...getModalPositionCss(position)
}
}}
scroll={false}
backdropColor="backgroundBackdrop"
onClose={() => setOpenWalletModal(false)}
>
<Box id="sequence-kit-wallet-content">
{getHeader(navigation as Navigation)}

{displayScrollbar ? (
<Scroll style={{ paddingTop: HEADER_HEIGHT, height: 'min(800px, 80vh)' }}>{getContent(navigation)}</Scroll>
) : (
getContent(navigation as Navigation)
)}
</Box>
</Modal>
)}
</AnimatePresence>
</ThemeProvider>
</div>
{children}
</>
)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'

import { Navigation } from '../../../contexts'
import { Navigation } from '../../../states'
import {
CoinDetails,
CollectibleDetails,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Transaction } from '@0xsequence/indexer'

import { createGenericContext } from './genericContext'
import { proxy } from 'valtio'

export interface CollectionDetailsParams {
contractAddress: string
Expand Down Expand Up @@ -97,9 +97,14 @@ export type Navigation =

export type History = Navigation[]

type NavigationContext = {
setHistory: (history: History) => void
type NavigationState = {
history: History
}

export const [useNavigationContext, NavigationContextProvider] = createGenericContext<NavigationContext>()
export const navigationState = proxy<NavigationState>({
history: []
})

export const setHistory = (history: History) => {
navigationState.history = history
}
13 changes: 13 additions & 0 deletions packages/wallet/src/states/WalletModal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { proxy } from 'valtio'

interface WalletModalState {
openWalletModalState: boolean
}

export const walletModalState = proxy<WalletModalState>({
openWalletModalState: false
})

export const setOpenWalletModal = (open: boolean) => {
walletModalState.openWalletModalState = open
}
File renamed without changes.
2 changes: 1 addition & 1 deletion packages/wallet/src/views/Search/components/WalletLink.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Box, Text, ChevronRightIcon } from '@0xsequence/design-system'
import React from 'react'

import { Navigation } from '../../../contexts'
import { Navigation } from '../../../states'
import { useNavigation } from '../../../hooks'

interface WalletLinkProps {
Expand Down
Loading

0 comments on commit 8151246

Please sign in to comment.