mattermost-mobile/app/components/post_draft/draft_handler/draft_handler.tsx
Christopher Speller 6000e69886
Agents "RHS" view (#9318)
* Add agents RHS functionality for mobile

Implements a dedicated agents interface accessible from the home screen, featuring:
- Agent chat screen with bot selector and message input
- Thread list screen showing all agent conversations
- Remote actions for fetching bots and threads from plugin API
- Navigation between chat, threads, and individual conversations
- Integration with plus menu for easy access

Follows similar pattern to playbooks with modal screens and navigation helpers.

* Fix icon name for agents menu item

Change robot-happy-outline to robot-happy to resolve PropType validation error.

* Add agents button to sidebar and fix agent chat issues

- Add AgentsButton component to channel list sidebar below Drafts
- Fix thread navigation to use fetchAndSwitchToThread instead of switchToChannelById
- Replace custom TextInput with PostDraft component in agent_chat
- Fix icon name from message-reply-text-outline to reply-outline
- Add missing i18n translations for agents UI

* Implement automatic navigation to thread after sending agent message

* Implement bot selector bottom sheet with avatars. Replace click-and-cycle behavior with a proper bottom sheet menu showing all available agents with their profile pictures. Add bot avatar display to the dropdown button for better visual identification.

* Top bar design modifications.

* Add intro graphic and text

* Fix autocomplete not working

* Tweak styles and icons.

* Remove unused

* Add version/enabled check

* Remove excessive agent button

* Style fixes

* Use non-blocking then() for onPostCreated callback in send message hook

* Add unit tests for agents product components and actions

* Review feedback, offline support

* I18n

* Tests

* Address PR review feedback: deletion handling, UI fixes, schema docs

* Remove unnecessary deleteNotPresent flag from agents handlers

* Fix agents archived channel, empty data guard, and stale relative time

* Add smoke test for AgentChat and extend ToolCard test coverage

* Fix test quality issues: remove useless tests, strengthen assertions

* Address PR review: remove barrel file, add version comment

* Mock reanimated in CitationsList test to fix CI failure

* Fix CitationsList tests after always-mounted animation change

* Address PR review: typography, Pressable, FormattedText, schema bump, and cleanup

* Apply CLAUDE.md patterns across agents codebase: Pressable, typography, FormattedText, logDebug

* Fix schema test to expect version 19
2026-03-16 09:48:32 -07:00

147 lines
4.9 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React, {useCallback, useEffect, useRef} from 'react';
import {useIntl} from 'react-intl';
import {addFilesToDraft, removeDraft} from '@actions/local/draft';
import {useServerUrl} from '@context/server';
import useFileUploadError from '@hooks/file_upload_error';
import DraftEditPostUploadManager from '@managers/draft_upload_manager';
import {fileMaxWarning, fileSizeWarning, uploadDisabledWarning} from '@utils/file';
import SendHandler from '../send_handler';
import type {ErrorHandlers} from '@typings/components/upload_error_handlers';
import type {AvailableScreens} from '@typings/screens/navigation';
type Props = {
testID?: string;
channelId: string;
cursorPosition: number;
rootId?: string;
canShowPostPriority?: boolean;
files?: FileInfo[];
maxFileCount: number;
maxFileSize: number;
canUploadFiles: boolean;
updateCursorPosition: React.Dispatch<React.SetStateAction<number>>;
updatePostInputTop: (top: number) => void;
updateValue: React.Dispatch<React.SetStateAction<string>>;
value: string;
setIsFocused: (isFocused: boolean) => void;
onPostCreated?: (postId: string) => void;
location?: AvailableScreens;
}
const emptyFileList: FileInfo[] = [];
export default function DraftHandler(props: Props) {
const {
testID,
channelId,
cursorPosition,
rootId = '',
canShowPostPriority,
files,
maxFileCount,
maxFileSize,
canUploadFiles,
updateCursorPosition,
updatePostInputTop,
updateValue,
value,
setIsFocused,
onPostCreated,
location,
} = props;
const serverUrl = useServerUrl();
const intl = useIntl();
const uploadErrorHandlers = useRef<ErrorHandlers>({});
const {uploadError, newUploadError} = useFileUploadError();
const clearDraft = useCallback(() => {
removeDraft(serverUrl, channelId, rootId);
updateValue('');
}, [serverUrl, channelId, rootId, updateValue]);
const addFiles = useCallback((newFiles: FileInfo[]) => {
if (!newFiles.length) {
return;
}
if (!canUploadFiles) {
newUploadError(uploadDisabledWarning(intl));
return;
}
const currentFileCount = files?.length || 0;
const availableCount = maxFileCount - currentFileCount;
if (newFiles.length > availableCount) {
newUploadError(fileMaxWarning(intl, maxFileCount));
return;
}
const largeFile = newFiles.find((file) => file.size > maxFileSize);
if (largeFile) {
newUploadError(fileSizeWarning(intl, maxFileSize));
return;
}
addFilesToDraft(serverUrl, channelId, rootId, newFiles);
for (const file of newFiles) {
DraftEditPostUploadManager.prepareUpload(serverUrl, file, channelId, rootId);
uploadErrorHandlers.current[file.clientId!] = DraftEditPostUploadManager.registerErrorHandler(file.clientId!, newUploadError);
}
newUploadError(null);
}, [intl, newUploadError, maxFileSize, serverUrl, files?.length, channelId, rootId, canUploadFiles, maxFileCount]);
// This effect mainly handles keeping clean the uploadErrorHandlers, and
// reinstantiate them on component mount and file retry.
useEffect(() => {
let loadingFiles: FileInfo[] = [];
if (files) {
loadingFiles = files.filter((v) => v.clientId && DraftEditPostUploadManager.isUploading(v.clientId));
}
for (const key of Object.keys(uploadErrorHandlers.current)) {
if (!loadingFiles.find((v) => v.clientId === key)) {
uploadErrorHandlers.current[key]?.();
delete (uploadErrorHandlers.current[key]);
}
}
for (const file of loadingFiles) {
if (!uploadErrorHandlers.current[file.clientId!]) {
uploadErrorHandlers.current[file.clientId!] = DraftEditPostUploadManager.registerErrorHandler(file.clientId!, newUploadError);
}
}
}, [files, newUploadError]);
return (
<SendHandler
testID={testID}
channelId={channelId}
rootId={rootId}
canShowPostPriority={canShowPostPriority}
// From draft handler
cursorPosition={cursorPosition}
value={value}
files={files || emptyFileList}
clearDraft={clearDraft}
addFiles={addFiles}
uploadFileError={uploadError}
updateCursorPosition={updateCursorPosition}
updatePostInputTop={updatePostInputTop}
updateValue={updateValue}
setIsFocused={setIsFocused}
onPostCreated={onPostCreated}
location={location}
/>
);
}