refactor: extract canAccountSign helper to useAccount

- Move canAccountSign function from relay-state-manager to useAccount.ts
- Import and reuse the shared helper in relay-state-manager
- Update useAccount hook to use the extracted helper internally
- Follows DRY principle by centralizing account sign capability logic

This keeps the account sign capability detection logic in one place,
making it easier to maintain and ensuring consistency across the app.
This commit is contained in:
Claude
2026-01-19 09:11:49 +00:00
parent bac7d2c2a9
commit df2e70f2d1
2 changed files with 15 additions and 21 deletions

View File

@@ -2,6 +2,19 @@ import { useMemo } from "react";
import { use$ } from "applesauce-react/hooks";
import accounts from "@/services/accounts";
/**
* Check if an account can sign events
* Read-only accounts cannot sign and should not be prompted for auth
*
* @param account - The account to check (can be undefined)
* @returns true if the account can sign, false otherwise
*/
export function canAccountSign(account: typeof accounts.active): boolean {
if (!account) return false;
const accountType = account.constructor.name;
return accountType !== "ReadonlyAccount";
}
/**
* Hook to access the active account with signing capability detection
*
@@ -45,18 +58,8 @@ export function useAccount() {
// Check if the account has a functional signer
// Read-only accounts have a signer that throws errors on sign operations
// We detect this by checking for the ReadonlySigner type or checking signer methods
const signer = account.signer;
let canSign = false;
if (signer) {
// ReadonlyAccount from applesauce-accounts has a ReadonlySigner
// that throws on signEvent, nip04, nip44 operations
// We can detect it by checking if it's an instance with the expected methods
// but we'll use a safer approach: check the account type name
const accountType = account.constructor.name;
canSign = accountType !== "ReadonlyAccount";
}
const canSign = canAccountSign(account);
return {
account,

View File

@@ -9,6 +9,7 @@ import type {
import { transitionAuthState, type AuthEvent } from "@/lib/auth-state-machine";
import { createLogger } from "@/lib/logger";
import { normalizeRelayURL } from "@/lib/relay-url";
import { canAccountSign } from "@/hooks/useAccount";
import pool from "./relay-pool";
import accountManager from "./accounts";
import db from "./db";
@@ -19,16 +20,6 @@ const MAX_NOTICES = 20;
const MAX_ERRORS = 20;
const CHALLENGE_TTL = 5 * 60 * 1000; // 5 minutes in milliseconds
/**
* Check if an account can sign events
* Read-only accounts cannot sign and should not be prompted for auth
*/
function canAccountSign(account: typeof accountManager.active): boolean {
if (!account) return false;
const accountType = account.constructor.name;
return accountType !== "ReadonlyAccount";
}
/**
* Observable values emitted by relay observables
* Note: Using startWith() to ensure immediate emission with current values