'use client' import Badge from '@/app/components/base/badge' import Tooltip from '@/app/components/base/tooltip' import PluginVersionPicker from '@/app/components/plugins/update-plugin/plugin-version-picker' import { RiArrowLeftRightLine, RiExternalLinkLine } from '@remixicon/react' import type { ReactNode } from 'react' import { type FC, useCallback, useState } from 'react' import { useBoolean } from 'ahooks' import { useCheckInstalled, useUpdatePackageFromMarketPlace } from '@/service/use-plugins' import cn from '@/utils/classnames' import PluginMutationModel from '@/app/components/plugins/plugin-mutation-model' import useGetIcon from '@/app/components/plugins/install-plugin/base/use-get-icon' import { pluginManifestToCardPluginProps } from '@/app/components/plugins/install-plugin/utils' import { Badge as Badge2, BadgeState } from '@/app/components/base/badge/index' import Link from 'next/link' import { useTranslation } from 'react-i18next' import { getMarketplaceUrl } from '@/utils/var' export type SwitchPluginVersionProps = { uniqueIdentifier: string tooltip?: ReactNode onChange?: (version: string) => void className?: string } export const SwitchPluginVersion: FC = (props) => { const { uniqueIdentifier, tooltip, onChange, className } = props const [pluginId] = uniqueIdentifier?.split(':') || [''] const [isShow, setIsShow] = useState(false) const [isShowUpdateModal, { setTrue: showUpdateModal, setFalse: hideUpdateModal }] = useBoolean(false) const [target, setTarget] = useState<{ version: string, pluginUniqueIden: string; }>() const pluginDetails = useCheckInstalled({ pluginIds: [pluginId], enabled: true, }) const pluginDetail = pluginDetails.data?.plugins.at(0) const handleUpdatedFromMarketplace = useCallback(() => { hideUpdateModal() pluginDetails.refetch() onChange?.(target!.version) }, [hideUpdateModal, onChange, pluginDetails, target]) const { getIconUrl } = useGetIcon() const icon = pluginDetail?.declaration.icon ? getIconUrl(pluginDetail.declaration.icon) : undefined const mutation = useUpdatePackageFromMarketPlace() const install = () => { mutation.mutate( { new_plugin_unique_identifier: target!.pluginUniqueIden, original_plugin_unique_identifier: uniqueIdentifier, }, { onSuccess() { handleUpdatedFromMarketplace() }, }) } const { t } = useTranslation() // Guard against null/undefined uniqueIdentifier to prevent app crash if (!uniqueIdentifier || !pluginId) return null return
e.stopPropagation()}> {isShowUpdateModal && pluginDetail && {`${pluginDetail.version} -> ${target!.version}`} } modalBottomLeft={ {t('workflow.nodes.agent.installPlugin.changelog')} } />} {pluginDetail && { setTarget({ pluginUniqueIden: state.unique_identifier, version: state.version, }) showUpdateModal() }} trigger={
{pluginDetail.version}
} hasRedCornerMark={true} /> } />}
}