import React, { useCallback, useMemo } from 'react' import Image from 'next/image' import { Button, Progress } from '@janhq/joi' import { useAtomValue, useSetAtom } from 'jotai' import { toaster } from '@/containers/Toast' import useAbortDownload from '@/hooks/useAbortDownload' import useAssistantQuery from '@/hooks/useAssistantQuery' import { downloadStateListAtom } from '@/hooks/useDownloadState' import useModelDownloadMutation from '@/hooks/useModelDownloadMutation' import { QuickStartModel } from '@/hooks/useModelHub' import useThreadCreateMutation from '@/hooks/useThreadCreateMutation' import { formatDownloadPercentage, toGibibytes } from '@/utils/converter' import { downloadProgress } from '@/utils/download' import { MainViewState, mainViewStateAtom } from '@/helpers/atoms/App.atom' import { localModelModalStageAtom } from '@/helpers/atoms/DownloadLocalModel.atom' import { downloadedModelsAtom } from '@/helpers/atoms/Model.atom' type Props = { model: QuickStartModel } const SliderItem: React.FC = ({ model }) => { const url = new URL(model.url) const pathArray = url.pathname.split('/').filter((segment) => segment !== '') const owner = pathArray[0] const repo = pathArray[1] const fileName = pathArray[pathArray.length - 1] const repoId = `${owner}/${repo}` const shouldShowOwnerLogo = model.logo !== undefined && model.logo !== '' return (
{model.model_name}
{shouldShowOwnerLogo && ( {model.author} )} {model.author}
{toGibibytes(model.size)}
) } type DownloadContainerProps = { modelHandle: string fileName: string } const DownloadContainer: React.FC = ({ modelHandle, fileName, }) => { const downloadModelMutation = useModelDownloadMutation() const createThreadMutation = useThreadCreateMutation() const setMainViewState = useSetAtom(mainViewStateAtom) const { data: assistants } = useAssistantQuery() const { abortDownload } = useAbortDownload() const setDownloadLocalModelModalStage = useSetAtom(localModelModalStageAtom) const downloadedModels = useAtomValue(downloadedModelsAtom) const allDownloadState = useAtomValue(downloadStateListAtom) const persistModelId = modelHandle .replaceAll('/', '_') .concat('_') .concat(fileName) const downloadState = allDownloadState.find((s) => s.id == persistModelId) const downloadedModel = useMemo( () => downloadedModels.find((m) => m.model === persistModelId), [downloadedModels, persistModelId] ) const onDownloadClick = useCallback(() => { downloadModelMutation.mutate({ modelId: modelHandle, fileName: fileName, persistedModelId: persistModelId, }) }, [downloadModelMutation, modelHandle, fileName, persistModelId]) const onUseModelClick = useCallback(async () => { if (!assistants || assistants.length === 0) { toaster({ title: 'No assistant available.', description: 'Please create an assistant to create a new thread', type: 'error', }) return } await createThreadMutation.mutateAsync({ modelId: persistModelId, assistant: { ...assistants[0], model: persistModelId, }, }) setDownloadLocalModelModalStage('NONE', undefined) setMainViewState(MainViewState.Thread) }, [ setDownloadLocalModelModalStage, setMainViewState, createThreadMutation, persistModelId, assistants, ]) const onAbortDownloadClick = useCallback(() => { abortDownload(persistModelId) }, [abortDownload, persistModelId]) return (
{downloadedModel ? ( ) : downloadState != null ? ( ) : ( )}
) } export default SliderItem