LogBoxInspectorHeader.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 Image from '../../Image/Image';
  12. import Platform from '../../Utilities/Platform';
  13. import * as React from 'react';
  14. import StyleSheet from '../../StyleSheet/StyleSheet';
  15. import Text from '../../Text/Text';
  16. import View from '../../Components/View/View';
  17. import StatusBar from '../../Components/StatusBar/StatusBar';
  18. import LogBoxButton from './LogBoxButton';
  19. import * as LogBoxStyle from './LogBoxStyle';
  20. import type {LogLevel} from '../Data/LogBoxLog';
  21. type Props = $ReadOnly<{|
  22. onSelectIndex: (selectedIndex: number) => void,
  23. selectedIndex: number,
  24. total: number,
  25. level: LogLevel,
  26. |}>;
  27. function LogBoxInspectorHeader(props: Props): React.Node {
  28. if (props.level === 'syntax') {
  29. return (
  30. <View style={[styles.safeArea, styles[props.level]]}>
  31. <View style={styles.header}>
  32. <View style={styles.title}>
  33. <Text style={styles.titleText}>Failed to compile</Text>
  34. </View>
  35. </View>
  36. </View>
  37. );
  38. }
  39. const prevIndex =
  40. props.selectedIndex - 1 < 0 ? props.total - 1 : props.selectedIndex - 1;
  41. const nextIndex =
  42. props.selectedIndex + 1 > props.total - 1 ? 0 : props.selectedIndex + 1;
  43. const titleText = `Log ${props.selectedIndex + 1} of ${props.total}`;
  44. return (
  45. <View style={[styles.safeArea, styles[props.level]]}>
  46. <View style={styles.header}>
  47. <LogBoxInspectorHeaderButton
  48. disabled={props.total <= 1}
  49. level={props.level}
  50. image={require('./LogBoxImages/chevron-left.png')}
  51. onPress={() => props.onSelectIndex(prevIndex)}
  52. />
  53. <View style={styles.title}>
  54. <Text style={styles.titleText}>{titleText}</Text>
  55. </View>
  56. <LogBoxInspectorHeaderButton
  57. disabled={props.total <= 1}
  58. level={props.level}
  59. image={require('./LogBoxImages/chevron-right.png')}
  60. onPress={() => props.onSelectIndex(nextIndex)}
  61. />
  62. </View>
  63. </View>
  64. );
  65. }
  66. const backgroundForLevel = (level: LogLevel) =>
  67. ({
  68. warn: {
  69. default: 'transparent',
  70. pressed: LogBoxStyle.getWarningDarkColor(),
  71. },
  72. error: {
  73. default: 'transparent',
  74. pressed: LogBoxStyle.getErrorDarkColor(),
  75. },
  76. fatal: {
  77. default: 'transparent',
  78. pressed: LogBoxStyle.getFatalDarkColor(),
  79. },
  80. syntax: {
  81. default: 'transparent',
  82. pressed: LogBoxStyle.getFatalDarkColor(),
  83. },
  84. }[level]);
  85. function LogBoxInspectorHeaderButton(
  86. props: $ReadOnly<{|
  87. disabled: boolean,
  88. image: number,
  89. level: LogLevel,
  90. onPress?: ?() => void,
  91. |}>,
  92. ): React.Node {
  93. return (
  94. <LogBoxButton
  95. backgroundColor={backgroundForLevel(props.level)}
  96. onPress={props.disabled ? null : props.onPress}
  97. style={headerStyles.button}>
  98. {props.disabled ? null : (
  99. <Image source={props.image} style={headerStyles.buttonImage} />
  100. )}
  101. </LogBoxButton>
  102. );
  103. }
  104. const headerStyles = StyleSheet.create({
  105. button: {
  106. alignItems: 'center',
  107. aspectRatio: 1,
  108. justifyContent: 'center',
  109. marginTop: 5,
  110. marginRight: 6,
  111. marginLeft: 6,
  112. marginBottom: -8,
  113. borderRadius: 3,
  114. },
  115. buttonImage: {
  116. height: 14,
  117. width: 8,
  118. tintColor: LogBoxStyle.getTextColor(),
  119. },
  120. });
  121. const styles = StyleSheet.create({
  122. syntax: {
  123. backgroundColor: LogBoxStyle.getFatalColor(),
  124. },
  125. fatal: {
  126. backgroundColor: LogBoxStyle.getFatalColor(),
  127. },
  128. warn: {
  129. backgroundColor: LogBoxStyle.getWarningColor(),
  130. },
  131. error: {
  132. backgroundColor: LogBoxStyle.getErrorColor(),
  133. },
  134. header: {
  135. flexDirection: 'row',
  136. height: Platform.select({
  137. android: 48,
  138. ios: 44,
  139. }),
  140. },
  141. title: {
  142. alignItems: 'center',
  143. flex: 1,
  144. justifyContent: 'center',
  145. },
  146. titleText: {
  147. color: LogBoxStyle.getTextColor(),
  148. fontSize: 16,
  149. fontWeight: '600',
  150. includeFontPadding: false,
  151. lineHeight: 20,
  152. },
  153. safeArea: {
  154. paddingTop: Platform.OS === 'android' ? StatusBar.currentHeight : 40,
  155. },
  156. });
  157. export default LogBoxInspectorHeader;