/* eslint-disable react-hooks/exhaustive-deps */ import { useCallback, useEffect, useMemo } from 'react' import { ModelCatalog, ModelVersion } from '@janhq/core/lib/types' import { Badge, Button } from '@janhq/uikit' import { atom, useAtomValue } from 'jotai' import ModalCancelDownload from '@/containers/ModalCancelDownload' import { MainViewState } from '@/constants/screens' import { ModelPerformance, TagType } from '@/constants/tagType' import useDownloadModel from '@/hooks/useDownloadModel' import { useDownloadState } from '@/hooks/useDownloadState' import { useGetDownloadedModels } from '@/hooks/useGetDownloadedModels' import useGetPerformanceTag from '@/hooks/useGetPerformanceTag' import { useMainViewState } from '@/hooks/useMainViewState' import { toGigabytes } from '@/utils/converter' type Props = { suitableModel: ModelVersion exploreModel: ModelCatalog } const ExploreModelItemHeader: React.FC = ({ suitableModel, exploreModel, }) => { const { downloadModel } = useDownloadModel() const { downloadedModels } = useGetDownloadedModels() const { modelDownloadStateAtom, downloadStates } = useDownloadState() const { performanceTag, title, getPerformanceForModel } = useGetPerformanceTag() const downloadAtom = useMemo( () => atom((get) => get(modelDownloadStateAtom)[suitableModel._id]), [suitableModel._id] ) const downloadState = useAtomValue(downloadAtom) const { setMainViewState } = useMainViewState() useEffect(() => { getPerformanceForModel(suitableModel) // eslint-disable-next-line react-hooks/exhaustive-deps }, [suitableModel]) const onDownloadClick = useCallback(() => { downloadModel(exploreModel, suitableModel) // eslint-disable-next-line react-hooks/exhaustive-deps }, [exploreModel, suitableModel]) const isDownloaded = downloadedModels.find((model) => model._id === suitableModel._id) != null let downloadButton = ( ) if (isDownloaded) { downloadButton = ( ) } if (downloadState != null && downloadStates.length > 0) { downloadButton = } const renderBadge = (performance: TagType) => { switch (performance) { case ModelPerformance.PerformancePositive: return {title} case ModelPerformance.PerformanceNeutral: return {title} case ModelPerformance.PerformanceNegative: return {title} default: break } } return (
{exploreModel.name} {performanceTag && renderBadge(performanceTag)}
{downloadButton}
) } export default ExploreModelItemHeader