wip: rework multi account

This commit is contained in:
2024-10-24 07:59:41 +07:00
parent c032dbea1a
commit 469296790e
11 changed files with 245 additions and 304 deletions

View File

@@ -48,15 +48,15 @@ async saveBootstrapRelays(relays: string) : Promise<Result<null, string>> {
async getAccounts() : Promise<string[]> {
return await TAURI_INVOKE("get_accounts");
},
async watchAccount(key: string) : Promise<Result<string, string>> {
async watchAccount(id: string) : Promise<Result<string, string>> {
try {
return { status: "ok", data: await TAURI_INVOKE("watch_account", { key }) };
return { status: "ok", data: await TAURI_INVOKE("watch_account", { id }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
}
},
async importAccount(key: string, password: string) : Promise<Result<string, string>> {
async importAccount(key: string, password: string | null) : Promise<Result<string, string>> {
try {
return { status: "ok", data: await TAURI_INVOKE("import_account", { key, password }) };
} catch (e) {
@@ -104,15 +104,15 @@ async hasSigner(id: string) : Promise<Result<boolean, string>> {
else return { status: "error", error: e as any };
}
},
async setSigner(account: string, password: string) : Promise<Result<null, string>> {
async setSigner(id: string) : Promise<Result<null, string>> {
try {
return { status: "ok", data: await TAURI_INVOKE("set_signer", { account, password }) };
return { status: "ok", data: await TAURI_INVOKE("set_signer", { id }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
}
},
async getProfile(id: string | null) : Promise<Result<string, string>> {
async getProfile(id: string) : Promise<Result<string, string>> {
try {
return { status: "ok", data: await TAURI_INVOKE("get_profile", { id }) };
} catch (e) {
@@ -168,7 +168,7 @@ async getAllProfiles() : Promise<Result<Mention[], string>> {
else return { status: "error", error: e as any };
}
},
async setGroup(title: string, description: string | null, image: string | null, users: string[]) : Promise<Result<string, string>> {
async setGroup(title: string, description: string | null, image: string | null, users: string[]) : Promise<Result<boolean, string>> {
try {
return { status: "ok", data: await TAURI_INVOKE("set_group", { title, description, image, users }) };
} catch (e) {
@@ -192,7 +192,7 @@ async getAllGroups() : Promise<Result<RichEvent[], string>> {
else return { status: "error", error: e as any };
}
},
async setInterest(title: string, description: string | null, image: string | null, hashtags: string[]) : Promise<Result<string, string>> {
async setInterest(title: string, description: string | null, image: string | null, hashtags: string[]) : Promise<Result<boolean, string>> {
try {
return { status: "ok", data: await TAURI_INVOKE("set_interest", { title, description, image, hashtags }) };
} catch (e) {
@@ -384,7 +384,7 @@ async search(query: string) : Promise<Result<RichEvent[], string>> {
else return { status: "error", error: e as any };
}
},
async publish(content: string, warning: string | null, difficulty: number | null) : Promise<Result<string, string>> {
async publish(content: string, warning: string | null, difficulty: number | null) : Promise<Result<boolean, string>> {
try {
return { status: "ok", data: await TAURI_INVOKE("publish", { content, warning, difficulty }) };
} catch (e) {
@@ -392,7 +392,7 @@ async publish(content: string, warning: string | null, difficulty: number | null
else return { status: "error", error: e as any };
}
},
async reply(content: string, to: string, root: string | null) : Promise<Result<string, string>> {
async reply(content: string, to: string, root: string | null) : Promise<Result<boolean, string>> {
try {
return { status: "ok", data: await TAURI_INVOKE("reply", { content, to, root }) };
} catch (e) {
@@ -400,7 +400,7 @@ async reply(content: string, to: string, root: string | null) : Promise<Result<s
else return { status: "error", error: e as any };
}
},
async repost(raw: string) : Promise<Result<string, string>> {
async repost(raw: string) : Promise<Result<boolean, string>> {
try {
return { status: "ok", data: await TAURI_INVOKE("repost", { raw }) };
} catch (e) {
@@ -501,11 +501,9 @@ async quit() : Promise<void> {
export const events = __makeEvents__<{
negentropyEvent: NegentropyEvent,
newSettings: NewSettings,
subscription: Subscription
}>({
negentropyEvent: "negentropy-event",
newSettings: "new-settings",
subscription: "subscription"
})
@@ -520,13 +518,12 @@ export type Mention = { pubkey: string; avatar: string; display_name: string; na
export type Meta = { content: string; images: string[]; events: string[]; mentions: string[]; hashtags: string[] }
export type NegentropyEvent = { kind: NegentropyKind; total_event: number }
export type NegentropyKind = "Profile" | "Metadata" | "Events" | "EventIds" | "Global" | "Notification" | "Others"
export type NewSettings = Settings
export type Profile = { name: string; display_name: string; about: string | null; picture: string; banner: string | null; nip05: string | null; lud16: string | null; website: string | null }
export type Relays = { connected: string[]; read: string[] | null; write: string[] | null; both: string[] | null }
export type RichEvent = { raw: string; parsed: Meta | null }
export type Settings = { proxy: string | null; image_resize_service: string | null; use_relay_hint: boolean; content_warning: boolean; trusted_only: boolean; display_avatar: boolean; display_zap_button: boolean; display_repost_button: boolean; display_media: boolean; transparent: boolean }
export type SubKind = "Subscribe" | "Unsubscribe"
export type Subscription = { label: string; kind: SubKind; event_id: string | null; contacts: string[] | null }
export type Subscription = { label: string; kind: SubscriptionMethod; event_id: string | null; contacts: string[] | null }
export type SubscriptionMethod = "Subscribe" | "Unsubscribe"
export type Window = { label: string; title: string; url: string; width: number; height: number; maximizable: boolean; minimizable: boolean; hidden_title: boolean; closable: boolean }
/** tauri-specta globals **/

View File

@@ -20,6 +20,7 @@ export const Route = createRootRouteWithContext<RouterContext>()({
function Screen() {
const { queryClient } = Route.useRouteContext();
/*
useEffect(() => {
const unlisten = events.newSettings.listen((data) => {
appSettings.setState((state) => {
@@ -31,6 +32,7 @@ function Screen() {
unlisten.then((f) => f());
};
}, []);
*/
useEffect(() => {
const unlisten = events.negentropyEvent.listen(async (data) => {

View File

@@ -9,7 +9,7 @@ import { Link, Outlet, createLazyFileRoute } from "@tanstack/react-router";
import { listen } from "@tauri-apps/api/event";
import { Menu, MenuItem, PredefinedMenuItem } from "@tauri-apps/api/menu";
import { writeText } from "@tauri-apps/plugin-clipboard-manager";
import { memo, useCallback, useEffect, useState } from "react";
import { useCallback, useEffect } from "react";
export const Route = createLazyFileRoute("/_layout")({
component: Layout,
@@ -80,41 +80,6 @@ function Topbar() {
);
}
const NegentropyBadge = memo(function NegentropyBadge() {
const [process, setProcess] = useState(null);
useEffect(() => {
const unlisten = listen("negentropy", async (data) => {
if (data.payload === "Ok") {
setProcess(null);
} else {
setProcess(data.payload);
}
});
return () => {
unlisten.then((f) => f());
};
}, []);
if (!process) {
return null;
}
return (
<div className="h-7 w-max px-3 inline-flex items-center justify-center text-[9px] font-medium rounded-full bg-black/5 dark:bg-white/5">
{process ? (
<span>
{process.message}
{process.total_event > 0 ? ` / ${process.total_event}` : null}
</span>
) : (
"Syncing"
)}
</div>
);
});
function Account({ pubkey }: { pubkey: string }) {
const navigate = Route.useNavigate();
const context = Route.useRouteContext();
@@ -183,6 +148,16 @@ function Account({ pubkey }: { pubkey: string }) {
[pubkey],
);
useEffect(() => {
const unlisten = listen("signer-updated", async () => {
await context.queryClient.invalidateQueries({ queryKey: ["signer"] });
});
return () => {
unlisten.then((f) => f());
};
}, []);
return (
<button
type="button"

View File

@@ -24,7 +24,7 @@ function Screen() {
const submit = () => {
startTransition(async () => {
if (!key.startsWith("nsec1") && !key.startsWith("ncryptsec")) {
if (!key.startsWith("nsec") && !key.startsWith("ncryptsec")) {
await message(
"You need to enter a valid private key starts with nsec or ncryptsec",
{ title: "Login", kind: "info" },
@@ -32,15 +32,18 @@ function Screen() {
return;
}
if (key.startsWith("nsec1") && !password.length) {
await message("You must set password to secure your key", {
if (key.startsWith("ncryptsec") && !password.length) {
await message("You must enter a password to decrypt your key", {
title: "Login",
kind: "info",
});
return;
}
const res = await commands.importAccount(key, password);
const res = await commands.importAccount(
key,
password.length ? password : null,
);
if (res.status === "ok") {
navigate({ to: "/", replace: true });
@@ -94,15 +97,13 @@ function Screen() {
</button>
</div>
</div>
{key.length ? (
{key.startsWith("ncryptsec") ? (
<div className="flex flex-col gap-1">
<label
htmlFor="password"
className="text-sm font-medium text-neutral-800 dark:text-neutral-200"
>
{!key.startsWith("ncryptsec")
? "Set password to secure your key"
: "Enter password to decrypt your key"}
Enter password to decrypt your key
</label>
<input
name="password"

View File

@@ -26,7 +26,7 @@ function Screen() {
if (!key.startsWith("npub") && !key.startsWith("nprofile")) {
await message(
"You need to enter a valid public key starts with npub or nprofile",
{ title: "Login", kind: "info" },
{ kind: "info" },
);
return;
}

View File

@@ -29,7 +29,7 @@ export function Screen() {
queryKey: ["events", "newsfeed", search.label],
initialPageParam: 0,
queryFn: async ({ pageParam }: { pageParam: number }) => {
const until = pageParam > 0 ? pageParam.toString() : undefined;
const until = pageParam > 0 ? pageParam.toString() : null;
const res = await commands.getAllEventsByAuthors(contacts, until);
if (res.status === "error") {
@@ -76,9 +76,9 @@ export function Screen() {
useEffect(() => {
events.subscription
.emit({
label: search.label,
label: search.label as string,
kind: "Subscribe",
event_id: undefined,
event_id: null,
contacts,
})
.then(() => console.log("Subscribe: ", search.label));
@@ -86,9 +86,9 @@ export function Screen() {
return () => {
events.subscription
.emit({
label: search.label,
label: search.label as string,
kind: "Unsubscribe",
event_id: undefined,
event_id: null,
contacts,
})
.then(() => console.log("Unsubscribe: ", search.label));