mirror of
https://github.com/purrgrammer/grimoire.git
synced 2026-04-15 01:46:53 +02:00
* fix: only prompt relay auth for accounts that can sign - Add canAccountSign() helper to check if account is read-only - Block auth prompts for read-only accounts in shouldPromptAuth() - Throw error when authenticateRelay() called with read-only account - Document all major app hooks in CLAUDE.md for future reference Read-only accounts cannot sign events, so they should never be prompted for relay authentication or attempt to authenticate. This prevents confusing UX where users are asked to sign but cannot. * 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. --------- Co-authored-by: Claude <noreply@anthropic.com>
73 lines
2.3 KiB
TypeScript
73 lines
2.3 KiB
TypeScript
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
|
|
*
|
|
* @returns {object} Account state
|
|
* @property {IAccount | undefined} account - The full active account object
|
|
* @property {string | undefined} pubkey - The account's public key (available for all account types)
|
|
* @property {boolean} canSign - Whether the account can sign events (false for read-only accounts)
|
|
* @property {ISigner | undefined} signer - The signer instance (undefined for read-only accounts)
|
|
* @property {boolean} isLoggedIn - Whether any account is active (including read-only)
|
|
*
|
|
* @example
|
|
* // For read-only operations (viewing profiles, loading data)
|
|
* const { pubkey, isLoggedIn } = useAccount();
|
|
* if (pubkey) {
|
|
* // Load user's relay list, emoji list, etc.
|
|
* }
|
|
*
|
|
* @example
|
|
* // For signing operations (posting, publishing, uploading)
|
|
* const { canSign, signer, pubkey } = useAccount();
|
|
* if (canSign) {
|
|
* // Can publish events
|
|
* await adapter.sendMessage({ activePubkey: pubkey, activeSigner: signer, ... });
|
|
* } else {
|
|
* // Show "log in to post" message
|
|
* }
|
|
*/
|
|
export function useAccount() {
|
|
const account = use$(accounts.active$);
|
|
|
|
return useMemo(() => {
|
|
if (!account) {
|
|
return {
|
|
account: undefined,
|
|
pubkey: undefined,
|
|
canSign: false,
|
|
signer: undefined,
|
|
isLoggedIn: false,
|
|
};
|
|
}
|
|
|
|
// Check if the account has a functional signer
|
|
// Read-only accounts have a signer that throws errors on sign operations
|
|
const signer = account.signer;
|
|
const canSign = canAccountSign(account);
|
|
|
|
return {
|
|
account,
|
|
pubkey: account.pubkey,
|
|
canSign,
|
|
signer: canSign ? signer : undefined,
|
|
isLoggedIn: true,
|
|
};
|
|
}, [account]);
|
|
}
|