webapp/src/components/Views/Inbox.tsx

337 lines
11 KiB
TypeScript
Raw Normal View History

2023-11-28 13:18:25 +00:00
import type { Chat, Message as MessageType } from '../../graphql/schema/chat.gen'
import type { Author } from '../../graphql/schema/core.gen'
import { clsx } from 'clsx'
2023-11-16 12:23:55 +00:00
import { For, createSignal, Show, onMount, createEffect, createMemo, on } from 'solid-js'
2023-11-28 13:18:25 +00:00
import { useInbox } from '../../context/inbox'
import { useLocalize } from '../../context/localize'
import { useSession } from '../../context/session'
import { useRouter } from '../../stores/router'
import { showModal } from '../../stores/ui'
2023-12-31 05:01:34 +00:00
// import { AuthorsSortBy, useAuthorsStore } from '../../stores/zine/authors'
2023-11-28 13:18:25 +00:00
import { Icon } from '../_shared/Icon'
import { Popover } from '../_shared/Popover'
import SimplifiedEditor from '../Editor/SimplifiedEditor'
2022-12-17 03:27:00 +00:00
import CreateModalContent from '../Inbox/CreateModalContent'
2023-11-28 13:18:25 +00:00
import DialogCard from '../Inbox/DialogCard'
2022-12-17 03:27:00 +00:00
import DialogHeader from '../Inbox/DialogHeader'
2023-11-28 13:18:25 +00:00
import { Message } from '../Inbox/Message'
2022-12-17 03:27:00 +00:00
import MessagesFallback from '../Inbox/MessagesFallback'
2023-11-28 13:18:25 +00:00
import Search from '../Inbox/Search'
2022-11-27 05:49:48 +00:00
import { Modal } from '../Nav/Modal'
2022-11-09 12:20:13 +00:00
2023-11-28 13:18:25 +00:00
import styles from '../../styles/Inbox.module.scss'
2022-12-17 03:27:00 +00:00
type InboxSearchParams = {
2023-12-26 23:28:26 +00:00
by?: string
2022-12-17 03:27:00 +00:00
initChat: string
chat: string
2022-11-09 12:20:13 +00:00
}
2023-05-01 18:32:32 +00:00
const userSearch = (array: Author[], keyword: string) => {
return array.filter((value) => new RegExp(keyword.trim(), 'gi').test(value.name))
}
const handleOpenInviteModal = () => {
showModal('inviteToChat')
}
2022-11-09 12:20:13 +00:00
2023-12-26 23:28:26 +00:00
type Props = {
authors: Author[]
isLoaded: boolean
}
export const InboxView = (props: Props) => {
2023-02-17 09:21:02 +00:00
const { t } = useLocalize()
2022-12-17 03:27:00 +00:00
const {
chats,
messages,
2023-12-31 05:01:34 +00:00
actions: { loadChats, getMessages, sendMessage, createChat },
2022-12-17 03:27:00 +00:00
} = useInbox()
2023-12-31 05:01:34 +00:00
const [recipients, setRecipients] = createSignal<Author[]>(props.authors)
const [sortByGroup, setSortByGroup] = createSignal(false)
const [sortByPerToPer, setSortByPerToPer] = createSignal(false)
2022-12-17 03:27:00 +00:00
const [currentDialog, setCurrentDialog] = createSignal<Chat>()
const [messageToReply, setMessageToReply] = createSignal<MessageType | null>(null)
const [isClear, setClear] = createSignal(false)
2023-11-16 14:20:05 +00:00
const [isScrollToNewVisible, setIsScrollToNewVisible] = createSignal(false)
2023-11-28 13:18:25 +00:00
const { author } = useSession()
2023-12-15 13:45:34 +00:00
const currentUserId = createMemo(() => author()?.id)
2023-12-24 12:56:30 +00:00
const { changeSearchParams, searchParams } = useRouter<InboxSearchParams>()
2023-12-27 23:04:12 +00:00
2023-11-16 12:23:55 +00:00
const messagesContainerRef: { current: HTMLDivElement } = {
2023-11-28 13:18:25 +00:00
current: null,
2023-11-16 12:23:55 +00:00
}
2022-11-11 04:34:18 +00:00
// Поиск по диалогам
2022-11-10 15:06:02 +00:00
const getQuery = (query) => {
2023-05-01 18:32:32 +00:00
if (query().length >= 2) {
const match = userSearch(recipients(), query())
setRecipients(match)
} else {
// setRecipients(cashedRecipients())
}
2022-11-10 15:06:02 +00:00
}
2022-11-11 04:34:18 +00:00
2022-12-17 03:27:00 +00:00
const handleOpenChat = async (chat: Chat) => {
setCurrentDialog(chat)
changeSearchParams({
2023-11-28 13:18:25 +00:00
chat: chat.id,
})
2022-11-15 13:55:00 +00:00
try {
2022-12-17 03:27:00 +00:00
await getMessages(chat.id)
2022-11-15 13:55:00 +00:00
} catch (error) {
2022-12-17 03:27:00 +00:00
console.error('[getMessages]', error)
2022-11-15 13:55:00 +00:00
} finally {
2023-11-16 12:23:55 +00:00
messagesContainerRef.current.scroll({
top: messagesContainerRef.current.scrollHeight,
2023-11-28 13:18:25 +00:00
behavior: 'instant',
2023-11-16 12:23:55 +00:00
})
2022-11-15 13:55:00 +00:00
}
2022-12-17 03:27:00 +00:00
}
2022-11-23 04:12:11 +00:00
2022-12-17 03:27:00 +00:00
onMount(async () => {
2022-11-23 04:12:11 +00:00
try {
2023-11-28 13:18:25 +00:00
const response = await loadRecipients() // time ago in seconds
2022-11-24 12:58:07 +00:00
setRecipients(response as unknown as Author[])
2022-11-27 07:10:04 +00:00
} catch (error) {
console.log(error)
}
2022-12-17 03:27:00 +00:00
await loadChats()
2022-11-10 15:06:02 +00:00
})
2022-11-11 04:34:18 +00:00
const handleSubmit = async (message: string) => {
2022-12-17 03:27:00 +00:00
await sendMessage({
body: message,
2023-12-15 13:45:34 +00:00
chat_id: currentDialog()?.id.toString(),
2023-11-28 13:18:25 +00:00
reply_to: messageToReply()?.id,
2022-12-17 03:27:00 +00:00
})
setClear(true)
2022-12-17 03:27:00 +00:00
setMessageToReply(null)
2023-11-16 12:23:55 +00:00
messagesContainerRef.current.scrollTop = messagesContainerRef.current.scrollHeight
setClear(false)
2022-11-09 12:20:13 +00:00
}
2022-11-16 04:56:15 +00:00
2022-12-17 03:27:00 +00:00
createEffect(async () => {
if (searchParams().chat) {
const chatToOpen = chats()?.find((chat) => chat.id === searchParams().chat)
if (!chatToOpen) return
await handleOpenChat(chatToOpen)
return
}
if (searchParams().initChat) {
try {
const newChat = await createChat([Number(searchParams().initChat)], '')
await loadChats()
changeSearchParams({
initChat: null,
2023-11-28 13:18:25 +00:00
chat: newChat.chat.id,
})
2022-12-17 03:27:00 +00:00
const chatToOpen = chats().find((chat) => chat.id === newChat.chat.id)
await handleOpenChat(chatToOpen)
} catch (error) {
console.error(error)
}
}
2022-11-16 04:56:15 +00:00
})
2022-11-09 12:20:13 +00:00
2022-12-17 03:27:00 +00:00
const chatsToShow = () => {
const sorted = chats().sort((a, b) => {
2023-11-17 09:43:54 +00:00
return b.updated_at - a.updated_at
2022-12-17 03:27:00 +00:00
})
if (sortByPerToPer()) {
2023-02-17 09:21:02 +00:00
return sorted.filter((chat) => (chat.title || '').trim().length === 0)
2022-12-17 03:27:00 +00:00
} else if (sortByGroup()) {
2023-02-17 09:21:02 +00:00
return sorted.filter((chat) => (chat.title || '').trim().length > 0)
2022-12-17 03:27:00 +00:00
} else {
return sorted
}
}
const findToReply = (messageId) => {
return messages().find((message) => message.id === messageId)
}
2023-11-16 12:23:55 +00:00
createEffect(
on(
() => messages(),
() => {
if (!messagesContainerRef.current) {
return
}
if (messagesContainerRef.current.scrollTop >= messagesContainerRef.current.scrollHeight) {
return
}
messagesContainerRef.current.scroll({
top: messagesContainerRef.current.scrollHeight,
2023-11-28 13:18:25 +00:00
behavior: 'smooth',
2023-11-16 12:23:55 +00:00
})
2023-11-28 13:18:25 +00:00
},
2023-11-16 12:23:55 +00:00
),
2023-11-28 13:18:25 +00:00
{ defer: true },
2023-11-16 12:23:55 +00:00
)
2023-11-16 14:20:05 +00:00
const handleScrollMessageContainer = () => {
if (
messagesContainerRef.current.scrollHeight - messagesContainerRef.current.scrollTop >
messagesContainerRef.current.clientHeight * 1.5
) {
setIsScrollToNewVisible(true)
} else {
setIsScrollToNewVisible(false)
}
}
const handleScrollToNew = () => {
messagesContainerRef.current.scroll({
top: messagesContainerRef.current.scrollHeight,
2023-11-28 13:18:25 +00:00
behavior: 'smooth',
2023-11-16 14:20:05 +00:00
})
setIsScrollToNewVisible(false)
}
2023-11-16 12:23:55 +00:00
2023-12-31 05:01:34 +00:00
const isLoaded = createMemo(() => props.isLoaded)
2022-09-09 11:53:35 +00:00
return (
2022-12-17 03:27:00 +00:00
<div class={clsx('container', styles.Inbox)}>
2022-11-27 05:49:48 +00:00
<Modal variant="narrow" name="inviteToChat">
<CreateModalContent users={recipients()} />
</Modal>
2022-12-17 03:27:00 +00:00
<div class={clsx('row', styles.row)}>
2023-03-10 17:42:48 +00:00
<div class={clsx(styles.chatList, 'col-md-8')}>
2022-12-17 03:27:00 +00:00
<div class={styles.sidebarHeader}>
2022-11-27 05:49:48 +00:00
<Search placeholder="Поиск" onChange={getQuery} />
2022-12-17 03:27:00 +00:00
<button type="button" onClick={handleOpenInviteModal}>
2022-11-27 05:49:48 +00:00
<Icon name="plus-button" style={{ width: '40px', height: '40px' }} />
2022-12-17 03:27:00 +00:00
</button>
2022-11-27 05:49:48 +00:00
</div>
2022-12-17 03:27:00 +00:00
<Show when={chatsToShow}>
2023-06-08 21:53:58 +00:00
<ul class="view-switcher">
<li class={clsx({ 'view-switcher__item--selected': !sortByPerToPer() && !sortByGroup() })}>
<button
2022-12-17 03:27:00 +00:00
onClick={() => {
setSortByPerToPer(false)
setSortByGroup(false)
}}
>
2023-06-08 21:53:58 +00:00
{t('All')}
</button>
</li>
<li class={clsx({ 'view-switcher__item--selected': sortByPerToPer() })}>
<button
2022-12-17 03:27:00 +00:00
onClick={() => {
setSortByPerToPer(true)
setSortByGroup(false)
}}
>
2023-06-08 21:53:58 +00:00
{t('Personal')}
</button>
</li>
<li class={clsx({ 'view-switcher__item--selected': sortByGroup() })}>
<button
2022-12-17 03:27:00 +00:00
onClick={() => {
setSortByGroup(true)
setSortByPerToPer(false)
}}
>
2023-06-08 21:53:58 +00:00
{t('Groups')}
</button>
</li>
</ul>
2022-12-17 03:27:00 +00:00
</Show>
<div class={styles.holder}>
<div class={styles.dialogs}>
<For each={chatsToShow()}>
{(chat) => (
<DialogCard
onClick={() => handleOpenChat(chat)}
isOpened={chat.id === currentDialog()?.id}
members={chat.members}
ownId={currentUserId()}
2023-11-17 09:43:54 +00:00
lastUpdate={chat.updated_at}
2022-12-17 03:27:00 +00:00
counter={chat.unread}
message={chat.messages.pop()?.body}
/>
2022-11-09 12:20:13 +00:00
)}
</For>
2022-09-09 11:53:35 +00:00
</div>
</div>
2022-12-17 03:27:00 +00:00
</div>
2023-03-10 17:42:48 +00:00
<div class={clsx('col-md-16', styles.conversation)}>
2022-12-17 03:27:00 +00:00
<Show
2023-11-16 14:20:05 +00:00
keyed={true}
2022-12-17 03:27:00 +00:00
when={currentDialog()}
fallback={
<MessagesFallback
message={t('Choose who you want to write to')}
onClick={handleOpenInviteModal}
actionText={t('Start conversation')}
/>
}
>
<DialogHeader ownId={currentUserId()} chat={currentDialog()} />
<div class={styles.conversationMessages}>
2023-11-16 14:20:05 +00:00
<Show when={isScrollToNewVisible()}>
<Popover content={t('To new messages')}>
{(triggerRef: (el) => void) => (
<div ref={triggerRef} class={styles.scrollToNew} onClick={handleScrollToNew}>
<Icon name="arrow-right" class={styles.icon} />
</div>
)}
</Popover>
</Show>
<div
class={styles.messagesContainer}
ref={(el) => (messagesContainerRef.current = el)}
onScroll={handleScrollMessageContainer}
>
2022-12-17 03:27:00 +00:00
<For each={messages()}>
{(message) => (
<Message
content={message}
ownId={currentUserId()}
members={currentDialog().members}
2023-11-17 09:43:54 +00:00
replyBody={message.reply_to && findToReply(message.reply_to).body}
2022-12-17 03:27:00 +00:00
replyClick={() => setMessageToReply(message)}
/>
)}
</For>
{/*<div class={styles.conversationDate}>*/}
{/* <time>12 сентября</time>*/}
{/*</div>*/}
</div>
</div>
2022-09-09 11:53:35 +00:00
2022-12-17 03:27:00 +00:00
<div class={styles.messageForm}>
<Show when={messageToReply()}>
2023-11-13 14:43:08 +00:00
<p>FIXME: messageToReply</p>
{/*<QuotedMessage*/}
{/* variant="reply"*/}
{/* author={*/}
{/* currentDialog().members.find((member) => member.id === Number(messageToReply().author))*/}
{/* .name*/}
{/* }*/}
{/* body={messageToReply().body}*/}
{/* cancel={() => setMessageToReply(null)}*/}
{/*/>*/}
2022-12-17 03:27:00 +00:00
</Show>
<div class={styles.wrapper}>
<SimplifiedEditor
smallHeight={true}
imageEnabled={true}
2023-11-13 14:43:08 +00:00
isCancelButtonVisible={false}
placeholder={t('Write message')}
setClear={isClear()}
onSubmit={(message) => handleSubmit(message)}
submitByCtrlEnter={true}
/>
2022-11-16 04:56:15 +00:00
</div>
</div>
2022-12-17 03:27:00 +00:00
</Show>
2022-09-09 11:53:35 +00:00
</div>
</div>
</div>
)
}