forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebugReportActionCreatePage.tsx
More file actions
137 lines (129 loc) · 6.59 KB
/
DebugReportActionCreatePage.tsx
File metadata and controls
137 lines (129 loc) · 6.59 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import React, {useCallback, useState} from 'react';
import {View} from 'react-native';
import type {OnyxEntry} from 'react-native-onyx';
import Button from '@components/Button';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import ScreenWrapper from '@components/ScreenWrapper';
import ScrollView from '@components/ScrollView';
import Text from '@components/Text';
import TextInput from '@components/TextInput';
import useLocalize from '@hooks/useLocalize';
import useOnyx from '@hooks/useOnyx';
import useThemeStyles from '@hooks/useThemeStyles';
import DateUtils from '@libs/DateUtils';
import DebugUtils from '@libs/DebugUtils';
import {canUseTouchScreen} from '@libs/DeviceCapabilities';
import Navigation from '@libs/Navigation/Navigation';
import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types';
import type {DebugParamList} from '@libs/Navigation/types';
import {rand64} from '@libs/NumberUtils';
import ReportActionItem from '@pages/inbox/report/ReportActionItem';
import Debug from '@userActions/Debug';
import CONST from '@src/CONST';
import type {TranslationPaths} from '@src/languages/types';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import type {PersonalDetailsList, ReportAction, Session} from '@src/types/onyx';
type DebugReportActionCreatePageProps = PlatformStackScreenProps<DebugParamList, typeof SCREENS.DEBUG.REPORT_ACTION_CREATE>;
const getInitialReportAction = (reportID: string, session: OnyxEntry<Session>, personalDetailsList: OnyxEntry<PersonalDetailsList>) =>
DebugUtils.stringifyJSON({
actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT,
reportID,
reportActionID: rand64(),
created: DateUtils.getDBTime(),
actorAccountID: session?.accountID,
avatar: (session?.accountID && personalDetailsList?.[session.accountID]?.avatar) ?? '',
message: [{type: CONST.REPORT.MESSAGE.TYPE.COMMENT, html: 'Hello world!', text: 'Hello world!'}],
} satisfies ReportAction);
function DebugReportActionCreatePage({
route: {
params: {reportID},
},
}: DebugReportActionCreatePageProps) {
const {translate} = useLocalize();
const styles = useThemeStyles();
const [session] = useOnyx(ONYXKEYS.SESSION);
const [personalDetailsList] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST);
const [draftReportAction, setDraftReportAction] = useState<string>(() => getInitialReportAction(reportID, session, personalDetailsList));
const [error, setError] = useState<string>();
const createReportAction = useCallback(() => {
const parsedReportAction = JSON.parse(draftReportAction.replaceAll('\n', '')) as ReportAction;
Debug.mergeDebugData(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, {
[parsedReportAction.reportActionID]: parsedReportAction,
});
Navigation.navigate(ROUTES.DEBUG_REPORT_TAB_ACTIONS.getRoute(reportID));
}, [draftReportAction, reportID]);
const editJSON = useCallback(
(updatedJSON: string) => {
try {
DebugUtils.validateReportActionJSON(updatedJSON);
setError('');
} catch (e) {
const {cause, message} = e as SyntaxError;
setError(cause ? translate(message as TranslationPaths, cause as never) : message);
} finally {
setDraftReportAction(updatedJSON);
}
},
[translate],
);
return (
<ScreenWrapper
includeSafeAreaPaddingBottom={false}
shouldEnableKeyboardAvoidingView={false}
shouldEnableMinHeight={canUseTouchScreen()}
testID="DebugReportActionCreatePage"
>
{({safeAreaPaddingBottomStyle}) => (
<View style={[styles.flex1, safeAreaPaddingBottomStyle]}>
<HeaderWithBackButton
title={`${translate('debug.debug')} - ${translate('debug.createReportAction')}`}
onBackButtonPress={Navigation.goBack}
/>
<ScrollView contentContainerStyle={[styles.ph5, styles.pb5, styles.gap5]}>
<View>
<Text style={[styles.textLabelSupporting, styles.mb2]}>{translate('debug.editJson')}</Text>
<TextInput
errorText={error}
accessibilityLabel={translate('debug.editJson')}
forceActiveLabel
numberOfLines={18}
multiline
value={draftReportAction}
onChangeText={editJSON}
// We need to explicitly add styles.pt5 and styles.pb5 to override the default top and bottom padding of the text input
textInputContainerStyles={[styles.border, styles.borderBottom, styles.ph5, styles.pt5, styles.pb5]}
/>
</View>
<View>
<Text style={[styles.textLabelSupporting, styles.mb2]}>{translate('debug.preview')}</Text>
{!error ? (
<ReportActionItem
action={JSON.parse(draftReportAction.replaceAll('\n', '')) as ReportAction}
report={{reportID}}
parentReportAction={undefined}
displayAsGroup={false}
shouldDisplayNewMarker={false}
isFirstVisibleReportAction={false}
shouldDisplayContextMenu={false}
personalDetails={personalDetailsList}
/>
) : (
<Text>{translate('debug.nothingToPreview')}</Text>
)}
</View>
<Text style={[styles.headerText, styles.textAlignCenter]}>{translate('debug.hint')}</Text>
<Button
success
text={translate('common.save')}
isDisabled={!draftReportAction || !!error}
onPress={createReportAction}
/>
</ScrollView>
</View>
)}
</ScreenWrapper>
);
}
export default DebugReportActionCreatePage;