-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: refactor containers to use composition
- Loading branch information
1 parent
cf35f60
commit e258fbd
Showing
82 changed files
with
870 additions
and
769 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Outlet } from 'react-router-dom'; | ||
|
||
import { ChainID } from '@stacks/transactions'; | ||
import { Box } from 'leather-styles/jsx'; | ||
|
||
import { Logo, NetworkModeBadge } from '@leather.io/ui'; | ||
|
||
import { useCurrentNetworkState } from '@app/store/networks/networks.hooks'; | ||
|
||
import { ContainerLayout } from '../../features/container/components/container.layout'; | ||
import { HomeHeader } from '../../features/container/components/home/home-header'; | ||
|
||
export function HomeLayout() { | ||
const { chain, name: chainName } = useCurrentNetworkState(); | ||
|
||
return ( | ||
<ContainerLayout | ||
header={ | ||
<HomeHeader | ||
networkBadge={ | ||
<NetworkModeBadge | ||
isTestnetChain={chain.stacks.chainId === ChainID.Testnet} | ||
name={chainName} | ||
/> | ||
} | ||
logo={ | ||
<Box height="headerContainerHeight" margin="auto" px="space.02"> | ||
<Logo /> | ||
</Box> | ||
} | ||
/> | ||
} | ||
content={<Outlet />} | ||
/> | ||
); | ||
} |
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,54 @@ | ||
import { Outlet, useLocation, useNavigate } from 'react-router-dom'; | ||
|
||
import { ChainID } from '@stacks/transactions'; | ||
import { OnboardingSelectors } from '@tests/selectors/onboarding.selectors'; | ||
import { Box } from 'leather-styles/jsx'; | ||
|
||
import { Logo, NetworkModeBadge } from '@leather.io/ui'; | ||
|
||
import { RouteUrls } from '@shared/route-urls'; | ||
|
||
import { useCurrentNetworkState } from '@app/store/networks/networks.hooks'; | ||
|
||
import { ContainerLayout } from '../../features/container/components/container.layout'; | ||
import { OnboardingHeader } from '../../features/container/components/onboarding/onboarding-header'; | ||
|
||
export function OnboardingLayout() { | ||
const navigate = useNavigate(); | ||
const { pathname: locationPathname } = useLocation(); | ||
const pathname = locationPathname as RouteUrls; | ||
|
||
const { chain, name: chainName } = useCurrentNetworkState(); | ||
|
||
const displayHeader = !pathname.match(RouteUrls.Onboarding); | ||
|
||
return ( | ||
<ContainerLayout | ||
header={ | ||
displayHeader ? ( | ||
<OnboardingHeader | ||
onGoBack={() => navigate(-1)} | ||
networkBadge={ | ||
<NetworkModeBadge | ||
isTestnetChain={chain.stacks.chainId === ChainID.Testnet} | ||
name={chainName} | ||
/> | ||
} | ||
logo={ | ||
// TODO: RouteUrls.ViewSecretKey needs show logo when viewing key but not when entering password | ||
pathname !== RouteUrls.ViewSecretKey && ( | ||
<Box height="headerContainerHeight" margin="auto" px="space.02"> | ||
<Logo | ||
data-testid={OnboardingSelectors.LogoRouteToHome} | ||
onClick={() => navigate(RouteUrls.Home)} | ||
/> | ||
</Box> | ||
) | ||
} | ||
/> | ||
) : null | ||
} | ||
content={<Outlet />} | ||
/> | ||
); | ||
} |
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 { 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; | ||
}; | ||
|
||
export function useUpdatePageHeaderContext(payload: HeaderPayloadState) { | ||
const { dispatch } = usePageContext(); | ||
|
||
useEffect(() => { | ||
dispatch({ type: 'update', payload }); | ||
return () => { | ||
dispatch({ type: 'reset' }); | ||
}; | ||
}); | ||
} |
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 { Outlet } from 'react-router-dom'; | ||
|
||
import { ContainerLayout } from '../../features/container/components/container.layout'; | ||
import { PageHeader } from '../../features/container/components/page/page.header'; | ||
import { PageProvider } from './page.context'; | ||
|
||
export function PageLayout() { | ||
return ( | ||
<PageProvider> | ||
<ContainerLayout header={<PageHeader />} content={<Outlet />} /> | ||
</PageProvider> | ||
); | ||
} |
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,106 @@ | ||
import { useState } from 'react'; | ||
import { Outlet, useLocation } from 'react-router-dom'; | ||
|
||
import { ChainID } from '@stacks/transactions'; | ||
import { Box } from 'leather-styles/jsx'; | ||
|
||
import { Flag, Logo, NetworkModeBadge } from '@leather.io/ui'; | ||
|
||
import { RouteUrls } from '@shared/route-urls'; | ||
|
||
import { CurrentAccountAvatar } from '@app/features/current-account/current-account-avatar'; | ||
import { CurrentAccountName } from '@app/features/current-account/current-account-name'; | ||
import { SwitchAccountDialog } from '@app/features/dialogs/switch-account-dialog/switch-account-dialog'; | ||
import { useCurrentNetworkState } from '@app/store/networks/networks.hooks'; | ||
|
||
import { ContainerLayout } from '../../features/container/components/container.layout'; | ||
import { PopupHeader } from '../../features/container/components/popup/popup-header'; | ||
import { TotalBalance } from '../../features/container/total-balance'; | ||
|
||
function showHeader(pathname: RouteUrls) { | ||
switch (pathname) { | ||
case RouteUrls.RpcGetAddresses: | ||
case RouteUrls.ChooseAccount: | ||
return false; | ||
default: | ||
return true; | ||
} | ||
} | ||
|
||
function showAccountInfo(pathname: RouteUrls) { | ||
switch (pathname) { | ||
case RouteUrls.TransactionRequest: | ||
case RouteUrls.ProfileUpdateRequest: | ||
case RouteUrls.RpcSendTransfer: | ||
return true; | ||
default: | ||
return false; | ||
} | ||
} | ||
|
||
function showBalanceInfo(pathname: RouteUrls) { | ||
switch (pathname) { | ||
case RouteUrls.ProfileUpdateRequest: | ||
case RouteUrls.RpcSendTransfer: | ||
return true; | ||
default: | ||
return false; | ||
} | ||
} | ||
|
||
export function PopupLayout() { | ||
const [isShowingSwitchAccount, setIsShowingSwitchAccount] = useState(false); | ||
const { pathname: locationPathname } = useLocation(); | ||
const pathname = locationPathname as RouteUrls; | ||
|
||
const { chain, name: chainName } = useCurrentNetworkState(); | ||
|
||
const isHeaderVisible = showHeader(pathname); | ||
|
||
return ( | ||
<> | ||
{isShowingSwitchAccount && ( | ||
<SwitchAccountDialog | ||
isShowing={isShowingSwitchAccount} | ||
onClose={() => setIsShowingSwitchAccount(false)} | ||
/> | ||
)} | ||
<ContainerLayout | ||
header={ | ||
isHeaderVisible ? ( | ||
<PopupHeader | ||
networkBadge={ | ||
<NetworkModeBadge | ||
isTestnetChain={chain.stacks.chainId === ChainID.Testnet} | ||
name={chainName} | ||
/> | ||
} | ||
logo={ | ||
<Box height="headerPopupHeight" margin="auto" px="space.02"> | ||
<Logo /> | ||
</Box> | ||
} | ||
account={ | ||
showAccountInfo(pathname) && ( | ||
<Flag | ||
align="middle" | ||
img={<CurrentAccountAvatar />} | ||
onClick={() => setIsShowingSwitchAccount(!isShowingSwitchAccount)} | ||
cursor="pointer" | ||
> | ||
<CurrentAccountName /> | ||
</Flag> | ||
) | ||
} | ||
totalBalance={ | ||
// We currently only show displayAddresssBalanceOf="all" in the total balance component | ||
showBalanceInfo(pathname) && <TotalBalance displayAddresssBalanceOf="all" /> | ||
} | ||
/> | ||
) : undefined | ||
} | ||
content={<Outlet />} | ||
/> | ||
</> | ||
); | ||
} |
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
16 changes: 16 additions & 0 deletions
16
src/app/features/container/components/container.layout.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,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> | ||
); | ||
} |
Oops, something went wrong.