-
-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mobile): upgrade FW report to analytics
- Loading branch information
Showing
7 changed files
with
213 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 18 additions & 2 deletions
20
suite-native/firmware/src/components/MayBeStuckedBottomSheet.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
suite-native/firmware/src/hooks/useFirmwareAnalytics.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { useCallback, useEffect, useRef } from 'react'; | ||
|
||
import { DeviceModelInternal, FirmwareType } from '@trezor/connect'; | ||
import { | ||
analytics, | ||
EventType, | ||
FirmwareUpdatePayload, | ||
FirmwareUpdateStartType, | ||
} from '@suite-native/analytics'; | ||
import { TrezorDevice } from '@suite-common/suite-types'; | ||
import { getFirmwareVersion, getBootloaderVersion } from '@trezor/device-utils'; | ||
|
||
export const useFirmwareAnalytics = ({ | ||
device, | ||
targetFirmwareType, | ||
}: { | ||
device?: TrezorDevice; | ||
targetFirmwareType: FirmwareType; | ||
}) => { | ||
const prepareAnalyticsPayload = useCallback(() => { | ||
return { | ||
model: device?.features?.internal_model ?? DeviceModelInternal.UNKNOWN, | ||
fromBootloaderVersion: getBootloaderVersion(device), | ||
fromFwVersion: device?.firmware === 'none' ? 'none' : getFirmwareVersion(device), | ||
toFwVersion: targetFirmwareType, | ||
fromFwType: (device?.firmwareType || 'none') as FirmwareType | 'none', | ||
toFwType: targetFirmwareType, | ||
}; | ||
}, [device, targetFirmwareType]); | ||
|
||
// Use refs to avoid any re-renders because of analytics and to make useCallback dependencies stable | ||
// so it won't trigger any useEffect which could interfere with other business logic. | ||
const analyticsPayload = useRef<FirmwareUpdatePayload>(prepareAnalyticsPayload()); | ||
const timeStarted = useRef<number>(Date.now()); | ||
|
||
useEffect(() => { | ||
analyticsPayload.current = prepareAnalyticsPayload(); | ||
}, [prepareAnalyticsPayload]); | ||
|
||
const getElapsedTimeInSeconds = useCallback(() => { | ||
return Math.floor((Date.now() - timeStarted.current) / 1000); | ||
}, []); | ||
|
||
const getAnalyticsPayload = useCallback(() => { | ||
return analyticsPayload.current; | ||
}, [analyticsPayload]); | ||
|
||
const resetTimeStarted = useCallback(() => { | ||
timeStarted.current = Date.now(); | ||
}, []); | ||
|
||
const handleAnalyticsReportStarted = useCallback( | ||
({ startType }: { startType: FirmwareUpdateStartType }) => { | ||
// Q: Maybe we should reset timeStarted only if it's not a retry? | ||
resetTimeStarted(); | ||
|
||
analytics.report({ | ||
type: EventType.FirmwareUpdateStarted, | ||
payload: { | ||
...getAnalyticsPayload(), | ||
startType, | ||
}, | ||
}); | ||
}, | ||
[getAnalyticsPayload, resetTimeStarted], | ||
); | ||
|
||
const handleAnalyticsReportStucked = useCallback( | ||
(state: 'modalPart1' | 'modalPart2' | 'buttonVisible') => { | ||
analytics.report({ | ||
type: EventType.FirmwareUpdateStucked, | ||
payload: { | ||
...getAnalyticsPayload(), | ||
duration: getElapsedTimeInSeconds(), | ||
state, | ||
}, | ||
}); | ||
}, | ||
[getElapsedTimeInSeconds, getAnalyticsPayload], | ||
); | ||
|
||
const handleAnalyticsReportFinished = useCallback( | ||
({ error }: { error?: string } = {}) => { | ||
analytics.report({ | ||
type: EventType.FirmwareUpdateFinished, | ||
payload: { | ||
...getAnalyticsPayload(), | ||
duration: getElapsedTimeInSeconds(), | ||
error, | ||
}, | ||
}); | ||
}, | ||
[getElapsedTimeInSeconds, getAnalyticsPayload], | ||
); | ||
|
||
const handleAnalyticsReportCancelled = useCallback(() => { | ||
analytics.report({ | ||
type: EventType.FirmwareUpdateCancel, | ||
payload: getAnalyticsPayload(), | ||
}); | ||
}, [getAnalyticsPayload]); | ||
|
||
return { | ||
getElapsedTimeInSeconds, | ||
getAnalyticsPayload, | ||
resetTimeStarted, | ||
handleAnalyticsReportStucked, | ||
handleAnalyticsReportFinished, | ||
handleAnalyticsReportCancelled, | ||
handleAnalyticsReportStarted, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters