minor updates

This commit is contained in:
Ren Amamiya
2023-06-07 14:45:20 +07:00
parent 2f3ea13613
commit 75a33d205a
8 changed files with 80 additions and 106 deletions

View File

@@ -4,14 +4,18 @@ import { useActiveAccount } from "@stores/accounts";
import { useChannels } from "@stores/channels";
import { useChatMessages, useChats } from "@stores/chats";
import { DEFAULT_AVATAR, READONLY_RELAYS } from "@stores/constants";
import { dateToUnix } from "@utils/date";
import { usePageContext } from "@utils/hooks/usePageContext";
import { useProfile } from "@utils/hooks/useProfile";
import { sendNativeNotification } from "@utils/notification";
import { createNote } from "@utils/storage";
import { useContext } from "react";
import useSWRSubscription from "swr/subscription";
export function ActiveAccount({ data }: { data: any }) {
const pool: any = useContext(RelayContext);
const account = useActiveAccount((state: any) => state.account);
const pageContext = usePageContext();
const pathname: any = pageContext.urlParsed.pathname;
@@ -27,11 +31,17 @@ export function ActiveAccount({ data }: { data: any }) {
const { user } = useProfile(data.pubkey);
useSWRSubscription(
user && lastLogin > 0 ? ["account", data.pubkey] : null,
user && lastLogin > 0 ? ["activeAccount", data.pubkey] : null,
() => {
const follows = JSON.parse(account.follows);
// subscribe to channel
const unsubscribe = pool.subscribe(
[
{
kinds: [1, 6],
authors: follows,
since: dateToUnix(),
},
{
"#p": [data.pubkey],
since: lastLogin,
@@ -41,7 +51,18 @@ export function ActiveAccount({ data }: { data: any }) {
(event) => {
switch (event.kind) {
case 1:
case 6: {
createNote(
event.id,
account.id,
event.pubkey,
event.kind,
event.tags,
event.content,
event.created_at,
);
break;
}
case 4:
if (!isChatPage) {
// save

View File

@@ -1,72 +0,0 @@
import { HeartBeatIcon } from "@shared/icons";
import { RelayContext } from "@shared/relayProvider";
import { useActiveAccount } from "@stores/accounts";
import { READONLY_RELAYS } from "@stores/constants";
import { dateToUnix } from "@utils/date";
import { createNote } from "@utils/storage";
import { getParentID } from "@utils/transform";
import { useContext } from "react";
import useSWRSubscription from "swr/subscription";
export function EventCollector() {
const pool: any = useContext(RelayContext);
const account = useActiveAccount((state: any) => state.account);
useSWRSubscription(account ? "eventCollector" : null, () => {
const follows = JSON.parse(account.follows);
const unsubscribe = pool.subscribe(
[
{
kinds: [1, 6],
authors: follows,
since: dateToUnix(),
},
],
READONLY_RELAYS,
(event: any) => {
switch (event.kind) {
// short text note
case 1: {
const parentID = getParentID(event.tags, event.id);
createNote(
event.id,
account.id,
event.pubkey,
event.kind,
event.tags,
event.content,
event.created_at,
parentID,
);
break;
}
// repost
case 6:
createNote(
event.id,
account.id,
event.pubkey,
event.kind,
event.tags,
event.content,
event.created_at,
event.id,
);
break;
default:
break;
}
},
);
return () => {
unsubscribe();
};
});
return (
<div className="inline-flex h-6 w-6 items-center justify-center rounded text-zinc-500 hover:bg-zinc-900 hover:text-green-500">
<HeartBeatIcon width={16} height={16} />
</div>
);
}