wip: migrate to zustand

This commit is contained in:
Ren Amamiya
2023-05-26 14:45:12 +07:00
parent 5c7b18bf29
commit 671b857077
34 changed files with 494 additions and 530 deletions

View File

@@ -1,24 +1,18 @@
import { Post } from "@shared/composer/types/post";
import { User } from "@shared/composer/user";
import { Dialog, Transition } from "@headlessui/react";
import CancelIcon from "@icons/cancel";
import ChevronDownIcon from "@icons/chevronDown";
import ChevronRightIcon from "@icons/chevronRight";
import ComposeIcon from "@icons/compose";
import { composerAtom } from "@stores/composer";
import { useActiveAccount } from "@utils/hooks/useActiveAccount";
import { Dialog, Transition } from "@headlessui/react";
import { useAtom } from "jotai";
import { Post } from "@shared/composer/types/post";
import { User } from "@shared/composer/user";
import { useActiveAccount } from "@stores/accounts";
import { Fragment, useState } from "react";
export function ComposerModal() {
const [isOpen, setIsOpen] = useState(false);
const [composer] = useAtom(composerAtom);
const [composer] = useState({ type: "post" });
const { account, isLoading, isError } = useActiveAccount();
const account = useActiveAccount((state: any) => state.account);
const closeModal = () => {
setIsOpen(false);
@@ -64,11 +58,7 @@ export function ComposerModal() {
<Dialog.Panel className="relative h-min w-full max-w-xl rounded-lg border border-zinc-800 bg-zinc-900">
<div className="flex items-center justify-between px-4 py-4">
<div className="flex items-center gap-2">
<div>
{!isLoading && !isError && account && (
<User data={account} />
)}
</div>
<div>{account && <User data={account} />}</div>
<span>
<ChevronRightIcon
width={14}

View File

@@ -1,141 +1,119 @@
import { RelayContext } from "@shared/relayProvider";
import HeartBeatIcon from "@icons/heartbeat";
import { RelayContext } from "@shared/relayProvider";
import { useActiveAccount } from "@stores/accounts";
import { READONLY_RELAYS } from "@stores/constants";
import { hasNewerNoteAtom } from "@stores/note";
import { TauriEvent } from "@tauri-apps/api/event";
import { appWindow, getCurrent } from "@tauri-apps/api/window";
import { dateToUnix } from "@utils/date";
import { useActiveAccount } from "@utils/hooks/useActiveAccount";
import { createChat, createNote, updateAccount } from "@utils/storage";
import {
createChat,
createNote,
updateAccount,
updateLastLogin,
} from "@utils/storage";
import { getParentID, nip02ToArray } from "@utils/transform";
import { useSetAtom } from "jotai";
import { useContext, useRef } from "react";
import { useContext, useEffect, useRef } from "react";
import useSWRSubscription from "swr/subscription";
function isJSON(str: string) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}
export default function EventCollector() {
const pool: any = useContext(RelayContext);
const setHasNewerNote = useSetAtom(hasNewerNoteAtom);
const account = useActiveAccount((state: any) => state.account);
const now = useRef(new Date());
const { account, isLoading, isError } = useActiveAccount();
useSWRSubscription(
!isLoading && !isError && account ? ["eventCollector", account] : null,
([, key]) => {
const follows = JSON.parse(key.follows);
const followsAsArray = nip02ToArray(follows);
const unsubscribe = pool.subscribe(
[
{
kinds: [1, 6],
authors: followsAsArray,
since: dateToUnix(now.current),
},
{
kinds: [3],
authors: [key.pubkey],
},
{
kinds: [4],
"#p": [key.pubkey],
since: dateToUnix(now.current),
},
{
kinds: [30023],
since: dateToUnix(now.current),
},
],
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,
);
// notify user reload to get newer note
setHasNewerNote(true);
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:
createChat(
event.id,
key.pubkey,
event.pubkey,
event.content,
event.created_at,
);
break;
// repost
case 6:
createNote(
event.id,
key.id,
event.pubkey,
event.kind,
event.tags,
event.content,
event.created_at,
event.id,
);
break;
// long post
case 30023: {
const verifyMetadata = isJSON(event.tags);
if (verifyMetadata) {
// insert event to local database
createNote(
event.id,
account.id,
event.pubkey,
event.kind,
event.tags,
event.content,
event.created_at,
"",
);
}
break;
}
default:
break;
}
useSWRSubscription(account ? "eventCollector" : null, () => {
const follows = JSON.parse(account.follows);
const unsubscribe = pool.subscribe(
[
{
kinds: [1, 6],
authors: follows,
since: dateToUnix(now.current),
},
);
{
kinds: [3],
authors: [account.pubkey],
},
{
kinds: [4],
"#p": [account.pubkey],
since: dateToUnix(now.current),
},
],
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,
);
// notify user reload to get newer note
setHasNewerNote(true);
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:
createChat(
event.id,
account.pubkey,
event.pubkey,
event.content,
event.created_at,
);
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 () => {
unsubscribe();
};
});
useEffect(() => {
// listen window close event
getCurrent().listen(TauriEvent.WINDOW_CLOSE_REQUESTED, () => {
// update last login time
updateLastLogin(dateToUnix(now.current));
// close window
appWindow.close();
});
}, []);
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">