You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

use-watch-form-change.ts 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { CodeTemplateStrMap, ProgrammingLanguage } from '@/constants/agent';
  2. import { settledModelVariableMap } from '@/constants/knowledge';
  3. import { omit } from 'lodash';
  4. import { useCallback, useEffect } from 'react';
  5. import { UseFormReturn } from 'react-hook-form';
  6. import { Operator } from '../constant';
  7. import useGraphStore from '../store';
  8. import { buildCategorizeObjectFromList } from '../utils';
  9. export const useHandleFormValuesChange = (
  10. operatorName: Operator,
  11. id?: string,
  12. form?: UseFormReturn,
  13. ) => {
  14. const updateNodeForm = useGraphStore((state) => state.updateNodeForm);
  15. const handleValuesChange = useCallback(
  16. (changedValues: any, values: any) => {
  17. let nextValues: any = values;
  18. // Fixed the issue that the related form value does not change after selecting the freedom field of the model
  19. if (
  20. Object.keys(changedValues).length === 1 &&
  21. 'parameter' in changedValues &&
  22. changedValues['parameter'] in settledModelVariableMap
  23. ) {
  24. nextValues = {
  25. ...values,
  26. ...settledModelVariableMap[
  27. changedValues['parameter'] as keyof typeof settledModelVariableMap
  28. ],
  29. };
  30. }
  31. if (id) {
  32. updateNodeForm(id, nextValues);
  33. }
  34. },
  35. [updateNodeForm, id],
  36. );
  37. useEffect(() => {
  38. const subscription = form?.watch((value, { name, type, values }) => {
  39. console.log('🚀 ~ subscription ~ value:', value);
  40. if (id && name) {
  41. console.log(
  42. '🚀 ~ useEffect ~ value:',
  43. name,
  44. type,
  45. values,
  46. operatorName,
  47. );
  48. let nextValues: any = value;
  49. // Fixed the issue that the related form value does not change after selecting the freedom field of the model
  50. if (
  51. name === 'parameter' &&
  52. value['parameter'] in settledModelVariableMap
  53. ) {
  54. nextValues = {
  55. ...value,
  56. ...settledModelVariableMap[
  57. value['parameter'] as keyof typeof settledModelVariableMap
  58. ],
  59. };
  60. }
  61. const categoryDescriptionRegex = /items\.\d+\.name/g;
  62. if (
  63. operatorName === Operator.Categorize &&
  64. categoryDescriptionRegex.test(name)
  65. ) {
  66. nextValues = {
  67. ...omit(value, 'items'),
  68. category_description: buildCategorizeObjectFromList(value.items),
  69. };
  70. }
  71. if (
  72. operatorName === Operator.Code &&
  73. type === 'change' &&
  74. name === 'lang'
  75. ) {
  76. nextValues = {
  77. ...value,
  78. script: CodeTemplateStrMap[value.lang as ProgrammingLanguage],
  79. };
  80. }
  81. if (operatorName === Operator.Message) {
  82. }
  83. // Manually triggered form updates are synchronized to the canvas
  84. if (form.formState.isDirty) {
  85. // run(id, nextValues);
  86. updateNodeForm(id, nextValues);
  87. }
  88. }
  89. });
  90. return () => subscription?.unsubscribe();
  91. }, [form, form?.watch, id, operatorName, updateNodeForm]);
  92. return { handleValuesChange };
  93. };