Files
upstream-mirror 30810eecd7
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Code scanning - action / CodeQL-Build (push) Failing after 2s
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:21:02 +05:00

192 lines
6.3 KiB
TypeScript

import type { ILivechatAgent, ILivechatAgentStatus, ILivechatDepartmentAgents } from '@rocket.chat/core-typings';
import { Field, FieldLabel, FieldGroup, FieldRow, TextInput, Button, Box, Icon, Select, ButtonGroup } from '@rocket.chat/fuselage';
import type { SelectOption } from '@rocket.chat/fuselage';
import { useStableCallback } from '@rocket.chat/fuselage-hooks';
import {
ContextualbarTitle,
ContextualbarClose,
ContextualbarHeader,
ContextualbarScrollableContent,
ContextualbarFooter,
} from '@rocket.chat/ui-client';
import { useToastMessageDispatch, useTranslation, useEndpoint, useRouter } from '@rocket.chat/ui-contexts';
import { useQueryClient } from '@tanstack/react-query';
import { useId, useMemo } from 'react';
import { useForm, Controller, FormProvider } from 'react-hook-form';
import { getUserEmailAddress } from '../../../../lib/getUserEmailAddress';
import { UserInfoAvatar } from '../../../components/UserInfo';
import { omnichannelQueryKeys } from '../../../lib/queryKeys';
import { MaxChatsPerAgent } from '../additionalForms';
import AutoCompleteDepartmentMultiple from '../components/AutoCompleteDepartmentMultiple';
type AgentEditFormData = {
name: string | undefined;
username: string | undefined;
email: string | undefined;
departments: { label: string; value: string }[];
status: ILivechatAgentStatus;
maxNumberSimultaneousChat: number;
};
export type AgentEditProps = {
agentData: Pick<ILivechatAgent, '_id' | 'username' | 'name' | 'status' | 'statusLivechat' | 'emails' | 'livechat'>;
agentDepartments: (Pick<ILivechatDepartmentAgents, 'departmentId'> & { departmentName: string })[];
};
const AgentEdit = ({ agentData, agentDepartments }: AgentEditProps) => {
const t = useTranslation();
const router = useRouter();
const queryClient = useQueryClient();
const dispatchToastMessage = useToastMessageDispatch();
const { name, username, livechat, statusLivechat } = agentData;
const email = getUserEmailAddress(agentData);
const statusOptions: SelectOption[] = useMemo(
() => [
['available', t('Available')],
['not-available', t('Not_Available')],
],
[t],
);
const initialDepartmentValue = useMemo(
() => agentDepartments.map(({ departmentName, departmentId }) => ({ label: departmentName, value: departmentId })) || [],
[agentDepartments],
);
const methods = useForm<AgentEditFormData>({
values: {
name,
username,
email,
departments: initialDepartmentValue,
status: statusLivechat,
maxNumberSimultaneousChat: livechat?.maxNumberSimultaneousChat || 0,
},
});
const {
control,
handleSubmit,
reset,
formState: { isDirty },
} = methods;
const saveAgentInfo = useEndpoint('POST', '/v1/livechat/agents.saveInfo');
const saveAgentStatus = useEndpoint('POST', '/v1/livechat/agent.status');
const handleSave = useStableCallback(async ({ status, departments, ...data }: AgentEditFormData) => {
try {
await saveAgentStatus({ agentId: agentData._id, status });
await saveAgentInfo({
agentId: agentData._id,
agentData: data,
agentDepartments: departments.map((dep) => dep.value),
});
dispatchToastMessage({ type: 'success', message: t('Success') });
router.navigate('/omnichannel/agents');
queryClient.invalidateQueries({ queryKey: omnichannelQueryKeys.agents() });
queryClient.invalidateQueries({ queryKey: omnichannelQueryKeys.agentDepartments(agentData._id) });
} catch (error) {
dispatchToastMessage({ type: 'error', message: error });
}
});
const formId = useId();
const nameField = useId();
const usernameField = useId();
const emailField = useId();
const departmentsFieldId = useId();
const statusField = useId();
return (
<>
<ContextualbarHeader>
<ContextualbarTitle>{t('Edit_User')}</ContextualbarTitle>
<ContextualbarClose onClick={() => router.navigate('/omnichannel/agents')} />
</ContextualbarHeader>
<ContextualbarScrollableContent>
<FormProvider {...methods}>
<form id={formId} onSubmit={handleSubmit(handleSave)}>
{username && (
<Box display='flex' flexDirection='column' alignItems='center'>
<UserInfoAvatar username={username} />
</Box>
)}
<FieldGroup>
<Field>
<FieldLabel htmlFor={nameField}>{t('Name')}</FieldLabel>
<FieldRow>
<Controller name='name' control={control} render={({ field }) => <TextInput id={nameField} {...field} readOnly />} />
</FieldRow>
</Field>
<Field>
<FieldLabel htmlFor={usernameField}>{t('Username')}</FieldLabel>
<FieldRow>
<Controller
name='username'
control={control}
render={({ field }) => <TextInput id={usernameField} {...field} readOnly endAddon={<Icon name='at' size='x20' />} />}
/>
</FieldRow>
</Field>
<Field>
<FieldLabel htmlFor={emailField}>{t('Email')}</FieldLabel>
<FieldRow>
<Controller
name='email'
control={control}
render={({ field }) => <TextInput id={emailField} {...field} readOnly endAddon={<Icon name='mail' size='x20' />} />}
/>
</FieldRow>
</Field>
<Field>
<FieldLabel id={departmentsFieldId}>{t('Departments')}</FieldLabel>
<FieldRow>
<Controller
name='departments'
control={control}
render={({ field }) => (
<AutoCompleteDepartmentMultiple aria-labelledby={departmentsFieldId} withCheckbox showArchived {...field} />
)}
/>
</FieldRow>
</Field>
<Field>
<FieldLabel htmlFor={statusField}>{t('Status')}</FieldLabel>
<FieldRow>
<Controller
name='status'
control={control}
render={({ field }) => (
<Select id={statusField} {...field} options={statusOptions} placeholder={t('Select_an_option')} />
)}
/>
</FieldRow>
</Field>
{MaxChatsPerAgent && <MaxChatsPerAgent />}
</FieldGroup>
</form>
</FormProvider>
</ContextualbarScrollableContent>
<ContextualbarFooter>
<ButtonGroup stretch>
<Button type='reset' disabled={!isDirty} onClick={() => reset()}>
{t('Reset')}
</Button>
<Button form={formId} primary type='submit' disabled={!isDirty}>
{t('Save')}
</Button>
</ButtonGroup>
</ContextualbarFooter>
</>
);
};
export default AgentEdit;