From 4d1499ef75087567ef2f465a28c1046469f1aaed Mon Sep 17 00:00:00 2001 From: JzoNg Date: Thu, 9 Apr 2026 19:46:18 +0800 Subject: [PATCH] refactor(web): refactor condition group --- .../evaluation/__tests__/index.spec.tsx | 12 ++-- .../evaluation/__tests__/store.spec.ts | 8 +-- .../condition-group.tsx | 60 ++++--------------- .../index.tsx} | 12 ++-- web/app/components/evaluation/mock.ts | 6 -- web/app/components/evaluation/types.ts | 4 +- web/app/components/evaluation/utils.ts | 3 - web/i18n/en-US/evaluation.json | 3 - web/i18n/zh-Hans/evaluation.json | 6 -- 9 files changed, 29 insertions(+), 85 deletions(-) rename web/app/components/evaluation/components/{ => conditions-section}/condition-group.tsx (82%) rename web/app/components/evaluation/components/{conditions-section.tsx => conditions-section/index.tsx} (81%) diff --git a/web/app/components/evaluation/__tests__/index.spec.tsx b/web/app/components/evaluation/__tests__/index.spec.tsx index 0e7e6b8c91..6257d38f77 100644 --- a/web/app/components/evaluation/__tests__/index.spec.tsx +++ b/web/app/components/evaluation/__tests__/index.spec.tsx @@ -117,13 +117,13 @@ describe('Evaluation', () => { vi.useRealTimers() }) - it('should render time placeholders and hide the value row for empty operators', () => { + it('should hide the value row for empty operators', () => { const resourceType = 'apps' const resourceId = 'app-2' const store = useEvaluationStore.getState() const config = getEvaluationMockConfig(resourceType) - const timeField = config.fieldOptions.find(field => field.type === 'time')! + const stringField = config.fieldOptions.find(field => field.type === 'string')! let groupId = '' let itemId = '' @@ -135,8 +135,8 @@ describe('Evaluation', () => { groupId = group.id itemId = group.items[0].id - store.updateConditionField(resourceType, resourceId, groupId, itemId, timeField.id) - store.updateConditionOperator(resourceType, resourceId, groupId, itemId, 'before') + store.updateConditionField(resourceType, resourceId, groupId, itemId, stringField.id) + store.updateConditionOperator(resourceType, resourceId, groupId, itemId, 'contains') }) let rerender: ReturnType['rerender'] @@ -144,14 +144,14 @@ describe('Evaluation', () => { ({ rerender } = render()) }) - expect(screen.getByText('evaluation.conditions.selectTime')).toBeInTheDocument() + expect(screen.getByPlaceholderText('evaluation.conditions.valuePlaceholder')).toBeInTheDocument() act(() => { store.updateConditionOperator(resourceType, resourceId, groupId, itemId, 'is_empty') rerender() }) - expect(screen.queryByText('evaluation.conditions.selectTime')).not.toBeInTheDocument() + expect(screen.queryByPlaceholderText('evaluation.conditions.valuePlaceholder')).not.toBeInTheDocument() }) it('should render the metric no-node empty state', () => { diff --git a/web/app/components/evaluation/__tests__/store.spec.ts b/web/app/components/evaluation/__tests__/store.spec.ts index 88610c6ccb..ce85c32e65 100644 --- a/web/app/components/evaluation/__tests__/store.spec.ts +++ b/web/app/components/evaluation/__tests__/store.spec.ts @@ -123,7 +123,7 @@ describe('evaluation store', () => { expect(getAllowedOperators(resourceType, booleanField.id)).toEqual(['is', 'is_not']) }) - it('should support time fields and clear values for empty operators', () => { + it('should clear values for empty operators', () => { const resourceType = 'apps' const resourceId = 'app-3' const store = useEvaluationStore.getState() @@ -131,15 +131,15 @@ describe('evaluation store', () => { store.ensureResource(resourceType, resourceId) - const timeField = config.fieldOptions.find(field => field.type === 'time')! + const stringField = config.fieldOptions.find(field => field.type === 'string')! const item = useEvaluationStore.getState().resources['apps:app-3'].conditions[0].items[0] - store.updateConditionField(resourceType, resourceId, useEvaluationStore.getState().resources['apps:app-3'].conditions[0].id, item.id, timeField.id) + store.updateConditionField(resourceType, resourceId, useEvaluationStore.getState().resources['apps:app-3'].conditions[0].id, item.id, stringField.id) store.updateConditionOperator(resourceType, resourceId, useEvaluationStore.getState().resources['apps:app-3'].conditions[0].id, item.id, 'is_empty') const updatedItem = useEvaluationStore.getState().resources['apps:app-3'].conditions[0].items[0] - expect(getAllowedOperators(resourceType, timeField.id)).toEqual(['is', 'before', 'after', 'is_empty', 'is_not_empty']) + expect(getAllowedOperators(resourceType, stringField.id)).toEqual(['contains', 'not_contains', 'is', 'is_not', 'is_empty', 'is_not_empty']) expect(requiresConditionValue('is_empty')).toBe(false) expect(updatedItem.value).toBeNull() }) diff --git a/web/app/components/evaluation/components/condition-group.tsx b/web/app/components/evaluation/components/conditions-section/condition-group.tsx similarity index 82% rename from web/app/components/evaluation/components/condition-group.tsx rename to web/app/components/evaluation/components/conditions-section/condition-group.tsx index a678776054..902194c460 100644 --- a/web/app/components/evaluation/components/condition-group.tsx +++ b/web/app/components/evaluation/components/conditions-section/condition-group.tsx @@ -5,12 +5,10 @@ import type { EvaluationFieldOption, EvaluationResourceProps, JudgmentConditionGroup, -} from '../types' +} from '../../types' import { useTranslation } from 'react-i18next' import Badge from '@/app/components/base/badge' import Button from '@/app/components/base/button' -import DatePicker from '@/app/components/base/date-and-time-picker/date-picker' -import dayjs from '@/app/components/base/date-and-time-picker/utils/dayjs' import Input from '@/app/components/base/input' import { Select, @@ -22,9 +20,9 @@ import { SelectValue, } from '@/app/components/base/ui/select' import { cn } from '@/utils/classnames' -import { getEvaluationMockConfig } from '../mock' -import { getAllowedOperators, requiresConditionValue, useEvaluationStore } from '../store' -import { getFieldTypeIconClassName, getOperatorLabel, groupFieldOptions } from '../utils' +import { getEvaluationMockConfig } from '../../mock' +import { getAllowedOperators, requiresConditionValue, useEvaluationStore } from '../../store' +import { getFieldTypeIconClassName, getOperatorLabel, groupFieldOptions } from '../../utils' type ConditionFieldLabelProps = { field?: EvaluationFieldOption @@ -62,15 +60,15 @@ const ConditionFieldLabel = ({ placeholder, }: ConditionFieldLabelProps) => { if (!field) - return {placeholder} + return {placeholder} return (
-
+
- {field.label} + {field.label}
- {field.type} + {field.type}
) } @@ -89,7 +87,7 @@ const ConditionFieldSelect = ({ {groupFieldOptions(fieldOptions).map(([groupName, fields]) => ( - {groupName} + {groupName} {fields.map(option => (
@@ -116,7 +114,7 @@ const ConditionOperatorSelect = ({ return (