Add mention in channels (#128)

This commit is contained in:
Szymon Szlachtowicz 2021-11-15 14:34:20 +01:00 committed by GitHub
parent 420271b4d1
commit a356f3b421
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 43 additions and 6 deletions

View File

@ -10,6 +10,7 @@ import { textMediumStyles } from "../Text";
interface ChannelProps { interface ChannelProps {
channel: ChannelData; channel: ChannelData;
notification?: number; notification?: number;
mention?: number;
isActive: boolean; isActive: boolean;
isMuted: boolean; isMuted: boolean;
activeView?: boolean; activeView?: boolean;
@ -23,13 +24,13 @@ export function Channel({
activeView, activeView,
onClick, onClick,
notification, notification,
mention,
}: ChannelProps) { }: ChannelProps) {
const narrow = useNarrow(); const narrow = useNarrow();
const className = useMemo( const className = useMemo(
() => (narrow && !activeView ? "narrow" : activeView ? "active" : ""), () => (narrow && !activeView ? "narrow" : activeView ? "active" : ""),
[narrow] [narrow]
); );
const mention = false;
return ( return (
<ChannelWrapper <ChannelWrapper
className={ className={
@ -74,7 +75,7 @@ export function Channel({
</ChannelTextInfo> </ChannelTextInfo>
</ChannelInfo> </ChannelInfo>
{notification && notification > 0 && !activeView && mention && ( {notification && notification > 0 && !activeView && mention && (
<NotificationBagde>{notification}</NotificationBagde> <NotificationBagde>{mention}</NotificationBagde>
)} )}
{isMuted && !notification && <MutedIcon />} {isMuted && !notification && <MutedIcon />}
</ChannelWrapper> </ChannelWrapper>

View File

@ -21,6 +21,8 @@ export function Channels({
}: ChannelsProps) { }: ChannelsProps) {
const { const {
clearNotifications, clearNotifications,
clearMentions,
mentions,
notifications, notifications,
activeChannel, activeChannel,
setActiveChannel, setActiveChannel,
@ -32,6 +34,7 @@ export function Channels({
if (channel) { if (channel) {
if (notifications[channel.id] > 0) { if (notifications[channel.id] > 0) {
clearNotifications(channel.id); clearNotifications(channel.id);
clearMentions(channel.id);
} }
} }
}, [notifications, activeChannelId]); }, [notifications, activeChannelId]);
@ -49,6 +52,11 @@ export function Channels({
? notifications[channel.id] ? notifications[channel.id]
: undefined : undefined
} }
mention={
mentions[channel.id] > 0 && !channel.isMuted
? mentions[channel.id]
: undefined
}
onClick={() => { onClick={() => {
setActiveChannel(channel); setActiveChannel(channel);
if (onCommunityClick) { if (onCommunityClick) {

View File

@ -9,6 +9,8 @@ const MessengerContext = createContext<MessengerType>({
sendMessage: async () => undefined, sendMessage: async () => undefined,
notifications: {}, notifications: {},
clearNotifications: () => undefined, clearNotifications: () => undefined,
mentions: {},
clearMentions: () => undefined,
loadPrevDay: async () => undefined, loadPrevDay: async () => undefined,
loadingMessages: false, loadingMessages: false,
communityData: undefined, communityData: undefined,

View File

@ -2,20 +2,28 @@ import { useCallback, useMemo, useState } from "react";
import { import {
ApplicationMetadataMessage, ApplicationMetadataMessage,
Contacts, Contacts,
Identity,
} from "status-communities/dist/cjs"; } from "status-communities/dist/cjs";
import { bufToHex } from "status-communities/dist/cjs/utils";
import { ChatMessage } from "../../models/ChatMessage"; import { ChatMessage } from "../../models/ChatMessage";
import { binarySetInsert } from "../../utils"; import { binarySetInsert } from "../../utils";
import { useNotifications } from "./useNotifications"; import { useNotifications } from "./useNotifications";
export function useMessages(chatId: string, contacts?: Contacts) { export function useMessages(
chatId: string,
identity: Identity | undefined,
contacts?: Contacts
) {
const [messages, setMessages] = useState<{ [chatId: string]: ChatMessage[] }>( const [messages, setMessages] = useState<{ [chatId: string]: ChatMessage[] }>(
{} {}
); );
const { notifications, incNotification, clearNotifications } = const { notifications, incNotification, clearNotifications } =
useNotifications(); useNotifications();
const mentions = useNotifications();
const addMessage = useCallback( const addMessage = useCallback(
(msg: ApplicationMetadataMessage, id: string, date: Date) => { (msg: ApplicationMetadataMessage, id: string, date: Date) => {
const newMessage = ChatMessage.fromMetadataMessage(msg, date); const newMessage = ChatMessage.fromMetadataMessage(msg, date);
@ -33,9 +41,15 @@ export function useMessages(chatId: string, contacts?: Contacts) {
}; };
}); });
incNotification(id); incNotification(id);
if (
identity &&
newMessage.content.includes(`@${bufToHex(identity.publicKey)}`)
) {
mentions.incNotification(id);
}
} }
}, },
[contacts] [contacts, identity]
); );
const activeMessages = useMemo( const activeMessages = useMemo(
@ -48,5 +62,7 @@ export function useMessages(chatId: string, contacts?: Contacts) {
addMessage, addMessage,
notifications, notifications,
clearNotifications, clearNotifications,
mentions: mentions.notifications,
clearMentions: mentions.clearNotifications,
}; };
} }

View File

@ -27,6 +27,8 @@ export type MessengerType = {
) => Promise<void>; ) => Promise<void>;
notifications: { [chatId: string]: number }; notifications: { [chatId: string]: number };
clearNotifications: (id: string) => void; clearNotifications: (id: string) => void;
mentions: { [chatId: string]: number };
clearMentions: (id: string) => void;
loadPrevDay: (id: string) => Promise<void>; loadPrevDay: (id: string) => Promise<void>;
loadingMessages: boolean; loadingMessages: boolean;
communityData: CommunityData | undefined; communityData: CommunityData | undefined;
@ -79,8 +81,14 @@ export function useMessenger(
}); });
}, [internalContacts]); }, [internalContacts]);
const { addMessage, clearNotifications, notifications, messages } = const {
useMessages(chatId, contactsClass); addMessage,
clearNotifications,
notifications,
messages,
mentions,
clearMentions,
} = useMessages(chatId, identity, contactsClass);
const [community, setCommunity] = useState<Community | undefined>(undefined); const [community, setCommunity] = useState<Community | undefined>(undefined);
const { loadPrevDay, loadingMessages } = useLoadPrevDay(chatId, messenger); const { loadPrevDay, loadingMessages } = useLoadPrevDay(chatId, messenger);
@ -180,5 +188,7 @@ export function useMessenger(
channels, channels,
activeChannel, activeChannel,
setActiveChannel, setActiveChannel,
mentions,
clearMentions,
}; };
} }