/* * Copyright 2025 coze-dev Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { type FC, createContext, useContext, useEffect } from 'react'; import { I18n } from '@coze-arch/i18n'; import { Input, Select } from '@coze-arch/coze-design'; import { type OutputTypeInfo } from '@coze-arch/bot-api/connector_api'; import { getIsNumberOutput, getIsTextOutput } from '../../validate/utils'; import { type BaseOutputStructLineType } from '../../types'; import type { ConfigStoreState } from '../../store'; import { useConfigStoreGuarded } from '../../context/store-context'; import { BigCheckbox } from '../../big-checkbox'; import { useRequireVerify } from './use-require-verify'; import { RequiredWarn } from './required-warn'; export const OutputLineCommonContext = createContext<{ onChange?: (val: BaseOutputStructLineType) => void; list?: OutputTypeInfo[]; getShowRequireWarn?: (val: BaseOutputStructLineType) => OutputStructVerifyRes; onToggleError?: (id: string, error: boolean) => void; }>({}); export interface OutputStructVerifyRes { groupByKey: { warn: boolean; tip?: string; }; primary: { warn: boolean; tip?: string; }; } const getOutputFieldConfig = (storeState: ConfigStoreState, id: string) => { const data = storeState.config?.output_sub_component.item_list?.find( item => item._id === id, ); if (!data) { throw new Error(`cannot find data of ${id}`); } return data; }; export const BaseOutputStructLine: FC<{ data: BaseOutputStructLineType; // eslint-disable-next-line @coze-arch/max-line-per-function -- / }> = ({ data: { _id: id } }) => { const { list, onChange, getShowRequireWarn, onToggleError } = useContext( OutputLineCommonContext, ); const store = useConfigStoreGuarded(); const data = useConfigStoreGuarded()(state => getOutputFieldConfig(state, id), ); if (!data) { throw new Error(`cannot find data of ${id}`); } if (!list || !onChange || !getShowRequireWarn || !onToggleError) { throw new Error('impossible context member miss'); } const { groupByKey: groupByKeyRequire, primary: primaryRequire } = getShowRequireWarn(data); const getVal = () => getOutputFieldConfig(store.getState(), id); const keyRequire = useRequireVerify({ getVal, verify: config => !!config?.key, onChange: isError => onToggleError(`${id}#key`, isError), }); const typeRequire = useRequireVerify({ getVal, verify: config => Number.isInteger(config.output_type), onChange: isError => onToggleError(`${id}$type`, isError), }); useEffect(() => { const hasError = groupByKeyRequire.warn || primaryRequire.warn; onToggleError(data._id, hasError); }, [groupByKeyRequire.warn, primaryRequire.warn]); return ( <>
{ onChange({ ...data, key: val, }); }} /> {keyRequire.showWarn ? : null}