Skip to content

Commit

Permalink
refactor: account managements (#190)
Browse files Browse the repository at this point in the history
* feat: add keyring-search

* feat: improve nostr connect
  • Loading branch information
reyamir authored May 22, 2024
1 parent 1f38eba commit 407fe40
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 143 deletions.
4 changes: 2 additions & 2 deletions apps/desktop2/src/routes/$account.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import * as Popover from "@radix-ui/react-popover";
export const Route = createFileRoute("/$account")({
beforeLoad: async ({ context }) => {
const ark = context.ark;
const accounts = await ark.get_all_accounts();
const accounts = await ark.get_accounts();

return { accounts };
},
Expand Down Expand Up @@ -106,7 +106,7 @@ function Accounts() {
}

// change current account and update signer
const select = await ark.load_selected_account(npub);
const select = await ark.load_account(npub);

if (select) {
return navigate({ to: "/$account/home", params: { account: npub } });
Expand Down
23 changes: 15 additions & 8 deletions apps/desktop2/src/routes/auth/remote.lazy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,21 @@ function Screen() {
className="h-11 rounded-lg border-transparent bg-neutral-100 px-3 placeholder:text-neutral-600 focus:border-blue-500 focus:ring-0 dark:bg-white/10 dark:placeholder:text-neutral-400"
/>
</div>
<button
type="button"
onClick={() => submit()}
disabled={loading}
className="mt-3 inline-flex h-11 w-full shrink-0 items-center justify-center rounded-lg bg-blue-500 font-semibold text-white hover:bg-blue-600 disabled:opacity-50"
>
{loading ? <Spinner /> : "Login"}
</button>
<div className="flex flex-col gap-1 items-center">
<button
type="button"
onClick={() => submit()}
disabled={loading}
className="mt-3 inline-flex h-11 w-full shrink-0 items-center justify-center rounded-lg bg-blue-500 font-semibold text-white hover:bg-blue-600 disabled:opacity-50"
>
{loading ? <Spinner /> : "Login"}
</button>
{loading ? (
<p className="text-neutral-600 dark:text-neutral-400 text-sm text-center">
Waiting confirmation...
</p>
) : null}
</div>
</div>
</div>
);
Expand Down
4 changes: 2 additions & 2 deletions apps/desktop2/src/routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const Route = createFileRoute("/")({
await checkForAppUpdates(true);

const ark = context.ark;
const accounts = await ark.get_all_accounts();
const accounts = await ark.get_accounts();

if (!accounts.length) {
throw redirect({
Expand All @@ -41,7 +41,7 @@ function Screen() {
try {
setLoading(true);

const loadAccount = await ark.load_selected_account(npub);
const loadAccount = await ark.load_account(npub);
if (loadAccount) {
return navigate({
to: "/$account/home",
Expand Down
2 changes: 1 addition & 1 deletion apps/desktop2/src/routes/settings/backup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const Route = createFileRoute("/settings/backup")({
component: Screen,
loader: async ({ context }) => {
const ark = context.ark;
const npubs = await ark.get_all_accounts();
const npubs = await ark.get_accounts();

const accounts: Account[] = [];

Expand Down
22 changes: 13 additions & 9 deletions packages/ark/src/ark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,26 @@ export class Ark {
this.settings = undefined;
}

public async get_all_accounts() {
public async get_accounts() {
try {
const cmd: string[] = await invoke("get_accounts");
const accounts: string[] = cmd.map((item) => item.replace(".npub", ""));
const cmd: string = await invoke("get_accounts");
const parse = cmd.split(/\s+/).filter((v) => v.startsWith("npub1"));
const accounts = [...new Set(parse)];

if (!this.accounts) this.accounts = accounts;
if (!this.accounts) {
this.accounts = accounts;
}

return accounts;
} catch (e) {
throw new Error(String(e));
console.info(String(e));
return [];
}
}

public async load_selected_account(npub: string) {
public async load_account(npub: string) {
try {
const cmd: boolean = await invoke("load_selected_account", {
const cmd: boolean = await invoke("load_account", {
npub,
});
return cmd;
Expand Down Expand Up @@ -73,7 +77,7 @@ export class Ark {

public async create_keys() {
try {
const cmd: Keys = await invoke("create_keys");
const cmd: Keys = await invoke("create_account");
return cmd;
} catch (e) {
console.error(String(e));
Expand All @@ -82,7 +86,7 @@ export class Ark {

public async save_account(nsec: string, password = "") {
try {
const cmd: string = await invoke("save_key", {
const cmd: string = await invoke("save_account", {
nsec,
password,
});
Expand Down
16 changes: 16 additions & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ tauri-plugin-shell = "2.0.0-beta"
tauri-plugin-updater = "2.0.0-beta"
tauri-plugin-upload = "2.0.0-beta"
tauri-plugin-window-state = "2.0.0-beta"
tauri-plugin-decorum = "0.1.0"
webpage = { version = "2.0", features = ["serde"] }
keyring = "2"
tauri-plugin-decorum = "0.1.0"
keyring-search = "0.2.0"

[target.'cfg(target_os = "macos")'.dependencies]
cocoa = "0.25.0"
Expand Down
24 changes: 0 additions & 24 deletions src-tauri/src/commands/folder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::process::Command;
use tauri::Manager;

#[tauri::command]
pub async fn show_in_folder(path: String) {
Expand Down Expand Up @@ -47,26 +46,3 @@ pub async fn show_in_folder(path: String) {
Command::new("open").args(["-R", &path]).spawn().unwrap();
}
}

#[tauri::command]
pub fn get_accounts(app_handle: tauri::AppHandle) -> Result<Vec<String>, ()> {
let dir = app_handle.path().home_dir().unwrap();

if let Ok(paths) = std::fs::read_dir(dir.join("Lume/")) {
let files = paths
.filter_map(|res| res.ok())
.map(|dir_entry| dir_entry.path())
.filter_map(|path| {
if path.extension().map_or(false, |ext| ext == "npub") {
Some(path.file_name().unwrap().to_str().unwrap().to_string())
} else {
None
}
})
.collect::<Vec<_>>();

Ok(files)
} else {
Err(())
}
}
9 changes: 4 additions & 5 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ fn main() {
nostr::relay::get_relays,
nostr::relay::connect_relay,
nostr::relay::remove_relay,
nostr::keys::create_keys,
nostr::keys::save_key,
nostr::keys::get_accounts,
nostr::keys::create_account,
nostr::keys::save_account,
nostr::keys::get_encrypted_key,
nostr::keys::get_stored_nsec,
nostr::keys::nostr_connect,
nostr::keys::load_selected_account,
nostr::keys::load_account,
nostr::keys::event_to_bech32,
nostr::keys::user_to_bech32,
nostr::keys::to_npub,
Expand Down Expand Up @@ -139,7 +139,6 @@ fn main() {
nostr::event::publish,
nostr::event::repost,
commands::folder::show_in_folder,
commands::folder::get_accounts,
commands::window::create_column,
commands::window::close_column,
commands::window::reposition_column,
Expand Down
Loading

0 comments on commit 407fe40

Please sign in to comment.