mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-11-12 08:04:02 +00:00
Feat: Display the connection lines between multiple conditions of the conditional operator #3221 (#8218)
### What problem does this PR solve? Feat: Display the connection lines between multiple conditions of the conditional operator #3221 ### Type of change - [x] New Feature (non-breaking change which adds functionality)
This commit is contained in:
parent
7fbbc9650d
commit
bf7f7c7027
@ -16,7 +16,7 @@ import { ISwitchForm } from '@/interfaces/database/flow';
|
|||||||
import { cn } from '@/lib/utils';
|
import { cn } from '@/lib/utils';
|
||||||
import { zodResolver } from '@hookform/resolvers/zod';
|
import { zodResolver } from '@hookform/resolvers/zod';
|
||||||
import { X } from 'lucide-react';
|
import { X } from 'lucide-react';
|
||||||
import { useMemo } from 'react';
|
import { useCallback, useMemo } from 'react';
|
||||||
import { useFieldArray, useForm, useFormContext } from 'react-hook-form';
|
import { useFieldArray, useForm, useFormContext } from 'react-hook-form';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
@ -26,10 +26,7 @@ import {
|
|||||||
SwitchOperatorOptions,
|
SwitchOperatorOptions,
|
||||||
} from '../../constant';
|
} from '../../constant';
|
||||||
import { useBuildFormSelectOptions } from '../../form-hooks';
|
import { useBuildFormSelectOptions } from '../../form-hooks';
|
||||||
import {
|
import { useBuildComponentIdAndBeginOptions } from '../../hooks/use-get-begin-query';
|
||||||
useBuildComponentIdAndBeginOptions,
|
|
||||||
useBuildVariableOptions,
|
|
||||||
} from '../../hooks/use-get-begin-query';
|
|
||||||
import { IOperatorForm } from '../../interface';
|
import { IOperatorForm } from '../../interface';
|
||||||
import { useValues } from './use-values';
|
import { useValues } from './use-values';
|
||||||
|
|
||||||
@ -38,9 +35,39 @@ const ItemKey = 'items';
|
|||||||
|
|
||||||
type ConditionCardsProps = {
|
type ConditionCardsProps = {
|
||||||
name: string;
|
name: string;
|
||||||
|
removeParent(index: number): void;
|
||||||
|
parentIndex: number;
|
||||||
|
parentLength: number;
|
||||||
} & IOperatorForm;
|
} & IOperatorForm;
|
||||||
|
|
||||||
function ConditionCards({ name: parentName, node }: ConditionCardsProps) {
|
function useBuildSwitchOperatorOptions() {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
|
||||||
|
const switchOperatorOptions = useMemo(() => {
|
||||||
|
return SwitchOperatorOptions.map((x) => ({
|
||||||
|
value: x.value,
|
||||||
|
icon: (
|
||||||
|
<IconFont
|
||||||
|
name={x.icon}
|
||||||
|
className={cn('size-4', {
|
||||||
|
'rotate-180': x.value === '>',
|
||||||
|
})}
|
||||||
|
></IconFont>
|
||||||
|
),
|
||||||
|
label: t(`flow.switchOperatorOptions.${x.label}`),
|
||||||
|
}));
|
||||||
|
}, [t]);
|
||||||
|
|
||||||
|
return switchOperatorOptions;
|
||||||
|
}
|
||||||
|
|
||||||
|
function ConditionCards({
|
||||||
|
name: parentName,
|
||||||
|
node,
|
||||||
|
parentIndex,
|
||||||
|
removeParent,
|
||||||
|
parentLength,
|
||||||
|
}: ConditionCardsProps) {
|
||||||
const form = useFormContext();
|
const form = useFormContext();
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
|
||||||
@ -49,18 +76,7 @@ function ConditionCards({ name: parentName, node }: ConditionCardsProps) {
|
|||||||
node?.parentId,
|
node?.parentId,
|
||||||
);
|
);
|
||||||
|
|
||||||
const switchOperatorOptions = useMemo(() => {
|
const switchOperatorOptions = useBuildSwitchOperatorOptions();
|
||||||
return SwitchOperatorOptions.map((x) => ({
|
|
||||||
value: x.value,
|
|
||||||
icon: (
|
|
||||||
<IconFont
|
|
||||||
name={x.icon}
|
|
||||||
className={cn('size-4', { 'rotate-180': x.value === '>' })}
|
|
||||||
></IconFont>
|
|
||||||
),
|
|
||||||
label: t(`flow.switchOperatorOptions.${x.label}`),
|
|
||||||
}));
|
|
||||||
}, [t]);
|
|
||||||
|
|
||||||
const name = `${parentName}.${ItemKey}`;
|
const name = `${parentName}.${ItemKey}`;
|
||||||
|
|
||||||
@ -69,12 +85,30 @@ function ConditionCards({ name: parentName, node }: ConditionCardsProps) {
|
|||||||
control: form.control,
|
control: form.control,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const handleRemove = useCallback(
|
||||||
|
(index: number) => () => {
|
||||||
|
remove(index);
|
||||||
|
if (parentIndex !== 0 && index === 0 && parentLength === 1) {
|
||||||
|
removeParent(parentIndex);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[parentIndex, parentLength, remove, removeParent],
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<section className="flex-1 space-y-2.5">
|
<section className="flex-1 space-y-2.5">
|
||||||
{fields.map((field, index) => {
|
{fields.map((field, index) => {
|
||||||
return (
|
return (
|
||||||
<div key={field.id} className="flex">
|
<div key={field.id} className="flex">
|
||||||
<Card className="bg-transparent border-input-border border flex-1">
|
<Card
|
||||||
|
className={cn(
|
||||||
|
'relative bg-transparent border-input-border border flex-1 ',
|
||||||
|
{
|
||||||
|
'before:w-10 before:absolute before:h-[1px] before:bg-input-border before:top-1/2 before:-left-10':
|
||||||
|
index === 0 || index === fields.length - 1,
|
||||||
|
},
|
||||||
|
)}
|
||||||
|
>
|
||||||
<section className="p-2 bg-background-card flex justify-between items-center">
|
<section className="p-2 bg-background-card flex justify-between items-center">
|
||||||
<FormField
|
<FormField
|
||||||
control={form.control}
|
control={form.control}
|
||||||
@ -129,7 +163,7 @@ function ConditionCards({ name: parentName, node }: ConditionCardsProps) {
|
|||||||
/>
|
/>
|
||||||
</CardContent>
|
</CardContent>
|
||||||
</Card>
|
</Card>
|
||||||
<Button variant={'ghost'} onClick={() => remove(index)}>
|
<Button variant={'ghost'} onClick={handleRemove(index)}>
|
||||||
<X />
|
<X />
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
@ -150,6 +184,7 @@ function ConditionCards({ name: parentName, node }: ConditionCardsProps) {
|
|||||||
const SwitchForm = ({ node }: IOperatorForm) => {
|
const SwitchForm = ({ node }: IOperatorForm) => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const values = useValues();
|
const values = useValues();
|
||||||
|
const switchOperatorOptions = useBuildSwitchOperatorOptions();
|
||||||
|
|
||||||
const FormSchema = z.object({
|
const FormSchema = z.object({
|
||||||
conditions: z.array(
|
conditions: z.array(
|
||||||
@ -199,8 +234,6 @@ const SwitchForm = ({ node }: IOperatorForm) => {
|
|||||||
}));
|
}));
|
||||||
}, [t]);
|
}, [t]);
|
||||||
|
|
||||||
const componentIdOptions = useBuildVariableOptions(node?.id, node?.parentId);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Form {...form}>
|
<Form {...form}>
|
||||||
<form
|
<form
|
||||||
@ -212,26 +245,33 @@ const SwitchForm = ({ node }: IOperatorForm) => {
|
|||||||
{fields.map((field, index) => {
|
{fields.map((field, index) => {
|
||||||
return (
|
return (
|
||||||
<FormContainer key={field.id} className="">
|
<FormContainer key={field.id} className="">
|
||||||
<div>IF</div>
|
<div>{index === 0 ? 'IF' : 'ELSEIF'}</div>
|
||||||
<section className="flex items-center gap-2 !mt-2">
|
<section className="flex gap-2 !mt-2 relative">
|
||||||
<FormField
|
<section className="flex flex-col">
|
||||||
control={form.control}
|
<div className="relative w-1 flex-1 before:absolute before:w-[1px] before:bg-input-border before:top-20 before:bottom-0 before:left-10"></div>
|
||||||
name={`${ConditionKey}.${index}.logical_operator`}
|
<FormField
|
||||||
render={({ field }) => (
|
control={form.control}
|
||||||
<FormItem>
|
name={`${ConditionKey}.${index}.logical_operator`}
|
||||||
<FormControl>
|
render={({ field }) => (
|
||||||
<RAGFlowSelect
|
<FormItem>
|
||||||
{...field}
|
<FormControl>
|
||||||
options={switchLogicOperatorOptions}
|
<RAGFlowSelect
|
||||||
triggerClassName="w-18"
|
{...field}
|
||||||
/>
|
options={switchLogicOperatorOptions}
|
||||||
</FormControl>
|
triggerClassName="w-18"
|
||||||
<FormMessage />
|
/>
|
||||||
</FormItem>
|
</FormControl>
|
||||||
)}
|
<FormMessage />
|
||||||
/>
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
<div className="relative w-1 flex-1 before:absolute before:w-[1px] before:bg-input-border before:top-0 before:bottom-36 before:left-10"></div>
|
||||||
|
</section>
|
||||||
<ConditionCards
|
<ConditionCards
|
||||||
name={`${ConditionKey}.${index}`}
|
name={`${ConditionKey}.${index}`}
|
||||||
|
removeParent={remove}
|
||||||
|
parentIndex={index}
|
||||||
|
parentLength={fields.length}
|
||||||
></ConditionCards>
|
></ConditionCards>
|
||||||
</section>
|
</section>
|
||||||
</FormContainer>
|
</FormContainer>
|
||||||
@ -239,7 +279,14 @@ const SwitchForm = ({ node }: IOperatorForm) => {
|
|||||||
})}
|
})}
|
||||||
<BlockButton
|
<BlockButton
|
||||||
onClick={() =>
|
onClick={() =>
|
||||||
append({ logical_operator: SwitchLogicOperatorOptions[0] })
|
append({
|
||||||
|
logical_operator: SwitchLogicOperatorOptions[0],
|
||||||
|
[ItemKey]: [
|
||||||
|
{
|
||||||
|
operator: switchOperatorOptions[0].value,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
add
|
add
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user