Fix notifications and refactor useMessenger (#91)

This commit is contained in:
Szymon Szlachtowicz 2021-10-21 16:22:47 +02:00 committed by GitHub
parent 46618e2dd0
commit fdab496b02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 64 additions and 55 deletions

View File

@ -23,8 +23,8 @@ export function Channels({
useEffect(() => {
const channel = channels.find((channel) => channel.id === activeChannelId);
if (channel) {
if (notifications[channel.name] > 0) {
clearNotifications(channel.name);
if (notifications[channel.id] > 0) {
clearNotifications(channel.id);
}
}
}, [notifications, activeChannelId]);
@ -38,8 +38,8 @@ export function Channels({
isActive={channel.id === activeChannelId}
isMuted={channel.isMuted || false}
notification={
notifications[channel.name] > 0 && !channel.isMuted
? notifications[channel.name]
notifications[channel.id] > 0 && !channel.isMuted
? notifications[channel.id]
: undefined
}
onClick={() => {

View File

@ -28,6 +28,7 @@ export function useMessages(chatId: string) {
),
};
});
console.log(`increase noti ${id}`);
incNotification(id);
}
},

View File

@ -1,9 +1,8 @@
// import { StoreCodec } from "js-waku";
import { StoreCodec } from "js-waku";
import { useCallback, useEffect, useState } from "react";
import { Community, Identity, Messenger } from "status-communities/dist/cjs";
import { Community, Messenger } from "status-communities/dist/cjs";
import { loadIdentity, saveIdentity } from "../../utils";
import { createCommunityMessenger } from "../../utils/createCommunityMessenger";
import { useLoadPrevDay } from "./useLoadPrevDay";
import { useMessages } from "./useMessages";
@ -16,53 +15,10 @@ export function useMessenger(chatId: string, communityKey: string) {
const { loadPrevDay, loadingMessages } = useLoadPrevDay(chatId, messenger);
useEffect(() => {
const createMessenger = async () => {
// Test password for now
// Need design for password input
let identity = await loadIdentity("test");
if (!identity) {
identity = Identity.generate();
await saveIdentity(identity, "test");
}
const messenger = await Messenger.create(identity, {
libp2p: {
config: {
pubsub: {
enabled: true,
emitSelf: true,
},
},
},
createCommunityMessenger(communityKey, addMessage).then((result) => {
setCommunity(result.community);
setMessenger(result.messenger);
});
await new Promise((resolve) => {
messenger.waku.libp2p.peerStore.on(
"change:protocols",
({ protocols }) => {
if (protocols.includes(StoreCodec)) {
resolve("");
}
}
);
});
const community = await Community.instantiateCommunity(
communityKey,
messenger.waku
);
setCommunity(community);
await Promise.all(
Array.from(community.chats.values()).map(async (chat) => {
await messenger.joinChat(chat);
messenger.addObserver(
(msg, date) => addMessage(msg, chat.id, date),
chat.id
);
clearNotifications(chat.id);
})
);
setMessenger(messenger);
};
createMessenger();
}, []);
useEffect(() => {

View File

@ -8,7 +8,7 @@ export function useNotifications() {
setNotifications((prevNotifications) => {
return {
...prevNotifications,
[id]: prevNotifications[id] + 1,
[id]: (prevNotifications?.[id] ?? 0) + 1,
};
});
}, []);

View File

@ -0,0 +1,52 @@
import { StoreCodec } from "js-waku";
import { Community, Identity, Messenger } from "status-communities/dist/cjs";
import { ApplicationMetadataMessage } from "status-communities/dist/cjs";
import { loadIdentity, saveIdentity } from "./";
const WAKU_OPTIONS = {
libp2p: {
config: {
pubsub: {
enabled: true,
emitSelf: true,
},
},
},
};
export async function createCommunityMessenger(
communityKey: string,
addMessage: (msg: ApplicationMetadataMessage, id: string, date: Date) => void
) {
// Test password for now
// Need design for password input
let identity = await loadIdentity("test");
if (!identity) {
identity = Identity.generate();
await saveIdentity(identity, "test");
}
const messenger = await Messenger.create(identity, WAKU_OPTIONS);
await new Promise((resolve) => {
messenger.waku.libp2p.peerStore.on("change:protocols", ({ protocols }) => {
if (protocols.includes(StoreCodec)) {
resolve("");
}
});
});
const community = await Community.instantiateCommunity(
communityKey,
messenger.waku
);
await Promise.all(
Array.from(community.chats.values()).map(async (chat) => {
await messenger.joinChat(chat);
messenger.addObserver(
(msg, date) => addMessage(msg, chat.id, date),
chat.id
);
})
);
return { messenger, community, identity };
}