LogBoxInspectorStackFrame.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @flow strict-local
  8. * @format
  9. */
  10. 'use strict';
  11. import * as React from 'react';
  12. import StyleSheet from '../../StyleSheet/StyleSheet';
  13. import Text from '../../Text/Text';
  14. import View from '../../Components/View/View';
  15. import Platform from '../../Utilities/Platform';
  16. import LogBoxButton from './LogBoxButton';
  17. import * as LogBoxStyle from './LogBoxStyle';
  18. import type {PressEvent} from '../../Types/CoreEventTypes';
  19. import type {StackFrame} from '../../Core/NativeExceptionsManager';
  20. type Props = $ReadOnly<{|
  21. frame: StackFrame,
  22. onPress?: ?(event: PressEvent) => void,
  23. |}>;
  24. function LogBoxInspectorStackFrame(props: Props): React.Node {
  25. const {frame, onPress} = props;
  26. const column = frame.column != null && parseInt(frame.column, 10);
  27. const location =
  28. getFileName(frame.file) +
  29. (frame.lineNumber != null
  30. ? ':' +
  31. frame.lineNumber +
  32. (column && !isNaN(column) ? ':' + (column + 1) : '')
  33. : '');
  34. return (
  35. <View style={styles.frameContainer}>
  36. <LogBoxButton
  37. backgroundColor={{
  38. default: 'transparent',
  39. pressed: onPress ? LogBoxStyle.getBackgroundColor(1) : 'transparent',
  40. }}
  41. onPress={onPress}
  42. style={styles.frame}>
  43. <Text style={[styles.name, frame.collapse === true && styles.dim]}>
  44. {frame.methodName}
  45. </Text>
  46. <Text
  47. ellipsizeMode="middle"
  48. numberOfLines={1}
  49. style={[styles.location, frame.collapse === true && styles.dim]}>
  50. {location}
  51. </Text>
  52. </LogBoxButton>
  53. </View>
  54. );
  55. }
  56. function getFileName(file) {
  57. if (file == null) {
  58. return '<unknown>';
  59. }
  60. const queryIndex = file.indexOf('?');
  61. return file.substring(
  62. file.lastIndexOf('/') + 1,
  63. queryIndex === -1 ? file.length : queryIndex,
  64. );
  65. }
  66. const styles = StyleSheet.create({
  67. frameContainer: {
  68. flexDirection: 'row',
  69. paddingHorizontal: 15,
  70. },
  71. frame: {
  72. flex: 1,
  73. paddingVertical: 4,
  74. paddingHorizontal: 10,
  75. borderRadius: 5,
  76. },
  77. lineLocation: {
  78. flexDirection: 'row',
  79. },
  80. name: {
  81. color: LogBoxStyle.getTextColor(1),
  82. fontSize: 14,
  83. includeFontPadding: false,
  84. lineHeight: 18,
  85. fontWeight: '400',
  86. fontFamily: Platform.select({android: 'monospace', ios: 'Menlo'}),
  87. },
  88. location: {
  89. color: LogBoxStyle.getTextColor(0.8),
  90. fontSize: 12,
  91. fontWeight: '300',
  92. includeFontPadding: false,
  93. lineHeight: 16,
  94. paddingLeft: 10,
  95. },
  96. dim: {
  97. color: LogBoxStyle.getTextColor(0.4),
  98. fontWeight: '300',
  99. },
  100. line: {
  101. color: LogBoxStyle.getTextColor(0.8),
  102. fontSize: 12,
  103. fontWeight: '300',
  104. includeFontPadding: false,
  105. lineHeight: 16,
  106. },
  107. });
  108. export default LogBoxInspectorStackFrame;