Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 31 additions & 17 deletions src/pages/settings/Profile/AgentAIPromptSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import type {RefObject} from 'react';
import type {ScrollView as RNScrollView, TextInputKeyPressEvent} from 'react-native';
import {Keyboard} from 'react-native';
import Button from '@components/Button';
import ErrorMessageRow from '@components/ErrorMessageRow';
import OfflineWithFeedback from '@components/OfflineWithFeedback';
import Section from '@components/Section';
import TextInput from '@components/TextInput';
import type {BaseTextInputRef} from '@components/TextInput/BaseTextInput/types';
import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import useOnyx from '@hooks/useOnyx';
import useThemeStyles from '@hooks/useThemeStyles';
import {clearAgentPromptUpdateError, openProfilePage, updateAgentPrompt} from '@libs/actions/Agent';
Expand Down Expand Up @@ -47,6 +49,7 @@ function scrollInputIntoView(parentScrollViewRef: RefObject<RNScrollView | null>

function AgentAIPromptSection({accountID, parentScrollViewRef}: AgentAIPromptSectionProps) {
const {translate} = useLocalize();
const {isOffline} = useNetwork();
const styles = useThemeStyles();
const icons = useMemoizedLazyExpensifyIcons(['Checkmark']);
const [agentPrompt] = useOnyx(`${ONYXKEYS.COLLECTION.SHARED_NVP_AGENT_PROMPT}${accountID}`);
Expand Down Expand Up @@ -95,19 +98,23 @@ function AgentAIPromptSection({accountID, parentScrollViewRef}: AgentAIPromptSec
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [agentPrompt?.promptErrors]);

Comment thread
NicolasBonet marked this conversation as resolved.
const triggerSavedConfirmation = useCallback(() => {
setShowSavedConfirmation(true);
if (savedConfirmationTimerRef.current) {
clearTimeout(savedConfirmationTimerRef.current);
}
savedConfirmationTimerRef.current = setTimeout(() => {
setShowSavedConfirmation(false);
savedConfirmationTimerRef.current = null;
}, SAVED_CONFIRMATION_DURATION_MS);
}, []);

useEffect(() => {
if (wasSavingRef.current && !isSaving && !hasPromptErrors) {
setShowSavedConfirmation(true);
if (savedConfirmationTimerRef.current) {
clearTimeout(savedConfirmationTimerRef.current);
}
savedConfirmationTimerRef.current = setTimeout(() => {
setShowSavedConfirmation(false);
savedConfirmationTimerRef.current = null;
}, SAVED_CONFIRMATION_DURATION_MS);
triggerSavedConfirmation();
}
wasSavingRef.current = isSaving;
}, [isSaving, hasPromptErrors]);
}, [isSaving, hasPromptErrors, triggerSavedConfirmation]);

useEffect(() => {
return () => {
Expand Down Expand Up @@ -147,6 +154,12 @@ function AgentAIPromptSection({accountID, parentScrollViewRef}: AgentAIPromptSec
}
dismissInput();
updateAgentPrompt(accountID, trimmed, agentPrompt?.prompt ?? '');

// Offline: treat the optimistic write as the final state for UX purposes. The request will be
// replayed on reconnect, so showing "Saved" immediately matches the queued-but-done intent.
if (isOffline) {
triggerSavedConfirmation();
}
};

const handleChangeText = (text: string) => {
Expand All @@ -172,11 +185,7 @@ function AgentAIPromptSection({accountID, parentScrollViewRef}: AgentAIPromptSec
childrenStyles={styles.pt5}
titleStyles={styles.accountSettingsSectionTitle}
>
<OfflineWithFeedback
errors={agentPrompt?.promptErrors}
pendingAction={agentPrompt?.pendingAction}
onClose={() => clearAgentPromptUpdateError(accountID)}
>
<OfflineWithFeedback pendingAction={agentPrompt?.pendingAction}>
<TextInput
ref={inputRef}
label={translate('profilePage.aiPromptSection.prompt')}
Expand All @@ -188,7 +197,7 @@ function AgentAIPromptSection({accountID, parentScrollViewRef}: AgentAIPromptSec
autoGrowHeight
maxAutoGrowHeight={variables.lineHeightNormal * MAX_VISIBLE_PROMPT_LINES}
errorText={errorText}
containerStyles={[styles.mb4]}
containerStyles={[styles.mb3]}
testID="ai-prompt-input"
onFocus={handleInputFocus}
/>
Expand All @@ -198,11 +207,16 @@ function AgentAIPromptSection({accountID, parentScrollViewRef}: AgentAIPromptSec
text={showSavedConfirmation ? translate('profilePage.aiPromptSection.saved') : translate('common.save')}
icon={showSavedConfirmation ? icons.Checkmark : undefined}
onPress={handleSave}
isLoading={isSaving}
isDisabled={hasHtmlTag || isSaving}
isLoading={isSaving && !isOffline}
isDisabled={hasHtmlTag || (isSaving && !isOffline)}
style={[styles.alignSelfStart]}
testID="save-prompt-button"
/>
<ErrorMessageRow
errors={agentPrompt?.promptErrors}
errorRowStyles={[styles.mt3]}
onDismiss={() => clearAgentPromptUpdateError(accountID)}
/>
</Section>
);
}
Expand Down
Loading