feat: 发送消息时带上mentions信息

pull/81/head
moonrailgun 3 years ago
parent 2871e941f6
commit d4566775a1

@ -129,7 +129,11 @@ export {
addReaction, addReaction,
removeReaction, removeReaction,
} from './model/message'; } from './model/message';
export type { ChatMessageReaction, ChatMessage } from './model/message'; export type {
ChatMessageReaction,
ChatMessage,
SendMessagePayloadMeta,
} from './model/message';
export type { PluginManifest } from './model/plugin'; export type { PluginManifest } from './model/plugin';
export type { UserBaseInfo, UserLoginInfo } from './model/user'; export type { UserBaseInfo, UserLoginInfo } from './model/user';
export { export {

@ -33,8 +33,12 @@ export interface SimpleMessagePayload {
content: string; content: string;
} }
export interface SendMessagePayloadMeta {
mentions?: string[];
}
export interface SendMessagePayload extends SimpleMessagePayload { export interface SendMessagePayload extends SimpleMessagePayload {
meta?: Record<string, unknown>; meta?: SendMessagePayloadMeta;
} }
/** /**

@ -6,18 +6,22 @@ import { ClipboardHelper } from './clipboard-helper';
import { ChatInputActionContext } from './context'; import { ChatInputActionContext } from './context';
import { uploadMessageImage } from './utils'; import { uploadMessageImage } from './utils';
import { ChatInputBoxInput } from './input'; import { ChatInputBoxInput } from './input';
import type { SendMessagePayloadMeta } from 'tailchat-shared';
interface ChatInputBoxProps { interface ChatInputBoxProps {
onSendMsg: (msg: string) => void; onSendMsg: (msg: string, meta?: SendMessagePayloadMeta) => void;
} }
export const ChatInputBox: React.FC<ChatInputBoxProps> = React.memo((props) => { export const ChatInputBox: React.FC<ChatInputBoxProps> = React.memo((props) => {
const inputRef = useRef<HTMLInputElement>(null); const inputRef = useRef<HTMLInputElement>(null);
const [message, setMessage] = useState(''); const [message, setMessage] = useState('');
const [mentions, setMentions] = useState<string[]>([]);
const handleSendMsg = useCallback(() => { const handleSendMsg = useCallback(() => {
props.onSendMsg(message); props.onSendMsg(message, {
mentions,
});
setMessage(''); setMessage('');
inputRef.current?.focus(); inputRef.current?.focus();
}, [message]); }, [message, mentions]);
const handleKeyDown = useCallback( const handleKeyDown = useCallback(
(e: React.KeyboardEvent<HTMLInputElement | HTMLTextAreaElement>) => { (e: React.KeyboardEvent<HTMLInputElement | HTMLTextAreaElement>) => {
@ -53,7 +57,10 @@ export const ChatInputBox: React.FC<ChatInputBoxProps> = React.memo((props) => {
<ChatInputBoxInput <ChatInputBoxInput
inputRef={inputRef} inputRef={inputRef}
value={message} value={message}
onChange={setMessage} onChange={(message, mentions) => {
setMessage(message);
setMentions(mentions);
}}
onKeyDown={handleKeyDown} onKeyDown={handleKeyDown}
onPaste={handlePaste} onPaste={handlePaste}
/> />

@ -14,11 +14,11 @@ interface ChatInputBoxInputProps
> { > {
inputRef?: React.Ref<HTMLInputElement>; inputRef?: React.Ref<HTMLInputElement>;
value: string; value: string;
onChange: (message: string) => void; onChange: (message: string, mentions: string[]) => void;
} }
export const ChatInputBoxInput: React.FC<ChatInputBoxInputProps> = React.memo( export const ChatInputBoxInput: React.FC<ChatInputBoxInputProps> = React.memo(
(props) => { (props) => {
const mentions = useChatInputMentionsContext(); const allMentions = useChatInputMentionsContext();
return ( return (
<MentionsInput <MentionsInput
@ -27,7 +27,12 @@ export const ChatInputBoxInput: React.FC<ChatInputBoxInputProps> = React.memo(
placeholder={t('输入一些什么')} placeholder={t('输入一些什么')}
singleLine={true} singleLine={true}
value={props.value} value={props.value}
onChange={(e) => props.onChange(e.target.value)} onChange={(e, newValue, _, mentions) =>
props.onChange(
newValue,
mentions.map((m) => m.id)
)
}
onKeyDown={props.onKeyDown} onKeyDown={props.onKeyDown}
onPaste={props.onPaste} onPaste={props.onPaste}
allowSuggestionsAboveCursor={true} allowSuggestionsAboveCursor={true}
@ -35,7 +40,7 @@ export const ChatInputBoxInput: React.FC<ChatInputBoxInputProps> = React.memo(
> >
<Mention <Mention
trigger="@" trigger="@"
data={mentions.users} data={allMentions.users}
displayTransform={(id, display) => `@${display}`} displayTransform={(id, display) => `@${display}`}
appendSpaceOnAdd={true} appendSpaceOnAdd={true}
renderSuggestion={(suggestion) => ( renderSuggestion={(suggestion) => (

@ -55,11 +55,12 @@ const ChatBoxInner: React.FC<ChatBoxProps> = React.memo((props) => {
<ChatReply /> <ChatReply />
<ChatInputBox <ChatInputBox
onSendMsg={(msg) => { onSendMsg={(msg, meta) => {
handleSendMessage({ handleSendMessage({
converseId: props.converseId, converseId: props.converseId,
groupId: props.groupId, groupId: props.groupId,
content: msg, content: msg,
meta,
}); });
}} }}
/> />

Loading…
Cancel
Save