minor updates

This commit is contained in:
Ren Amamiya
2023-06-03 19:40:36 +07:00
parent 93711e1d36
commit be6de2344e
14 changed files with 185 additions and 176 deletions

View File

@@ -2,7 +2,7 @@ import { Image } from "@shared/image";
import { RelayContext } from "@shared/relayProvider";
import { useActiveAccount } from "@stores/accounts";
import { useChannels } from "@stores/channels";
import { useChats } from "@stores/chats";
import { useChatMessages, useChats } from "@stores/chats";
import { DEFAULT_AVATAR, READONLY_RELAYS } from "@stores/constants";
import { usePageContext } from "@utils/hooks/usePageContext";
import { useProfile } from "@utils/hooks/useProfile";
@@ -13,26 +13,27 @@ import useSWRSubscription from "swr/subscription";
export function ActiveAccount({ data }: { data: any }) {
const pool: any = useContext(RelayContext);
const pageContext = usePageContext();
const pathnames: any = pageContext.urlParsed.pathname;
const pathname: any = pageContext.urlParsed.pathname;
const notChatPage = pathnames.includes("/chat") ? false : true;
const notChannelPage = pathnames.includes("/channel") ? false : true;
const isChatPage = pathname.includes("/chat");
const isChannelPage = pathname.includes("/channel");
// const notSpacePage = pathnames.includes("/space") ? false : true;
const lastLogin = useActiveAccount((state: any) => state.lastLogin);
const notifyChat = useChats((state: any) => state.add);
const saveChat = useChatMessages((state: any) => state.add);
const notifyChannel = useChannels((state: any) => state.add);
const { user } = useProfile(data.pubkey);
useSWRSubscription(
user && lastLogin > 0 ? ["account", data.pubkey] : null,
([, key]) => {
() => {
// subscribe to channel
const unsubscribe = pool.subscribe(
[
{
"#p": [key],
"#p": [data.pubkey],
since: lastLogin,
},
],
@@ -42,7 +43,9 @@ export function ActiveAccount({ data }: { data: any }) {
case 1:
break;
case 4:
if (notChatPage) {
if (!isChatPage) {
// save
saveChat(data.pubkey, event);
// update state
notifyChat(event.pubkey);
// send native notifiation
@@ -50,7 +53,7 @@ export function ActiveAccount({ data }: { data: any }) {
}
break;
case 42:
if (notChannelPage) {
if (!isChannelPage) {
// update state
notifyChannel(event);
// send native notifiation

View File

@@ -1,4 +1,3 @@
import { EventCollector } from "@shared/eventCollector";
import { ArrowLeftIcon, ArrowRightIcon } from "@shared/icons";
export function AppHeader() {
@@ -39,7 +38,6 @@ export function AppHeader() {
/>
</button>
</div>
<EventCollector />
</div>
);
}

View File

@@ -3,8 +3,8 @@ import { RelayContext } from "@shared/relayProvider";
import { useActiveAccount } from "@stores/accounts";
import { READONLY_RELAYS } from "@stores/constants";
import { dateToUnix } from "@utils/date";
import { createChat, createNote, updateAccount } from "@utils/storage";
import { getParentID, nip02ToArray } from "@utils/transform";
import { createNote } from "@utils/storage";
import { getParentID } from "@utils/transform";
import { useContext } from "react";
import useSWRSubscription from "swr/subscription";
@@ -21,20 +21,6 @@ export function EventCollector() {
authors: follows,
since: dateToUnix(),
},
{
kinds: [3],
authors: [account.pubkey],
},
{
kinds: [4],
"#p": [account.pubkey],
since: dateToUnix(),
},
{
kinds: [4],
authors: [account.pubkey],
since: dateToUnix(),
},
],
READONLY_RELAYS,
(event: any) => {
@@ -54,37 +40,6 @@ export function EventCollector() {
);
break;
}
// contacts
case 3: {
const follows = nip02ToArray(event.tags);
// update account's folllows with NIP-02 tag list
updateAccount("follows", follows, event.pubkey);
break;
}
// chat
case 4: {
if (event.pubkey === account.pubkey) {
const receiver = event.tags.find((t) => t[0] === "p")[1];
createChat(
event.id,
receiver,
event.pubkey,
event.content,
event.tags,
event.created_at,
);
} else {
createChat(
event.id,
account.pubkey,
event.pubkey,
event.content,
event.tags,
event.created_at,
);
}
break;
}
// repost
case 6:
createNote(