-
Notifications
You must be signed in to change notification settings - Fork 145
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
chore: refactor containers #5619
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { ReactNode, createContext, useContext, useEffect, useReducer } from 'react'; | ||
|
||
import { RouteUrls } from '@shared/route-urls'; | ||
|
||
interface HeaderPayloadState { | ||
title?: string; | ||
isSummaryPage?: boolean; | ||
isSessionLocked?: boolean; | ||
isSettingsVisibleOnSm?: boolean; | ||
onBackLocation?: RouteUrls; | ||
onClose?(): void; | ||
} | ||
|
||
interface UpdateAction { | ||
type: 'update'; | ||
payload: HeaderPayloadState; | ||
} | ||
|
||
interface ResetAction { | ||
type: 'reset'; | ||
} | ||
type Action = UpdateAction | ResetAction; | ||
|
||
const initialPageState = { isSessionLocked: false, isSettingsVisibleOnSm: true }; | ||
const pageReducer = (state: HeaderPayloadState, action: Action): HeaderPayloadState => { | ||
switch (action.type) { | ||
case 'update': | ||
return { ...state, ...action.payload }; | ||
case 'reset': | ||
default: | ||
return initialPageState; | ||
} | ||
}; | ||
|
||
const PageContext = createContext< | ||
{ state: HeaderPayloadState; dispatch: React.Dispatch<Action> } | undefined | ||
>(undefined); | ||
|
||
export function PageProvider({ children }: { children: ReactNode }) { | ||
const [state, dispatch] = useReducer(pageReducer, initialPageState); | ||
const value = { state, dispatch }; | ||
return <PageContext.Provider value={value}>{children}</PageContext.Provider>; | ||
} | ||
|
||
export const usePageContext = () => { | ||
const context = useContext(PageContext); | ||
if (context === undefined) { | ||
throw new Error('usePageContext must be used within a PageProvider'); | ||
} | ||
return context; | ||
}; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm using a context to help dynamically update the page header as sometimes it needs to vary based on the page. That is working well generally apart from on the See here I tried using |
||
export function useUpdatePageHeaderContext(payload: HeaderPayloadState) { | ||
const { dispatch } = usePageContext(); | ||
|
||
useEffect(() => { | ||
dispatch({ type: 'update', payload }); | ||
return () => { | ||
dispatch({ type: 'reset' }); | ||
}; | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,7 @@ export function Sip10TokenAssetList({ | |
{tokens.map(token => ( | ||
<Sip10TokenAssetItem | ||
balance={token.balance} | ||
key={token.info.name} | ||
key={token.info.name + token.info.contractId} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding this as it was throwing a duplicate keys error for |
||
info={token.info} | ||
isLoading={isLoading} | ||
marketData={priceAsMarketData( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Flex } from 'leather-styles/jsx'; | ||
|
||
interface ContainerLayoutProps { | ||
content?: React.JSX.Element | React.JSX.Element[]; | ||
header?: React.JSX.Element | null; | ||
} | ||
export function ContainerLayout({ content, header }: ContainerLayoutProps) { | ||
return ( | ||
<Flex flexDirection="column" flexGrow={1} width="100%" height={{ base: '100vh', sm: '100%' }}> | ||
{header} | ||
<Flex className="main-content" flexGrow={1} position="relative" width="100%"> | ||
{content} | ||
</Flex> | ||
</Flex> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { ReactNode, useState } from 'react'; | ||
|
||
import { SettingsSelectors } from '@tests/selectors/settings.selectors'; | ||
import { Flex, Grid, GridItem, HStack, styled } from 'leather-styles/jsx'; | ||
|
||
import { HamburgerIcon } from '@leather.io/ui'; | ||
|
||
import { SwitchAccountDialog } from '@app/features/dialogs/switch-account-dialog/switch-account-dialog'; | ||
|
||
import { Settings } from '../../../settings/settings'; | ||
|
||
interface HomeHeaderProps { | ||
networkBadge?: ReactNode; | ||
logo?: ReactNode; | ||
} | ||
|
||
export function HomeHeader({ networkBadge, logo }: HomeHeaderProps) { | ||
const [isShowingSwitchAccount, setIsShowingSwitchAccount] = useState(false); | ||
return ( | ||
<> | ||
{isShowingSwitchAccount && ( | ||
<SwitchAccountDialog | ||
isShowing={isShowingSwitchAccount} | ||
onClose={() => setIsShowingSwitchAccount(false)} | ||
/> | ||
)} | ||
<styled.header | ||
justifyContent="center" | ||
margin={{ base: 0, md: 'auto' }} | ||
p="space.04" | ||
bg="transparent" | ||
maxWidth={{ base: '100vw', md: 'fullPageMaxWidth' }} | ||
width="100%" | ||
> | ||
<Grid alignItems="center" gridTemplateColumns="auto" gridAutoFlow="column" width="100%"> | ||
<GridItem justifySelf="start"> | ||
<Flex py={{ base: 0, md: 'space.01' }}>{logo}</Flex> | ||
</GridItem> | ||
|
||
<GridItem> | ||
<HStack alignItems="center" justifyContent="flex-end"> | ||
{networkBadge} | ||
<Settings | ||
triggerButton={<HamburgerIcon data-testid={SettingsSelectors.SettingsMenuBtn} />} | ||
toggleSwitchAccount={() => setIsShowingSwitchAccount(!isShowingSwitchAccount)} | ||
/> | ||
</HStack> | ||
</GridItem> | ||
</Grid> | ||
</styled.header> | ||
</> | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm placing this here for now as I'm not sure where it should be located