Skip to content

Commit 6695479

Browse files
authored
Merge pull request #97876 from FitseTLT/fix/96902-agent-instructions-markdown
Fix - Add react-native-live-markdown to the Custom Agent Instructions input
2 parents 207ce11 + 62d8975 commit 6695479

5 files changed

Lines changed: 33 additions & 1 deletion

File tree

src/pages/settings/Agents/AddAgentPage.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import AvatarButtonWithIcon from '@components/AvatarButtonWithIcon';
22
import FormProvider from '@components/Form/FormProvider';
33
import InputWrapper from '@components/Form/InputWrapper';
4-
import type {FormOnyxValues} from '@components/Form/types';
4+
import type {FormOnyxValues, FormRef} from '@components/Form/types';
55
import FullScreenLoadingIndicator from '@components/FullscreenLoadingIndicator';
66
import HeaderWithBackButton from '@components/HeaderWithBackButton';
77
import ScreenWrapper from '@components/ScreenWrapper';
@@ -37,6 +37,8 @@ import type NewAgentTemplate from '@src/types/onyx/NewAgentTemplate';
3737
import type {Errors} from '@src/types/onyx/OnyxCommon';
3838
import isLoadingOnyxValue from '@src/types/utils/isLoadingOnyxValue';
3939

40+
import type {TextInputKeyPressEvent} from 'react-native';
41+
4042
import React, {useCallback, useEffect, useRef} from 'react';
4143
import {View} from 'react-native';
4244

@@ -63,6 +65,18 @@ function AddAgentPageContent({route, template}: AddAgentPageContentProps) {
6365
const [avatarDraft, avatarDraftMetadata] = useOnyx(ONYXKEYS.AGENT_NEW_AVATAR_DRAFT);
6466
const isDraftLoading = isLoadingOnyxValue(avatarDraftMetadata);
6567
const hasSubmittedRef = useRef(false);
68+
const formRef = useRef<FormRef>(null);
69+
70+
const submitFormOnModEnter = (event: TextInputKeyPressEvent | KeyboardEvent) => {
71+
if (!('key' in event)) {
72+
return;
73+
}
74+
if (event.key === 'Enter' && (event.metaKey || event.ctrlKey)) {
75+
// The markdown input inserts a line break for any Enter keydown whose default is not already prevented, so the submit combo has to claim it first.
76+
event.preventDefault();
77+
formRef.current?.submit();
78+
}
79+
};
6680

6781
const uploadedAvatar = avatarDraft?.uploadedAvatar;
6882
const selectedPresetID = avatarDraft?.customExpensifyAvatarID && AGENT_AVATARS.isAvatarID(avatarDraft.customExpensifyAvatarID) ? avatarDraft.customExpensifyAvatarID : undefined;
@@ -150,6 +164,7 @@ function AddAgentPageContent({route, template}: AddAgentPageContentProps) {
150164
onBackButtonPress={() => Navigation.goBack(ROUTES.SETTINGS_AGENTS_NEW.getRoute(policyID ? {policyID} : undefined))}
151165
/>
152166
<FormProvider
167+
ref={formRef}
153168
formID={ONYXKEYS.FORMS.ADD_AGENT_FORM}
154169
onSubmit={handleSubmit}
155170
validate={validate}
@@ -192,6 +207,9 @@ function AddAgentPageContent({route, template}: AddAgentPageContentProps) {
192207
label={translate('addAgentPage.instructions')}
193208
accessibilityLabel={translate('addAgentPage.instructions')}
194209
role={CONST.ROLE.PRESENTATION}
210+
type="markdown"
211+
excludedMarkdownStyles={['mentionReport']}
212+
onKeyPress={submitFormOnModEnter}
195213
defaultValue={defaultPrompt}
196214
multiline
197215
containerStyles={[styles.flex1]}

src/pages/settings/Agents/Fields/EditPromptPage.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ function EditPromptPage({route}: EditPromptPageProps) {
9999
label={translate('editAgentPage.instructions')}
100100
accessibilityLabel={translate('editAgentPage.instructions')}
101101
role={CONST.ROLE.PRESENTATION}
102+
type="markdown"
103+
excludedMarkdownStyles={['mentionReport']}
102104
defaultValue={Str.htmlDecode(agentPrompt?.prompt ?? '')}
103105
multiline
104106
containerStyles={[styles.flex1]}

src/pages/settings/Profile/AgentAIPromptSection.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ function AgentAIPromptSection({accountID, parentScrollViewRef}: AgentAIPromptSec
203203
const handleKeyPress = (e: TextInputKeyPressEvent) => {
204204
const event = e as unknown as KeyboardEvent;
205205
if (event.key === 'Enter' && (event.metaKey || event.ctrlKey)) {
206+
// The markdown input inserts a line break for any Enter keydown whose default is not already prevented, so the submit combo has to claim it first.
207+
event.preventDefault();
206208
handleSave();
207209
}
208210
};
@@ -221,6 +223,8 @@ function AgentAIPromptSection({accountID, parentScrollViewRef}: AgentAIPromptSec
221223
ref={inputRef}
222224
label={translate('profilePage.aiPromptSection.prompt')}
223225
accessibilityLabel={translate('profilePage.aiPromptSection.prompt')}
226+
type="markdown"
227+
excludedMarkdownStyles={['mentionReport']}
224228
value={draftPrompt}
225229
onChangeText={handleChangeText}
226230
onKeyPress={handleKeyPress}

src/pages/workspace/rules/AgentRules/AddAgentRuleWriteTab.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ function AddAgentRuleWriteTab({onSave}: AddAgentRuleWriteTabProps) {
4343
return;
4444
}
4545
if (event.key === 'Enter' && (event.metaKey || event.ctrlKey)) {
46+
// The markdown input inserts a line break for any Enter keydown whose default is not already prevented, so the submit combo has to claim it first.
47+
event.preventDefault();
4648
formRef.current?.submit();
4749
}
4850
};
@@ -89,6 +91,8 @@ function AddAgentRuleWriteTab({onSave}: AddAgentRuleWriteTabProps) {
8991
label={describeRuleLabel}
9092
accessibilityLabel={describeRuleLabel}
9193
role={CONST.ROLE.PRESENTATION}
94+
type="markdown"
95+
excludedMarkdownStyles={['mentionReport']}
9296
onKeyPress={submitFormOnModEnter}
9397
multiline
9498
shouldSaveDraft

src/pages/workspace/rules/AgentRules/EditAgentRulePage.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ function EditAgentRulePage({
6060
return;
6161
}
6262
if (event.key === 'Enter' && (event.metaKey || event.ctrlKey)) {
63+
// The markdown input inserts a line break for any Enter keydown whose default is not already prevented, so the submit combo has to claim it first.
64+
event.preventDefault();
6365
formRef.current?.submit();
6466
}
6567
};
@@ -158,6 +160,8 @@ function EditAgentRulePage({
158160
label={describeRuleLabel}
159161
accessibilityLabel={describeRuleLabel}
160162
role={CONST.ROLE.PRESENTATION}
163+
type="markdown"
164+
excludedMarkdownStyles={['mentionReport']}
161165
onKeyPress={submitFormOnModEnter}
162166
defaultValue={agentRule.prompt}
163167
multiline

0 commit comments

Comments
 (0)