Skip to content

Commit 60190e4

Browse files
authored
fix: backup/recover related issues. (#12187)
* fix: mf-6675 adjust background color of file table * fix: mf-6677 back up => backup * fix: mf-6678 backup dialog * refactor: replace merge modal with restore modal * fix: mf-6680 handle failed restore backup * fix: mf-6681 navigate to onboarding page after recovery * fix: mf-6674 mf-6679 adjust login and logout * fix: mf-6682 format datetime * fixup! fix: mf-6674 mf-6679 adjust login and logout * fix: mf-6683 scrollbar in collectible card * fix: mf-6676 actively request drive permission
1 parent 226dfc7 commit 60190e4

File tree

30 files changed

+248
-367
lines changed

30 files changed

+248
-367
lines changed

packages/mask/background/services/backup/google_drive.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,11 @@
1-
import { checkAndRequestPermission } from '../../../shared/helpers/index.js'
1+
import { checkAndRequestPermission, requestDriveAccessToken } from '../../../shared/helpers/index.js'
22

33
/* eslint-disable @typescript-eslint/ban-ts-comment */
44
export async function getAccessToken(interactive = false) {
55
const granted = await checkAndRequestPermission()
66
if (!granted) return
77

8-
return new Promise<string>((resolve, reject) => {
9-
// @ts-expect-error
10-
chrome.identity.getAuthToken({ interactive }, (token, error) => {
11-
// @ts-expect-error
12-
if (chrome.runtime.lastError) {
13-
// @ts-expect-error
14-
reject(new Error(chrome.runtime.lastError.message))
15-
return
16-
}
17-
resolve(token)
18-
})
19-
})
8+
return requestDriveAccessToken(interactive)
209
}
2110

2211
export async function clearAccessToken() {

packages/mask/background/services/backup/restore.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getBackupSummary, normalizeBackup } from '@masknet/backup-format'
1+
import { getBackupSummary, normalizeBackup, type BackupSummary } from '@masknet/backup-format'
22
import { restoreNormalizedBackup } from './internal_restore.js'
33
import { Result } from 'ts-results-es'
44
import { SmartPayBundler, SmartPayOwner } from '@masknet/web3-providers'
@@ -7,7 +7,7 @@ import { bytesToHex, privateToPublic, publicToAddress } from '@ethereumjs/util'
77
import { fromBase64URL } from '@masknet/shared-base'
88

99
export async function generateBackupSummary(raw: string) {
10-
return Result.wrapAsync(async () => {
10+
return Result.wrapAsync(async (): Promise<BackupSummary> => {
1111
const backupObj: unknown = JSON.parse(raw)
1212
const backup = await normalizeBackup(backupObj)
1313

packages/mask/dashboard/components/GoogleDriveFileTable.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const useStyles = makeStyles()((theme) => ({
2626
border: `1px solid ${theme.palette.maskColor.line}`,
2727
borderRadius: 8,
2828
overflow: 'hidden',
29+
backgroundColor: theme.palette.maskColor.bottom,
2930
},
3031
table: {
3132
borderRadius: 8,

packages/mask/dashboard/components/GoogleDriveLogin.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Box, Typography } from '@mui/material'
66
import { memo, useMemo } from 'react'
77
import { useAsyncFn } from 'react-use'
88
import { UserContext } from '../../shared-ui/index.js'
9-
import { checkAndRequestPermission } from '../../shared/helpers/index.js'
9+
import { checkAndRequestPermission, requestDriveAccessToken } from '../../shared/helpers/index.js'
1010
import { clearGoogleDriveAccessToken, getGoogleDriveAccessToken } from '../utils/api.js'
1111

1212
const useStyles = makeStyles()((theme) => ({
@@ -43,11 +43,15 @@ export const GoogleDriveLogin = memo(function GoogleDriveLogin() {
4343
if (!granted) return
4444

4545
const userInfo = await googleDriveClient.login(true)
46+
await requestDriveAccessToken(true) // request permission to manipulate files
4647
updateUser({
4748
googleAccount: userInfo.email || '',
4849
})
4950
} catch (err) {
50-
showSnackbar(t`Failed to login: ${(err as Error).message}`, { variant: 'error' })
51+
showSnackbar(t`Authorization Failed`, {
52+
variant: 'warning',
53+
message: t`Failed to authorize Google Drive. Please try again.`,
54+
})
5155
}
5256
}, [googleDriveClient, updateUser, showSnackbar])
5357
return (

packages/mask/dashboard/modals/BackupPreviewModal/BackupPreviewDialog.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ export interface BackupPreviewDialogProps {
6363
type: BackupAccountType
6464
account: string
6565
abstract?: string
66-
onClose: () => void
66+
title?: React.ReactNode | string
6767
uploadButtonLabel?: React.ReactNode | string
68+
onClose: () => void
6869
onUpload?: (content: ArrayBuffer, signal: AbortSignal) => Promise<void>
6970
}
7071
export const BackupPreviewDialog = memo<BackupPreviewDialogProps>(function BackupPreviewDialog({
@@ -74,8 +75,9 @@ export const BackupPreviewDialog = memo<BackupPreviewDialogProps>(function Backu
7475
type,
7576
account,
7677
abstract,
77-
onClose,
78+
title,
7879
uploadButtonLabel,
80+
onClose,
7981
onUpload,
8082
}) {
8183
const { _ } = useLingui()
@@ -259,7 +261,7 @@ export const BackupPreviewDialog = memo<BackupPreviewDialogProps>(function Backu
259261
])
260262

261263
return (
262-
<InjectedDialog title={<Trans>Upload backup</Trans>} open={open} onClose={handleClose}>
264+
<InjectedDialog title={title ?? <Trans>Upload backup</Trans>} open={open} onClose={handleClose}>
263265
<DialogContent data-hide-scrollbar>{content}</DialogContent>
264266
<DialogActions>{action}</DialogActions>
265267
</InjectedDialog>

packages/mask/dashboard/modals/MergeBackupModal/MergeBackupDialog.tsx

Lines changed: 0 additions & 198 deletions
This file was deleted.

packages/mask/dashboard/modals/MergeBackupModal/index.tsx

Lines changed: 0 additions & 21 deletions
This file was deleted.

packages/mask/dashboard/modals/RestoreBackupModal/RestoreBackupDialog.tsx

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import Services from '#services'
22
import { Trans, useLingui } from '@lingui/react/macro'
3-
import { decryptBackup } from '@masknet/backup-format'
3+
import { decryptBackup, type BackupSummary } from '@masknet/backup-format'
44
import { Icons } from '@masknet/icons'
55
import { formatFileSize, InjectedDialog } from '@masknet/shared'
66
import { ActionButton, makeStyles, useCustomSnackbar } from '@masknet/theme'
77
import { decode, encode } from '@msgpack/msgpack'
88
import { Box, DialogActions, DialogContent, LinearProgress, Typography } from '@mui/material'
9-
import { format as formatDateTime, fromUnixTime } from 'date-fns'
9+
import { format as formatDateTime } from 'date-fns'
1010
import { memo, useCallback, useState, type ReactNode } from 'react'
1111
import { useAsync, useAsyncFn } from 'react-use'
1212
import PasswordField from '../../components/PasswordField/index.js'
@@ -54,7 +54,7 @@ export interface RestoreBackupDialogProps {
5454
* and return the content of the downloaded file at the end
5555
*/
5656
download: () => AsyncGenerator<number, ArrayBuffer | undefined>
57-
onClose: () => void
57+
onClose: (summary?: BackupSummary) => void
5858
}
5959

6060
export const RestoreBackupDialog = memo<RestoreBackupDialogProps>(function RestoreBackupDialog({
@@ -124,7 +124,7 @@ export const RestoreBackupDialog = memo<RestoreBackupDialogProps>(function Resto
124124
<Trans>Your file has been successfully restore into the browser data.</Trans>
125125
: <Trans>Your file has been successfully merged into the browser data.</Trans>),
126126
})
127-
onClose()
127+
onClose(backupSummary)
128128
} catch (err) {
129129
if (isImport) {
130130
showSnackbar(<Trans>Restore Failed</Trans>, {
@@ -133,10 +133,11 @@ export const RestoreBackupDialog = memo<RestoreBackupDialogProps>(function Resto
133133
<Trans>Failed to restore the backup: {(err as Error).message}</Trans>
134134
),
135135
})
136+
} else {
137+
showSnackbar(<Trans>Failed to download and merge the backup: {(err as Error).message}</Trans>, {
138+
variant: 'error',
139+
})
136140
}
137-
showSnackbar(<Trans>Failed to download and merge the backup: {(err as Error).message}</Trans>, {
138-
variant: 'error',
139-
})
140141
}
141142
}, [
142143
encrypted,
@@ -158,7 +159,7 @@ export const RestoreBackupDialog = memo<RestoreBackupDialogProps>(function Resto
158159
: <Trans>Merge data to local database</Trans>
159160
}
160161
open={open}
161-
onClose={onClose}>
162+
onClose={() => onClose()}>
162163
<DialogContent>
163164
<Typography className={classes.account}>{account}</Typography>
164165
<Box className={classes.box}>
@@ -189,7 +190,7 @@ export const RestoreBackupDialog = memo<RestoreBackupDialogProps>(function Resto
189190
fontSize={12}
190191
lineHeight="16px"
191192
color={theme.palette.maskColor.third}>
192-
{formatDateTime(fromUnixTime(Number(uploadedAt)), 'yyyy-MM-dd HH:mm')}
193+
{formatDateTime(new Date(Number(uploadedAt)), 'yyyy-mm-dd HH:mm')}
193194
</Typography>
194195
</>
195196
}

0 commit comments

Comments
 (0)