/*
* 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.
*/
/* eslint-disable @coze-arch/max-line-per-function */
import { useMemo, type MutableRefObject } from 'react';
import { useSize } from 'ahooks';
import { I18n } from '@coze-arch/i18n';
import { type ColumnProps } from '@coze-arch/bot-semi/Table';
import {
Popconfirm,
Radio,
Space,
Spin,
Switch,
Table,
Tooltip,
Typography,
UIButton,
UIIconButton,
} from '@coze-arch/bot-semi';
import { type MockSet, TrafficScene } from '@coze-arch/bot-api/debugger_api';
import { IconAlertCircle } from '@douyinfe/semi-icons';
import {
builtinSuccessCallback,
MockSetEditModal,
} from '@coze-studio/mockset-edit-modal-adapter';
import {
IconAdd,
IconDeleteOutline,
IconEdit,
IconInfo,
} from '@coze-arch/bot-icons';
import { type MockSetSelectProps } from '@coze-agent-ide/plugin-shared';
import { isRealData } from '@coze-agent-ide/bot-plugin-mock-set/util';
import { useMockSetInSettingModalController } from '@coze-agent-ide/bot-plugin-mock-set/hook/use-mock-set-in-setting-modal';
// @ts-expect-error -- linter-disable-autofix
const FormTitle = titleInfo => (
{titleInfo.name}
{titleInfo.required ? (
{' * '}
) : null}
{titleInfo.toolTipText ? (
) : null}
);
const GAP = 200;
const PartMockSet = ({
bindSubjectInfo,
bizCtx,
readonly,
contentRef,
}: MockSetSelectProps & {
contentRef: MutableRefObject;
}) => {
const {
isEnabled,
doEnabled,
doSetCreateModal,
showCreateModal,
doHandleView,
selectedMockSet,
mockSetData,
isSettingLoading,
isListLoading,
doChangeMock,
initialInfo,
doSetDeleteId,
deleteRenderTitle,
doConfirmDelete,
} = useMockSetInSettingModalController({ bindSubjectInfo, bizCtx, readonly });
const size = useSize(contentRef);
const scroll = useMemo(() => ({ y: (size?.height ?? 0) - GAP }), [size]);
const columns: Array> = [
{
title: () => ,
key: 'name',
width: 200,
render: record => (
{record.name}
),
},
{
title: () => ,
key: 'description',
width: 360,
render: record => (
{record.description ? record.description : '-'}
),
},
{
title: () => ,
width: 95,
render: record => {
const isInValid =
!isRealData(record) &&
(record?.schemaIncompatible || !record?.mockRuleQuantity);
const getTooltipInfo = () => {
if (record?.schemaIncompatible) {
return I18n.t('tool_updated_check_mockset_compatibility');
} else if ((record?.mockRuleQuantity || 0) <= 0) {
return I18n.t('mockset_is_empty_add_data_before_use');
}
return '';
};
return isInValid ? (
{
if (selectedMockSet?.id !== record.id) {
doChangeMock(record);
}
}}
/>
) : (
{
if (selectedMockSet?.id !== record.id) {
doChangeMock(record);
}
}}
/>
);
},
},
{
title: () => ,
key: 'action',
width: 60,
render: record => (
doHandleView(record)}
icon={}
type="secondary"
/>
}
trigger="click"
position="bottomRight"
title={deleteRenderTitle}
content={I18n.t('operation_cannot_be_reversed')}
onConfirm={() => doConfirmDelete()}
onCancel={() => doSetDeleteId(undefined)}
>
}
type="secondary"
onClick={() => doSetDeleteId(record.id)}
/>
),
},
];
if (isListLoading) {
return ;
}
return (
<>
MockSet
{!isEnabled && (
{I18n.t('mock_enable_switch')}
)}
{isEnabled ? (
}
/>
}
type="tertiary"
onClick={() => doSetCreateModal(!0)}
>
{I18n.t('binding_add_card')}
) : null}
{showCreateModal ? (
doSetCreateModal(false)}
onSuccess={(info, config) => {
const { id } = info || {};
doSetCreateModal(false);
builtinSuccessCallback(config);
doHandleView({ id }, config);
}}
initialInfo={initialInfo}
needResetPopoverContainer={
bizCtx.trafficScene === TrafficScene.CozeWorkflowDebug
}
/>
) : null}
>
);
};
export { PartMockSet };