diff --git a/src/app/chat/pages/index.page.tsx b/src/app/chat/pages/index.page.tsx
index d03d998f..196ab742 100644
--- a/src/app/chat/pages/index.page.tsx
+++ b/src/app/chat/pages/index.page.tsx
@@ -1,13 +1,9 @@
import ChatMessageForm from "@app/chat/components/messages/form";
-
import { RelayContext } from "@shared/relayProvider";
-
+import { useActiveAccount } from "@stores/accounts";
import { chatMessagesAtom } from "@stores/chat";
import { READONLY_RELAYS } from "@stores/constants";
-
-import { useActiveAccount } from "@utils/hooks/useActiveAccount";
import { usePageContext } from "@utils/hooks/usePageContext";
-
import { useSetAtom } from "jotai";
import { useResetAtom } from "jotai/utils";
import { Suspense, lazy, useContext, useEffect } from "react";
@@ -17,13 +13,12 @@ const ChatMessageList = lazy(() => import("@app/chat/components/messageList"));
export function Page() {
const pool: any = useContext(RelayContext);
+ const account = useActiveAccount((state: any) => state.account);
+
const pageContext = usePageContext();
const searchParams: any = pageContext.urlParsed.search;
-
const pubkey = searchParams.pubkey;
- const { account } = useActiveAccount();
-
const setChatMessages = useSetAtom(chatMessagesAtom);
const resetChatMessages = useResetAtom(chatMessagesAtom);
diff --git a/src/app/index/pages/index.page.tsx b/src/app/index/pages/index.page.tsx
index 31c349fa..5c669d28 100644
--- a/src/app/index/pages/index.page.tsx
+++ b/src/app/index/pages/index.page.tsx
@@ -1,17 +1,23 @@
-import { useActiveAccount } from "@utils/hooks/useActiveAccount";
+import { useActiveAccount } from "@stores/accounts";
+import { useEffect } from "react";
import { navigate } from "vite-plugin-ssr/client/router";
export function Page() {
- const { account, isLoading } = useActiveAccount();
+ const fetchAccount = useActiveAccount((state: any) => state.fetch);
+ const account = useActiveAccount((state: any) => state.account);
- if (!isLoading && !account) {
+ if (!account) {
navigate("/app/auth", { overwriteLastHistoryEntry: true });
}
- if (!isLoading && account) {
+ if (account) {
navigate("/app/prefetch", { overwriteLastHistoryEntry: true });
}
+ useEffect(() => {
+ fetchAccount();
+ }, [fetchAccount]);
+
return (
);
diff --git a/src/app/note/components/metadata/like.tsx b/src/app/note/components/metadata/like.tsx
index 3f074abb..955ca5b3 100644
--- a/src/app/note/components/metadata/like.tsx
+++ b/src/app/note/components/metadata/like.tsx
@@ -1,12 +1,8 @@
-import { RelayContext } from "@shared/relayProvider";
-
import LikeIcon from "@icons/like";
-
+import { RelayContext } from "@shared/relayProvider";
+import { useActiveAccount } from "@stores/accounts";
import { WRITEONLY_RELAYS } from "@stores/constants";
-
import { dateToUnix } from "@utils/date";
-import { useActiveAccount } from "@utils/hooks/useActiveAccount";
-
import { getEventHash, getSignature } from "nostr-tools";
import { useContext, useEffect, useState } from "react";
@@ -16,33 +12,31 @@ export default function NoteLike({
likes,
}: { id: string; pubkey: string; likes: number }) {
const pool: any = useContext(RelayContext);
- const { account, isLoading, isError } = useActiveAccount();
+ const account = useActiveAccount((state: any) => state.account);
const [count, setCount] = useState(0);
const submitEvent = (e: any) => {
e.stopPropagation();
- if (!isLoading && !isError && account) {
- const event: any = {
- content: "+",
- kind: 7,
- tags: [
- ["e", id],
- ["p", pubkey],
- ],
- created_at: dateToUnix(),
- pubkey: account.pubkey,
- };
- event.id = getEventHash(event);
- event.sig = getSignature(event, account.privkey);
- // publish event to all relays
- pool.publish(event, WRITEONLY_RELAYS);
- // update state
- setCount(count + 1);
- } else {
- console.log("error");
- }
+ const event: any = {
+ content: "+",
+ kind: 7,
+ tags: [
+ ["e", id],
+ ["p", pubkey],
+ ],
+ created_at: dateToUnix(),
+ pubkey: account.pubkey,
+ };
+
+ event.id = getEventHash(event);
+ event.sig = getSignature(event, account.privkey);
+
+ // publish event to all relays
+ pool.publish(event, WRITEONLY_RELAYS);
+ // update state
+ setCount(count + 1);
};
useEffect(() => {
diff --git a/src/app/note/components/metadata/reply.tsx b/src/app/note/components/metadata/reply.tsx
index 30740a7c..7ccd9f5c 100644
--- a/src/app/note/components/metadata/reply.tsx
+++ b/src/app/note/components/metadata/reply.tsx
@@ -1,14 +1,10 @@
+import { Dialog, Transition } from "@headlessui/react";
+import ReplyIcon from "@icons/reply";
import { Image } from "@shared/image";
import { RelayContext } from "@shared/relayProvider";
-
-import ReplyIcon from "@icons/reply";
-
+import { useActiveAccount } from "@stores/accounts";
import { WRITEONLY_RELAYS } from "@stores/constants";
-
import { dateToUnix } from "@utils/date";
-import { useActiveAccount } from "@utils/hooks/useActiveAccount";
-
-import { Dialog, Transition } from "@headlessui/react";
import { compactNumber } from "@utils/number";
import { getEventHash, getSignature } from "nostr-tools";
import { Fragment, useContext, useEffect, useState } from "react";
@@ -18,13 +14,12 @@ export default function NoteReply({
replies,
}: { id: string; replies: number }) {
const pool: any = useContext(RelayContext);
+ const account = useActiveAccount((state: any) => state.account);
const [count, setCount] = useState(0);
const [isOpen, setIsOpen] = useState(false);
const [value, setValue] = useState("");
- const { account, isLoading, isError } = useActiveAccount();
-
const closeModal = () => {
setIsOpen(false);
};
@@ -34,25 +29,24 @@ export default function NoteReply({
};
const submitEvent = () => {
- if (!isLoading && !isError && account) {
- const event: any = {
- content: value,
- created_at: dateToUnix(),
- kind: 1,
- pubkey: account.pubkey,
- tags: [["e", id]],
- };
- event.id = getEventHash(event);
- event.sig = getSignature(event, account.privkey);
+ const event: any = {
+ content: value,
+ created_at: dateToUnix(),
+ kind: 1,
+ pubkey: account.pubkey,
+ tags: [["e", id]],
+ };
- // publish event
- pool.publish(event, WRITEONLY_RELAYS);
- // close modal
- setIsOpen(false);
- setCount(count + 1);
- } else {
- console.log("error");
- }
+ event.id = getEventHash(event);
+ event.sig = getSignature(event, account.privkey);
+
+ // publish event
+ pool.publish(event, WRITEONLY_RELAYS);
+
+ // close modal
+ setIsOpen(false);
+ // increment replies
+ setCount(count + 1);
};
useEffect(() => {
diff --git a/src/app/note/components/metadata/repost.tsx b/src/app/note/components/metadata/repost.tsx
index f12dd7e9..0bfa3bfb 100644
--- a/src/app/note/components/metadata/repost.tsx
+++ b/src/app/note/components/metadata/repost.tsx
@@ -1,12 +1,8 @@
-import { RelayContext } from "@shared/relayProvider";
-
import RepostIcon from "@icons/repost";
-
+import { RelayContext } from "@shared/relayProvider";
+import { useActiveAccount } from "@stores/accounts";
import { WRITEONLY_RELAYS } from "@stores/constants";
-
import { dateToUnix } from "@utils/date";
-import { useActiveAccount } from "@utils/hooks/useActiveAccount";
-
import { compactNumber } from "@utils/number";
import { getEventHash, getSignature } from "nostr-tools";
import { useContext, useEffect, useState } from "react";
@@ -17,33 +13,32 @@ export default function NoteRepost({
reposts,
}: { id: string; pubkey: string; reposts: number }) {
const pool: any = useContext(RelayContext);
- const { account, isLoading, isError } = useActiveAccount();
+ const account = useActiveAccount((state: any) => state.account);
const [count, setCount] = useState(0);
const submitEvent = (e: any) => {
e.stopPropagation();
- if (!isLoading && !isError && account) {
- const event: any = {
- content: "",
- kind: 6,
- tags: [
- ["e", id],
- ["p", pubkey],
- ],
- created_at: dateToUnix(),
- pubkey: account.pubkey,
- };
- event.id = getEventHash(event);
- event.sig = getSignature(event, account.privkey);
- // publish event to all relays
- pool.publish(event, WRITEONLY_RELAYS);
- // update state
- setCount(count + 1);
- } else {
- console.log("error");
- }
+ const event: any = {
+ content: "",
+ kind: 6,
+ tags: [
+ ["e", id],
+ ["p", pubkey],
+ ],
+ created_at: dateToUnix(),
+ pubkey: account.pubkey,
+ };
+
+ event.id = getEventHash(event);
+ event.sig = getSignature(event, account.privkey);
+
+ // publish event to all relays
+ pool.publish(event, WRITEONLY_RELAYS);
+
+ // update state
+ setCount(count + 1);
};
useEffect(() => {
diff --git a/src/app/note/components/replies/form.tsx b/src/app/note/components/replies/form.tsx
index ec8ca266..a922048a 100644
--- a/src/app/note/components/replies/form.tsx
+++ b/src/app/note/components/replies/form.tsx
@@ -1,39 +1,34 @@
import { Image } from "@shared/image";
import { RelayContext } from "@shared/relayProvider";
-
+import { useActiveAccount } from "@stores/accounts";
import { WRITEONLY_RELAYS } from "@stores/constants";
-
import { dateToUnix } from "@utils/date";
-import { useActiveAccount } from "@utils/hooks/useActiveAccount";
-
import { getEventHash, getSignature } from "nostr-tools";
import { useContext, useState } from "react";
export default function NoteReplyForm({ id }: { id: string }) {
const pool: any = useContext(RelayContext);
+ const account = useActiveAccount((state: any) => state.account);
- const { account, isLoading, isError } = useActiveAccount();
const [value, setValue] = useState("");
const submitEvent = () => {
- if (!isLoading && !isError && account) {
- const event: any = {
- content: value,
- created_at: dateToUnix(),
- kind: 1,
- pubkey: account.pubkey,
- tags: [["e", id]],
- };
- event.id = getEventHash(event);
- event.sig = getSignature(event, account.privkey);
+ const event: any = {
+ content: value,
+ created_at: dateToUnix(),
+ kind: 1,
+ pubkey: account.pubkey,
+ tags: [["e", id]],
+ };
- // publish note
- pool.publish(event, WRITEONLY_RELAYS);
- // reset form
- setValue("");
- } else {
- console.log("error");
- }
+ event.id = getEventHash(event);
+ event.sig = getSignature(event, account.privkey);
+
+ // publish note
+ pool.publish(event, WRITEONLY_RELAYS);
+
+ // reset form
+ setValue("");
};
return (
diff --git a/src/app/prefetch/pages/index.page.tsx b/src/app/prefetch/pages/index.page.tsx
index 109d264a..03513770 100644
--- a/src/app/prefetch/pages/index.page.tsx
+++ b/src/app/prefetch/pages/index.page.tsx
@@ -1,8 +1,8 @@
import LumeIcon from "@icons/lume";
import { RelayContext } from "@shared/relayProvider";
+import { useActiveAccount } from "@stores/accounts";
import { READONLY_RELAYS } from "@stores/constants";
import { dateToUnix, getHourAgo } from "@utils/date";
-import { useActiveAccount } from "@utils/hooks/useActiveAccount";
import {
addToBlacklist,
countTotalNotes,
@@ -15,15 +15,6 @@ import { useCallback, useContext, useRef } from "react";
import useSWRSubscription from "swr/subscription";
import { navigate } from "vite-plugin-ssr/client/router";
-function isJSON(str: string) {
- try {
- JSON.parse(str);
- } catch (e) {
- return false;
- }
- return true;
-}
-
let lastLogin: string;
let totalNotes: number;
@@ -34,12 +25,11 @@ if (typeof window !== "undefined") {
export function Page() {
const pool: any = useContext(RelayContext);
+ const account = useActiveAccount((state: any) => state.account);
const now = useRef(new Date());
const eose = useRef(0);
- const { account, isLoading, isError } = useActiveAccount();
-
const getQuery = useCallback(() => {
const query = [];
const follows = JSON.parse(account.follows);
@@ -79,98 +69,95 @@ export function Page() {
});
return query;
- }, [account.follows]);
+ }, [account]);
- useSWRSubscription(
- !isLoading && !isError && account ? "prefetch" : null,
- () => {
- const query = getQuery();
- const unsubscribe = pool.subscribe(
- query,
- READONLY_RELAYS,
- (event: any) => {
- switch (event.kind) {
- // short text note
- case 1: {
- const parentID = getParentID(event.tags, event.id);
- // insert event to local database
- createNote(
- event.id,
- account.id,
- event.pubkey,
- event.kind,
- event.tags,
- event.content,
- event.created_at,
- parentID,
- );
- break;
+ useSWRSubscription(account ? "prefetch" : null, () => {
+ const query = getQuery();
+ const unsubscribe = pool.subscribe(
+ query,
+ READONLY_RELAYS,
+ (event: any) => {
+ switch (event.kind) {
+ // short text note
+ case 1: {
+ const parentID = getParentID(event.tags, event.id);
+ // insert event to local database
+ createNote(
+ event.id,
+ account.id,
+ event.pubkey,
+ event.kind,
+ event.tags,
+ event.content,
+ event.created_at,
+ parentID,
+ );
+ 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;
+ // hide message (channel only)
+ case 43:
+ if (event.tags[0][0] === "e") {
+ addToBlacklist(account.id, event.tags[0][1], 43, 1);
}
- // 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;
- // hide message (channel only)
- case 43:
- if (event.tags[0][0] === "e") {
- addToBlacklist(account.id, event.tags[0][1], 43, 1);
- }
- break;
- // mute user (channel only)
- case 44:
- if (event.tags[0][0] === "p") {
- addToBlacklist(account.id, event.tags[0][1], 44, 1);
- }
- break;
- case 1063:
- createNote(
- event.id,
- account.id,
- event.pubkey,
- event.kind,
- event.tags,
- event.content,
- event.created_at,
- "",
- );
- break;
- default:
- break;
- }
- },
- undefined,
- () => {
- eose.current += 1;
- if (eose.current === READONLY_RELAYS.length) {
- navigate("/app/space", { overwriteLastHistoryEntry: true });
- }
- },
- );
+ break;
+ // mute user (channel only)
+ case 44:
+ if (event.tags[0][0] === "p") {
+ addToBlacklist(account.id, event.tags[0][1], 44, 1);
+ }
+ break;
+ case 1063:
+ createNote(
+ event.id,
+ account.id,
+ event.pubkey,
+ event.kind,
+ event.tags,
+ event.content,
+ event.created_at,
+ "",
+ );
+ break;
+ default:
+ break;
+ }
+ },
+ undefined,
+ () => {
+ eose.current += 1;
+ if (eose.current === READONLY_RELAYS.length) {
+ navigate("/app/space", { overwriteLastHistoryEntry: true });
+ }
+ },
+ );
- return () => {
- unsubscribe();
- };
- },
- );
+ return () => {
+ unsubscribe();
+ };
+ });
return (
diff --git a/src/app/space/components/create.tsx b/src/app/space/components/create.tsx
index 1a1026d5..40aa7465 100644
--- a/src/app/space/components/create.tsx
+++ b/src/app/space/components/create.tsx
@@ -3,19 +3,15 @@ import { Dialog, Transition } from "@headlessui/react";
import CancelIcon from "@icons/cancel";
import PlusIcon from "@icons/plus";
import { Image } from "@shared/image";
+import { useActiveAccount } from "@stores/accounts";
import { DEFAULT_AVATAR } from "@stores/constants";
-import { useActiveAccount } from "@utils/hooks/useActiveAccount";
-import { createBlock, getPlebs } from "@utils/storage";
+import { createBlock } from "@utils/storage";
import { Fragment, useEffect, useState } from "react";
import { useForm } from "react-hook-form";
-import useSWR from "swr";
-
-const fetcher = () => getPlebs();
export function CreateBlockModal() {
- const { account } = useActiveAccount();
+ const account = useActiveAccount((state: any) => state.account);
const { register, handleSubmit, reset, watch, setValue } = useForm();
- const { data: plebs } = useSWR("plebs", fetcher);
const [image, setImage] = useState(DEFAULT_AVATAR);
const [isOpen, setIsOpen] = useState(false);
diff --git a/src/shared/composer/modal.tsx b/src/shared/composer/modal.tsx
index e5a8c1be..0a260548 100644
--- a/src/shared/composer/modal.tsx
+++ b/src/shared/composer/modal.tsx
@@ -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() {
-
- {!isLoading && !isError && account && (
-
- )}
-
+
{account && }
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 (
diff --git a/src/stores/accounts.tsx b/src/stores/accounts.tsx
new file mode 100644
index 00000000..0f220971
--- /dev/null
+++ b/src/stores/accounts.tsx
@@ -0,0 +1,13 @@
+import { getActiveAccount } from "@utils/storage";
+import { create } from "zustand";
+
+export const useActiveAccount = create((set) => ({
+ account: null,
+ fetch: async () => {
+ const response = await getActiveAccount();
+ set({ account: response });
+ },
+ updateFollows: (list: any) => {
+ set((state: any) => ({ account: { ...state.account, follows: list } }));
+ },
+}));
diff --git a/src/stores/channels.tsx b/src/stores/channels.tsx
new file mode 100644
index 00000000..4af42377
--- /dev/null
+++ b/src/stores/channels.tsx
@@ -0,0 +1,17 @@
+import { getChannels } from "@utils/storage";
+import { create } from "zustand";
+
+export const useChannels = create((set) => ({
+ channels: [],
+ fetch: async () => {
+ const response = await getChannels(10, 0);
+ set({ channels: response });
+ },
+}));
+
+export const useChannelMessage = create((set) => ({
+ messages: [],
+ add: (message: any) => {
+ set((state: any) => ({ messages: [...state.messages, message] }));
+ },
+}));
diff --git a/src/stores/chats.tsx b/src/stores/chats.tsx
new file mode 100644
index 00000000..a6b55f27
--- /dev/null
+++ b/src/stores/chats.tsx
@@ -0,0 +1,21 @@
+import { getChatMessages, getChatsByPubkey } from "@utils/storage";
+import { create } from "zustand";
+
+export const useChats = create((set) => ({
+ chats: [],
+ fetch: async (pubkey: string) => {
+ const response = await getChatsByPubkey(pubkey);
+ set({ chats: response });
+ },
+}));
+
+export const useChatMessages = create((set) => ({
+ messages: [],
+ fetch: async (receiver_pubkey: string, sender_pubkey: string) => {
+ const response = await getChatMessages(receiver_pubkey, sender_pubkey);
+ set({ messages: response });
+ },
+ add: (message: any) => {
+ set((state: any) => ({ messages: [...state.messages, message] }));
+ },
+}));
diff --git a/src/stores/composer.tsx b/src/stores/composer.tsx
deleted file mode 100644
index 63849403..00000000
--- a/src/stores/composer.tsx
+++ /dev/null
@@ -1,3 +0,0 @@
-import { atom } from "jotai";
-
-export const composerAtom = atom({ type: "post" });
diff --git a/src/stores/note.tsx b/src/stores/note.tsx
deleted file mode 100644
index 237d84a9..00000000
--- a/src/stores/note.tsx
+++ /dev/null
@@ -1,8 +0,0 @@
-import { atom } from "jotai";
-import { atomWithReset } from "jotai/utils";
-
-// note content
-export const noteContentAtom = atomWithReset("");
-
-// notify user that connector has receive newer note
-export const hasNewerNoteAtom = atom(false);
diff --git a/src/stores/onboarding.tsx b/src/stores/onboarding.tsx
deleted file mode 100644
index ba81a9b3..00000000
--- a/src/stores/onboarding.tsx
+++ /dev/null
@@ -1,8 +0,0 @@
-import { atom } from "jotai";
-
-export const onboardingAtom = atom({
- pubkey: null,
- privkey: null,
- metadata: null,
- follows: null,
-});
diff --git a/src/utils/hooks/useActiveAccount.tsx b/src/utils/hooks/useActiveAccount.tsx
deleted file mode 100644
index 1db28ae5..00000000
--- a/src/utils/hooks/useActiveAccount.tsx
+++ /dev/null
@@ -1,14 +0,0 @@
-import { getActiveAccount } from "@utils/storage";
-import useSWR from "swr";
-
-const fetcher = () => getActiveAccount();
-
-export function useActiveAccount() {
- const { data, error, isLoading } = useSWR("activeAcount", fetcher);
-
- return {
- account: data,
- isLoading,
- isError: error,
- };
-}
diff --git a/src/utils/storage.tsx b/src/utils/storage.tsx
index 3da2aa78..50c8bccc 100644
--- a/src/utils/storage.tsx
+++ b/src/utils/storage.tsx
@@ -272,7 +272,7 @@ export async function updateChannelMetadata(event_id: string, value: string) {
);
}
-// get all chats
+// get all chats by pubkey
export async function getChatsByPubkey(pubkey: string) {
const db = await connect();
return await db.select(
@@ -280,6 +280,17 @@ export async function getChatsByPubkey(pubkey: string) {
);
}
+// get chat messages
+export async function getChatMessages(
+ receiver_pubkey: string,
+ sender_pubkey: string,
+) {
+ const db = await connect();
+ return await db.select(
+ `SELECT * FROM chats WHERE receiver_pubkey = "${receiver_pubkey}" AND sender_pubkey = "${sender_pubkey}" ORDER BY created_at ASC;`,
+ );
+}
+
// create chat
export async function createChat(
event_id: string,