-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
BaseClientSideLoggingToolMenu.tsx
96 lines (84 loc) · 3.35 KB
/
BaseClientSideLoggingToolMenu.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import React from 'react';
import {Alert} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import type {OnyxEntry} from 'react-native-onyx';
import Button from '@components/Button';
import Switch from '@components/Switch';
import TestToolRow from '@components/TestToolRow';
import Text from '@components/Text';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import * as Console from '@libs/actions/Console';
import {parseStringifiedMessages} from '@libs/Console';
import ONYXKEYS from '@src/ONYXKEYS';
import type {CapturedLogs, Log} from '@src/types/onyx';
type BaseClientSideLoggingToolMenuOnyxProps = {
/** Logs captured on the current device */
capturedLogs: OnyxEntry<CapturedLogs>;
/** Whether or not logs should be stored */
shouldStoreLogs: OnyxEntry<boolean>;
};
type BaseClientSideLoggingToolProps = {
/** Locally created file */
file?: {path: string; newFileName: string; size: number};
/** Action to run when pressing Share button */
onShareLogs?: () => void;
/** Action to run when disabling the switch */
onDisableLogging: (logs: Log[]) => void;
/** Action to run when enabling logging */
onEnableLogging?: () => void;
} & BaseClientSideLoggingToolMenuOnyxProps;
function BaseClientSideLoggingToolMenu({shouldStoreLogs, capturedLogs, file, onShareLogs, onDisableLogging, onEnableLogging}: BaseClientSideLoggingToolProps) {
const {translate} = useLocalize();
const onToggle = () => {
if (!shouldStoreLogs) {
Console.setShouldStoreLogs(true);
if (onEnableLogging) {
onEnableLogging();
}
return;
}
if (!capturedLogs) {
Alert.alert(translate('initialSettingsPage.troubleshoot.noLogsToShare'));
Console.disableLoggingAndFlushLogs();
return;
}
const logs = Object.values(capturedLogs);
const logsWithParsedMessages = parseStringifiedMessages(logs);
onDisableLogging(logsWithParsedMessages);
Console.disableLoggingAndFlushLogs();
};
const styles = useThemeStyles();
return (
<>
<TestToolRow title={translate('initialSettingsPage.troubleshoot.clientSideLogging')}>
<Switch
accessibilityLabel={translate('initialSettingsPage.troubleshoot.clientSideLogging')}
isOn={!!shouldStoreLogs}
onToggle={onToggle}
/>
</TestToolRow>
{!!file && (
<>
<Text style={[styles.textLabelSupporting, styles.mb4]}>{`path: ${file.path}`}</Text>
<TestToolRow title={translate('initialSettingsPage.debugConsole.logs')}>
<Button
small
text={translate('common.share')}
onPress={onShareLogs}
/>
</TestToolRow>
</>
)}
</>
);
}
BaseClientSideLoggingToolMenu.displayName = 'BaseClientSideLoggingToolMenu';
export default withOnyx<BaseClientSideLoggingToolProps, BaseClientSideLoggingToolMenuOnyxProps>({
capturedLogs: {
key: ONYXKEYS.LOGS,
},
shouldStoreLogs: {
key: ONYXKEYS.SHOULD_STORE_LOGS,
},
})(BaseClientSideLoggingToolMenu);