mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-05 16:33:36 +00:00
Review fixes
This commit is contained in:
1 parent
d462a8d679
commit
45cdcecf9e
3 files changed
+8
-73
No files matched your search
@@ -69,7 +69,6 @@ AddWalletContent.displayName = 'AddWalletContent';
|
|||||||
// Extracted WalletContent to prevent re-renders
|
// Extracted WalletContent to prevent re-renders
|
||||||
const WalletContent = forwardRef<HTMLDivElement, {
|
const WalletContent = forwardRef<HTMLDivElement, {
|
||||||
hasWebLN: boolean;
|
hasWebLN: boolean;
|
||||||
isDetecting: boolean;
|
|
||||||
hasNWC: boolean;
|
hasNWC: boolean;
|
||||||
connections: NWCConnection[];
|
connections: NWCConnection[];
|
||||||
connectionInfo: Record<string, NWCInfo>;
|
connectionInfo: Record<string, NWCInfo>;
|
||||||
@@ -79,7 +78,6 @@ const WalletContent = forwardRef<HTMLDivElement, {
|
|||||||
setAddDialogOpen: (open: boolean) => void;
|
setAddDialogOpen: (open: boolean) => void;
|
||||||
}>(({
|
}>(({
|
||||||
hasWebLN,
|
hasWebLN,
|
||||||
isDetecting,
|
|
||||||
hasNWC,
|
hasNWC,
|
||||||
connections,
|
connections,
|
||||||
connectionInfo,
|
connectionInfo,
|
||||||
@@ -105,7 +103,7 @@ const WalletContent = forwardRef<HTMLDivElement, {
|
|||||||
<div className="flex items-center gap-2">
|
<div className="flex items-center gap-2">
|
||||||
{hasWebLN && <CheckCircle className="h-4 w-4 text-green-600" />}
|
{hasWebLN && <CheckCircle className="h-4 w-4 text-green-600" />}
|
||||||
<Badge variant={hasWebLN ? "default" : "secondary"} className="text-xs">
|
<Badge variant={hasWebLN ? "default" : "secondary"} className="text-xs">
|
||||||
{isDetecting ? "..." : hasWebLN ? "Ready" : "Not Found"}
|
{hasWebLN ? "Ready" : "Not Found"}
|
||||||
</Badge>
|
</Badge>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -222,7 +220,7 @@ export function WalletModal({ children, className }: WalletModalProps) {
|
|||||||
setActiveConnection
|
setActiveConnection
|
||||||
} = useNWC();
|
} = useNWC();
|
||||||
|
|
||||||
const { hasWebLN, isDetecting } = useWallet();
|
const { hasWebLN } = useWallet();
|
||||||
|
|
||||||
const hasNWC = connections.length > 0 && connections.some(c => c.isConnected);
|
const hasNWC = connections.length > 0 && connections.some(c => c.isConnected);
|
||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
@@ -264,7 +262,6 @@ export function WalletModal({ children, className }: WalletModalProps) {
|
|||||||
|
|
||||||
const walletContentProps = {
|
const walletContentProps = {
|
||||||
hasWebLN,
|
hasWebLN,
|
||||||
isDetecting,
|
|
||||||
hasNWC,
|
hasNWC,
|
||||||
connections,
|
connections,
|
||||||
connectionInfo,
|
connectionInfo,
|
||||||
|
|||||||
@@ -234,13 +234,12 @@ const ZapContent = forwardRef<HTMLDivElement, ZapContentProps>(({
|
|||||||
));
|
));
|
||||||
ZapContent.displayName = 'ZapContent';
|
ZapContent.displayName = 'ZapContent';
|
||||||
|
|
||||||
|
|
||||||
export function ZapDialog({ target, children, className }: ZapDialogProps) {
|
export function ZapDialog({ target, children, className }: ZapDialogProps) {
|
||||||
const [open, setOpen] = useState(false);
|
const [open, setOpen] = useState(false);
|
||||||
const { user } = useCurrentUser();
|
const { user } = useCurrentUser();
|
||||||
const { data: author } = useAuthor(target.pubkey);
|
const { data: author } = useAuthor(target.pubkey);
|
||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
const { webln, activeNWC, hasWebLN, detectWebLN } = useWallet();
|
const { webln, activeNWC, hasWebLN } = useWallet();
|
||||||
const { zap, isZapping, invoice, setInvoice } = useZaps(target, webln, activeNWC, () => setOpen(false));
|
const { zap, isZapping, invoice, setInvoice } = useZaps(target, webln, activeNWC, () => setOpen(false));
|
||||||
const [amount, setAmount] = useState<number | string>(100);
|
const [amount, setAmount] = useState<number | string>(100);
|
||||||
const [comment, setComment] = useState<string>('');
|
const [comment, setComment] = useState<string>('');
|
||||||
@@ -255,13 +254,6 @@ export function ZapDialog({ target, children, className }: ZapDialogProps) {
|
|||||||
}
|
}
|
||||||
}, [target]);
|
}, [target]);
|
||||||
|
|
||||||
// Detect WebLN when dialog opens
|
|
||||||
useEffect(() => {
|
|
||||||
if (open && !hasWebLN) {
|
|
||||||
detectWebLN();
|
|
||||||
}
|
|
||||||
}, [open, hasWebLN, detectWebLN]);
|
|
||||||
|
|
||||||
// Generate QR code
|
// Generate QR code
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let isCancelled = false;
|
let isCancelled = false;
|
||||||
|
|||||||
+5
-59
@@ -7,69 +7,21 @@ export interface WalletStatus {
|
|||||||
hasNWC: boolean;
|
hasNWC: boolean;
|
||||||
webln: WebLNProvider | null;
|
webln: WebLNProvider | null;
|
||||||
activeNWC: ReturnType<typeof useNWC>['getActiveConnection'] extends () => infer T ? T : null;
|
activeNWC: ReturnType<typeof useNWC>['getActiveConnection'] extends () => infer T ? T : null;
|
||||||
isDetecting: boolean;
|
|
||||||
preferredMethod: 'nwc' | 'webln' | 'manual';
|
preferredMethod: 'nwc' | 'webln' | 'manual';
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useWallet() {
|
export function useWallet() {
|
||||||
const [webln, setWebln] = useState<WebLNProvider | null>(null);
|
const [webln, setWebln] = useState<WebLNProvider | null>(null);
|
||||||
const [isDetecting, setIsDetecting] = useState(false);
|
|
||||||
const [hasAttemptedDetection, setHasAttemptedDetection] = useState(false);
|
|
||||||
const { connections, getActiveConnection } = useNWC();
|
const { connections, getActiveConnection } = useNWC();
|
||||||
|
|
||||||
// Get the active connection directly - no memoization to avoid stale state
|
// Get the active connection directly - no memoization to avoid stale state
|
||||||
const activeNWC = getActiveConnection();
|
const activeNWC = getActiveConnection();
|
||||||
|
|
||||||
// Detect WebLN
|
// Detect WebLN synchronously on mount
|
||||||
const detectWebLN = useCallback(async () => {
|
|
||||||
if (webln || isDetecting) return webln;
|
|
||||||
|
|
||||||
setIsDetecting(true);
|
|
||||||
try {
|
|
||||||
// Check if native WebLN is available without enabling it yet
|
|
||||||
const webLn = (window as Window & { webln?: WebLNProvider }).webln;
|
|
||||||
if (webLn) {
|
|
||||||
setWebln(webLn);
|
|
||||||
setHasAttemptedDetection(true);
|
|
||||||
return webLn;
|
|
||||||
} else {
|
|
||||||
throw new Error('no WebLN provider');
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
// Only log the error if it's not the common "no provider" error
|
|
||||||
if (error instanceof Error && !error.message.includes('no WebLN provider')) {
|
|
||||||
console.warn('WebLN detection error:', error);
|
|
||||||
}
|
|
||||||
setWebln(null);
|
|
||||||
setHasAttemptedDetection(true);
|
|
||||||
return null;
|
|
||||||
} finally {
|
|
||||||
setIsDetecting(false);
|
|
||||||
}
|
|
||||||
}, [webln, isDetecting]);
|
|
||||||
|
|
||||||
// Only auto-detect once on mount
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!hasAttemptedDetection) {
|
const webLn = (globalThis as { webln?: WebLNProvider }).webln;
|
||||||
detectWebLN();
|
setWebln(webLn || null);
|
||||||
}
|
}, []);
|
||||||
}, [detectWebLN, hasAttemptedDetection]);
|
|
||||||
|
|
||||||
// Test WebLN connection
|
|
||||||
const testWebLN = useCallback(async (): Promise<boolean> => {
|
|
||||||
if (!webln) return false;
|
|
||||||
|
|
||||||
try {
|
|
||||||
// Test the connection with a getInfo call if available, otherwise just check if provider exists
|
|
||||||
if (webln.getInfo) {
|
|
||||||
await webln.getInfo();
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
} catch (error) {
|
|
||||||
console.error('WebLN test failed:', error);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}, [webln]);
|
|
||||||
|
|
||||||
// Calculate status values reactively
|
// Calculate status values reactively
|
||||||
const hasNWC = useMemo(() => {
|
const hasNWC = useMemo(() => {
|
||||||
@@ -88,14 +40,8 @@ export function useWallet() {
|
|||||||
hasNWC,
|
hasNWC,
|
||||||
webln,
|
webln,
|
||||||
activeNWC,
|
activeNWC,
|
||||||
isDetecting,
|
|
||||||
preferredMethod,
|
preferredMethod,
|
||||||
};
|
};
|
||||||
|
|
||||||
return {
|
return status;
|
||||||
...status,
|
|
||||||
hasAttemptedDetection,
|
|
||||||
detectWebLN,
|
|
||||||
testWebLN,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user