import { useContext } from "react"; import { GpgContext, GpgStatusState } from "./GpgContext"; type HookProps = { // What the GPG screen renders: message: string | null; // the challenge to copy/sign command: string | null; // the ready-to-run terminal command // State: status: GpgStatusState; error?: Error | any; isLoading: boolean; isSuccess: boolean; isError: boolean; isReady: boolean; // Actions: signIn: (pastedBlob: string) => Promise; reset: () => void; }; // Consumer-facing hook. Degrades gracefully (returns a disabled shape) when no // GpgProvider is mounted, so non-GPG apps don't crash — same contract as useSIWE. export const useGpg = (): HookProps | any => { const gpg = useContext(GpgContext); if (!gpg) { return { message: null, command: null, status: GpgStatusState.ERROR, error: new Error("useGpg hook must be inside a GpgProvider."), isLoading: false, isSuccess: false, isError: true, isReady: false, signIn: () => Promise.reject(), reset: () => {}, }; } const { nonce, message, command, status, signIn, resetStatus } = gpg; const isLoading = status === GpgStatusState.LOADING || nonce.isLoading; const isSuccess = status === GpgStatusState.SUCCESS; const isError = status === GpgStatusState.ERROR; const isReady = !!message && !isLoading; return { message, command, status, error: nonce.error, isLoading, isSuccess, isError, isReady, signIn, reset: () => resetStatus(), }; };