feat: detect file type

This commit is contained in:
Joel 2024-08-12 18:22:00 +08:00
parent 079c802b5c
commit 20922fde1c
5 changed files with 108 additions and 3 deletions

View File

@ -438,7 +438,23 @@ export const FILE_STRUCT: Var[] = [
},
{
variable: 'type',
type: VarType.number,
type: VarType.string,
},
{
variable: 'extension',
type: VarType.string,
},
{
variable: 'mimetype',
type: VarType.string,
},
{
variable: 'transfer_method',
type: VarType.string,
},
{
variable: 'url',
type: VarType.string,
},
]

View File

@ -28,6 +28,8 @@ export enum ComparisonOperator {
lessThanOrEqual = '≤',
isNull = 'is null',
isNotNull = 'is not null',
in = 'in',
notIn = 'not in',
}
export type Condition = {
@ -49,6 +51,7 @@ export type IfElseNodeType = CommonNodeType & {
logical_operator?: LogicalOperator
conditions?: Condition[]
cases: CaseItem[]
isInIteration: boolean
}
export type HandleAddCondition = (caseId: string, valueSelector: ValueSelector, varItem: Var) => void

View File

@ -1,4 +1,4 @@
import { useCallback } from 'react'
import { useCallback, useMemo } from 'react'
import produce from 'immer'
import { v4 as uuid4 } from 'uuid'
import type {
@ -18,6 +18,7 @@ import {
branchNameCorrect,
getOperators,
} from './utils'
import useIsVarFileAttribute from './use-is-var-file-attribute'
import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud'
import {
useEdgesInteractions,
@ -46,6 +47,23 @@ const useConfig = (id: string, payload: IfElseNodeType) => {
return varPayload.type === VarType.number
}, [])
const {
getIsVarFileAttribute,
} = useIsVarFileAttribute({
nodeId: id,
isInIteration: payload.isInIteration,
})
const varsIsVarFileAttribute = useMemo(() => {
const conditions: Record<string, boolean> = {}
inputs.cases?.forEach((c) => {
c.conditions.forEach((condition) => {
conditions[condition.id] = getIsVarFileAttribute(condition.variable_selector)
})
})
return conditions
}, [inputs.cases, getIsVarFileAttribute])
const {
availableVars: availableNumberVars,
availableNodesWithParent: availableNumberNodesWithParent,

View File

@ -0,0 +1,45 @@
import { useStoreApi } from 'reactflow'
import { useMemo } from 'react'
import { useIsChatMode, useWorkflow, useWorkflowVariables } from '../../hooks'
import type { ValueSelector } from '../../types'
import { VarType } from '../../types'
type Params = {
nodeId: string
isInIteration: boolean
}
const useIsVarFileAttribute = ({
nodeId,
isInIteration,
}: Params) => {
const isChatMode = useIsChatMode()
const store = useStoreApi()
const { getBeforeNodesInSameBranch } = useWorkflow()
const {
getNodes,
} = store.getState()
const currentNode = getNodes().find(n => n.id === nodeId)
const iterationNode = isInIteration ? getNodes().find(n => n.id === currentNode!.parentId) : null
const availableNodes = useMemo(() => {
return getBeforeNodesInSameBranch(nodeId)
}, [getBeforeNodesInSameBranch, nodeId])
const { getCurrentVariableType } = useWorkflowVariables()
const getIsVarFileAttribute = (variable: ValueSelector) => {
if (variable.length !== 3)
return false
const parentVariable = variable.slice(0, 2)
const varType = getCurrentVariableType({
parentNode: iterationNode,
valueSelector: parentVariable,
availableNodes,
isChatMode,
isConstant: false,
})
return varType === VarType.file
}
return {
getIsVarFileAttribute,
}
}
export default useIsVarFileAttribute

View File

@ -18,7 +18,30 @@ export const isComparisonOperatorNeedTranslate = (operator?: ComparisonOperator)
return !notTranslateKey.includes(operator)
}
export const getOperators = (type?: VarType) => {
export const getOperators = (type?: VarType, file?: { key: string }) => {
const isFile = !!file
if (isFile) {
const { key } = file
switch (key) {
case 'name':
return [
ComparisonOperator.contains,
ComparisonOperator.notContains,
ComparisonOperator.startWith,
ComparisonOperator.endWith,
ComparisonOperator.is,
ComparisonOperator.isNot,
ComparisonOperator.empty,
ComparisonOperator.notEmpty,
]
case 'type':
return [
ComparisonOperator.in,
ComparisonOperator.notIn,
]
}
}
switch (type) {
case VarType.string:
return [