LogBoxInspectorMessageHeader.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 * as LogBoxStyle from './LogBoxStyle';
  16. import LogBoxMessage from './LogBoxMessage';
  17. import type {LogLevel} from '../Data/LogBoxLog';
  18. import type {Message} from '../Data/parseLogBoxLog';
  19. type Props = $ReadOnly<{|
  20. collapsed: boolean,
  21. message: Message,
  22. level: LogLevel,
  23. title: string,
  24. onPress: () => void,
  25. |}>;
  26. const SHOW_MORE_MESSAGE_LENGTH = 300;
  27. function LogBoxInspectorMessageHeader(props: Props): React.Node {
  28. function renderShowMore() {
  29. if (
  30. props.message.content.length < SHOW_MORE_MESSAGE_LENGTH ||
  31. !props.collapsed
  32. ) {
  33. return null;
  34. }
  35. return (
  36. <Text style={messageStyles.collapse} onPress={() => props.onPress()}>
  37. ... See More
  38. </Text>
  39. );
  40. }
  41. return (
  42. <View style={messageStyles.body}>
  43. <View style={messageStyles.heading}>
  44. <Text style={[messageStyles.headingText, messageStyles[props.level]]}>
  45. {props.title}
  46. </Text>
  47. </View>
  48. <Text style={messageStyles.bodyText}>
  49. <LogBoxMessage
  50. maxLength={props.collapsed ? SHOW_MORE_MESSAGE_LENGTH : Infinity}
  51. message={props.message}
  52. style={messageStyles.messageText}
  53. />
  54. {renderShowMore()}
  55. </Text>
  56. </View>
  57. );
  58. }
  59. const messageStyles = StyleSheet.create({
  60. body: {
  61. backgroundColor: LogBoxStyle.getBackgroundColor(1),
  62. shadowColor: '#000',
  63. shadowOffset: {width: 0, height: 2},
  64. shadowRadius: 2,
  65. shadowOpacity: 0.5,
  66. flex: 0,
  67. },
  68. bodyText: {
  69. color: LogBoxStyle.getTextColor(1),
  70. fontSize: 14,
  71. includeFontPadding: false,
  72. lineHeight: 20,
  73. fontWeight: '500',
  74. paddingHorizontal: 12,
  75. paddingBottom: 10,
  76. },
  77. heading: {
  78. alignItems: 'center',
  79. flexDirection: 'row',
  80. paddingHorizontal: 12,
  81. marginTop: 10,
  82. marginBottom: 5,
  83. },
  84. headingText: {
  85. flex: 1,
  86. fontSize: 20,
  87. fontWeight: '600',
  88. includeFontPadding: false,
  89. lineHeight: 28,
  90. },
  91. warn: {
  92. color: LogBoxStyle.getWarningColor(1),
  93. },
  94. error: {
  95. color: LogBoxStyle.getErrorColor(1),
  96. },
  97. fatal: {
  98. color: LogBoxStyle.getFatalColor(1),
  99. },
  100. syntax: {
  101. color: LogBoxStyle.getFatalColor(1),
  102. },
  103. messageText: {
  104. color: LogBoxStyle.getTextColor(0.6),
  105. },
  106. collapse: {
  107. color: LogBoxStyle.getTextColor(0.7),
  108. fontSize: 14,
  109. fontWeight: '300',
  110. lineHeight: 12,
  111. },
  112. button: {
  113. paddingVertical: 5,
  114. paddingHorizontal: 10,
  115. borderRadius: 3,
  116. },
  117. });
  118. export default LogBoxInspectorMessageHeader;