From d54a9ab1b35c27d36d8b4c1c361537cccb230046 Mon Sep 17 00:00:00 2001 From: Pavel Prichodko <14926950+prichodko@users.noreply.github.com> Date: Wed, 15 Jun 2022 16:04:07 +0200 Subject: [PATCH] feat: add member to chat input reply --- .../src/contexts/chat-context.tsx | 15 ++++++++------ .../chat/components/chat-input/index.tsx | 7 +++++-- .../components/chat-input/input-reply.tsx | 20 ++++++++----------- .../chat/components/chat-message/index.tsx | 2 +- 4 files changed, 23 insertions(+), 21 deletions(-) diff --git a/packages/status-react/src/contexts/chat-context.tsx b/packages/status-react/src/contexts/chat-context.tsx index 932ee1a..bae8a57 100644 --- a/packages/status-react/src/contexts/chat-context.tsx +++ b/packages/status-react/src/contexts/chat-context.tsx @@ -1,6 +1,6 @@ import React, { createContext, useContext, useMemo, useReducer } from 'react' -import type { Message } from '../protocol/use-messages' +import type { Member, Message } from '../protocol' import type { Dispatch, Reducer } from 'react' type Context = { @@ -12,25 +12,27 @@ const ChatContext = createContext(undefined) interface State { message?: Message + member?: Member } type Action = - | { type: 'SET_REPLY'; message?: Message } - | { type: 'CANCEL_REPLY' } + | { type: 'SET_REPLY'; message?: Message; member?: Member } + | { type: 'DELETE_REPLY' } const reducer: Reducer = (state, action) => { switch (action.type) { case 'SET_REPLY': { - return { ...state, message: action.message } + return { ...state, message: action.message, member: action.member } } - case 'CANCEL_REPLY': { - return { ...state, message: undefined } + case 'DELETE_REPLY': { + return { ...initialState } } } } const initialState: State = { message: undefined, + member: undefined, } interface Props { @@ -41,6 +43,7 @@ export const ChatProvider = (props: Props) => { const { children } = props const [state, dispatch] = useReducer(reducer, initialState) + const value = useMemo(() => ({ state, dispatch }), [state]) return {children} 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 fbb5406..a2c788f 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 @@ -21,7 +21,7 @@ export const ChatInput = (props: Props) => { const { value, editing, onSubmit } = props const [inputValue, setInputValue] = useState(value ?? '') - const { state } = useChatContext() + const { state, dispatch } = useChatContext() const inputRef = useRef(null) @@ -37,6 +37,7 @@ export const ChatInput = (props: Props) => { if (event.key === 'Enter' && event.shiftKey === false) { onSubmit(inputValue) setInputValue('') + dispatch({ type: 'DELETE_REPLY' }) } } @@ -48,7 +49,9 @@ export const ChatInput = (props: Props) => { - {state.message && } + {state.message && ( + + )} { const { dispatch } = useChatContext() - const { message } = props + const { message, member } = props return ( - - - - TODO: Add name - - + + {member.username} + dispatch({ type: 'CANCEL_REPLY' })} + onClick={() => dispatch({ type: 'DELETE_REPLY' })} > 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 4297326..ccad097 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 @@ -85,7 +85,7 @@ export const ChatMessage = (props: Props) => { } const handleReplyClick = () => { - dispatch({ type: 'SET_REPLY', message }) + dispatch({ type: 'SET_REPLY', message, member }) } const handlePinClick = () => {