import { useCallback } from 'react' import { useDropzone } from 'react-dropzone' import { ImportingModel, fs } from '@janhq/core' import { Modal, ModalContent, ModalHeader, ModalTitle } from '@janhq/uikit' import { useAtomValue, useSetAtom } from 'jotai' import { UploadCloudIcon } from 'lucide-react' import { v4 as uuidv4 } from 'uuid' import { getImportModelStageAtom, setImportModelStageAtom, } from '@/hooks/useImportModel' import { FilePathWithSize, getFileInfoFromFile, getFileNameFromPath, } from '@/utils/file' import { importingModelsAtom } from '@/helpers/atoms/Model.atom' const SelectingModelModal: React.FC = () => { const setImportModelStage = useSetAtom(setImportModelStageAtom) const importModelStage = useAtomValue(getImportModelStageAtom) const setImportingModels = useSetAtom(importingModelsAtom) const onSelectFileClick = useCallback(async () => { const filePaths = await window.core?.api?.selectModelFiles() if (!filePaths || filePaths.length === 0) return const sanitizedFilePaths: FilePathWithSize[] = [] for (const filePath of filePaths) { const fileStats = await fs.fileStat(filePath, true) if (!fileStats || fileStats.isDirectory) continue const fileName = getFileNameFromPath(filePath) sanitizedFilePaths.push({ path: filePath, name: fileName, size: fileStats.size, }) } const importingModels: ImportingModel[] = sanitizedFilePaths.map( ({ path, name, size }: FilePathWithSize) => { return { importId: uuidv4(), modelId: undefined, name: name, description: '', path: path, tags: [], size: size, status: 'PREPARING', format: 'gguf', } } ) if (importingModels.length === 0) return setImportingModels(importingModels) setImportModelStage('MODEL_SELECTED') }, [setImportingModels, setImportModelStage]) const onDrop = useCallback( (acceptedFiles: File[]) => { const filePathWithSize = getFileInfoFromFile(acceptedFiles) const importingModels: ImportingModel[] = filePathWithSize.map( (file) => ({ importId: uuidv4(), modelId: undefined, name: file.name, description: '', path: file.path, tags: [], size: file.size, status: 'PREPARING', format: 'gguf', }) ) if (importingModels.length === 0) return setImportingModels(importingModels) setImportModelStage('MODEL_SELECTED') }, [setImportModelStage, setImportingModels] ) const { isDragActive, getRootProps } = useDropzone({ noClick: true, multiple: true, onDrop, }) const borderColor = isDragActive ? 'border-primary' : 'border-[#F4F4F5]' const textColor = isDragActive ? 'text-blue-600' : 'text-[#71717A]' const dragAndDropBgColor = isDragActive ? 'bg-[#EFF6FF]' : 'bg-white' return ( { setImportModelStage('NONE') }} > Import Model

Import any model file (GGUF) or folder. Your imported model will be private to you.

Click to upload {' '} or drag and drop
(GGUF)
) } export default SelectingModelModal