wip: refactor

This commit is contained in:
2024-02-17 16:01:53 +07:00
parent f47eba5af7
commit 0f06522c28
18 changed files with 168 additions and 525 deletions

View File

@@ -15,14 +15,20 @@ export class Ark {
this.account = { npub: "" };
}
public async load_account() {
public async verify_signer() {
try {
const cmd: string = await invoke("load_account");
if (cmd) {
this.account.npub = cmd;
const signer: boolean = await invoke("verify_signer");
if (signer) {
const account: string = await invoke("load_account");
this.account.npub = account;
return true;
} else {
return false;
}
} catch (e) {
console.error(String(e));
return false;
}
}

View File

@@ -0,0 +1,4 @@
import { createContext } from "react";
import { type Ark } from "./ark";
export const ArkContext = createContext<Ark | null>(undefined);

View File

@@ -0,0 +1,10 @@
import { useContext } from "react";
import { ArkContext } from "../context";
export const useArk = () => {
const context = useContext(ArkContext);
if (context === undefined) {
throw new Error("useArk must be used within an ArkProvider");
}
return context;
};

View File

@@ -1,5 +1,5 @@
import { useQuery } from "@tanstack/react-query";
import { useArk } from "../provider";
import { useArk } from "./useArk";
export function useEvent(id: string) {
const ark = useArk();

View File

@@ -1,5 +1,5 @@
import { useQuery } from "@tanstack/react-query";
import { useArk } from "../provider";
import { useArk } from "./useArk";
export function useProfile(pubkey: string) {
const ark = useArk();

View File

@@ -1,3 +1,5 @@
export * from "./provider";
export * from "./ark";
export * from "./context";
export * from "./hooks/useArk";
export * from "./hooks/useEvent";
export * from "./hooks/useProfile";

View File

@@ -1,17 +0,0 @@
import { PropsWithChildren, createContext, useContext, useMemo } from "react";
import { Ark } from "./ark";
export const ArkContext = createContext<Ark>(undefined);
export const ArkProvider = ({ children }: PropsWithChildren<object>) => {
const ark = useMemo(() => new Ark(), []);
return <ArkContext.Provider value={ark}>{children}</ArkContext.Provider>;
};
export const useArk = () => {
const context = useContext(ArkContext);
if (context === undefined) {
throw new Error("Ark Provider is not import");
}
return context;
};

View File

@@ -22,6 +22,7 @@ export class LumeStorage {
translateApiKey: "",
instantZap: false,
defaultZapAmount: 21,
nwc: "",
};
}