deviceName.tsx 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import {useMemo} from 'react';
  2. import {iOSDeviceMapping} from 'sentry/constants/ios-device-list';
  3. export function deviceNameMapper(model: string | undefined): string | null {
  4. // If we have no model, render nothing
  5. if (typeof model !== 'string') {
  6. return null;
  7. }
  8. // If module has not loaded yet, render the unparsed model
  9. if (module === null) {
  10. return model;
  11. }
  12. const [identifier, ...rest] = model.split(' ');
  13. const modelName = iOSDeviceMapping[identifier];
  14. return modelName === undefined ? model : `${modelName} ${rest.join(' ')}`;
  15. }
  16. interface DeviceNameProps {
  17. value: string;
  18. children?: (name: string) => React.ReactNode;
  19. }
  20. /**
  21. * This is used to map iOS Device Names to model name.
  22. * This asynchronously loads the ios-device-list library because of its size
  23. */
  24. function DeviceName({value, children}: DeviceNameProps): React.ReactElement | null {
  25. const deviceName = useMemo(() => deviceNameMapper(value), [value]);
  26. return deviceName ? (
  27. <span data-test-id="loaded-device-name">
  28. {children ? children(deviceName) : deviceName}
  29. </span>
  30. ) : null;
  31. }
  32. export {DeviceName};