您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import { FormContainer } from '@/components/form-container';
  2. import {
  3. Form,
  4. FormControl,
  5. FormField,
  6. FormItem,
  7. FormLabel,
  8. FormMessage,
  9. } from '@/components/ui/form';
  10. import { Input } from '@/components/ui/input';
  11. import { useTranslate } from '@/hooks/common-hooks';
  12. import { zodResolver } from '@hookform/resolvers/zod';
  13. import { ReactNode } from 'react';
  14. import { useForm, useFormContext } from 'react-hook-form';
  15. import { z } from 'zod';
  16. import { initialEmailValues } from '../../constant';
  17. import { useFormValues } from '../../hooks/use-form-values';
  18. import { useWatchFormChange } from '../../hooks/use-watch-form-change';
  19. import { INextOperatorForm } from '../../interface';
  20. import { buildOutputList } from '../../utils/build-output-list';
  21. import { FormWrapper } from '../components/form-wrapper';
  22. import { Output } from '../components/output';
  23. import { PromptEditor } from '../components/prompt-editor';
  24. interface InputFormFieldProps {
  25. name: string;
  26. label: ReactNode;
  27. type?: string;
  28. }
  29. function InputFormField({ name, label, type }: InputFormFieldProps) {
  30. const form = useFormContext();
  31. return (
  32. <FormField
  33. control={form.control}
  34. name={name}
  35. render={({ field }) => (
  36. <FormItem>
  37. <FormLabel>{label}</FormLabel>
  38. <FormControl>
  39. <Input {...field} type={type}></Input>
  40. </FormControl>
  41. <FormMessage />
  42. </FormItem>
  43. )}
  44. />
  45. );
  46. }
  47. function PromptFormField({ name, label }: InputFormFieldProps) {
  48. const form = useFormContext();
  49. return (
  50. <FormField
  51. control={form.control}
  52. name={name}
  53. render={({ field }) => (
  54. <FormItem>
  55. <FormLabel>{label}</FormLabel>
  56. <FormControl>
  57. <PromptEditor
  58. {...field}
  59. showToolbar={false}
  60. multiLine={false}
  61. ></PromptEditor>
  62. </FormControl>
  63. <FormMessage />
  64. </FormItem>
  65. )}
  66. />
  67. );
  68. }
  69. export function EmailFormWidgets() {
  70. const { t } = useTranslate('flow');
  71. return (
  72. <>
  73. <InputFormField
  74. name="smtp_server"
  75. label={t('smtpServer')}
  76. ></InputFormField>
  77. <InputFormField
  78. name="smtp_port"
  79. label={t('smtpPort')}
  80. type="number"
  81. ></InputFormField>
  82. <InputFormField name="email" label={t('senderEmail')}></InputFormField>
  83. <InputFormField
  84. name="password"
  85. label={t('authCode')}
  86. type="password"
  87. ></InputFormField>
  88. <InputFormField
  89. name="sender_name"
  90. label={t('senderName')}
  91. ></InputFormField>
  92. </>
  93. );
  94. }
  95. export const EmailFormPartialSchema = {
  96. smtp_server: z.string(),
  97. smtp_port: z.number(),
  98. email: z.string(),
  99. password: z.string(),
  100. sender_name: z.string(),
  101. };
  102. const FormSchema = z.object({
  103. to_email: z.string(),
  104. cc_email: z.string(),
  105. content: z.string(),
  106. subject: z.string(),
  107. ...EmailFormPartialSchema,
  108. });
  109. const outputList = buildOutputList(initialEmailValues.outputs);
  110. const EmailForm = ({ node }: INextOperatorForm) => {
  111. const { t } = useTranslate('flow');
  112. const defaultValues = useFormValues(initialEmailValues, node);
  113. const form = useForm<z.infer<typeof FormSchema>>({
  114. defaultValues,
  115. resolver: zodResolver(FormSchema),
  116. });
  117. useWatchFormChange(node?.id, form);
  118. return (
  119. <Form {...form}>
  120. <FormWrapper>
  121. <FormContainer>
  122. <PromptFormField
  123. name="to_email"
  124. label={t('toEmail')}
  125. ></PromptFormField>
  126. <PromptFormField
  127. name="cc_email"
  128. label={t('ccEmail')}
  129. ></PromptFormField>
  130. <PromptFormField
  131. name="content"
  132. label={t('content')}
  133. ></PromptFormField>
  134. <PromptFormField
  135. name="subject"
  136. label={t('subject')}
  137. ></PromptFormField>
  138. <EmailFormWidgets></EmailFormWidgets>
  139. </FormContainer>
  140. </FormWrapper>
  141. <div className="p-5">
  142. <Output list={outputList}></Output>
  143. </div>
  144. </Form>
  145. );
  146. };
  147. export default EmailForm;