import MyEmpty from '@/components/common/MyEmpty'; import { IResource } from '@/types/app'; import { DeleteOutlined, PlusOutlined } from '@ant-design/icons'; import { Button, Popconfirm, Select, Typography } from 'antd'; import classNames from 'classnames'; import { concat } from 'lodash'; import React, { useEffect, useRef, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { v4 as uuid } from 'uuid'; import { resourceTypeIcon } from '../../config'; import ResourceContentV2 from './ResourceContentV2'; interface ResourceTabProps extends IResource { uid?: string; icon?: string; initVal?: any; name?: string; } const ResourcesCardV2: React.FC<{ name: string; updateData: (data: any) => void; initValue?: any; resourceTypeOptions: Record[]; }> = ({ name, updateData, resourceTypeOptions, initValue }) => { const { t } = useTranslation(); const resources = useRef(initValue || []); const [curIcon, setCurIcon] = useState<{ uid: string; icon: string }>({ uid: '', icon: '', }); // Track the last update to avoid circular updates const lastUpdateRef = useRef(''); // Initialize resource tabs const [resourcesTabs, setResourcesTabs] = useState( initValue?.map((item: any) => { return { ...item, icon: item.type, initVal: item, }; }) || [], ); const [filterResourcesTabs, setFilterResourcesTabs] = useState([...resourcesTabs]); const [activeKey, setActiveKey] = useState(resourcesTabs?.[0]?.uid || ''); const [hoverKey, setHoverKey] = useState(''); // Delete resource const remove = (e: React.MouseEvent, item: ResourceTabProps) => { e?.stopPropagation(); const findActiveIndex = resourcesTabs.findIndex(i => i.uid === activeKey); const filteredResources = resourcesTabs?.filter(i => i.uid !== item.uid); resources.current = resources.current.filter(i => i.uid !== item.uid) || []; // Send the updated resource data to the parent component const newData = [name, resources.current]; const newUpdateStr = JSON.stringify(newData); if (newUpdateStr !== lastUpdateRef.current) { lastUpdateRef.current = newUpdateStr; updateData(newData); } setResourcesTabs(filteredResources); // Handle the selected tab after deletion if (item.uid === activeKey) { if (findActiveIndex === resourcesTabs.length - 1 && findActiveIndex !== 0) { // If the last tab is deleted, select the new last tab setActiveKey(filteredResources?.[filteredResources.length - 1]?.uid || ''); } else { // Otherwise, select the same position or the previous tab const newActiveIndex = Math.min(findActiveIndex, filteredResources.length - 1); setActiveKey(filteredResources?.[newActiveIndex]?.uid || ''); } } }; const addSource = () => { const uid = uuid(); const defaultResourceType = resourceTypeOptions?.filter(item => item.value !== 'all')?.[0]?.value || ''; const newResource = { is_dynamic: false, type: defaultResourceType, value: '', uid, name: t('resource') + ` ${resources.current.length + 1}`, }; resources.current = concat(resources.current, [newResource]); // Send the updated resource data to the parent component const newData = [name, resources.current]; const newUpdateStr = JSON.stringify(newData); if (newUpdateStr !== lastUpdateRef.current) { lastUpdateRef.current = newUpdateStr; updateData(newData); } setResourcesTabs(prev => [ ...prev, { icon: defaultResourceType, type: defaultResourceType, uid, initVal: newResource, name: t('resource') + ` ${prev.length + 1}`, }, ]); setActiveKey(uid); setCurIcon({ uid, icon: defaultResourceType, }); }; // Update the icon of the resource tab useEffect(() => { if (curIcon.uid && curIcon.icon) { setResourcesTabs( resourcesTabs.map(item => { if (curIcon?.uid === item.uid) { return { ...item, icon: curIcon.icon, type: curIcon.icon, }; } return item; }), ); } }, [curIcon]); // Update the filtered resource tabs useEffect(() => { setFilterResourcesTabs([...resourcesTabs]); }, [resourcesTabs]); return (
{/* Resource list on the left */}
{/* Resource type filter */}