webapp/src/components/Views/Inbox.tsx

216 lines
6.4 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-02 11:42:06 +00:00
import type { Author, Chat, ChatMember } from '../../graphql/types.gen'
2022-09-09 11:53:35 +00:00
import { AuthorCard } from '../Author/Card'
2022-11-14 17:41:05 +00:00
import { Icon } from '../_shared/Icon'
2022-11-09 12:20:13 +00:00
import { Loading } from '../Loading'
2022-11-10 15:06:02 +00:00
import DialogCard from '../Inbox/DialogCard'
import Search from '../Inbox/Search'
2022-11-23 04:12:11 +00:00
import { useSession } from '../../context/session'
2022-11-09 12:20:13 +00:00
import { createClient } from '@urql/core'
2022-11-21 05:06:53 +00:00
import Message from '../Inbox/Message'
2022-11-24 09:06:48 +00:00
import { loadRecipients, loadChats } from '../../stores/inbox'
2022-11-24 06:52:31 +00:00
import { t } from '../../utils/intl'
2022-11-24 09:07:52 +00:00
import '../../styles/Inbox.scss'
2022-11-24 15:39:31 +00:00
import { useInbox } from '../../context/inbox'
2022-11-27 05:49:48 +00:00
import { Modal } from '../Nav/Modal'
import { showModal } from '../../stores/ui'
import InviteUser from '../Inbox/InviteUser'
import CreateModalContent from '../Inbox/CreateModalContent'
2022-11-09 12:20:13 +00:00
const OWNER_ID = '501'
const client = createClient({
url: 'https://graphqlzero.almansi.me/api'
})
2022-09-09 11:53:35 +00:00
2022-11-09 12:20:13 +00:00
const messageQuery = `
query Comments ($options: PageQueryOptions) {
comments(options: $options) {
data {
id
body
email
}
}
}
`
const newMessageQuery = `
mutation postComment($messageBody: String!) {
createComment(
input: { body: $messageBody, email: "test@test.com", name: "User" }
) {
id
body
name
email
}
}
`
2022-11-10 15:06:02 +00:00
const userSearch = (array: Author[], keyword: string) => {
const searchTerm = keyword.toLowerCase()
return array.filter((value) => {
return value.name.toLowerCase().match(new RegExp(searchTerm, 'g'))
})
2022-11-09 12:20:13 +00:00
}
2022-11-15 13:55:00 +00:00
const postMessage = async (msg: string) => {
const response = await client.mutation(newMessageQuery, { messageBody: msg }).toPromise()
return response.data.createComment
}
2022-09-22 09:37:49 +00:00
export const InboxView = () => {
2022-11-10 15:06:02 +00:00
const [messages, setMessages] = createSignal([])
2022-11-24 12:58:07 +00:00
const [recipients, setRecipients] = createSignal<Author[]>([])
2022-11-27 07:10:04 +00:00
const [chats, setChats] = createSignal<Chat[]>([])
2022-11-24 12:58:07 +00:00
const [cashedRecipients, setCashedRecipients] = createSignal<Author[]>([])
2022-11-10 15:06:02 +00:00
const [postMessageText, setPostMessageText] = createSignal('')
const [loading, setLoading] = createSignal<boolean>(false)
2022-11-15 12:48:42 +00:00
const { session } = useSession()
2022-11-10 15:06:02 +00:00
2022-11-11 04:34:18 +00:00
// Поиск по диалогам
2022-11-10 15:06:02 +00:00
const getQuery = (query) => {
2022-11-10 15:58:43 +00:00
if (query().length >= 2) {
2022-11-24 12:58:07 +00:00
const match = userSearch(recipients(), query())
setRecipients(match)
2022-11-10 15:06:02 +00:00
} else {
2022-11-24 12:58:07 +00:00
setRecipients(cashedRecipients())
2022-11-10 15:06:02 +00:00
}
}
const fetchMessages = async (query) => {
const response = await client
.query(query, {
options: { slice: { start: 0, end: 3 } }
})
.toPromise()
if (response.error) console.debug('getMessages', response.error)
setMessages(response.data.comments.data)
}
2022-11-11 04:34:18 +00:00
let chatWindow
2022-11-10 15:06:02 +00:00
onMount(async () => {
setLoading(true)
2022-11-15 13:55:00 +00:00
try {
await fetchMessages(messageQuery)
} catch (error) {
setLoading(false)
console.error([fetchMessages], error)
} finally {
setLoading(false)
chatWindow.scrollTop = chatWindow.scrollHeight
}
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[])
setCashedRecipients(response as unknown as Author[])
2022-11-23 04:12:11 +00:00
} catch (error) {
console.log(error)
}
2022-11-27 07:10:04 +00:00
try {
const response = await loadChats()
setChats(response as unknown as Chat[])
} catch (error) {
console.log(error)
}
2022-11-10 15:06:02 +00:00
})
2022-11-11 04:34:18 +00:00
2022-11-09 12:20:13 +00:00
const handleSubmit = async () => {
2022-11-15 13:55:00 +00:00
try {
const post = await postMessage(postMessageText())
setMessages((prev) => [...prev, post])
setPostMessageText('')
chatWindow.scrollTop = chatWindow.scrollHeight
} catch (error) {
console.error('[post message error]:', error)
}
2022-11-09 12:20:13 +00:00
}
2022-11-16 04:56:15 +00:00
2022-11-27 07:10:04 +00:00
let textareaParent // textarea autoresize ghost element
2022-11-09 12:20:13 +00:00
const handleChangeMessage = (event) => {
setPostMessageText(event.target.value)
}
2022-11-16 04:56:15 +00:00
createEffect(() => {
2022-11-27 07:10:04 +00:00
textareaParent.dataset.replicatedValue = postMessageText()
2022-11-16 04:56:15 +00:00
})
2022-11-09 12:20:13 +00:00
2022-11-27 05:49:48 +00:00
const handleOpenInviteModal = (event: Event) => {
event.preventDefault()
showModal('inviteToChat')
}
2022-11-27 07:10:04 +00:00
2022-09-09 11:53:35 +00:00
return (
<div class="messages container">
2022-11-27 05:49:48 +00:00
<Modal variant="narrow" name="inviteToChat">
<CreateModalContent users={recipients()} />
</Modal>
2022-09-09 11:53:35 +00:00
<div class="row">
<div class="chat-list col-md-4">
2022-11-27 05:49:48 +00:00
<div class="sidebar-header">
<Search placeholder="Поиск" onChange={getQuery} />
<div onClick={handleOpenInviteModal}>
<Icon name="plus-button" style={{ width: '40px', height: '40px' }} />
</div>
</div>
2022-09-09 11:53:35 +00:00
<div class="chat-list__types">
<ul>
<li>
2022-11-24 06:52:31 +00:00
<strong>{t('All')}</strong>
2022-09-09 11:53:35 +00:00
</li>
2022-11-27 07:10:04 +00:00
<li>{t('Personal')}</li>
2022-11-24 06:52:31 +00:00
<li>{t('Groups')}</li>
2022-09-09 11:53:35 +00:00
</ul>
</div>
2022-11-11 04:34:18 +00:00
<div class="holder">
<div class="dialogs">
2022-12-02 11:42:06 +00:00
<For each={chats()}>{(chat: Chat) => <DialogCard members={chat.members} />}</For>
2022-11-11 04:34:18 +00:00
</div>
2022-09-09 11:53:35 +00:00
</div>
</div>
<div class="col-md-8 conversation">
<div class="interlocutor user--online">
<AuthorCard author={{} as Author} hideFollow={true} />
<div class="user-status">Online</div>
</div>
<div class="conversation__messages">
2022-11-09 12:20:13 +00:00
<div class="conversation__messages-container" ref={chatWindow}>
<Show when={loading()}>
<Loading />
</Show>
<For each={messages()}>
{(comment: { body: string; id: string; email: string }) => (
2022-11-21 05:06:53 +00:00
<Message body={comment.body} isOwn={OWNER_ID === comment.id} />
2022-11-09 12:20:13 +00:00
)}
</For>
2022-09-09 11:53:35 +00:00
2022-11-09 12:20:13 +00:00
{/*<div class="conversation__date">*/}
{/* <time>12 сентября</time>*/}
{/*</div>*/}
2022-09-09 11:53:35 +00:00
</div>
</div>
2022-11-16 04:56:15 +00:00
<div class="message-form">
<div class="wrapper">
2022-11-27 07:10:04 +00:00
<div class="grow-wrap" ref={textareaParent}>
2022-11-16 04:56:15 +00:00
<textarea
value={postMessageText()}
rows={1}
onInput={(event) => handleChangeMessage(event)}
placeholder="Написать сообщение"
/>
</div>
<button type="submit" disabled={postMessageText().length === 0} onClick={handleSubmit}>
<Icon name="send-message" />
</button>
</div>
2022-11-09 12:20:13 +00:00
</div>
2022-09-09 11:53:35 +00:00
</div>
</div>
</div>
)
}