|
1 | 1 | import Services from '#services' |
2 | 2 | import { Trans } from '@lingui/react/macro' |
3 | 3 | import { Icons } from '@masknet/icons' |
| 4 | +import { PopupRoutes } from '@masknet/shared-base' |
4 | 5 | import { makeStyles } from '@masknet/theme' |
5 | | -import { Typography } from '@mui/material' |
6 | | -import { memo } from 'react' |
| 6 | +import { Button, Dialog, DialogActions, DialogContent, DialogTitle, Typography } from '@mui/material' |
| 7 | +import { memo, useState } from 'react' |
| 8 | +import { useAsyncFn, useAsyncRetry } from 'react-use' |
7 | 9 | import { PrimaryButton } from '../../../components/PrimaryButton/index.js' |
8 | 10 | import { SetupFrameController } from '../../../components/SetupFrame/index.js' |
9 | | -import { useAsyncFn } from 'react-use' |
10 | | -import { PopupRoutes } from '@masknet/shared-base' |
| 11 | +import { XOAuthRequestOrigins } from '../../../../shared/definitions/extension.js' |
11 | 12 |
|
12 | 13 | const useStyles = makeStyles()((theme) => ({ |
13 | 14 | container: { |
@@ -63,20 +64,79 @@ const useStyles = makeStyles()((theme) => ({ |
63 | 64 | listStyle: 'disc', |
64 | 65 | }, |
65 | 66 | }, |
| 67 | + dialog: { |
| 68 | + width: 600, |
| 69 | + boxSizing: 'border-box', |
| 70 | + padding: 24, |
| 71 | + borderRadius: 12, |
| 72 | + display: 'flex', |
| 73 | + flexDirection: 'column', |
| 74 | + gap: 24, |
| 75 | + }, |
| 76 | + dialogTitle: { |
| 77 | + color: theme.palette.maskColor.main, |
| 78 | + fontSize: 18, |
| 79 | + fontWeight: 700, |
| 80 | + lineHeight: '22px', |
| 81 | + padding: 0, |
| 82 | + }, |
| 83 | + dialogContent: { |
| 84 | + display: 'flex', |
| 85 | + flexDirection: 'column', |
| 86 | + gap: 12, |
| 87 | + padding: 0, |
| 88 | + }, |
| 89 | + permissions: { |
| 90 | + backgroundColor: theme.palette.maskColor.bg, |
| 91 | + padding: 12, |
| 92 | + borderRadius: 8, |
| 93 | + height: 212, |
| 94 | + boxSizing: 'border-box', |
| 95 | + minHeight: 0, |
| 96 | + flexGrow: 1, |
| 97 | + overflow: 'auto', |
| 98 | + }, |
| 99 | + dialogActions: { |
| 100 | + display: 'flex', |
| 101 | + justifyContent: 'center', |
| 102 | + gap: 12, |
| 103 | + padding: 0, |
| 104 | + }, |
| 105 | + actionButton: { |
| 106 | + minWidth: 110, |
| 107 | + '&&': { |
| 108 | + marginLeft: 0, |
| 109 | + }, |
| 110 | + }, |
66 | 111 | })) |
67 | 112 |
|
68 | 113 | export const Component = memo(function CreateWalletForm() { |
69 | 114 | const { classes, cx } = useStyles() |
| 115 | + const [open, setOpen] = useState(false) |
| 116 | + |
| 117 | + const { retry, value: hasPermission } = useAsyncRetry(() => { |
| 118 | + const hasPermission = browser.permissions.contains({ |
| 119 | + origins: XOAuthRequestOrigins, |
| 120 | + }) |
| 121 | + setOpen(!hasPermission) |
| 122 | + return hasPermission |
| 123 | + }, []) |
| 124 | + |
70 | 125 | const [{ loading }, request] = useAsyncFn(async () => { |
| 126 | + if (!hasPermission) { |
| 127 | + setOpen(true) |
| 128 | + return |
| 129 | + } |
71 | 130 | try { |
72 | 131 | const data = await Services.Helper.loginFireflyViaTwitter() |
73 | | - console.log('login data', data) |
74 | 132 | if (!data) return |
75 | | - await Services.Helper.openPopupWindow(PopupRoutes.CreateWallet, undefined) |
| 133 | + await Services.Helper.openPopupWindow(PopupRoutes.CreateWallet, { |
| 134 | + creatingFireflyWallet: true, |
| 135 | + }) |
76 | 136 | } catch (err) { |
77 | | - console.log('login error', err) |
| 137 | + console.error('Failed to login firefly', err) |
78 | 138 | } |
79 | | - }, []) |
| 139 | + }, [hasPermission]) |
80 | 140 |
|
81 | 141 | return ( |
82 | 142 | <div className={classes.container}> |
@@ -121,6 +181,41 @@ export const Component = memo(function CreateWalletForm() { |
121 | 181 | <Trans>Continue</Trans> |
122 | 182 | </PrimaryButton> |
123 | 183 | </SetupFrameController> |
| 184 | + <Dialog |
| 185 | + open={open} |
| 186 | + PaperProps={{ |
| 187 | + elevation: 0, |
| 188 | + className: classes.dialog, |
| 189 | + }}> |
| 190 | + <DialogTitle className={classes.dialogTitle}> |
| 191 | + <Trans>Mask needs the following permissions</Trans> |
| 192 | + </DialogTitle> |
| 193 | + <DialogContent className={classes.dialogContent}> |
| 194 | + <Typography> |
| 195 | + <Trans>Sites</Trans> |
| 196 | + </Typography> |
| 197 | + <div className={classes.permissions} data-hide-scrollbar> |
| 198 | + {XOAuthRequestOrigins.map((origin) => ( |
| 199 | + <Typography key={origin} lineHeight="18px"> |
| 200 | + {origin} |
| 201 | + </Typography> |
| 202 | + ))} |
| 203 | + </div> |
| 204 | + </DialogContent> |
| 205 | + <DialogActions className={classes.dialogActions}> |
| 206 | + <Button onClick={() => setOpen(false)} variant="outlined" className={classes.actionButton}> |
| 207 | + <Trans>Cancel</Trans> |
| 208 | + </Button> |
| 209 | + <Button |
| 210 | + onClick={() => { |
| 211 | + browser.permissions.request({ origins: XOAuthRequestOrigins }).finally(retry) |
| 212 | + }} |
| 213 | + variant="contained" |
| 214 | + className={classes.actionButton}> |
| 215 | + <Trans>Approve</Trans> |
| 216 | + </Button> |
| 217 | + </DialogActions> |
| 218 | + </Dialog> |
124 | 219 | </div> |
125 | 220 | ) |
126 | 221 | }) |
0 commit comments