Skip to content

Commit c1c4ca2

Browse files
committed
Merge branch 'frontend-type-newtxs'
2 parents 0d57def + 61572de commit c1c4ca2

File tree

2 files changed

+50
-33
lines changed

2 files changed

+50
-33
lines changed

frontends/web/src/api/transactions.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright 2023 Shift Crypto AG
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { subscribe as subscribeLegacy } from '../utils/event-legacy';
18+
19+
export const syncNewTxs = (
20+
cb: (
21+
meta: {
22+
count: number,
23+
accountName: string,
24+
}
25+
) => void,
26+
) => {
27+
return subscribeLegacy('newTxs', event => {
28+
if (event.type === 'backend') {
29+
cb(event.meta);
30+
}
31+
});
32+
};

frontends/web/src/app.tsx

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { getAccounts, IAccount } from './api/account';
2121
import { syncAccountsList } from './api/accountsync';
2222
import { getDeviceList, TDevices } from './api/devices';
2323
import { syncDeviceList } from './api/devicessync';
24+
import { syncNewTxs } from './api/transactions';
2425
import { notifyUser } from './api/system';
2526
import { unsubscribe, UnsubscribeList } from './utils/subscriptions';
2627
import { ConnectedApp } from './connected';
@@ -33,70 +34,55 @@ import { MobileDataWarning } from './components/mobiledatawarning';
3334
import { Sidebar, toggleSidebar } from './components/sidebar/sidebar';
3435
import { Update } from './components/update/update';
3536
import { translate, TranslateProps } from './decorators/translate';
36-
import { apiWebsocket } from './utils/websocket';
3737
import { route, RouterWatcher } from './utils/route';
3838
import { Darkmode } from './components/darkmode/darkmode';
3939
import { DarkModeProvider } from './contexts/DarkmodeProvider';
4040
import { AppProvider } from './contexts/AppProvider';
4141

42-
interface State {
43-
accounts: IAccount[];
44-
devices: TDevices;
45-
}
42+
type State = {
43+
accounts: IAccount[];
44+
devices: TDevices;
45+
}
4646

47-
type Props = TranslateProps;
47+
type Props = TranslateProps;
4848

4949
class App extends Component<Props, State> {
5050
public readonly state: State = {
5151
accounts: [],
5252
devices: {},
5353
};
5454

55-
private unsubscribe!: () => void;
5655
private unsubscribeList: UnsubscribeList = [];
5756

5857
/**
59-
* Gets fired when the route changes.
60-
*/
58+
* Gets fired when the route changes.
59+
*/
6160
private handleRoute = () => {
6261
if (panelStore.state.activeSidebar) {
6362
toggleSidebar();
6463
}
6564
};
6665

6766
public componentDidMount() {
68-
this.unsubscribe = apiWebsocket((payload) => {
69-
if ('type' in payload) {
70-
const { data, meta, type } = payload;
71-
switch (type) {
72-
case 'backend':
73-
switch (data) {
74-
case 'newTxs':
75-
notifyUser(this.props.t('notification.newTxs', {
76-
count: meta.count,
77-
accountName: meta.accountName,
78-
}));
79-
break;
80-
}
81-
break;
82-
}
83-
}
84-
});
85-
8667
Promise.all([getDeviceList(), getAccounts()])
8768
.then(([devices, accounts]) => {
8869
this.setStateWithDeviceList({ accounts, devices });
8970
})
9071
.catch(console.error);
9172

9273
this.unsubscribeList.push(
74+
syncNewTxs((meta) => {
75+
notifyUser(this.props.t('notification.newTxs', {
76+
count: meta.count,
77+
accountName: meta.accountName,
78+
}));
79+
}),
9380
syncAccountsList(accounts => {
9481
this.setState({ accounts }, this.maybeRoute);
9582
}),
9683
syncDeviceList((devices) => {
9784
this.setStateWithDeviceList({ devices });
9885
}),
99-
// TODO: add syncBackendNewTX
10086
);
10187
}
10288

@@ -107,7 +93,7 @@ class App extends Component<Props, State> {
10793
// if the first device is new
10894
if (
10995
newDeviceIDList.length > 0
110-
&& newDeviceIDList[0] !== oldDeviceIDList[0]
96+
&& newDeviceIDList[0] !== oldDeviceIDList[0]
11197
) {
11298
// route new unlocked device with accounts
11399
if (this.state.accounts.length) {
@@ -124,7 +110,6 @@ class App extends Component<Props, State> {
124110
}
125111

126112
public componentWillUnmount() {
127-
this.unsubscribe();
128113
unsubscribe(this.unsubscribeList);
129114
}
130115

@@ -143,9 +128,9 @@ class App extends Component<Props, State> {
143128
// if no accounts are registered on specified views route to /
144129
if (accounts.length === 0 && (
145130
currentURL.startsWith('/account-summary')
146-
|| currentURL.startsWith('/add-account')
147-
|| currentURL.startsWith('/settings/manage-accounts')
148-
|| currentURL.startsWith('/passphrase')
131+
|| currentURL.startsWith('/add-account')
132+
|| currentURL.startsWith('/settings/manage-accounts')
133+
|| currentURL.startsWith('/passphrase')
149134
)) {
150135
route('/', true);
151136
return;

0 commit comments

Comments
 (0)