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,30 +71,32 @@ const LimitConfig: FC<Props> = ({
/>
}
>
{payload && (
<div className='flex justify-between items-center h-8 space-x-2'>
<input
value={(payload?.size || LIMIT_SIZE_DEFAULT) as number}
className='shrink-0 block pl-3 w-12 h-8 appearance-none outline-none rounded-lg bg-gray-100 text-[13px] text-gra-900'
type='number'
min={LIMIT_SIZE_MIN}
max={LIMIT_SIZE_MAX}
step={1}
onChange={e => handleLimitSizeChange(e.target.value)}
onBlur={handleBlur}
disabled={readonly || !payload?.enabled}
/>
<Slider
className='grow'
value={(payload?.size || LIMIT_SIZE_DEFAULT) as number}
min={LIMIT_SIZE_MIN}
max={LIMIT_SIZE_MAX}
step={1}
onChange={handleLimitSizeChange}
disabled={readonly || !payload?.enabled}
/>
</div>
)}
{payload?.enabled
? (
<div className='flex justify-between items-center h-8 space-x-2'>
<input
value={(payload?.size || LIMIT_SIZE_DEFAULT) as number}
className='shrink-0 block pl-3 w-12 h-8 appearance-none outline-none rounded-lg bg-gray-100 text-[13px] text-gra-900'
type='number'
min={LIMIT_SIZE_MIN}
max={LIMIT_SIZE_MAX}
step={1}
onChange={e => handleLimitSizeChange(e.target.value)}
onBlur={handleBlur}
disabled={readonly || !payload?.enabled}
/>
<Slider
className='grow'
value={(payload?.size || LIMIT_SIZE_DEFAULT) as number}
min={LIMIT_SIZE_MIN}
max={LIMIT_SIZE_MAX}
step={1}
onChange={handleLimitSizeChange}
disabled={readonly || !payload?.enabled}
/>
</div>
)
: null}
</Field>
</div>
)

View File

@ -1,12 +1,21 @@
import { BlockEnum } 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'
const i18nPrefix = 'workflow.errorMsg'
const nodeDefault: NodeDefault<ListFilterNodeType> = {
defaultValue: {
variable: [],
orderBy: {
enabled: false,
key: '',
value: OrderBy.ASC,
},
limit: {
enabled: false,
size: 10,
},
},
getAvailablePrevNodes(isChatMode: boolean) {
const nodes = isChatMode

View File

@ -3,11 +3,13 @@ import React from 'react'
import { useTranslation } from 'react-i18next'
import VarReferencePicker from '../_base/components/variable/var-reference-picker'
import OutputVars, { VarItem } from '../_base/components/output-vars'
import OptionCard from '../_base/components/option-card'
import useConfig from './use-config'
import type { ListFilterNodeType } from './types'
import { type ListFilterNodeType, OrderBy } from './types'
import LimitConfig from './components/limit-config'
import Field from '@/app/components/workflow/nodes/_base/components/field'
import { type NodePanelProps } from '@/app/components/workflow/types'
import Switch from '@/app/components/base/switch'
const i18nPrefix = 'workflow.nodes.listFilter'
@ -23,6 +25,8 @@ const Panel: FC<NodePanelProps<ListFilterNodeType>> = ({
handleVarChanges,
filterVar,
handleLimitChange,
handleOrderByEnabledChange,
handleOrderByTypeChange,
} = useConfig(id, data)
return (
@ -41,6 +45,38 @@ const Panel: FC<NodePanelProps<ListFilterNodeType>> = ({
/>
</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
config={inputs.limit}
onChange={handleLimitChange}

View File

@ -2,7 +2,7 @@ import { useCallback } from 'react'
import produce from 'immer'
import type { ValueSelector, Var } 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 {
useNodesReadOnly,
@ -31,12 +31,30 @@ const useConfig = (id: string, payload: ListFilterNodeType) => {
setInputs(newInputs)
}, [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 {
readOnly,
inputs,
filterVar,
handleVarChanges,
handleLimitChange,
handleOrderByEnabledChange,
handleOrderByTypeChange,
}
}

View File

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

View File

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