|
| 1 | +import React from 'react'; |
| 2 | +import { useEffect, useState, ReactNode } from 'react'; |
| 3 | +import { styled, useTheme } from '@mui/material/styles'; |
| 4 | +import Box from '@mui/material/Box'; |
| 5 | +import Drawer from '@mui/material/Drawer'; |
| 6 | +import CssBaseline from '@mui/material/CssBaseline'; |
| 7 | +import MuiAppBar from '@mui/material/AppBar'; |
| 8 | +import Toolbar from '@mui/material/Toolbar'; |
| 9 | +import List from '@mui/material/List'; |
| 10 | +import Divider from '@mui/material/Divider'; |
| 11 | +import IconButton from '@mui/material/IconButton'; |
| 12 | +import MenuIcon from '@mui/icons-material/Menu'; |
| 13 | +import ChevronLeftIcon from '@mui/icons-material/ChevronLeft'; |
| 14 | +import ChevronRightIcon from '@mui/icons-material/ChevronRight'; |
| 15 | +import ListItem from '@mui/material/ListItem'; |
| 16 | +import ListItemButton from '@mui/material/ListItemButton'; |
| 17 | +import ListItemText from '@mui/material/ListItemText'; |
| 18 | +import { useRouter } from 'next/router'; |
| 19 | +import { Button, Container, InputAdornment, TextField } from '@mui/material'; |
| 20 | +import { usePortal } from '@/providers/portal'; |
| 21 | +import { ContentCopy, Pending, Send } from '@mui/icons-material'; |
| 22 | +import { useSnackbar } from '@/providers/snackbar'; |
| 23 | + |
| 24 | +const DRAWER_WIDTH = 240; |
| 25 | +const DRAWER_ITEMS = [ |
| 26 | + { |
| 27 | + name: 'Dashboard', |
| 28 | + link: '/', |
| 29 | + }, |
| 30 | + { |
| 31 | + name: 'Send Tokens', |
| 32 | + link: '/send', |
| 33 | + }, |
| 34 | +]; |
| 35 | + |
| 36 | +const DrawerHeader = styled('div')(({ theme }) => ({ |
| 37 | + display: 'flex', |
| 38 | + alignItems: 'center', |
| 39 | + padding: theme.spacing(0, 1), |
| 40 | + ...theme.mixins.toolbar, |
| 41 | + justifyContent: 'flex-end', |
| 42 | +})); |
| 43 | + |
| 44 | +export default function Layout({ children }: { children: ReactNode }) { |
| 45 | + const theme = useTheme(); |
| 46 | + const router = useRouter(); |
| 47 | + const snackbar = useSnackbar(); |
| 48 | + |
| 49 | + const portal = usePortal(); |
| 50 | + const [solanaAddress, setSolanaAddress] = useState(''); |
| 51 | + const [generatingSolanaAddress, setGeneratingSolanaAddress] = useState(false); |
| 52 | + |
| 53 | + const [open, setOpen] = useState(false); |
| 54 | + |
| 55 | + const handleDrawerOpen = () => { |
| 56 | + setOpen(true); |
| 57 | + }; |
| 58 | + |
| 59 | + const handleDrawerClose = () => { |
| 60 | + setOpen(false); |
| 61 | + }; |
| 62 | + |
| 63 | + const generateSolanaAddress = async () => { |
| 64 | + try { |
| 65 | + setGeneratingSolanaAddress(true); |
| 66 | + const address = await portal.getSolanaAddress(); |
| 67 | + setSolanaAddress(address); |
| 68 | + |
| 69 | + snackbar.setSnackbarOpen(true); |
| 70 | + snackbar.setSnackbarContent({ |
| 71 | + severity: 'success', |
| 72 | + message: `Successfully generated Solana address`, |
| 73 | + }); |
| 74 | + } catch (e) { |
| 75 | + snackbar.setSnackbarOpen(true); |
| 76 | + snackbar.setSnackbarContent({ |
| 77 | + severity: 'error', |
| 78 | + message: `Something went wrong - ${e}`, |
| 79 | + }); |
| 80 | + } finally { |
| 81 | + setGeneratingSolanaAddress(false); |
| 82 | + } |
| 83 | + }; |
| 84 | + |
| 85 | + useEffect(() => { |
| 86 | + if (portal.ready && !generatingSolanaAddress) { |
| 87 | + (async () => { |
| 88 | + await generateSolanaAddress(); |
| 89 | + })(); |
| 90 | + } |
| 91 | + }, [portal.ready, generatingSolanaAddress]); |
| 92 | + |
| 93 | + return ( |
| 94 | + <Box sx={{ display: 'flex' }}> |
| 95 | + <Container maxWidth="xl"> |
| 96 | + <CssBaseline /> |
| 97 | + <MuiAppBar position="fixed"> |
| 98 | + <Container maxWidth="xl"> |
| 99 | + <Toolbar> |
| 100 | + <IconButton |
| 101 | + color="inherit" |
| 102 | + aria-label="open drawer" |
| 103 | + onClick={handleDrawerOpen} |
| 104 | + edge="start" |
| 105 | + sx={{ mr: 2, ...(open && { display: 'none' }) }} |
| 106 | + > |
| 107 | + <MenuIcon /> |
| 108 | + </IconButton> |
| 109 | + <Box sx={{ flexGrow: 1 }}></Box> |
| 110 | + <Box> |
| 111 | + {solanaAddress ? ( |
| 112 | + <TextField |
| 113 | + size="small" |
| 114 | + label="Solana Address" |
| 115 | + value={solanaAddress} |
| 116 | + spellCheck={false} |
| 117 | + InputProps={{ |
| 118 | + sx: { |
| 119 | + color: 'white', |
| 120 | + }, |
| 121 | + disableUnderline: true, |
| 122 | + endAdornment: ( |
| 123 | + <InputAdornment position="end"> |
| 124 | + <IconButton |
| 125 | + size="small" |
| 126 | + onClick={() => |
| 127 | + navigator.clipboard.writeText(solanaAddress) |
| 128 | + } |
| 129 | + edge="end" |
| 130 | + > |
| 131 | + <ContentCopy fontSize="small" /> |
| 132 | + </IconButton> |
| 133 | + </InputAdornment> |
| 134 | + ), |
| 135 | + }} |
| 136 | + /> |
| 137 | + ) : ( |
| 138 | + <Button |
| 139 | + color="inherit" |
| 140 | + variant="outlined" |
| 141 | + onClick={async () => await generateSolanaAddress()} |
| 142 | + endIcon={generatingSolanaAddress ? <Pending /> : <Send />} |
| 143 | + > |
| 144 | + Get Solana Wallet |
| 145 | + </Button> |
| 146 | + )} |
| 147 | + </Box> |
| 148 | + </Toolbar> |
| 149 | + </Container> |
| 150 | + </MuiAppBar> |
| 151 | + <Drawer |
| 152 | + sx={{ |
| 153 | + width: DRAWER_WIDTH, |
| 154 | + flexShrink: 0, |
| 155 | + '& .MuiDrawer-paper': { |
| 156 | + width: DRAWER_WIDTH, |
| 157 | + boxSizing: 'border-box', |
| 158 | + }, |
| 159 | + }} |
| 160 | + variant="persistent" |
| 161 | + anchor="left" |
| 162 | + open={open} |
| 163 | + > |
| 164 | + <DrawerHeader> |
| 165 | + <IconButton onClick={handleDrawerClose}> |
| 166 | + {theme.direction === 'ltr' ? ( |
| 167 | + <ChevronLeftIcon /> |
| 168 | + ) : ( |
| 169 | + <ChevronRightIcon /> |
| 170 | + )} |
| 171 | + </IconButton> |
| 172 | + </DrawerHeader> |
| 173 | + <Divider /> |
| 174 | + <List> |
| 175 | + {DRAWER_ITEMS.map((item, index) => ( |
| 176 | + <ListItem key={index} disablePadding> |
| 177 | + <ListItemButton onClick={() => router.push(item.link)}> |
| 178 | + <ListItemText primary={item.name} /> |
| 179 | + </ListItemButton> |
| 180 | + </ListItem> |
| 181 | + ))} |
| 182 | + </List> |
| 183 | + </Drawer> |
| 184 | + <Container maxWidth="xl"> |
| 185 | + <DrawerHeader /> |
| 186 | + {children} |
| 187 | + </Container> |
| 188 | + </Container> |
| 189 | + </Box> |
| 190 | + ); |
| 191 | +} |
0 commit comments