Skip to content

Commit

Permalink
Merge pull request #23 from ecency/feature/get-rid-of-ls
Browse files Browse the repository at this point in the history
Use abstract storage instead of local's one
  • Loading branch information
feruzm authored Apr 11, 2024
2 parents 4f76771 + 3819bf7 commit f4f1820
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 11 deletions.
4 changes: 4 additions & 0 deletions lib/chat-context-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ interface Context {
activeUserData: AccountData | undefined;
privateApiHost: string;
ecencyAccessToken: string;
storage?: Storage;
}

interface Props {
activeUsername?: string;
activeUserData?: AccountData;
privateApiHost: string;
ecencyAccessToken: string;
storage?: Storage;
}

export const ChatContext = createContext<Context>({
Expand All @@ -50,6 +52,7 @@ export const ChatContextProvider = (props: PropsWithChildren<Props>) => {
props.ecencyAccessToken,
);
const [privateApiHost, setPrivateApiHost] = useState(props.privateApiHost);
const [storage, setStorage] = useState<Storage | undefined>(props.storage);

useEffect(() => {
setEcencyAccessToken(props.ecencyAccessToken);
Expand All @@ -75,6 +78,7 @@ export const ChatContextProvider = (props: PropsWithChildren<Props>) => {
activeUserData,
privateApiHost,
ecencyAccessToken,
storage,
}}
>
<NostrProvider>
Expand Down
4 changes: 2 additions & 2 deletions lib/mutations/import-chat-by-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function useImportChatByKeys(
meta?: Record<string, unknown>,
) {
const queryClient = useQueryClient();
const { activeUsername, activeUserData } = useContext(ChatContext);
const { activeUsername, storage } = useContext(ChatContext);

const { mutateAsync: uploadChatKeys } = useSaveKeys();
const { mutateAsync: uploadKeys } = useMutation(
Expand Down Expand Up @@ -79,7 +79,7 @@ export function useImportChatByKeys(
iv: initialVector,
});

localStorage.setItem("ecency_nostr_pr_" + activeUsername, pin);
storage?.setItem("ecency_nostr_pr_" + activeUsername, pin);
queryClient.setQueryData(
[NostrQueries.PUBLIC_KEY, activeUsername],
publicKey,
Expand Down
4 changes: 2 additions & 2 deletions lib/mutations/join-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function useJoinChat(
meta?: Record<string, unknown>,
) {
const queryClient = useQueryClient();
const { activeUsername } = useContext(ChatContext);
const { activeUsername, storage } = useContext(ChatContext);

const { mutateAsync: updateProfile } = useNostrPublishMutation(
["chats/update-nostr-profile"],
Expand All @@ -37,7 +37,7 @@ export function useJoinChat(
["chat-join-chat"],
async (pin: string) => {
const keys = createNoStrAccount();
localStorage.setItem("ecency_nostr_pr_" + activeUsername, pin);
storage?.setItem("ecency_nostr_pr_" + activeUsername, pin);

const initialVector = crypto.randomBytes(16);
const encryptedKey = EncryptionTools.encrypt(
Expand Down
4 changes: 2 additions & 2 deletions lib/mutations/logout-from-chats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import { ChatContext } from "../chat-context-provider";

export function useLogoutFromChats() {
const queryClient = useQueryClient();
const { activeUsername } = useContext(ChatContext);
const { activeUsername, storage } = useContext(ChatContext);

return useMutation(["chats/logout-from-chats"], async () => {
localStorage.removeItem("ecency_nostr_pr_" + activeUsername);
storage?.removeItem("ecency_nostr_pr_" + activeUsername);
queryClient.setQueryData([NostrQueries.PUBLIC_KEY, activeUsername], "");
queryClient.setQueryData([NostrQueries.PRIVATE_KEY, activeUsername], "");
queryClient.setQueryData([ChatQueries.JOINED_CHANNELS, activeUsername], []);
Expand Down
4 changes: 2 additions & 2 deletions lib/mutations/restore-chat-by-pin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useGetKeysQuery, useSaveKeys } from "../api";

export function useRestoreChatByPin() {
const queryClient = useQueryClient();
const { activeUserData, activeUsername } = useContext(ChatContext);
const { activeUserData, activeUsername, storage } = useContext(ChatContext);

const { publicKey } = useKeysQuery();
const { data: keys } = useGetKeysQuery();
Expand Down Expand Up @@ -43,7 +43,7 @@ export function useRestoreChatByPin() {
decryptedKey,
);

localStorage.setItem("ecency_nostr_pr_" + activeUsername, pin);
storage?.setItem("ecency_nostr_pr_" + activeUsername, pin);

await updateProfile({
tags: [["p", publicKey]],
Expand Down
4 changes: 2 additions & 2 deletions lib/nostr/core/keys-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ChatContext } from "../../chat-context-provider";
import { useGetKeysQuery } from "../../api";

export function useKeysQuery() {
const { activeUsername, activeUserData } = useContext(ChatContext);
const { activeUsername, activeUserData, storage } = useContext(ChatContext);
const getKeysQuery = useGetKeysQuery();

useMount(() => {
Expand All @@ -25,7 +25,7 @@ export function useKeysQuery() {
{
queryKey: [NostrQueries.PRIVATE_KEY, activeUsername],
queryFn: async () => {
const pin = localStorage.getItem("ecency_nostr_pr_" + activeUsername);
const pin = storage?.getItem("ecency_nostr_pr_" + activeUsername);

if (!pin) {
return null;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@ecency/ns-query",
"description": "React-query based Nostr protocol SDK for Ecency vision and mobile",
"version": "1.2.2",
"version": "1.2.3",
"repository": "https://github.com/ecency/ns-query",
"author": "ildar.timerbaev <dkildar@gmail.com>",
"license": "MIT",
Expand Down

0 comments on commit f4f1820

Please sign in to comment.