Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TS migration] Migrate 'Device' lib to TypeScript #27709

Merged
merged 20 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import DeviceInfo from 'react-native-device-info';
import Str from 'expensify-common/lib/str';
import {GenerateDeviceID} from "./types";

const deviceID = DeviceInfo.getDeviceId();
const uniqueID = Str.guid(deviceID);
Expand All @@ -22,10 +23,8 @@ const uniqueID = Str.guid(deviceID);
*
* Furthermore, the deviceID prefix is not unique to a specific device, but is likely to change from one type of device to another.
* Including this prefix will tell us with a reasonable degree of confidence if the user just uninstalled and reinstalled the app, or if they got a new device.
*
* @returns {Promise<String>}
*/
function generateDeviceID() {
const generateDeviceID: GenerateDeviceID = () => {
return Promise.resolve(uniqueID);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import ELECTRON_EVENTS from '../../../../../desktop/ELECTRON_EVENTS';
import {GenerateDeviceID} from "./types";

/**
* Get the unique ID of the current device. This should remain the same even if the user uninstalls and reinstalls the app.
*
* @returns {Promise<String>}
*/
function generateDeviceID() {
return window.electron.invoke(ELECTRON_EVENTS.REQUEST_DEVICE_ID);

const generateDeviceID: GenerateDeviceID = () => {
return window.electron.invoke(ELECTRON_EVENTS.REQUEST_DEVICE_ID) as Promise<string>;
}

export default generateDeviceID;
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import DeviceInfo from 'react-native-device-info';
import {GenerateDeviceID} from "./types";

const deviceID = DeviceInfo.getDeviceId();

/**
* Get the unique ID of the current device. This should remain the same even if the user uninstalls and reinstalls the app.
*
* @returns {Promise<String>}
*/
function generateDeviceID() {
return DeviceInfo.getUniqueId().then((uniqueID) => `${deviceID}_${uniqueID}`);
const generateDeviceID: GenerateDeviceID = () => {
return DeviceInfo.getUniqueId().then((uniqueID: string) => `${deviceID}_${uniqueID}`);
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
}

export default generateDeviceID;
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Str from 'expensify-common/lib/str';
import {GenerateDeviceID} from "./types";

const uniqueID = Str.guid();

Expand All @@ -13,10 +14,9 @@ const uniqueID = Str.guid();
*
* While this isn't perfect, it's just as good as any other obvious web solution, such as this one https://developer.mozilla.org/en-US/docs/Web/API/MediaDeviceInfo/deviceId
* which is also different/reset under the same circumstances
*
* @returns {Promise<String>}
*/
function generateDeviceID() {

const generateDeviceID: GenerateDeviceID = () => {
return Promise.resolve(uniqueID);
}

Expand Down
1 change: 1 addition & 0 deletions src/libs/actions/Device/generateDeviceID/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type GenerateDeviceID = () => Promise<string>;
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import packageConfig from '../../../../../package.json';
import {GetBaseInfo} from "./types";

export default function getBaseInfo() {
export const getBaseInfo: GetBaseInfo = () => {
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
return {
app_version: packageConfig.version,
timestamp: new Date().toISOString().slice(0, 19),
};
}

export default getBaseInfo;
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import Str from 'expensify-common/lib/str';
import RNDeviceInfo from 'react-native-device-info';
import {GetOSAndName} from "./types";

export default function getOSAndName() {
const getOSAndName: GetOSAndName = () => {
const deviceName = RNDeviceInfo.getDeviceNameSync();
const prettyName = `${Str.UCFirst(RNDeviceInfo.getManufacturerSync() || '')} ${deviceName}`;
return {
device_name: RNDeviceInfo.isEmulatorSync() ? `Emulator - ${prettyName}` : prettyName,
os_version: RNDeviceInfo.getSystemVersion(),
};
}

export default getOSAndName;
5 changes: 5 additions & 0 deletions src/libs/actions/Device/getDeviceInfo/getOSAndName/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type GetOSAndName = () => OSAndName;
export type OSAndName = {
device_name?: string;
os_version?: string;
}
10 changes: 0 additions & 10 deletions src/libs/actions/Device/getDeviceInfo/index.android.js

This file was deleted.

13 changes: 13 additions & 0 deletions src/libs/actions/Device/getDeviceInfo/index.android.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import getBaseInfo from './getBaseInfo';
import getOSAndName from './getOSAndName/index.native';
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
import {GetDeviceInfo} from './types';

const getDeviceInfo: GetDeviceInfo = () => {
return {
...getBaseInfo(),
...getOSAndName(),
os: 'Android',
};
}

export default getDeviceInfo;
10 changes: 0 additions & 10 deletions src/libs/actions/Device/getDeviceInfo/index.desktop.js

This file was deleted.

12 changes: 12 additions & 0 deletions src/libs/actions/Device/getDeviceInfo/index.desktop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import getBaseInfo from './getBaseInfo';
import getOSAndName from './getOSAndName/index.native';
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
import {GetDeviceInfo} from './types';
const getDeviceInfo: GetDeviceInfo = () => {
return {
...getBaseInfo(),
...getOSAndName(),
device_name: 'Desktop',
};
}

export default getDeviceInfo;
10 changes: 0 additions & 10 deletions src/libs/actions/Device/getDeviceInfo/index.ios.js

This file was deleted.

12 changes: 12 additions & 0 deletions src/libs/actions/Device/getDeviceInfo/index.ios.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import getBaseInfo from './getBaseInfo';
import getOSAndName from './getOSAndName/index.native';
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
import {GetDeviceInfo} from './types';
const getDeviceInfo: GetDeviceInfo = () => {
return {
...getBaseInfo(),
...getOSAndName(),
os: 'iOS',
};
}

export default getDeviceInfo;
23 changes: 0 additions & 23 deletions src/libs/actions/Device/getDeviceInfo/index.js

This file was deleted.

12 changes: 12 additions & 0 deletions src/libs/actions/Device/getDeviceInfo/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import getBaseInfo from './getBaseInfo';
import getOSAndName from "./getOSAndName/index";
import {GetDeviceInfo} from "./types";

const getDeviceInfo: GetDeviceInfo = () => {
return {
...getBaseInfo(),
...getOSAndName(),
};
}

export default getDeviceInfo;
10 changes: 10 additions & 0 deletions src/libs/actions/Device/getDeviceInfo/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {OSAndName} from "./getOSAndName/types";

export type BaseInfo = {
app_version: string;
timestamp: string;
};

export type GetDeviceInfo = () => DeviceInfo;
export type DeviceInfo = BaseInfo & OSAndName & {os?: string, device_name?: string, device_version?: string};
export type GetBaseInfo = () => BaseInfo;
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import Onyx from 'react-native-onyx';
import Onyx, {OnyxEntry} from 'react-native-onyx';
import ONYXKEYS from '../../../ONYXKEYS';
import Log from '../../Log';
import generateDeviceID from './generateDeviceID';
import getDeviceInfo from './getDeviceInfo';
import generateDeviceID from './generateDeviceID/index';
import getDeviceInfo from './getDeviceInfo/index';

let deviceID;
let deviceID: string | null = null;

/**
* @returns {Promise<String>}
* @returns - device ID string or null in case of failure
*/
function getDeviceID() {
function getDeviceID(): Promise<string | null> {
return new Promise((resolve) => {
if (deviceID) {
return resolve(deviceID);
}

const connectionID = Onyx.connect({
key: ONYXKEYS.DEVICE_ID,
callback: (ID) => {
callback: (ID: OnyxEntry<string>) => {
Onyx.disconnect(connectionID);
deviceID = ID;
return resolve(ID);
Expand All @@ -38,7 +38,7 @@ function setDeviceID() {
throw new Error(existingDeviceID);
})
.then(generateDeviceID)
.then((uniqueID) => {
.then((uniqueID: string) => {
Log.info('Got new deviceID', false, uniqueID);
Onyx.set(ONYXKEYS.DEVICE_ID, uniqueID);
})
Expand All @@ -47,9 +47,9 @@ function setDeviceID() {

/**
* Returns a string object with device info and uniqueID
* @returns {Promise<string>}
* @returns - device info with ID
*/
function getDeviceInfoWithID() {
function getDeviceInfoWithID(): Promise<string> {
return new Promise((resolve) => {
getDeviceID().then((currentDeviceID) =>
resolve(
Expand Down
Loading