import { useCallback, useEffect } from 'react' import { Thread } from '@janhq/core/' import { motion as m } from 'framer-motion' import { useAtomValue, useSetAtom } from 'jotai' import { GalleryHorizontalEndIcon, MoreVerticalIcon } from 'lucide-react' import { twMerge } from 'tailwind-merge' import { useCreateNewThread } from '@/hooks/useCreateNewThread' import useRecommendedModel from '@/hooks/useRecommendedModel' import useSetActiveThread from '@/hooks/useSetActiveThread' import { displayDate } from '@/utils/datetime' import CleanThreadModal from '../CleanThreadModal' import DeleteThreadModal from '../DeleteThreadModal' import { assistantsAtom } from '@/helpers/atoms/Assistant.atom' import { editMessageAtom } from '@/helpers/atoms/ChatMessage.atom' import { getActiveThreadIdAtom, threadDataReadyAtom, threadStatesAtom, threadsAtom, } from '@/helpers/atoms/Thread.atom' export default function ThreadList() { const threadStates = useAtomValue(threadStatesAtom) const threads = useAtomValue(threadsAtom) const activeThreadId = useAtomValue(getActiveThreadIdAtom) const { setActiveThread } = useSetActiveThread() const assistants = useAtomValue(assistantsAtom) const threadDataReady = useAtomValue(threadDataReadyAtom) const { requestCreateNewThread } = useCreateNewThread() const setEditMessage = useSetAtom(editMessageAtom) const { recommendedModel, downloadedModels } = useRecommendedModel() const onThreadClick = useCallback( (thread: Thread) => { setActiveThread(thread) setEditMessage('') }, [setActiveThread, setEditMessage] ) /** * Auto create thread * This will create a new thread if there are assistants available * and there are no threads available */ useEffect(() => { if ( threadDataReady && assistants.length > 0 && threads.length === 0 && (recommendedModel || downloadedModels[0]) ) { const model = recommendedModel || downloadedModels[0] requestCreateNewThread(assistants[0], model) } else if (threadDataReady && !activeThreadId) { setActiveThread(threads[0]) } }, [ assistants, threads, threadDataReady, requestCreateNewThread, activeThreadId, setActiveThread, recommendedModel, downloadedModels, ]) return (
{thread.updated && displayDate(thread.updated)}
{threadStates[thread.id]?.lastMessage ? threadStates[thread.id]?.lastMessage : 'No new message'}