mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
fe82f402a9
Summary: This diff makes a few changes to how errors and warnings are handled in LogBox: - Soft errors continue to notify the user collapsed. - Fatal errors pop up full screen over the content, but are dismissible. - Syntax errors pop up full screen, and are **not** dismissible. - Removed the "Reload" button on fatals, and added back the dismiss/close. - Removed all buttons from syntax errors so users are encouraged to fix it and safe without reloading. To do this we needed to: - Move the selectedLogIndex state out of the component and up to the rest of the log state. - Change the way popping a log works, it's now done by setting selectedLogIndex at log time instead of deriving it at render time, that means `selectedLogIndex` is now the sole state value for deciding if the log inspector is open or not - Whenever the state is updated, find a syntax error if it's there, instead of doing this at render time Changelog: [Internal] Reviewed By: cpojer Differential Revision: D18421089 fbshipit-source-id: d2c4937f666f1302ed1a7b1b9c6679b0509136c5
134 lines
3.5 KiB
JavaScript
134 lines
3.5 KiB
JavaScript
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @flow strict-local
|
|
* @format
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
import * as React from 'react';
|
|
import SafeAreaView from '../../Components/SafeAreaView/SafeAreaView';
|
|
import StyleSheet from '../../StyleSheet/StyleSheet';
|
|
import View from '../../Components/View/View';
|
|
import LogBoxInspector from './LogBoxInspector';
|
|
import LogBoxLog from '../Data/LogBoxLog';
|
|
import LogBoxLogNotification from './LogBoxLogNotification';
|
|
import type {LogBoxLogs} from '../Data/LogBoxData';
|
|
|
|
type Props = $ReadOnly<{|
|
|
onDismiss: (log: LogBoxLog) => void,
|
|
onDismissWarns: () => void,
|
|
onDismissErrors: () => void,
|
|
setSelectedLog: number => void,
|
|
logs: LogBoxLogs,
|
|
selectedLogIndex: number,
|
|
isDisabled?: ?boolean,
|
|
|}>;
|
|
|
|
function LogBoxContainer(props: Props): React.Node {
|
|
const {selectedLogIndex, setSelectedLog} = props;
|
|
|
|
const logs = Array.from(props.logs);
|
|
|
|
function handleInspectorDismiss() {
|
|
// Here we handle the cases when the log is dismissed and it
|
|
// was either the last log, or when the current index
|
|
// is now outside the bounds of the log array.
|
|
if (selectedLogIndex != null) {
|
|
if (logs.length - 1 <= 0) {
|
|
setSelectedLog(-1);
|
|
} else if (selectedLogIndex >= logs.length - 1) {
|
|
setSelectedLog(selectedLogIndex - 1);
|
|
}
|
|
props.onDismiss(logs[selectedLogIndex]);
|
|
}
|
|
}
|
|
|
|
function handleInspectorMinimize() {
|
|
setSelectedLog(-1);
|
|
}
|
|
|
|
function openLog(log: LogBoxLog) {
|
|
let index = logs.length - 1;
|
|
|
|
// Stop at zero because if we don't find any log, we'll open the first log.
|
|
while (index > 0 && logs[index] !== log) {
|
|
index -= 1;
|
|
}
|
|
setSelectedLog(index);
|
|
}
|
|
|
|
if (selectedLogIndex > -1) {
|
|
return (
|
|
<View style={StyleSheet.absoluteFill}>
|
|
<LogBoxInspector
|
|
onDismiss={handleInspectorDismiss}
|
|
onMinimize={handleInspectorMinimize}
|
|
onChangeSelectedIndex={setSelectedLog}
|
|
logs={logs}
|
|
selectedIndex={selectedLogIndex}
|
|
/>
|
|
</View>
|
|
);
|
|
}
|
|
|
|
if (logs.length === 0 || props.isDisabled === true) {
|
|
return null;
|
|
}
|
|
|
|
const warnings = logs.filter(log => log.level === 'warn');
|
|
const errors = logs.filter(
|
|
log => log.level === 'error' || log.level === 'fatal',
|
|
);
|
|
return (
|
|
<View style={styles.list}>
|
|
{warnings.length > 0 && (
|
|
<View style={styles.toast}>
|
|
<LogBoxLogNotification
|
|
log={warnings[warnings.length - 1]}
|
|
level="warn"
|
|
totalLogCount={warnings.length}
|
|
onPressOpen={() => openLog(warnings[warnings.length - 1])}
|
|
onPressDismiss={props.onDismissWarns}
|
|
/>
|
|
</View>
|
|
)}
|
|
{errors.length > 0 && (
|
|
<View style={styles.toast}>
|
|
<LogBoxLogNotification
|
|
log={errors[errors.length - 1]}
|
|
level="error"
|
|
totalLogCount={errors.length}
|
|
onPressOpen={() => openLog(errors[errors.length - 1])}
|
|
onPressDismiss={props.onDismissErrors}
|
|
/>
|
|
</View>
|
|
)}
|
|
<SafeAreaView style={styles.safeArea} />
|
|
</View>
|
|
);
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
list: {
|
|
bottom: 10,
|
|
left: 10,
|
|
right: 10,
|
|
position: 'absolute',
|
|
},
|
|
toast: {
|
|
borderRadius: 8,
|
|
marginBottom: 5,
|
|
overflow: 'hidden',
|
|
},
|
|
safeArea: {
|
|
flex: 1,
|
|
},
|
|
});
|
|
|
|
export default LogBoxContainer;
|