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 2 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
Expand Up @@ -23,9 +23,9 @@ 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>}
* @returns - deviceID
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
*/
function generateDeviceID() {
function generateDeviceID(): Promise<string> {
return Promise.resolve(uniqueID);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import ELECTRON_EVENTS from '../../../../../desktop/ELECTRON_EVENTS';
/**
* 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>}
* @returns - device ID
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
*/
function generateDeviceID() {

function generateDeviceID(): Promise<string> {
return window.electron.invoke(ELECTRON_EVENTS.REQUEST_DEVICE_ID);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ 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>}
* @returns - device ID
*/
function generateDeviceID() {
return DeviceInfo.getUniqueId().then((uniqueID) => `${deviceID}_${uniqueID}`);
function generateDeviceID(): Promise<string> {
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
Expand Up @@ -14,9 +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>}
* @returns - device ID
*/
function generateDeviceID() {
function generateDeviceID(): Promise<string> {
return Promise.resolve(uniqueID);
}

Expand Down
8 changes: 0 additions & 8 deletions src/libs/actions/Device/getDeviceInfo/getBaseInfo.js

This file was deleted.

16 changes: 16 additions & 0 deletions src/libs/actions/Device/getDeviceInfo/getBaseInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import packageConfig from '../../../../../package.json';
import {OSAndName} from "./getOSAndName/index.native";

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

export type DeviceInfo = BaseInfo & OSAndName & {os?: string, device_name?: string, device_version?: string};

export default function getBaseInfo(): BaseInfo {
return {
app_version: packageConfig.version,
timestamp: new Date().toISOString().slice(0, 19),
};
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import Str from 'expensify-common/lib/str';
import RNDeviceInfo from 'react-native-device-info';

export default function getOSAndName() {
export type OSAndName = {
device_name: string;
os_version: string;
}
export default function getOSAndName(): OSAndName {
const deviceName = RNDeviceInfo.getDeviceNameSync();
const prettyName = `${Str.UCFirst(RNDeviceInfo.getManufacturerSync() || '')} ${deviceName}`;
return {
Expand Down
10 changes: 0 additions & 10 deletions src/libs/actions/Device/getDeviceInfo/index.android.js

This file was deleted.

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

export default function getDeviceInfo(): DeviceInfo {
return {
...getBaseInfo(),
...getOSAndName(),
os: 'Android',
};
}
10 changes: 0 additions & 10 deletions src/libs/actions/Device/getDeviceInfo/index.desktop.js

This file was deleted.

9 changes: 9 additions & 0 deletions src/libs/actions/Device/getDeviceInfo/index.desktop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import getBaseInfo, {DeviceInfo} from './getBaseInfo';
import getOSAndName from './getOSAndName/index.native';
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
export default function getDeviceInfo(): DeviceInfo {
return {
...getBaseInfo(),
...getOSAndName(),
device_name: 'Desktop',
};
}
10 changes: 0 additions & 10 deletions src/libs/actions/Device/getDeviceInfo/index.ios.js

This file was deleted.

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

export default function getDeviceInfo(): DeviceInfo {
return {
...getBaseInfo(),
...getOSAndName(),
os: 'iOS',
};
}
23 changes: 0 additions & 23 deletions src/libs/actions/Device/getDeviceInfo/index.js

This file was deleted.

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

/**
* @typedef DeviceInfo
* @type {object}

*/

/**
* @returns {DeviceInfo}
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
*/
export default function getDeviceInfo(): DeviceInfo {
return {
...getBaseInfo(),
...getOSAndName(),
};
}
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.ios';
import getDeviceInfo from './getDeviceInfo/index.ios';

fvlvte marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -29,7 +29,7 @@ function getDeviceID() {
/**
* Saves a unique deviceID into Onyx.
*/
function setDeviceID() {
function setDeviceID(): void {
getDeviceID()
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
.then((existingDeviceID) => {
if (!existingDeviceID) {
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
8 changes: 8 additions & 0 deletions src/types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,12 @@ declare module '*.svg' {
export default content;
}

declare global {
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
interface Window {
electron: Electron.IpcRenderer
}
}

declare module 'react-native-device-info/jest/react-native-device-info-mock';

export {}
Loading