/* eslint-disable react-hooks/exhaustive-deps */ import { useCallback, useEffect, useMemo, useState } from 'react' import { Model, ModelCatalog } 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' import { totalRamAtom } from '@/helpers/atoms/SystemBar.atom' type Props = { suitableModel: Model exploreModel: ModelCatalog } const ExploreModelItemHeader: React.FC = ({ suitableModel, exploreModel, }) => { const { downloadModel } = useDownloadModel() const { downloadedModels } = useGetDownloadedModels() const { modelDownloadStateAtom, downloadStates } = useDownloadState() const { getPerformanceForModel } = useGetPerformanceTag() const [title, setTitle] = useState('Recommended') const totalRam = useAtomValue(totalRamAtom) const [performanceTag, setPerformanceTag] = useState( ModelPerformance.PerformancePositive ) const downloadAtom = useMemo( () => atom((get) => get(modelDownloadStateAtom)[suitableModel.name]), [suitableModel.name] ) const downloadState = useAtomValue(downloadAtom) const { setMainViewState } = useMainViewState() const calculatePerformance = useCallback( (suitableModel: Model) => async () => { const { title, performanceTag } = await getPerformanceForModel( suitableModel, totalRam ) setPerformanceTag(performanceTag) setTitle(title) }, [totalRam] ) useEffect(() => { calculatePerformance(suitableModel) }, [suitableModel]) const onDownloadClick = useCallback(() => { downloadModel(suitableModel) // eslint-disable-next-line react-hooks/exhaustive-deps }, [suitableModel]) // TODO: Comparing between Model Id and Version Name? const isDownloaded = downloadedModels.find((model) => model.id === suitableModel.name) != 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