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

Handling modal windows #70

Merged
merged 25 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
bdd3623
Handle of modal window for stake/unstake actions
kkosiorowska Dec 8, 2023
7cd3446
Open the docs drawer by button
kkosiorowska Dec 8, 2023
e32f154
Add basic styles for the Modal, Drawer, Sidebar
kkosiorowska Dec 11, 2023
4be5fce
Merge branch 'main' of github.com:thesis/acre into handling-modal-window
kkosiorowska Dec 11, 2023
9f2e4c1
Merge branch 'main' of github.com:thesis/acre into handling-modal-window
kkosiorowska Dec 11, 2023
8cbb73b
Use variables for colors
kkosiorowska Dec 11, 2023
0892776
Fix a eslint issue
kkosiorowska Dec 11, 2023
1d4e834
Fix for incorrect colors
kkosiorowska Dec 12, 2023
dcac86c
Change the approach to opening modals
kkosiorowska Dec 14, 2023
567d04a
Merge branch 'main' of github.com:thesis/acre into handling-modal-window
kkosiorowska Dec 14, 2023
bb9bb62
Fix issues for eslint
kkosiorowska Dec 14, 2023
7ee5708
Define a new color
kkosiorowska Dec 14, 2023
4ebb61a
Create a multi-style config for Sidebar
kkosiorowska Dec 14, 2023
6b6b3ef
Add a missing comma
kkosiorowska Dec 14, 2023
5868aab
Return an activeStep as a number
kkosiorowska Dec 14, 2023
45bd3a7
Remove unnecessary overwriting for zIndices
kkosiorowska Dec 14, 2023
f74104e
Move a `ModalCloseButton` to `ModalBase`
kkosiorowska Dec 15, 2023
eebd8a6
Use a `useBoolean` to open the staking modal
kkosiorowska Dec 15, 2023
94c2e9c
Keep all staking-related modals in one dir
kkosiorowska Dec 15, 2023
ba3298b
Simplify style definitions
kkosiorowska Dec 15, 2023
0d3e971
Merge branch 'main' of github.com:thesis/acre into handling-modal-window
kkosiorowska Dec 15, 2023
e57f802
Rename from `Staking` to `StakingModal `
kkosiorowska Dec 19, 2023
81d7de5
Update the `Sidebar` component.
kkosiorowska Dec 19, 2023
8c5fd08
Use a `semanticTokens` to declare header height
kkosiorowska Dec 19, 2023
6f6be13
Merge branch 'main' into handling-modal-window
kkosiorowska Dec 20, 2023
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
23 changes: 18 additions & 5 deletions dapp/src/DApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@ import React from "react"
import { Box, ChakraProvider } from "@chakra-ui/react"
import { useDetectThemeMode } from "./hooks"
import theme from "./theme"
import { LedgerWalletAPIProvider, WalletContextProvider } from "./contexts"
import {
DocsDrawerContextProvider,
LedgerWalletAPIProvider,
SidebarContextProvider,
WalletContextProvider,
} from "./contexts"
import Header from "./components/Header"
import Overview from "./components/Overview"
import Sidebar from "./components/Sidebar"
import DocsDrawer from "./components/DocsDrawer"
import GlobalStyles from "./components/GlobalStyles"

function DApp() {
Expand All @@ -16,6 +23,8 @@ function DApp() {
<Box as="main">
<Overview />
</Box>
<Sidebar />
<DocsDrawer />
</>
)
}
Expand All @@ -24,10 +33,14 @@ function DAppProviders() {
return (
<LedgerWalletAPIProvider>
<WalletContextProvider>
<ChakraProvider theme={theme}>
<GlobalStyles />
<DApp />
</ChakraProvider>
<DocsDrawerContextProvider>
<SidebarContextProvider>
<ChakraProvider theme={theme}>
<GlobalStyles />
<DApp />
</ChakraProvider>
</SidebarContextProvider>
</DocsDrawerContextProvider>
</WalletContextProvider>
</LedgerWalletAPIProvider>
)
Expand Down
25 changes: 25 additions & 0 deletions dapp/src/components/DocsDrawer/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from "react"
import {
Drawer,
DrawerBody,
DrawerContent,
DrawerOverlay,
} from "@chakra-ui/react"
import { useDocsDrawer } from "../../hooks"
import { TextMd } from "../shared/Typography"

export default function DocsDrawer() {
const { isOpen, onClose } = useDocsDrawer()

return (
<Drawer size="xl" placement="right" isOpen={isOpen} onClose={onClose}>
<DrawerOverlay mt="header_height" />
<DrawerContent mt="header_height">
<DrawerBody>
{/* TODO: Add a documentation */}
<TextMd>Documentation</TextMd>
</DrawerBody>
</DrawerContent>
</Drawer>
)
}
19 changes: 19 additions & 0 deletions dapp/src/components/Modals/Staking/Overview.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from "react"
import { Button, ModalBody, ModalFooter } from "@chakra-ui/react"
import { ModalStep } from "../../../contexts"
import { TextMd } from "../../shared/Typography"

export default function Overview({ goNext }: ModalStep) {
return (
<>
<ModalBody>
<TextMd>Staking overview</TextMd>
</ModalBody>
<ModalFooter>
<Button width="100%" onClick={goNext}>
Get started
</Button>
</ModalFooter>
</>
)
}
15 changes: 15 additions & 0 deletions dapp/src/components/Modals/Staking/StakeForm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from "react"
import { Button, ModalBody } from "@chakra-ui/react"
import { ModalStep } from "../../../contexts"
import { TextMd } from "../../shared/Typography"

export default function StakeModal({ goNext }: ModalStep) {
return (
<ModalBody>
<TextMd>Stake modal</TextMd>
<Button width="100%" onClick={goNext}>
Stake
</Button>
</ModalBody>
)
}
32 changes: 32 additions & 0 deletions dapp/src/components/Modals/Staking/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React from "react"
import { useModalFlowContext } from "../../../hooks"
import StakeForm from "./StakeForm"
import Overview from "./Overview"
import ModalBase from "../../shared/ModalBase"

function StakingSteps() {
const { activeStep, goNext } = useModalFlowContext()

switch (activeStep) {
case 1:
return <Overview goNext={goNext} />
case 2:
return <StakeForm goNext={goNext} />
default:
return null
}
}

export default function StakingModal({
isOpen,
onClose,
}: {
isOpen: boolean
onClose: () => void
}) {
return (
<ModalBase isOpen={isOpen} onClose={onClose} numberOfSteps={2}>
<StakingSteps />
</ModalBase>
)
}
9 changes: 8 additions & 1 deletion dapp/src/components/Overview/PositionDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ import {
CardFooter,
HStack,
CardProps,
useBoolean,
} from "@chakra-ui/react"
import { Info } from "../../static/icons"
import StakingModal from "../Modals/Staking"
import { CurrencyBalanceWithConversion } from "../shared/CurrencyBalanceWithConversion"
import { TextMd } from "../shared/Typography"

export default function PositionDetails(props: CardProps) {
const [isOpenStakingModal, stakingModal] = useBoolean()
r-czajkowski marked this conversation as resolved.
Show resolved Hide resolved

return (
<Card {...props}>
<CardBody>
Expand All @@ -40,11 +44,14 @@ export default function PositionDetails(props: CardProps) {
</CardBody>
<CardFooter flexDirection="column" gap={2}>
{/* TODO: Handle click actions */}
<Button size="lg">Stake</Button>
<Button size="lg" onClick={stakingModal.on}>
Stake
</Button>
<Button size="lg" variant="outline">
Unstake
</Button>
</CardFooter>
<StakingModal isOpen={isOpenStakingModal} onClose={stakingModal.off} />
</Card>
)
}
4 changes: 4 additions & 0 deletions dapp/src/components/Overview/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import TransactionHistory from "./TransactionHistory"
import { USD } from "../../constants"
import { ArrowUpRight } from "../../static/icons"
import { TextSm } from "../shared/Typography"
import { useDocsDrawer } from "../../hooks"

export default function Overview() {
const { onOpen } = useDocsDrawer()

return (
<Flex direction="column" gap={2} p={6}>
<Flex justifyContent="space-between" alignItems="center">
Expand All @@ -18,6 +21,7 @@ export default function Overview() {
</HStack>
<Button
variant="card"
onClick={onOpen}
leftIcon={<Icon as={ArrowUpRight} color="brand.400" boxSize={4} />}
>
Docs
Expand Down
23 changes: 23 additions & 0 deletions dapp/src/components/Sidebar/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from "react"
import { Box, Button, useMultiStyleConfig } from "@chakra-ui/react"
import { useDocsDrawer, useSidebar } from "../../hooks"

export default function Sidebar() {
const { isOpen } = useSidebar()
const { onOpen: openDocsDrawer } = useDocsDrawer()
const styles = useMultiStyleConfig("Sidebar")

return (
<Box
as="aside"
mt="header_height"
w={isOpen ? 80 : 0}
__css={styles.sidebarContainer}
>
<Box __css={styles.sidebar}>
{/* TODO: Add a correct content for the sidebar */}
<Button onClick={openDocsDrawer}>Open a documentation</Button>
</Box>
</Box>
)
}
78 changes: 78 additions & 0 deletions dapp/src/components/shared/ModalBase/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import React, { useCallback, useEffect, useMemo, useState } from "react"
import {
Modal,
ModalCloseButton,
ModalContent,
ModalOverlay,
} from "@chakra-ui/react"
import { ModalFlowContext, ModalFlowContextValue } from "../../../contexts"
import { useSidebar } from "../../../hooks"

export default function ModalBase({
isOpen,
onClose,
numberOfSteps,
defaultStep = 1,
children,
}: {
isOpen: boolean
onClose: () => void
numberOfSteps: number
defaultStep?: number
children: React.ReactNode
}) {
const { onOpen: openSideBar, onClose: closeSidebar } = useSidebar()

const [activeStep, setActiveStep] = useState(defaultStep)

const handleGoNext = useCallback(() => {
setActiveStep((prevStep) => prevStep + 1)
}, [])

const handleClose = useCallback(() => {
onClose()
}, [onClose])

const resetState = useCallback(() => {
setActiveStep(defaultStep)
}, [defaultStep])

useEffect(() => {
if (activeStep > numberOfSteps) {
handleClose()
}
}, [activeStep, numberOfSteps, handleClose])

useEffect(() => {
let timeout: NodeJS.Timeout

if (isOpen) {
openSideBar()
} else {
closeSidebar()
timeout = setTimeout(resetState, 100)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to reset state in timeout?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After closing the modal, we should reset the active step state. However, if we do this without timeout we will see the default state in the modal for one moment. See the video below.

Screen.Recording.2023-12-19.at.16.25.28.mov

}
return () => clearTimeout(timeout)
}, [isOpen, resetState, openSideBar, closeSidebar])

const contextValue: ModalFlowContextValue = useMemo<ModalFlowContextValue>(
() => ({
activeStep,
onClose: handleClose,
goNext: handleGoNext,
}),
[activeStep, handleGoNext, handleClose],
)

return (
<ModalFlowContext.Provider value={contextValue}>
<Modal size="md" isOpen={isOpen} onClose={handleClose}>
<ModalOverlay mt="header_height" />
<ModalContent mt="modal_shift">
<ModalCloseButton />
{children}
</ModalContent>
</Modal>
</ModalFlowContext.Provider>
)
}
44 changes: 44 additions & 0 deletions dapp/src/contexts/DocsDrawerContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React, { createContext, useCallback, useMemo, useState } from "react"

type DocsDrawerContextValue = {
isOpen: boolean
onOpen: () => void
onClose: () => void
}

export const DocsDrawerContext = createContext<DocsDrawerContextValue>({
isOpen: false,
onOpen: () => {},
onClose: () => {},
})

export function DocsDrawerContextProvider({
children,
}: {
children: React.ReactNode
}): React.ReactElement {
const [isOpen, setIsOpen] = useState(false)

const onOpen = useCallback(() => {
setIsOpen(true)
}, [])

const onClose = useCallback(() => {
setIsOpen(false)
}, [])

const contextValue: DocsDrawerContextValue = useMemo<DocsDrawerContextValue>(
() => ({
isOpen,
onOpen,
onClose,
}),
[isOpen, onClose, onOpen],
)

return (
<DocsDrawerContext.Provider value={contextValue}>
{children}
</DocsDrawerContext.Provider>
)
}
17 changes: 17 additions & 0 deletions dapp/src/contexts/ModalFlowContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { createContext } from "react"

export type ModalStep = {
goNext: () => void
}

export type ModalFlowContextValue = {
activeStep?: number
onClose: () => void
goNext: () => void
}

export const ModalFlowContext = createContext<ModalFlowContextValue>({
activeStep: undefined,
goNext: () => {},
onClose: () => {},
})
Loading
Loading