mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-08 20:42:30 +08:00
### What problem does this PR solve? Feat: Allow users to enter text in the middle of a chat #3221 ### Type of change - [x] New Feature (non-breaking change which adds functionality)
32 lines
923 B
TypeScript
32 lines
923 B
TypeScript
import { omit } from 'lodash';
|
|
import { useEffect } from 'react';
|
|
import { UseFormReturn, useWatch } from 'react-hook-form';
|
|
import { BeginQuery } from '../../interface';
|
|
import useGraphStore from '../../store';
|
|
|
|
function transferInputsArrayToObject(inputs: BeginQuery[] = []) {
|
|
return inputs.reduce<Record<string, Omit<BeginQuery, 'key'>>>((pre, cur) => {
|
|
pre[cur.key] = omit(cur, 'key');
|
|
|
|
return pre;
|
|
}, {});
|
|
}
|
|
|
|
export function useWatchFormChange(id?: string, form?: UseFormReturn) {
|
|
let values = useWatch({ control: form?.control });
|
|
const updateNodeForm = useGraphStore((state) => state.updateNodeForm);
|
|
|
|
useEffect(() => {
|
|
if (id) {
|
|
values = form?.getValues() || {};
|
|
|
|
const nextValues = {
|
|
...values,
|
|
inputs: transferInputsArrayToObject(values.inputs),
|
|
};
|
|
|
|
updateNodeForm(id, nextValues);
|
|
}
|
|
}, [form?.formState.isDirty, id, updateNodeForm, values]);
|
|
}
|