'use client' import type { FC } from 'react' import React, { useEffect, useState } from 'react' import { useTranslation } from 'react-i18next' import { useDebounce, useGetState } from 'ahooks' import cn from 'classnames' import produce from 'immer' import { LinkExternal02, Settings01 } from '../../base/icons/src/vender/line/general' import type { Credential, CustomCollectionBackend, CustomParamSchema, Emoji } from '../types' import { AuthHeaderPrefix, AuthType } from '../types' import GetSchema from './get-schema' import ConfigCredentials from './config-credentials' import TestApi from './test-api' import Drawer from '@/app/components/base/drawer-plus' import Button from '@/app/components/base/button' import EmojiPicker from '@/app/components/base/emoji-picker' import AppIcon from '@/app/components/base/app-icon' import { parseParamsSchema } from '@/service/tools' const fieldNameClassNames = 'py-2 leading-5 text-sm font-medium text-gray-900' type Props = { payload: any onHide: () => void onAdd?: (payload: CustomCollectionBackend) => void onRemove?: () => void onEdit?: (payload: CustomCollectionBackend) => void } // Add and Edit const EditCustomCollectionModal: FC = ({ payload, onHide, onAdd, onEdit, onRemove, }) => { const { t } = useTranslation() const isAdd = !payload const isEdit = !!payload const [editFirst, setEditFirst] = useState(!isAdd) const [paramsSchemas, setParamsSchemas] = useState(payload?.tools || []) const [customCollection, setCustomCollection, getCustomCollection] = useGetState(isAdd ? { provider: '', credentials: { auth_type: AuthType.none, api_key_header: 'Authorization', api_key_header_prefix: AuthHeaderPrefix.basic, }, icon: { content: '🕵️', background: '#FEF7C3', }, schema_type: '', schema: '', } : payload) const originalProvider = isEdit ? payload.provider : '' const [showEmojiPicker, setShowEmojiPicker] = useState(false) const emoji = customCollection.icon const setEmoji = (emoji: Emoji) => { const newCollection = produce(customCollection, (draft) => { draft.icon = emoji }) setCustomCollection(newCollection) } const schema = customCollection.schema const debouncedSchema = useDebounce(schema, { wait: 500 }) const setSchema = (schema: string) => { const newCollection = produce(customCollection, (draft) => { draft.schema = schema }) setCustomCollection(newCollection) } useEffect(() => { if (!debouncedSchema) return if (isEdit && editFirst) { setEditFirst(false) return } (async () => { const customCollection = getCustomCollection() try { const { parameters_schema, schema_type } = await parseParamsSchema(debouncedSchema) const newCollection = produce(customCollection, (draft) => { draft.schema_type = schema_type }) setCustomCollection(newCollection) setParamsSchemas(parameters_schema) } catch (e) { const newCollection = produce(customCollection, (draft) => { draft.schema_type = '' }) setCustomCollection(newCollection) setParamsSchemas([]) } })() }, [debouncedSchema]) const [credentialsModalShow, setCredentialsModalShow] = useState(false) const credential = customCollection.credentials const setCredential = (credential: Credential) => { const newCollection = produce(customCollection, (draft) => { draft.credentials = credential }) setCustomCollection(newCollection) } const [currTool, setCurrTool] = useState(null) const [isShowTestApi, setIsShowTestApi] = useState(false) const handleSave = () => { // const postData = clone(customCollection) const postData = produce(customCollection, (draft) => { delete draft.tools if (draft.credentials.auth_type === AuthType.none) { delete draft.credentials.api_key_header delete draft.credentials.api_key_header_prefix delete draft.credentials.api_key_value } }) if (isAdd) { onAdd?.(postData) return } onEdit?.({ ...postData, original_provider: originalProvider, }) } const getPath = (url: string) => { if (!url) return '' try { const path = new URL(url).pathname return path || '' } catch (e) { return url } } return ( <>
{t('tools.createTool.name')}
{ setShowEmojiPicker(true) }} className='cursor-pointer' icon={emoji.content} background={emoji.background} /> { const newCollection = produce(customCollection, (draft) => { draft.provider = e.target.value }) setCustomCollection(newCollection) }} />
{/* Schema */}
{t('tools.createTool.schema')}
{t('tools.createTool.viewSchemaSpec')}
{/* Available Tools */}
{t('tools.createTool.availableTools.title')}
0 && 'border-b', 'border-gray-200')}> {paramsSchemas.map((item, index) => ( ))}
{t('tools.createTool.availableTools.name')} {t('tools.createTool.availableTools.description')} {t('tools.createTool.availableTools.method')} {t('tools.createTool.availableTools.path')} {t('tools.createTool.availableTools.action')}
{item.operation_id} {item.summary} {item.method} {getPath(item.server_url)}
{/* Authorization method */}
{t('tools.createTool.authMethod.title')}
setCredentialsModalShow(true)}>
{t(`tools.createTool.authMethod.types.${credential.auth_type}`)}
{t('tools.createTool.privacyPolicy')}
{ const newCollection = produce(customCollection, (draft) => { draft.privacy_policy = e.target.value }) setCustomCollection(newCollection) }} className='w-full h-10 px-3 text-sm font-normal bg-gray-100 rounded-lg grow' placeholder={t('tools.createTool.privacyPolicyPlaceholder') || ''} />
{t('tools.createTool.customDisclaimer')}
{ const newCollection = produce(customCollection, (draft) => { draft.custom_disclaimer = e.target.value }) setCustomCollection(newCollection) }} className='w-full h-10 px-3 text-sm font-normal bg-gray-100 rounded-lg grow' placeholder={t('tools.createTool.customDisclaimerPlaceholder') || ''} />
{ isEdit && ( ) }
} isShowMask={true} clickOutsideNotOpen={true} /> {showEmojiPicker && { setEmoji({ content: icon, background: icon_background }) setShowEmojiPicker(false) }} onClose={() => { setShowEmojiPicker(false) }} />} {credentialsModalShow && ( setCredentialsModalShow(false)} />) } {isShowTestApi && ( setIsShowTestApi(false)} /> )} ) } export default React.memo(EditCustomCollectionModal)