import React, { Fragment, useCallback, useEffect } from 'react' import { Button, Modal, Badge } from '@janhq/joi' import { useAtom, useAtomValue } from 'jotai' import { AlertTriangleIcon } from 'lucide-react' import { twMerge } from 'tailwind-merge' import Spinner from '@/containers/Loader/Spinner' import useMigratingData from '@/hooks/useMigratingData' import { didShowMigrationWarningAtom, modelsMigrationSuccessAtom, threadsMessagesMigrationSuccessAtom, skipMigrationAtom, } from '@/helpers/atoms/AppConfig.atom' const ModalMigrations = () => { const [didShowMigrationWarning, setDidShowMigrationWarning] = useAtom( didShowMigrationWarningAtom ) const [skipMigration, setSkipMigration] = useAtom(skipMigrationAtom) const modelsMigrationSuccess = useAtomValue(modelsMigrationSuccessAtom) const threadsMessagesMigrationSuccess = useAtomValue( threadsMessagesMigrationSuccessAtom ) const [step, setStep] = React.useState(1) const [loaderThreads, setLoaderThreads] = React.useState(false) const [loaderModels, setLoaderModels] = React.useState(false) const getStepTitle = () => { switch (step) { case 1: return 'Important Update: Data Migration Needed' default: return loaderThreads || loaderModels ? 'Migration In Progress' : 'Migration Completed' } } const handleStartMigration = async () => { setStep(2) await handleStartMigrationModels() await handleStartMigrationThreads() } const handleStartMigrationThreads = async () => { setLoaderThreads(true) await migrateThreadsAndMessages() setTimeout(() => { setLoaderThreads(false) }, 1200) } const handleStartMigrationModels = async () => { setLoaderModels(true) await migrateModels() setTimeout(() => { setLoaderModels(false) }, 1200) } const { getJanThreadsAndMessages, migrateModels, migrateThreadsAndMessages, getJanLocalModels, } = useMigratingData() const getMigrationNotif = useCallback(async () => { try { const resultThreadsAndMessages = await getJanThreadsAndMessages() const resultLocalModels = await getJanLocalModels() if ( resultThreadsAndMessages.threads.length > 0 || resultThreadsAndMessages.messages.length > 0 || resultLocalModels ) { setDidShowMigrationWarning(true) } else { setDidShowMigrationWarning(false) } } catch (error) { setDidShowMigrationWarning(false) console.error(error) } }, [getJanLocalModels, getJanThreadsAndMessages, setDidShowMigrationWarning]) useEffect(() => { if ( skipMigration || (threadsMessagesMigrationSuccess && modelsMigrationSuccess) ) { return setDidShowMigrationWarning(false) } else { getMigrationNotif() } // eslint-disable-next-line react-hooks/exhaustive-deps }, [ skipMigration, setDidShowMigrationWarning, threadsMessagesMigrationSuccess, modelsMigrationSuccess, ]) return ( {step === 1 && (

{`We've made some exciting improvements to the app, but we need your help to update your data.`}

What to expect:

  • Some threads or models{' '} might be missing after the migration.
  • This will take a few seconds and reload the app.
)} {step === 2 && (
{!loaderThreads && ( <> {threadsMessagesMigrationSuccess ? ( Success ) : ( Failed )} )}

Threads

{loaderThreads ? ( ) : ( !threadsMessagesMigrationSuccess && ( ) )}
{!loaderModels && ( <> {modelsMigrationSuccess ? ( Success ) : ( Failed )} )}

Models

{loaderModels ? ( ) : ( !modelsMigrationSuccess && ( ) )}
)} } /> ) } export default ModalMigrations