Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid lose title threads #3307

Merged
merged 2 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions web/constants/Threads.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const defaultThreadTitle = 'New Thread'
6 changes: 4 additions & 2 deletions web/hooks/useCortex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {

import { useAtomValue } from 'jotai'

import { defaultThreadTitle } from '@/constants/Threads'

import { UpdateConfigMutationVariables } from './useEngineMutation'
import { MessageCreateMutationVariables } from './useMessageCreateMutation'
import { MessageDeleteMutationVariables } from './useMessageDeleteMutation'
Expand Down Expand Up @@ -80,7 +82,7 @@ const useCortex = () => {
if (!assistants || assistants.length === 0) continue

// @ts-expect-error each thread must have a title, else default to 'New Thread'
const title: string = thread['title'] ?? 'New Thread'
const title: string = thread['title'] ?? defaultThreadTitle

threads.push({
...thread,
Expand Down Expand Up @@ -227,7 +229,7 @@ const useCortex = () => {
const thread: Thread = {
...createThreadResponse,
// @ts-expect-error each thread will have a title, else default to 'New Thread'
title: createThreadResponse.title ?? 'New Thread',
title: createThreadResponse.title ?? defaultThreadTitle,
assistants: [assistant],
}
return thread
Expand Down
4 changes: 3 additions & 1 deletion web/hooks/useMigratingData.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { useCallback } from 'react'

import { defaultThreadTitle } from '@/constants/Threads'

import useAssistantQuery from './useAssistantQuery'

import useCortex from './useCortex'
Expand Down Expand Up @@ -43,7 +45,7 @@ const useMigratingData = () => {
console.error(`Ignore thread ${thread.id} because modelId is not found`)
continue
}
const threadTitle: string = thread.title ?? 'New Thread'
const threadTitle: string = thread.title ?? defaultThreadTitle
const instructions: string = thread.assistants[0]?.instructions ?? ''
// currently, we don't have api support for creating thread with messages
const cortexThread = await createThread(modelId, assistants[0])
Expand Down
4 changes: 3 additions & 1 deletion web/hooks/useSendMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { currentPromptAtom, editPromptAtom } from '@/containers/Providers/Jotai'

import { toaster } from '@/containers/Toast'

import { defaultThreadTitle } from '@/constants/Threads'

import { inferenceErrorAtom } from '@/screens/HubScreen2/components/InferenceErrorModal'

import { showWarningMultipleModelModalAtom } from '@/screens/HubScreen2/components/WarningMultipleModelModal'
Expand Down Expand Up @@ -512,7 +514,7 @@ const useSendMessage = () => {
if (!isValid) return

let shouldSummarize =
activeThread!.title === 'New Thread' ||
activeThread!.title === defaultThreadTitle ||
activeThread!.title.trim() === ''
const modelId = activeThread!.assistants[0].model

Expand Down
20 changes: 16 additions & 4 deletions web/screens/Thread/ThreadLeftPanel/ModalCleanThread/index.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
import { useCallback, memo } from 'react'

import { Thread } from '@janhq/core'
import { Button, Modal, ModalClose } from '@janhq/joi'
import { useSetAtom } from 'jotai'
import { Paintbrush } from 'lucide-react'

import { defaultThreadTitle } from '@/constants/Threads'

import useCortex from '@/hooks/useCortex'
import useThreads from '@/hooks/useThreads'

import { updateThreadTitleAtom } from '@/helpers/atoms/Thread.atom'

type Props = {
threadId: string
thread: Thread
closeContextMenu?: () => void
}

const ModalCleanThread = ({ threadId, closeContextMenu }: Props) => {
const ModalCleanThread = ({ thread, closeContextMenu }: Props) => {
const { cleanThread } = useThreads()
const updateThreadTitle = useSetAtom(updateThreadTitleAtom)
const { updateThread } = useCortex()

const onCleanThreadClick = useCallback(
(e: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
e.stopPropagation()
cleanThread(threadId)
cleanThread(thread.id)
updateThreadTitle(thread.id, defaultThreadTitle)
updateThread({ ...thread, title: defaultThreadTitle })
},
[cleanThread, threadId]
[cleanThread, thread, updateThread, updateThreadTitle]
)

return (
Expand Down
12 changes: 9 additions & 3 deletions web/screens/Thread/ThreadLeftPanel/ThreadItem/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { useAtomValue, useSetAtom } from 'jotai'
import { MoreHorizontalIcon } from 'lucide-react'
import { twMerge } from 'tailwind-merge'

import { defaultThreadTitle } from '@/constants/Threads'

import useThreads from '@/hooks/useThreads'

import ModalCleanThread from '../ModalCleanThread'
Expand All @@ -29,6 +31,7 @@ const ThreadItem: React.FC<Props> = ({ thread }) => {
const getThreadIdsShouldAnimateTitle = useAtomValue(
getThreadIdsShouldAnimateTitleAtom
)

const setEditMessage = useSetAtom(editMessageAtom)
const { setActiveThread } = useThreads()
const [contextMenu, setContextMenu] = useState<{
Expand Down Expand Up @@ -81,10 +84,13 @@ const ThreadItem: React.FC<Props> = ({ thread }) => {
>
<div className="relative z-10 break-all p-2">
{getThreadIdsShouldAnimateTitle.includes(thread.id) ? (
<TypingAnimated text={thread.title} speed={20} />
<TypingAnimated
text={thread.title || defaultThreadTitle}
speed={20}
/>
) : (
<span className="line-clamp-1 group-hover/message:pr-6">
{thread.title}
{thread.title || defaultThreadTitle}
</span>
)}
</div>
Expand Down Expand Up @@ -112,7 +118,7 @@ const ThreadItem: React.FC<Props> = ({ thread }) => {
closeContextMenu={closeContextMenu}
/>
<ModalCleanThread
threadId={thread.id}
thread={thread}
closeContextMenu={closeContextMenu}
/>
<ModalDeleteThread
Expand Down
16 changes: 10 additions & 6 deletions web/screens/Thread/ThreadLeftPanel/TypingAnimated/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@ const TypingAnimated: React.FC<Props> = ({ text, speed }) => {
const [index, setIndex] = useState(0)

useEffect(() => {
if (index < text.length) {
const timeout = setTimeout(() => {
setDisplayedText(displayedText + text[index])
const typingEffect = setInterval(() => {
if (text.length) {
setDisplayedText(text.substring(0, index + 1))
setIndex(index + 1)
}, speed)
} else {
clearInterval(typingEffect)
}
}, speed)

return () => clearTimeout(timeout)
return () => {
clearInterval(typingEffect)
}
}, [index, text, displayedText, speed])
}, [index, speed, text])

return (
<span className="line-clamp-1 group-hover/message:pr-6">
Expand Down
Loading