webapp/src/components/Views/Inbox.tsx

283 lines
9.1 KiB
TypeScript
Raw Normal View History

2022-11-24 07:11:24 +00:00
import { For, createSignal, Show, onMount, createEffect, createMemo } from 'solid-js'
2022-12-17 03:27:00 +00:00
import type { Author, Chat, Message as MessageType } from '../../graphql/types.gen'
2022-11-10 15:06:02 +00:00
import DialogCard from '../Inbox/DialogCard'
import Search from '../Inbox/Search'
2023-02-17 09:21:02 +00:00
import { Message } from '../Inbox/Message'
2022-12-17 03:27:00 +00:00
import CreateModalContent from '../Inbox/CreateModalContent'
import DialogHeader from '../Inbox/DialogHeader'
import MessagesFallback from '../Inbox/MessagesFallback'
import QuotedMessage from '../Inbox/QuotedMessage'
import { Icon } from '../_shared/Icon'
import { useSession } from '../../context/session'
import { loadRecipients } from '../../stores/inbox'
2023-02-17 09:21:02 +00:00
2022-11-27 05:49:48 +00:00
import { Modal } from '../Nav/Modal'
import { showModal } from '../../stores/ui'
2022-12-17 03:27:00 +00:00
import { useInbox } from '../../context/inbox'
import { useRouter } from '../../stores/router'
import { clsx } from 'clsx'
import styles from '../../styles/Inbox.module.scss'
2023-02-17 09:21:02 +00:00
import { useLocalize } from '../../context/localize'
import SimplifiedEditor from '../Editor/SimplifiedEditor'
2022-11-09 12:20:13 +00:00
2022-12-17 03:27:00 +00:00
type InboxSearchParams = {
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
2022-09-22 09:37:49 +00:00
export const InboxView = () => {
2023-02-17 09:21:02 +00:00
const { t } = useLocalize()
2022-12-17 03:27:00 +00:00
const {
chats,
messages,
actions: { loadChats, getMessages, sendMessage, createChat }
} = useInbox()
2022-11-24 12:58:07 +00:00
const [recipients, setRecipients] = createSignal<Author[]>([])
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)
2022-11-15 12:48:42 +00:00
const { session } = useSession()
2022-12-17 03:27:00 +00:00
const currentUserId = createMemo(() => session()?.user.id)
const { changeSearchParam, searchParams } = useRouter<InboxSearchParams>()
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
let chatWindow
2022-12-17 03:27:00 +00:00
const handleOpenChat = async (chat: Chat) => {
setCurrentDialog(chat)
changeSearchParam({
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 {
chatWindow.scrollTop = chatWindow.scrollHeight
}
2022-12-17 03:27:00 +00:00
}
2022-11-23 04:12:11 +00:00
2023-02-23 14:07:25 +00:00
/*
2022-12-17 03:27:00 +00:00
createEffect(() => {
setInterval(async () => {
if (!currentDialog()) return
try {
await getMessages(currentDialog().id)
} catch (error) {
console.error('[getMessages]', error)
} finally {
chatWindow.scrollTop = chatWindow.scrollHeight
}
}, 2000)
})
2023-02-23 14:07:25 +00:00
*/
2022-12-17 03:27:00 +00:00
onMount(async () => {
2022-11-23 04:12:11 +00:00
try {
2022-11-24 09:06:48 +00:00
const response = await loadRecipients({ days: 365 })
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,
2022-12-17 03:27:00 +00:00
chat: currentDialog().id.toString(),
replyTo: messageToReply()?.id
})
setClear(true)
2022-12-17 03:27:00 +00:00
setMessageToReply(null)
chatWindow.scrollTop = chatWindow.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()
changeSearchParam({
initChat: null,
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) => {
return b.updatedAt - a.updatedAt
})
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)
}
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()}
lastUpdate={chat.updatedAt}
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
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}>
<div class={styles.messagesContainer} ref={chatWindow}>
<For each={messages()}>
{(message) => (
<Message
content={message}
ownId={currentUserId()}
members={currentDialog().members}
replyBody={message.replyTo && findToReply(message.replyTo).body}
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()}>
<QuotedMessage
variant="reply"
author={
currentDialog().members.find((member) => member.id === Number(messageToReply().author))
.name
}
body={messageToReply().body}
cancel={() => setMessageToReply(null)}
2022-11-16 04:56:15 +00:00
/>
2022-12-17 03:27:00 +00:00
</Show>
<div class={styles.wrapper}>
<SimplifiedEditor
smallHeight={true}
imageEnabled={true}
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>
)
}