Skip to content

Commit 8c28c49

Browse files
committed
frontend: convert manage-backups switch to functional component
This is a container for the manage-backup route and a switch to render backup component for BB01/BB02. This is not used for setup restore-from-backup, but only on the route when the user accesses manage backups from settings. The class methods were mostly used to render different parts of the component. Moved this to separate components in the same file. Also changed to named export instead of default export.
1 parent 365dfd4 commit 8c28c49

File tree

4 files changed

+152
-131
lines changed

4 files changed

+152
-131
lines changed

frontends/web/src/routes/device/bitbox02/sdcardcheck.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,17 @@
1515
*/
1616

1717
import { ReactNode, useCallback, useEffect, useState } from 'react';
18-
import { TranslateProps } from '../../../decorators/translate';
18+
import { useTranslation } from 'react-i18next';
19+
import { checkSDCard } from '../../../api/bitbox02';
1920
import { Dialog, DialogButtons } from '../../../components/dialog/dialog';
2021
import { Button, ButtonLink } from '../../../components/forms';
21-
import { checkSDCard } from '../../../api/bitbox02';
22-
import { useTranslation } from 'react-i18next';
2322
import { HorizontallyCenteredSpinner } from '../../../components/spinner/SpinnerAnimation';
2423

25-
type SDCardCheckProps = {
24+
type TProps = {
2625
deviceID: string;
2726
children: ReactNode;
2827
}
2928

30-
type TProps = SDCardCheckProps & TranslateProps;
31-
3229
const SDCardCheck = ({ deviceID, children }: TProps) => {
3330
const { t } = useTranslation();
3431
const [sdCardInserted, setSdCardInserted] = useState<boolean | undefined>();

frontends/web/src/routes/device/manage-backups/manage-backups.jsx

Lines changed: 0 additions & 124 deletions
This file was deleted.
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/**
2+
* Copyright 2018 Shift Devices AG
3+
* Copyright 2024 Shift Crypto AG
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import { useTranslation } from 'react-i18next';
19+
import { useNavigate } from 'react-router-dom';
20+
import { TDevices } from '../../../api/devices';
21+
import { ButtonLink } from '../../../components/forms';
22+
import { Guide } from '../../../components/guide/guide';
23+
import { Entry } from '../../../components/guide/entry';
24+
import { Header } from '../../../components/layout';
25+
import { Backups } from '../bitbox01/backups';
26+
import { BackupsV2 } from '../bitbox02/backups';
27+
import { SDCardCheck } from '../bitbox02/sdcardcheck';
28+
29+
type TProps = {
30+
deviceID: string | null;
31+
devices: TDevices
32+
}
33+
34+
export const ManageBackups = ({
35+
deviceID,
36+
devices,
37+
}: TProps) => {
38+
const { t } = useTranslation();
39+
const navigate = useNavigate();
40+
41+
if (!deviceID || !devices[deviceID]) {
42+
navigate('/');
43+
return null;
44+
}
45+
46+
return (
47+
<div className="contentWithGuide">
48+
<div className="container">
49+
<div className="innerContainer scrollableContainer">
50+
<Header
51+
title={<h2>{t('backup.title')}</h2>}
52+
/>
53+
<div className="content padded">
54+
<h3 className="subTitle">{t('backup.list')}</h3>
55+
<BackupsList
56+
deviceID={deviceID}
57+
devices={devices}
58+
/>
59+
</div>
60+
</div>
61+
</div>
62+
<ManageBackupGuide
63+
deviceID={deviceID}
64+
devices={devices}
65+
/>
66+
</div>
67+
);
68+
};
69+
70+
const BackupsList = ({
71+
deviceID,
72+
devices,
73+
}: TProps) => {
74+
if (!deviceID) {
75+
return null;
76+
}
77+
switch (devices[deviceID]) {
78+
case 'bitbox':
79+
return (
80+
<Backups
81+
deviceID={deviceID}
82+
showCreate={true}
83+
showRestore={false}>
84+
<BackButton deviceID={deviceID} />
85+
</Backups>
86+
);
87+
case 'bitbox02':
88+
return (
89+
<SDCardCheck deviceID={deviceID}>
90+
<BackupsV2
91+
deviceID={deviceID}
92+
showCreate={true}
93+
showRestore={false}
94+
showRadio={false}>
95+
<BackButton deviceID={deviceID} />
96+
</BackupsV2>
97+
</SDCardCheck>
98+
);
99+
default:
100+
return null;
101+
}
102+
};
103+
104+
const BackButton = ({ deviceID }: { deviceID: string }) => {
105+
const { t } = useTranslation();
106+
return (
107+
<ButtonLink
108+
secondary
109+
to={`/settings/device-settings/${deviceID}`}>
110+
{t('button.back')}
111+
</ButtonLink>
112+
);
113+
};
114+
115+
const ManageBackupGuide = ({
116+
deviceID,
117+
devices,
118+
}: TProps) => {
119+
const { t } = useTranslation();
120+
121+
if (!deviceID) {
122+
return null;
123+
}
124+
125+
switch (devices[deviceID]) {
126+
case 'bitbox':
127+
return (
128+
<Guide>
129+
<Entry key="guide.backups.whatIsABackup" entry={t('guide.backups.whatIsABackup')} />
130+
<Entry key="guide.backups.encrypt" entry={t('guide.backups.encrypt')} />
131+
<Entry key="guide.backups.check" entry={t('guide.backups.check')} />
132+
<Entry key="guide.backups.howOften" entry={t('guide.backups.howOften')} />
133+
</Guide>
134+
);
135+
case 'bitbox02':
136+
return (
137+
<Guide>
138+
<Entry key="guide.backupsBB02.whatIsABackup" entry={t('guide.backupsBB02.whatIsABackup')} />
139+
<Entry key="guide.backupsBB02.encrypt" entry={t('guide.backupsBB02.encrypt')} shown={true} />
140+
<Entry key="guide.backupsBB02.check" entry={t('guide.backupsBB02.check')} />
141+
<Entry key="guide.backups.howOften" entry={t('guide.backups.howOften')} />
142+
</Guide>
143+
);
144+
default:
145+
return null;
146+
}
147+
};

frontends/web/src/routes/router.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { Receive } from './account/receive';
1212
import { SendWrapper } from './account/send/send-wrapper';
1313
import { AccountsSummary } from './account/summary/accountssummary';
1414
import { DeviceSwitch } from './device/deviceswitch';
15-
import ManageBackups from './device/manage-backups/manage-backups';
15+
import { ManageBackups } from './device/manage-backups/manage-backups';
1616
import { ManageAccounts } from './settings/manage-accounts';
1717
import { ElectrumSettings } from './settings/electrum';
1818
import { Passphrase } from './device/bitbox02/passphrase';
@@ -151,6 +151,7 @@ export const AppRouter = ({ devices, deviceIDs, devicesKey, accounts, activeAcco
151151

152152
const ManageBackupsEl = <InjectParams><ManageBackups
153153
key={devicesKey('manage-backups')}
154+
deviceID={null}
154155
devices={devices}
155156
/></InjectParams>;
156157

0 commit comments

Comments
 (0)