mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-06-08 08:39:03 +02:00
c22e44438e
* Initial server-side delete of chat message plus dropdown * Update chat pane after deleting message * Chat message dropdown styling * Add confirmation dialog for deleting a message * Refactor chat message grouping to allow deletion of individual messages * Delete other user's deleted message from chat pane * Implement message editing * Styling * Make the dropdown appear overlap with the button slightly so that the menu stays visible when the user moves their cursor into the menu when the menu is positioned above the button * Submit edit with Enter key * Add edited indicator to edited chat messages * Add animation to chat message deletion * Tidying, edit chat message textarea improvements * Add types to message-list-utils * update dependencies * edit/delete for ide-redesign * fix type errors in tests * filter deleted messages from group * promisify ChatController * fix tests and translations * add new tests * chat-context tests * fix message-list-appender tests * add new tests for message-list-utils * Update services/web/test/frontend/features/chat/context/chat-context.test.tsx Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * preserve original content when canceling edits * update delete message translation * hide dropdown only if not already shown * remove delete animation * fix lint error * fix chat.yaml * hide under feature flag --------- Co-authored-by: Tim Down <158919+timdown@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> GitOrigin-RevId: 12521886a1a59ccd564851df19e5d46c70d328f5
115 lines
3.2 KiB
TypeScript
115 lines
3.2 KiB
TypeScript
import React, { lazy, Suspense, useEffect, useState } from 'react'
|
|
import { useTranslation } from 'react-i18next'
|
|
|
|
import MessageInput from './message-input'
|
|
import InfiniteScroll from './infinite-scroll'
|
|
import ChatFallbackError from './chat-fallback-error'
|
|
import { useLayoutContext } from '../../../shared/context/layout-context'
|
|
import { useUserContext } from '../../../shared/context/user-context'
|
|
import withErrorBoundary from '../../../infrastructure/error-boundary'
|
|
import { FetchError } from '../../../infrastructure/fetch-json'
|
|
import { useChatContext } from '../context/chat-context'
|
|
import { FullSizeLoadingSpinner } from '../../../shared/components/loading-spinner'
|
|
import MaterialIcon from '@/shared/components/material-icon'
|
|
|
|
const MessageList = lazy(() => import('./message-list'))
|
|
|
|
const Loading = () => <FullSizeLoadingSpinner delay={500} className="pt-4" />
|
|
|
|
const ChatPane = React.memo(function ChatPane() {
|
|
const { t } = useTranslation()
|
|
|
|
const { chatIsOpen } = useLayoutContext()
|
|
const user = useUserContext()
|
|
|
|
const {
|
|
status,
|
|
messages,
|
|
initialMessagesLoaded,
|
|
atEnd,
|
|
loadInitialMessages,
|
|
loadMoreMessages,
|
|
reset,
|
|
sendMessage,
|
|
markMessagesAsRead,
|
|
error,
|
|
} = useChatContext()
|
|
|
|
useEffect(() => {
|
|
if (chatIsOpen && !initialMessagesLoaded) {
|
|
loadInitialMessages()
|
|
}
|
|
}, [chatIsOpen, loadInitialMessages, initialMessagesLoaded])
|
|
|
|
const shouldDisplayPlaceholder = status !== 'pending' && messages.length === 0
|
|
|
|
const messageContentCount = messages.length
|
|
|
|
// Keep the chat pane in the DOM to avoid resetting the form input and re-rendering MathJax content.
|
|
const [chatOpenedOnce, setChatOpenedOnce] = useState(chatIsOpen)
|
|
useEffect(() => {
|
|
if (chatIsOpen) {
|
|
setChatOpenedOnce(true)
|
|
}
|
|
}, [chatIsOpen])
|
|
|
|
if (error) {
|
|
// let user try recover from fetch errors
|
|
if (error instanceof FetchError) {
|
|
return <ChatFallbackError reconnect={reset} />
|
|
}
|
|
throw error
|
|
}
|
|
|
|
if (!user) {
|
|
return null
|
|
}
|
|
if (!chatOpenedOnce) {
|
|
return null
|
|
}
|
|
|
|
return (
|
|
<aside className="chat" aria-label={t('chat')}>
|
|
<InfiniteScroll
|
|
atEnd={atEnd}
|
|
className="messages"
|
|
fetchData={loadMoreMessages}
|
|
isLoading={status === 'pending'}
|
|
itemCount={messageContentCount}
|
|
>
|
|
<div>
|
|
<h2 className="visually-hidden">{t('chat')}</h2>
|
|
<Suspense fallback={<Loading />}>
|
|
{status === 'pending' && <Loading />}
|
|
{shouldDisplayPlaceholder && <Placeholder />}
|
|
<MessageList
|
|
messages={messages}
|
|
resetUnreadMessages={markMessagesAsRead}
|
|
/>
|
|
</Suspense>
|
|
</div>
|
|
</InfiniteScroll>
|
|
<MessageInput
|
|
resetUnreadMessages={markMessagesAsRead}
|
|
sendMessage={sendMessage}
|
|
/>
|
|
</aside>
|
|
)
|
|
})
|
|
|
|
function Placeholder() {
|
|
const { t } = useTranslation()
|
|
return (
|
|
<>
|
|
<div className="no-messages text-center small">{t('no_messages')}</div>
|
|
<div className="first-message text-center">
|
|
{t('send_first_message')}
|
|
<br />
|
|
<MaterialIcon type="arrow_downward" />
|
|
</div>
|
|
</>
|
|
)
|
|
}
|
|
|
|
export default withErrorBoundary(ChatPane, () => <ChatFallbackError />)
|