Skip to content

Commit 94dd438

Browse files
committed
refactor: organize routes
1 parent 05b6989 commit 94dd438

File tree

29 files changed

+458
-217
lines changed

29 files changed

+458
-217
lines changed

packages/mask/.webpack/manifest/manifest-mv3.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "Mask Network",
33
"version": "0",
44
"manifest_version": 3,
5-
"permissions": ["storage", "webNavigation", "activeTab", "scripting"],
5+
"permissions": ["storage", "webNavigation", "activeTab", "scripting", "identity", "tabs"],
66
"optional_permissions": ["notifications", "clipboardRead"],
77
"optional_host_permissions": ["<all_urls>"],
88
"background": { "service_worker": "/manifest-v3.entry.js" },

packages/mask/.webpack/manifest/manifest.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "Mask Network",
33
"version": "0",
44
"manifest_version": 2,
5-
"permissions": ["storage", "webNavigation", "activeTab"],
5+
"permissions": ["storage", "webNavigation", "activeTab", "identity"],
66
"optional_permissions": ["<all_urls>", "notifications", "clipboardRead", "tabs"],
77
"background": { "page": "background.html" },
88
"icons": {
@@ -14,5 +14,9 @@
1414
"browser_action": { "default_popup": "popups.html" },
1515
"homepage_url": "https://mask.io",
1616
"description": "The portal to the new & open Internet. Send encrypted message and decentralized Apps right on top of social networks.",
17-
"web_accessible_resources": ["js/*", "bundled/*", "entry/*", "*.svg", "*.png", "*.jpg", "*.css", "build-info.json"]
17+
"web_accessible_resources": ["js/*", "bundled/*", "entry/*", "*.svg", "*.png", "*.jpg", "*.css", "build-info.json"],
18+
"oauth2": {
19+
"client_id": "884348965548-npd1mffii0tbrr87eb2fm73ue022k3t3.apps.googleusercontent.com",
20+
"scopes": ["https://www.googleapis.com/auth/drive.file", "https://www.googleapis.com/auth/userinfo.email"]
21+
}
1822
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// cspell:disable
2+
const clientId =
3+
process.env.GOOGLE_CLIENT_ID || '18954568633-c7has4fcrm5b7fop5si83fleb51oodji.apps.googleusercontent.com'
4+
// cspell:enable
5+
const SCOPES = ['https://www.googleapis.com/auth/drive.file', 'https://www.googleapis.com/auth/userinfo.email']
6+
const redirectUri = browser.runtime.getURL('oauth2.html')
7+
8+
export async function getAccessToken() {
9+
const url = browser.identity.getRedirectURL()
10+
const token = await browser.identity.launchWebAuthFlow({
11+
url: `https://accounts.google.com/o/oauth2/v2/auth?client_id=${clientId}&redirect_uri=${encodeURIComponent(url)}&response_type=code&scope=${encodeURIComponent(SCOPES.join(' '))}&access_type=offline`,
12+
interactive: true,
13+
})
14+
return token
15+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export { generateBackupPreviewInfo, createBackupFile, type BackupOptions } from './create.js'
22
export { generateBackupSummary, restoreBackup } from './restore.js'
33
export { backupPersonaPrivateKey } from './persona.js'
4+
export { getAccessToken } from './google_drive.js'

packages/mask/background/tasks/NotCancellable/OnInstall.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ browser.runtime.onInstalled.addListener(async (detail) => {
3939
localBackupAt: backupMethod && backupMethod === 'local' ? localStorage.getItem('backupAt') : null,
4040
cloudBackupMethod: null,
4141
googleToken: null,
42+
googleAccount: null,
4243
})
4344
}
4445
// remove old data after migrate

packages/mask/dashboard/contexts/CloudBackupFormContext.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,5 @@ function useCloudBackupFormContext() {
7070
}
7171
}
7272

73+
/** @deprecated */
7374
export const CloudBackupFormContext = createContainer(useCloudBackupFormContext)

packages/mask/dashboard/hooks/useBackupFormState.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ export function useBackupFormState() {
1919
const { user } = UserContext.useContainer()
2020
const [backupWallets, setBackupWallets] = useState(false)
2121

22+
console.log('user', user)
23+
2224
const formState = useForm<BackupFormInputs>({
2325
mode: 'onBlur',
2426
context: {

packages/mask/dashboard/pages/SetupPersona/Backup/CloudBackup/useCloudBackupForm.ts renamed to packages/mask/dashboard/pages/SetupPersona/Backup/CloudBackup/CloudBackupFormContext.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { zodResolver } from '@hookform/resolvers/zod'
2+
import { createContainer } from '@masknet/shared-base-ui'
23
import { useLingui } from '@lingui/react/macro'
34
import { BackupAccountType } from '@masknet/shared-base'
45
import guessCallingCode from 'guess-calling-code'
56
import { useForm } from 'react-hook-form'
67
import { z } from 'zod'
7-
import { emailRegexp, phoneRegexp } from '../../../../utils/regexp.js'
8+
import { phoneRegexp } from '../../../../utils/regexp.js'
9+
import { useState } from 'react'
810

911
export interface CloudBackupFormInputs {
1012
email: string
@@ -13,10 +15,13 @@ export interface CloudBackupFormInputs {
1315
countryCode: string
1416
}
1517

16-
export function useCloudBackupForm(backupType: BackupAccountType) {
18+
export function useCloudBackupForm() {
1719
const { t } = useLingui()
1820

19-
const formState = useForm<CloudBackupFormInputs>({
21+
const [backupType, setBackupType] = useState<BackupAccountType>(BackupAccountType.Email)
22+
const isEmail = backupType === BackupAccountType.Email
23+
24+
const form = useForm<CloudBackupFormInputs>({
2025
mode: 'onSubmit',
2126
context: {
2227
backupType,
@@ -30,21 +35,17 @@ export function useCloudBackupForm(backupType: BackupAccountType) {
3035
resolver: zodResolver(
3136
z
3237
.object({
33-
email:
34-
backupType === BackupAccountType.Email ?
35-
z.string().regex(emailRegexp, t`Invalid email address format.`)
36-
: z.string().optional(),
37-
countryCode: backupType === BackupAccountType.Phone ? z.string() : z.string().optional(),
38-
phone:
39-
backupType === BackupAccountType.Phone ? z.string().regex(phoneRegexp) : z.string().optional(),
38+
email: isEmail ? z.string().email(t`Invalid email address format.`) : z.string().optional(),
39+
countryCode: isEmail ? z.string().optional() : z.string(),
40+
phone: isEmail ? z.string().optional() : z.string().regex(phoneRegexp),
4041
code: z
4142
.string()
4243
.min(1, t`The code is incorrect.`)
4344
.max(6, t`The code is incorrect.`),
4445
})
4546
.refine(
4647
(data) => {
47-
if (backupType !== BackupAccountType.Phone) return true
48+
if (isEmail) return true
4849
if (!data.countryCode || !data.phone) return false
4950
return phoneRegexp.test(`+${data.countryCode} ${data.phone}`)
5051
},
@@ -57,6 +58,10 @@ export function useCloudBackupForm(backupType: BackupAccountType) {
5758
})
5859

5960
return {
60-
formState,
61+
form,
62+
backupType,
63+
setBackupType,
6164
}
6265
}
66+
67+
export const CloudBackupFormContext = createContainer(useCloudBackupForm)

packages/mask/dashboard/pages/SetupPersona/Backup/CloudBackup/EmailForm.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import { Box, TextField } from '@mui/material'
55
import { memo, useCallback } from 'react'
66
import { Controller } from 'react-hook-form'
77
import { UserContext, useLanguage } from '../../../../../shared-ui/index.js'
8-
import { CloudBackupFormContext } from '../../../../contexts/CloudBackupFormContext.js'
98
import { sendCode } from '../../../../utils/api.js'
109
import { Locale, Scenario } from '../../../../utils/type.js'
10+
import { CloudBackupFormContext } from './CloudBackupFormContext.js'
1111

1212
const useStyles = makeStyles()((theme) => ({
1313
send: {
@@ -23,7 +23,7 @@ export const EmailForm = memo(function EmailForm() {
2323
const { user } = UserContext.useContainer()
2424
const { showSnackbar } = useCustomSnackbar()
2525
const {
26-
formState: {
26+
form: {
2727
clearErrors,
2828
control,
2929
watch,

packages/mask/dashboard/pages/SetupPersona/Backup/CloudBackup/GoogleDriveBackup.tsx

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { memo, useEffect } from 'react'
55
import { UserContext } from '../../../../../shared-ui/index.js'
66
import { requestGoogleDriveAccessToken } from '../helpers.js'
77
import { t } from '@lingui/core/macro'
8+
import Services from '#services'
89

910
const useStyles = makeStyles()((theme) => ({
1011
container: {
@@ -30,15 +31,15 @@ interface Props extends BoxProps {}
3031
const clientId =
3132
process.env.GOOGLE_CLIENT_ID || '18954568633-c7has4fcrm5b7fop5si83fleb51oodji.apps.googleusercontent.com'
3233
// cspell:enable
33-
const SCOPE = 'https://www.googleapis.com/auth/drive.file'
34+
const SCOPES = ['https://www.googleapis.com/auth/drive.file', 'https://www.googleapis.com/auth/userinfo.email']
3435
const redirectUri = browser.runtime.getURL('oauth2.html')
3536

3637
const handleSignIn = () => {
37-
const authUrl = `https://accounts.google.com/o/oauth2/v2/auth?client_id=${clientId}&redirect_uri=${encodeURIComponent(redirectUri)}&response_type=code&scope=${encodeURIComponent(SCOPE)}&access_type=offline`
38+
const authUrl = `https://accounts.google.com/o/oauth2/v2/auth?client_id=${clientId}&redirect_uri=${encodeURIComponent(redirectUri)}&response_type=code&scope=${encodeURIComponent(SCOPES.join(' '))}&access_type=offline`
3839
window.location.assign(authUrl)
3940
}
40-
export const GoogleDriveBackup = memo<Props>(function GoogleDriveBackup(props) {
41-
const { classes, cx } = useStyles()
41+
export const Component = memo<Props>(function GoogleDriveBackup() {
42+
const { classes } = useStyles()
4243
const { user, updateUser } = UserContext.useContainer()
4344
const { showSnackbar } = useCustomSnackbar()
4445

@@ -54,7 +55,6 @@ export const GoogleDriveBackup = memo<Props>(function GoogleDriveBackup(props) {
5455
redirectUri,
5556
})
5657
if (res.access_token) {
57-
debugger
5858
// TODO account
5959
updateUser((user) => ({
6060
...user,
@@ -70,9 +70,22 @@ export const GoogleDriveBackup = memo<Props>(function GoogleDriveBackup(props) {
7070
return () => controller.abort()
7171
}, [])
7272

73+
const login = async () => {
74+
// const token = await browser.identity.launchWebAuthFlow({
75+
// url: `https://accounts.google.com/o/oauth2/v2/auth?client_id=${clientId}&redirect_uri=${encodeURIComponent(redirectUri)}&response_type=code&scope=${encodeURIComponent(SCOPES.join(' '))}&access_type=offline`,
76+
// interactive: true,
77+
// })
78+
const token = await Services.Backup.getAccessToken()
79+
if (!token) return
80+
updateUser((user) => ({
81+
...user,
82+
googleAccessToken: token,
83+
}))
84+
}
85+
7386
if (!user.googleAccount) {
7487
return (
75-
<Box {...props} className={cx(classes.container, props.className)}>
88+
<Box className={classes.container}>
7689
<Typography className={classes.title}>
7790
<Trans>Add google Drive</Trans>
7891
</Typography>
@@ -82,15 +95,15 @@ export const GoogleDriveBackup = memo<Props>(function GoogleDriveBackup(props) {
8295
</Trans>
8396
</Typography>
8497
<Box display="flex" justifyContent="center" mt="48px">
85-
<Button variant="contained" onClick={handleSignIn}>
98+
<Button variant="contained" onClick={login}>
8699
Add Google Drive
87100
</Button>
88101
</Box>
89102
</Box>
90103
)
91104
}
92105
return (
93-
<Box {...props}>
106+
<Box>
94107
<Box>
95108
<Typography>test@gmail.com</Typography>
96109
<Button variant="text">Switch other accounts</Button>

0 commit comments

Comments
 (0)