feat: order by

This commit is contained in:
Joel 2024-08-01 11:40:55 +08:00
parent f1b61861b6
commit 573f653789
6 changed files with 98 additions and 27 deletions

View File

@ -71,7 +71,8 @@ const LimitConfig: FC<Props> = ({
/> />
} }
> >
{payload && ( {payload?.enabled
? (
<div className='flex justify-between items-center h-8 space-x-2'> <div className='flex justify-between items-center h-8 space-x-2'>
<input <input
value={(payload?.size || LIMIT_SIZE_DEFAULT) as number} value={(payload?.size || LIMIT_SIZE_DEFAULT) as number}
@ -94,7 +95,8 @@ const LimitConfig: FC<Props> = ({
disabled={readonly || !payload?.enabled} disabled={readonly || !payload?.enabled}
/> />
</div> </div>
)} )
: null}
</Field> </Field>
</div> </div>
) )

View File

@ -1,12 +1,21 @@
import { BlockEnum } from '../../types' import { BlockEnum } from '../../types'
import type { NodeDefault } from '../../types' import type { NodeDefault } from '../../types'
import { type ListFilterNodeType } from './types' import { type ListFilterNodeType, OrderBy } from './types'
import { ALL_CHAT_AVAILABLE_BLOCKS, ALL_COMPLETION_AVAILABLE_BLOCKS } from '@/app/components/workflow/constants' import { ALL_CHAT_AVAILABLE_BLOCKS, ALL_COMPLETION_AVAILABLE_BLOCKS } from '@/app/components/workflow/constants'
const i18nPrefix = 'workflow.errorMsg' const i18nPrefix = 'workflow.errorMsg'
const nodeDefault: NodeDefault<ListFilterNodeType> = { const nodeDefault: NodeDefault<ListFilterNodeType> = {
defaultValue: { defaultValue: {
variable: [], variable: [],
orderBy: {
enabled: false,
key: '',
value: OrderBy.ASC,
},
limit: {
enabled: false,
size: 10,
},
}, },
getAvailablePrevNodes(isChatMode: boolean) { getAvailablePrevNodes(isChatMode: boolean) {
const nodes = isChatMode const nodes = isChatMode

View File

@ -3,11 +3,13 @@ import React from 'react'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import VarReferencePicker from '../_base/components/variable/var-reference-picker' import VarReferencePicker from '../_base/components/variable/var-reference-picker'
import OutputVars, { VarItem } from '../_base/components/output-vars' import OutputVars, { VarItem } from '../_base/components/output-vars'
import OptionCard from '../_base/components/option-card'
import useConfig from './use-config' import useConfig from './use-config'
import type { ListFilterNodeType } from './types' import { type ListFilterNodeType, OrderBy } from './types'
import LimitConfig from './components/limit-config' import LimitConfig from './components/limit-config'
import Field from '@/app/components/workflow/nodes/_base/components/field' import Field from '@/app/components/workflow/nodes/_base/components/field'
import { type NodePanelProps } from '@/app/components/workflow/types' import { type NodePanelProps } from '@/app/components/workflow/types'
import Switch from '@/app/components/base/switch'
const i18nPrefix = 'workflow.nodes.listFilter' const i18nPrefix = 'workflow.nodes.listFilter'
@ -23,6 +25,8 @@ const Panel: FC<NodePanelProps<ListFilterNodeType>> = ({
handleVarChanges, handleVarChanges,
filterVar, filterVar,
handleLimitChange, handleLimitChange,
handleOrderByEnabledChange,
handleOrderByTypeChange,
} = useConfig(id, data) } = useConfig(id, data)
return ( return (
@ -41,6 +45,38 @@ const Panel: FC<NodePanelProps<ListFilterNodeType>> = ({
/> />
</Field> </Field>
<Field
title={t(`${i18nPrefix}.orderBy`)}
operations={
<Switch
defaultValue={inputs.orderBy?.enabled}
onChange={handleOrderByEnabledChange}
size='md'
disabled={readOnly}
/>
}
>
{inputs.orderBy?.enabled
? (
<div className='flex items-center justify-between'>
<div className='grow'>Variable Picker placeholder</div>
<div className='shrink-0 flex space-x-1'>
<OptionCard
title={t(`${i18nPrefix}.asc`)}
onSelect={handleOrderByTypeChange(OrderBy.ASC)}
selected={inputs.orderBy.value === OrderBy.ASC}
/>
<OptionCard
title={t(`${i18nPrefix}.desc`)}
onSelect={handleOrderByTypeChange(OrderBy.DESC)}
selected={inputs.orderBy.value === OrderBy.DESC}
/>
</div>
</div>
)
: null}
</Field>
<LimitConfig <LimitConfig
config={inputs.limit} config={inputs.limit}
onChange={handleLimitChange} onChange={handleLimitChange}

View File

@ -2,7 +2,7 @@ import { useCallback } from 'react'
import produce from 'immer' import produce from 'immer'
import type { ValueSelector, Var } from '../../types' import type { ValueSelector, Var } from '../../types'
import { VarType } from '../../types' import { VarType } from '../../types'
import type { Limit, ListFilterNodeType } from './types' import type { Limit, ListFilterNodeType, OrderBy } from './types'
import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud' import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud'
import { import {
useNodesReadOnly, useNodesReadOnly,
@ -31,12 +31,30 @@ const useConfig = (id: string, payload: ListFilterNodeType) => {
setInputs(newInputs) setInputs(newInputs)
}, [inputs, setInputs]) }, [inputs, setInputs])
const handleOrderByEnabledChange = useCallback((enabled: boolean) => {
const newInputs = produce(inputs, (draft) => {
draft.orderBy.enabled = enabled
})
setInputs(newInputs)
}, [inputs, setInputs])
const handleOrderByTypeChange = useCallback((type: OrderBy) => {
return () => {
const newInputs = produce(inputs, (draft) => {
draft.orderBy.value = type
})
setInputs(newInputs)
}
}, [inputs, setInputs])
return { return {
readOnly, readOnly,
inputs, inputs,
filterVar, filterVar,
handleVarChanges, handleVarChanges,
handleLimitChange, handleLimitChange,
handleOrderByEnabledChange,
handleOrderByTypeChange,
} }
} }

View File

@ -502,6 +502,9 @@ const translation = {
listFilter: { listFilter: {
inputVar: 'Input Variable', inputVar: 'Input Variable',
limit: 'Limit', limit: 'Limit',
orderBy: 'Order by',
asc: 'ASC',
desc: 'DESC',
outputVars: { outputVars: {
result: 'Filter result', result: 'Filter result',
first_record: 'First record', first_record: 'First record',

View File

@ -502,6 +502,9 @@ const translation = {
listFilter: { listFilter: {
inputVar: '输入变量', inputVar: '输入变量',
limit: '限制', limit: '限制',
orderBy: '排序',
asc: '升序',
desc: '降序',
outputVars: { outputVars: {
result: '过滤结果', result: '过滤结果',
first_record: '第一条记录', first_record: '第一条记录',