From b2119719e0c62a2fda66641df26104bc15422acf Mon Sep 17 00:00:00 2001 From: Pavel Prichodko <14926950+prichodko@users.noreply.github.com> Date: Fri, 1 Jul 2022 14:58:19 +0200 Subject: [PATCH] fix typechecking errors in status-react --- .../src/contexts/chat-context.tsx | 21 ++++++---- .../src/protocol/use-messages.tsx | 38 ++++++------------- .../chat/components/chat-input/index.tsx | 8 ++-- .../components/chat-input/input-reply.tsx | 15 +++++--- .../chat/components/chat-message/index.tsx | 21 +++++----- .../components/chat-message/message-reply.tsx | 13 +++++-- .../status-react/src/routes/chat/index.tsx | 4 +- 7 files changed, 60 insertions(+), 60 deletions(-) diff --git a/packages/status-react/src/contexts/chat-context.tsx b/packages/status-react/src/contexts/chat-context.tsx index bae8a57..3e1e5ba 100644 --- a/packages/status-react/src/contexts/chat-context.tsx +++ b/packages/status-react/src/contexts/chat-context.tsx @@ -11,18 +11,26 @@ type Context = { const ChatContext = createContext(undefined) interface State { - message?: Message - member?: Member + reply?: { + message: Message + member: Member + } } type Action = - | { type: 'SET_REPLY'; message?: Message; member?: Member } + | { type: 'SET_REPLY'; message: Message; member: Member } | { type: 'DELETE_REPLY' } -const reducer: Reducer = (state, action) => { +const reducer: Reducer = (state, action): State => { switch (action.type) { case 'SET_REPLY': { - return { ...state, message: action.message, member: action.member } + return { + ...state, + reply: { + message: action.message, + member: action.member, + }, + } } case 'DELETE_REPLY': { return { ...initialState } @@ -31,8 +39,7 @@ const reducer: Reducer = (state, action) => { } const initialState: State = { - message: undefined, - member: undefined, + reply: undefined, } interface Props { diff --git a/packages/status-react/src/protocol/use-messages.tsx b/packages/status-react/src/protocol/use-messages.tsx index 2df1568..2a5d3ae 100644 --- a/packages/status-react/src/protocol/use-messages.tsx +++ b/packages/status-react/src/protocol/use-messages.tsx @@ -2,60 +2,46 @@ import { useEffect, useState } from 'react' import { useProtocol } from './provider' -import type { Message } from '@status-im/js' +import type { Message, Reactions } from '@status-im/js' -export type Reaction = - | 'LOVE' - | 'THUMBS_UP' - | 'THUMBS_DOWN' - | 'LAUGH' - | 'SAD' - | 'ANGRY' - -export type Reactions = { - [key in Reaction]: { - count: number - me: boolean - } -} - -export type { Message } +type Reaction = keyof Reactions interface Result { data: Message[] loading: boolean - error?: Error + // error?: Error // fetchMore: () => void } export const useMessages = (channelId: string): Result => { const { client } = useProtocol() + const chat = client.community.chats.get(channelId)! // const [state, dispatch] = useReducer((state,action) => {}, {}) - const [data, setData] = useState(() => - client.community.chats.get(channelId).getMessages() - ) + const [data, setData] = useState(() => chat.getMessages()) const [loading, setLoading] = useState(true) - const [error, setError] = useState() + // const [error, setError] = useState() useEffect(() => { - setData(client.community.chats.get(channelId).getMessages()) + setData(chat.getMessages()) const handleUpdate = (messages: Message[]) => { setLoading(false) setData(messages) } - return client.community.chats.get(channelId).onMessage(handleUpdate) - }, [channelId]) + return chat.onMessage(handleUpdate) + }, [chat]) return { data, loading, - error, + // error, // hasMore // fetchMore, // refetch } } + +export type { Message, Reaction, Reactions } diff --git a/packages/status-react/src/routes/chat/components/chat-input/index.tsx b/packages/status-react/src/routes/chat/components/chat-input/index.tsx index 48f7949..a0d41ca 100644 --- a/packages/status-react/src/routes/chat/components/chat-input/index.tsx +++ b/packages/status-react/src/routes/chat/components/chat-input/index.tsx @@ -26,8 +26,8 @@ export const ChatInput = (props: Props) => { const inputRef = useRef(null) useEffect(() => { - state.message && !editing && inputRef.current?.focus() - }, [state.message, editing]) + state.reply && !editing && inputRef.current?.focus() + }, [state.reply, editing]) const handleChange = (event: React.ChangeEvent) => { setInputValue(event.target.value) @@ -53,9 +53,7 @@ export const ChatInput = (props: Props) => { */} - {state.message && ( - - )} + {state.reply && } { const { dispatch } = useChatContext() - const { message, member } = props + const { reply } = props + const { message, member } = reply return ( @@ -38,7 +41,7 @@ export const InputReply = (props: Props) => { )} - {message.contentType === 'IMAGE' && ( + {/* {message.contentType === 'IMAGE' && ( { radius="bubble" alt="message" /> - )} + )} */} ) } diff --git a/packages/status-react/src/routes/chat/components/chat-message/index.tsx b/packages/status-react/src/routes/chat/components/chat-message/index.tsx index c05fbe5..6f576c2 100644 --- a/packages/status-react/src/routes/chat/components/chat-message/index.tsx +++ b/packages/status-react/src/routes/chat/components/chat-message/index.tsx @@ -2,7 +2,7 @@ import React, { useState } from 'react' import { useMatch } from 'react-router-dom' -import { UserProfileDialog } from '~/src/components/user-profile-dialog' +// import { UserProfileDialog } from '~/src/components/user-profile-dialog' import { useChatContext } from '~/src/contexts/chat-context' // import { BellIcon } from '~/src/icons/bell-icon' // import { PinIcon } from '~/src/icons/pin-icon' @@ -20,7 +20,7 @@ import { Flex, Image, Text, - useDialog, + // useDialog, } from '~/src/system' import { ChatInput } from '../chat-input' @@ -71,16 +71,16 @@ export const ChatMessage = (props: Props) => { // TODO: remove usage of 0x prefix const owner = '0x' + account?.publicKey === signer - const chat = client.community.getChat(chatId) + const chat = client.community.getChat(chatId)! - const member = client.community.getMember(signer) ?? {} + const member = client.community.getMember(signer)! const [editing, setEditing] = useState(false) const [reacting, setReacting] = useState(false) const { dispatch } = useChatContext() - const userProfileDialog = useDialog(UserProfileDialog) + // const userProfileDialog = useDialog(UserProfileDialog) const handleMessageSubmit = (message: string) => { chat.sendTextMessage(message) @@ -120,7 +120,8 @@ export const ChatMessage = (props: Props) => { > Cancel - @@ -180,14 +181,14 @@ export const ChatMessage = (props: Props) => { */} {/* - {member.username} + {member!.username} @@ -224,7 +225,7 @@ export const ChatMessage = (props: Props) => { - {member.username} + {member!.username} {new Date(Number(clock)).toLocaleTimeString([], { diff --git a/packages/status-react/src/routes/chat/components/chat-message/message-reply.tsx b/packages/status-react/src/routes/chat/components/chat-message/message-reply.tsx index ee06604..3bbb001 100644 --- a/packages/status-react/src/routes/chat/components/chat-message/message-reply.tsx +++ b/packages/status-react/src/routes/chat/components/chat-message/message-reply.tsx @@ -17,7 +17,7 @@ export const MessageReply = (props: Props) => { // TODO: use protocol hook const { params } = useMatch(':id')! // eslint-disable-line @typescript-eslint/no-non-null-assertion - const message = client.community.getChat(params.id).getMessage(messageId) + const message = client.community.getChat(params.id!)!.getMessage(messageId) if (!message) { return ( @@ -31,14 +31,19 @@ export const MessageReply = (props: Props) => { const { contentType, text, signer } = message - const { username } = client.community.getMember(signer) + // TODO: can this happen? + const member = client.community.getMember(signer) + + if (!member) { + return null + } return ( - + - {username} + {member.username} {contentType === 'TEXT_PLAIN' && ( diff --git a/packages/status-react/src/routes/chat/index.tsx b/packages/status-react/src/routes/chat/index.tsx index f0d93d7..0e9583c 100644 --- a/packages/status-react/src/routes/chat/index.tsx +++ b/packages/status-react/src/routes/chat/index.tsx @@ -43,7 +43,7 @@ const Body = () => { const { params } = useMatch(':id')! // eslint-disable-line @typescript-eslint/no-non-null-assertion const chatId = params.id! - const chat = client.community.getChat(chatId) + const chat = client.community.getChat(chatId)! const messages = useMessages(chatId) const contentRef = useRef(null) @@ -53,7 +53,7 @@ const Body = () => { }, [chatId, messages.data.length]) const handleMessageSubmit = (message: string) => { - chat.sendTextMessage(message, state.message?.messageId) + chat.sendTextMessage(message, state.reply?.message.messageId) } return (