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 1 commit
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
18 changes: 14 additions & 4 deletions web/screens/Thread/ThreadLeftPanel/ModalCleanThread/index.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
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 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, 'New Thread')
updateThread({ ...thread, title: 'New Thread' })
urmauur marked this conversation as resolved.
Show resolved Hide resolved
},
[cleanThread, threadId]
[cleanThread, thread, updateThread, updateThreadTitle]
)

return (
Expand Down
7 changes: 4 additions & 3 deletions web/screens/Thread/ThreadLeftPanel/ThreadItem/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,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 +82,10 @@ 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 || 'New Thread'} speed={20} />
) : (
<span className="line-clamp-1 group-hover/message:pr-6">
{thread.title}
{thread.title || 'New Thread'}
</span>
)}
</div>
Expand Down Expand Up @@ -112,7 +113,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