Skip to content

Commit 6b81962

Browse files
committed
chore(suite-analytics): remove suite-analytics from common code
1 parent 1029708 commit 6b81962

File tree

6 files changed

+96
-57
lines changed

6 files changed

+96
-57
lines changed

packages/suite/src/middlewares/suite/analyticsMiddleware.ts

+52
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { MiddlewareAPI } from 'redux';
2+
import { isAnyOf } from '@reduxjs/toolkit';
23

34
import { BigNumber } from '@trezor/utils/src/bigNumber';
45
import { getPhysicalDeviceCount } from '@suite-common/suite-utils';
@@ -7,6 +8,11 @@ import {
78
selectDevices,
89
selectDevicesCount,
910
authorizeDeviceThunk,
11+
deviceActions,
12+
firmwareActions,
13+
handleFwHashError,
14+
INVALID_HASH_ERROR,
15+
firmwareUpdate,
1016
} from '@suite-common/wallet-core';
1117
import { analytics, EventType } from '@trezor/suite-analytics';
1218
import { TRANSPORT, DEVICE } from '@trezor/connect';
@@ -56,6 +62,34 @@ const analyticsMiddleware =
5662
});
5763
}
5864

65+
if (handleFwHashError.fulfilled.match(action)) {
66+
analytics.report({
67+
type: EventType.FirmwareValidateHashError,
68+
payload: {
69+
error: action.meta.arg.errorMessage,
70+
},
71+
});
72+
}
73+
74+
if (isAnyOf(firmwareUpdate.fulfilled, firmwareUpdate.rejected)(action)) {
75+
const { device, toBtcOnly, toFwVersion, error = '' } = action.payload ?? {};
76+
77+
if (device?.features) {
78+
analytics.report({
79+
type: EventType.DeviceUpdateFirmware,
80+
payload: {
81+
model: device.features.internal_model,
82+
fromFwVersion:
83+
device?.firmware === 'none' ? 'none' : getFirmwareVersion(device),
84+
fromBlVersion: getBootloaderVersion(device),
85+
error,
86+
toBtcOnly,
87+
toFwVersion,
88+
},
89+
});
90+
}
91+
}
92+
5993
switch (action.type) {
6094
case SUITE.READY:
6195
// reporting can start when analytics is properly initialized and enabled
@@ -222,6 +256,24 @@ const analyticsMiddleware =
222256
break;
223257
}
224258

259+
case deviceActions.rememberDevice.type: {
260+
analytics.report({
261+
type: action.payload.remember
262+
? EventType.SwitchDeviceRemember
263+
: EventType.SwitchDeviceForget,
264+
});
265+
break;
266+
}
267+
268+
case firmwareActions.setFirmwareUpdateError.type: {
269+
if (action.payload === INVALID_HASH_ERROR) {
270+
analytics.report({
271+
type: EventType.FirmwareValidateHashMismatch,
272+
});
273+
}
274+
break;
275+
}
276+
225277
default:
226278
break;
227279
}

suite-common/wallet-core/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
"@trezor/connect": "workspace:*",
3333
"@trezor/device-utils": "workspace:*",
3434
"@trezor/env-utils": "workspace:*",
35-
"@trezor/suite-analytics": "workspace:*",
3635
"@trezor/type-utils": "workspace:*",
3736
"@trezor/utils": "workspace:*",
3837
"date-fns": "^2.30.0",

suite-common/wallet-core/src/device/deviceThunks.ts

+3-9
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import TrezorConnect, {
1111
DeviceState,
1212
} from '@trezor/connect';
1313
import { TrezorDevice } from '@suite-common/suite-types';
14-
import { analytics, EventType } from '@trezor/suite-analytics';
1514
import { notificationsActions } from '@suite-common/toast-notifications';
1615
import {
1716
sortByTimestamp,
@@ -87,20 +86,15 @@ export const selectDeviceThunk = createThunk<void, SelectDeviceThunkParams, void
8786
*/
8887
export const toggleRememberDevice = createThunk(
8988
`${DEVICE_MODULE_PREFIX}/toggleRememberDevice`,
90-
({ device, forceRemember }: { device: TrezorDevice; forceRemember?: true }, { dispatch }) => {
91-
analytics.report({
92-
type: device.remember ? EventType.SwitchDeviceForget : EventType.SwitchDeviceRemember,
93-
});
94-
95-
return dispatch(
89+
({ device, forceRemember }: { device: TrezorDevice; forceRemember?: true }, { dispatch }) =>
90+
dispatch(
9691
deviceActions.rememberDevice({
9792
device,
9893
remember: !device.remember || !!forceRemember,
9994
// if device is already remembered, do not force it, it would remove the remember on return to suite
10095
forceRemember: device.remember ? undefined : forceRemember,
10196
}),
102-
);
103-
},
97+
),
10498
);
10599

106100
export type CreateDeviceInstanceError = {

suite-common/wallet-core/src/firmware/firmwareThunks.ts

+41-43
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
import {
2-
getBootloaderVersion,
3-
getFirmwareVersion,
4-
hasBitcoinOnlyFirmware,
5-
isBitcoinOnlyDevice,
6-
} from '@trezor/device-utils';
7-
import { analytics, EventType } from '@trezor/suite-analytics';
1+
import { hasBitcoinOnlyFirmware, isBitcoinOnlyDevice } from '@trezor/device-utils';
82
import TrezorConnect, { FirmwareType } from '@trezor/connect';
93
import { createThunk } from '@suite-common/redux-utils';
104
import { TrezorDevice } from '@suite-common/suite-types';
@@ -13,7 +7,7 @@ import { selectFirmware } from './firmwareReducer';
137
import { FIRMWARE_MODULE_PREFIX, firmwareActions } from './firmwareActions';
148
import { getBinFilesBaseUrlThunk } from './getBinFilesBaseUrlThunk';
159

16-
const handleFwHashError = createThunk(
10+
export const handleFwHashError = createThunk(
1711
`${FIRMWARE_MODULE_PREFIX}/handleFwHashError`,
1812
({ device, errorMessage }: { device: TrezorDevice; errorMessage: string }, { dispatch }) => {
1913
// device.id should always be present here (device is initialized and in normal mode) during successful TrezorConnect.getFirmwareHash call
@@ -25,12 +19,6 @@ const handleFwHashError = createThunk(
2519
`${errorMessage}. Unable to validate firmware hash. If you want to check authenticity of newly installed firmware please proceed to device settings and reinstall firmware.`,
2620
),
2721
);
28-
analytics.report({
29-
type: EventType.FirmwareValidateHashError,
30-
payload: {
31-
error: errorMessage,
32-
},
33-
});
3422
},
3523
);
3624

@@ -44,9 +32,6 @@ const handleFwHashMismatch = createThunk(
4432
dispatch(firmwareActions.setHashInvalid(device.id));
4533
}
4634
dispatch(firmwareActions.setFirmwareUpdateError(INVALID_HASH_ERROR));
47-
analytics.report({
48-
type: EventType.FirmwareValidateHashMismatch,
49-
});
5035
},
5136
);
5237

@@ -62,11 +47,27 @@ const handleFwHashValid = createThunk(
6247
},
6348
);
6449

65-
export const firmwareUpdate = createThunk(
50+
type FirmwareUpdateProps = {
51+
firmwareType?: FirmwareType;
52+
binary?: ArrayBuffer;
53+
};
54+
55+
type FirmwareUpdateResult = {
56+
device?: TrezorDevice;
57+
toFwVersion?: string;
58+
toBtcOnly?: boolean;
59+
error?: string;
60+
};
61+
62+
export const firmwareUpdate = createThunk<
63+
FirmwareUpdateResult,
64+
FirmwareUpdateProps,
65+
{ rejectValue: FirmwareUpdateResult }
66+
>(
6667
`${FIRMWARE_MODULE_PREFIX}/firmwareUpdate`,
6768
async (
68-
{ firmwareType, binary }: { firmwareType?: FirmwareType; binary?: ArrayBuffer },
69-
{ dispatch, getState, extra },
69+
{ firmwareType, binary },
70+
{ dispatch, getState, extra, fulfillWithValue, rejectWithValue },
7071
) => {
7172
dispatch(firmwareActions.setStatus('started'));
7273

@@ -92,7 +93,9 @@ export const firmwareUpdate = createThunk(
9293
dispatch(firmwareActions.setStatus('error'));
9394
dispatch(firmwareActions.setFirmwareUpdateError('Device not connected'));
9495

95-
return;
96+
return rejectWithValue({
97+
error: 'Device not connected',
98+
});
9699
}
97100

98101
// Cache device when firmware installation starts so that we can reference the original firmware version and type during the installation process.
@@ -130,32 +133,22 @@ export const firmwareUpdate = createThunk(
130133
language: device.firmware === 'none' ? targetTranslationLanguage : undefined,
131134
});
132135

133-
// condition to satisfy TS
134-
if (device.features) {
135-
const targetProperties = binary
136-
? {}
137-
: {
138-
toFwVersion: device?.firmwareRelease?.release.version.join('.'),
139-
toBtcOnly: toBitcoinOnlyFirmware,
140-
};
141-
analytics.report({
142-
type: EventType.DeviceUpdateFirmware,
143-
payload: {
144-
model: device.features.internal_model,
145-
fromFwVersion:
146-
device?.firmware === 'none' ? 'none' : getFirmwareVersion(device),
147-
fromBlVersion: getBootloaderVersion(device),
148-
error: !firmwareUpdateResponse.success
149-
? firmwareUpdateResponse.payload.error
150-
: '',
151-
...targetProperties,
152-
},
153-
});
154-
}
136+
const targetProperties = binary
137+
? {}
138+
: {
139+
toFwVersion: device?.firmwareRelease?.release.version.join('.'),
140+
toBtcOnly: toBitcoinOnlyFirmware,
141+
};
155142

156143
if (!firmwareUpdateResponse.success) {
157144
dispatch(firmwareActions.setStatus('error'));
158145
dispatch(firmwareActions.setFirmwareUpdateError(firmwareUpdateResponse.payload.error));
146+
147+
return rejectWithValue({
148+
device,
149+
error: firmwareUpdateResponse.payload.error,
150+
...targetProperties,
151+
});
159152
} else {
160153
const { check } = firmwareUpdateResponse.payload;
161154
if (check === 'mismatch') {
@@ -172,6 +165,11 @@ export const firmwareUpdate = createThunk(
172165
} else {
173166
dispatch(handleFwHashValid(device));
174167
}
168+
169+
return fulfillWithValue({
170+
device,
171+
...targetProperties,
172+
});
175173
}
176174
},
177175
);

suite-common/wallet-core/tsconfig.json

-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@
2424
{ "path": "../../packages/connect" },
2525
{ "path": "../../packages/device-utils" },
2626
{ "path": "../../packages/env-utils" },
27-
{
28-
"path": "../../packages/suite-analytics"
29-
},
3027
{ "path": "../../packages/type-utils" },
3128
{ "path": "../../packages/utils" }
3229
]

yarn.lock

-1
Original file line numberDiff line numberDiff line change
@@ -9207,7 +9207,6 @@ __metadata:
92079207
"@trezor/connect": "workspace:*"
92089208
"@trezor/device-utils": "workspace:*"
92099209
"@trezor/env-utils": "workspace:*"
9210-
"@trezor/suite-analytics": "workspace:*"
92119210
"@trezor/type-utils": "workspace:*"
92129211
"@trezor/utils": "workspace:*"
92139212
date-fns: "npm:^2.30.0"

0 commit comments

Comments
 (0)