2025-08-05 20:01:33 +03:00
|
|
|
'use client';
|
|
|
|
|
2025-07-15 21:03:50 +03:00
|
|
|
import { Controller, useForm, useWatch } from 'react-hook-form';
|
|
|
|
import { zodResolver } from '@hookform/resolvers/zod';
|
|
|
|
|
|
|
|
import { useAuthSuspense } from '@/features/auth';
|
2025-07-21 15:09:30 +03:00
|
|
|
import { HelpTopic } from '@/features/help';
|
2025-07-15 21:03:50 +03:00
|
|
|
|
|
|
|
import { Checkbox, TextArea, TextInput } from '@/components/input';
|
|
|
|
import { ModalForm } from '@/components/modal';
|
|
|
|
import { useDialogsStore } from '@/stores/dialogs';
|
|
|
|
import { type RO } from '@/utils/meta';
|
|
|
|
|
|
|
|
import { type ICreatePromptTemplateDTO, type IPromptTemplateDTO, schemaCreatePromptTemplate } from '../backend/types';
|
|
|
|
import { useAvailableTemplatesSuspense } from '../backend/use-available-templates';
|
|
|
|
import { useCreatePromptTemplate } from '../backend/use-create-prompt-template';
|
|
|
|
|
|
|
|
export interface DlgCreatePromptTemplateProps {
|
|
|
|
onCreate?: (data: RO<IPromptTemplateDTO>) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function DlgCreatePromptTemplate() {
|
|
|
|
const { onCreate } = useDialogsStore(state => state.props as DlgCreatePromptTemplateProps);
|
|
|
|
const { createPromptTemplate } = useCreatePromptTemplate();
|
|
|
|
const { items: templates } = useAvailableTemplatesSuspense();
|
|
|
|
const { user } = useAuthSuspense();
|
|
|
|
|
2025-07-17 19:04:51 +03:00
|
|
|
const {
|
|
|
|
handleSubmit,
|
|
|
|
control,
|
|
|
|
register,
|
|
|
|
formState: { errors }
|
|
|
|
} = useForm<ICreatePromptTemplateDTO>({
|
2025-07-15 21:03:50 +03:00
|
|
|
resolver: zodResolver(schemaCreatePromptTemplate),
|
|
|
|
defaultValues: {
|
|
|
|
label: '',
|
|
|
|
description: '',
|
|
|
|
text: '',
|
|
|
|
is_shared: false
|
2025-07-17 19:04:51 +03:00
|
|
|
},
|
|
|
|
mode: 'onChange'
|
2025-07-15 21:03:50 +03:00
|
|
|
});
|
|
|
|
const label = useWatch({ control, name: 'label' });
|
2025-07-17 19:04:51 +03:00
|
|
|
const isValid = !!label && !templates.find(template => template.label === label);
|
2025-07-15 21:03:50 +03:00
|
|
|
|
|
|
|
function onSubmit(data: ICreatePromptTemplateDTO) {
|
|
|
|
void createPromptTemplate(data).then(onCreate);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ModalForm
|
|
|
|
header='Создание шаблона'
|
|
|
|
submitText='Создать'
|
|
|
|
canSubmit={isValid}
|
|
|
|
onSubmit={event => void handleSubmit(onSubmit)(event)}
|
|
|
|
submitInvalidTooltip='Введите уникальное название шаблона'
|
|
|
|
className='cc-column w-140 max-h-120 py-2 px-6'
|
2025-07-21 15:09:30 +03:00
|
|
|
helpTopic={HelpTopic.ASSISTANT}
|
2025-07-15 21:03:50 +03:00
|
|
|
>
|
2025-07-17 19:04:51 +03:00
|
|
|
<TextInput id='dlg_prompt_label' {...register('label')} label='Название шаблона' error={errors.label} />
|
|
|
|
<TextArea id='dlg_prompt_description' {...register('description')} label='Описание' error={errors.description} />
|
2025-07-15 21:03:50 +03:00
|
|
|
{user.is_staff ? (
|
|
|
|
<Controller
|
|
|
|
name='is_shared'
|
|
|
|
control={control}
|
|
|
|
render={({ field }) => (
|
|
|
|
<Checkbox
|
|
|
|
id='dlg_prompt_is_shared'
|
|
|
|
label='Общий шаблон'
|
|
|
|
value={field.value}
|
|
|
|
onChange={field.onChange}
|
|
|
|
onBlur={field.onBlur}
|
|
|
|
ref={field.ref}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
) : null}
|
|
|
|
</ModalForm>
|
|
|
|
);
|
|
|
|
}
|