mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-05 16:33:36 +00:00
Remove webln from state, simplify webln detection.
This commit is contained in:
1 parent
45cdcecf9e
commit
b77885093c
4 files changed
+32
-38
No files matched your search
@@ -68,7 +68,7 @@ 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;
|
webln: import('@webbtc/webln-types').WebLNProvider | null;
|
||||||
hasNWC: boolean;
|
hasNWC: boolean;
|
||||||
connections: NWCConnection[];
|
connections: NWCConnection[];
|
||||||
connectionInfo: Record<string, NWCInfo>;
|
connectionInfo: Record<string, NWCInfo>;
|
||||||
@@ -77,7 +77,7 @@ const WalletContent = forwardRef<HTMLDivElement, {
|
|||||||
handleRemoveConnection: (cs: string) => void;
|
handleRemoveConnection: (cs: string) => void;
|
||||||
setAddDialogOpen: (open: boolean) => void;
|
setAddDialogOpen: (open: boolean) => void;
|
||||||
}>(({
|
}>(({
|
||||||
hasWebLN,
|
webln,
|
||||||
hasNWC,
|
hasNWC,
|
||||||
connections,
|
connections,
|
||||||
connectionInfo,
|
connectionInfo,
|
||||||
@@ -101,9 +101,9 @@ const WalletContent = forwardRef<HTMLDivElement, {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex items-center gap-2">
|
<div className="flex items-center gap-2">
|
||||||
{hasWebLN && <CheckCircle className="h-4 w-4 text-green-600" />}
|
{webln && <CheckCircle className="h-4 w-4 text-green-600" />}
|
||||||
<Badge variant={hasWebLN ? "default" : "secondary"} className="text-xs">
|
<Badge variant={webln ? "default" : "secondary"} className="text-xs">
|
||||||
{hasWebLN ? "Ready" : "Not Found"}
|
{webln ? "Ready" : "Not Found"}
|
||||||
</Badge>
|
</Badge>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -189,7 +189,7 @@ const WalletContent = forwardRef<HTMLDivElement, {
|
|||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
{/* Help */}
|
{/* Help */}
|
||||||
{!hasWebLN && connections.length === 0 && (
|
{!webln && connections.length === 0 && (
|
||||||
<>
|
<>
|
||||||
<Separator />
|
<Separator />
|
||||||
<div className="text-center py-4 space-y-2">
|
<div className="text-center py-4 space-y-2">
|
||||||
@@ -220,7 +220,7 @@ export function WalletModal({ children, className }: WalletModalProps) {
|
|||||||
setActiveConnection
|
setActiveConnection
|
||||||
} = useNWC();
|
} = useNWC();
|
||||||
|
|
||||||
const { hasWebLN } = useWallet();
|
const { webln } = 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();
|
||||||
@@ -261,7 +261,7 @@ export function WalletModal({ children, className }: WalletModalProps) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const walletContentProps = {
|
const walletContentProps = {
|
||||||
hasWebLN,
|
webln,
|
||||||
hasNWC,
|
hasNWC,
|
||||||
connections,
|
connections,
|
||||||
connectionInfo,
|
connectionInfo,
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ interface ZapContentProps {
|
|||||||
isZapping: boolean;
|
isZapping: boolean;
|
||||||
qrCodeUrl: string;
|
qrCodeUrl: string;
|
||||||
copied: boolean;
|
copied: boolean;
|
||||||
hasWebLN: boolean;
|
webln: import('@webbtc/webln-types').WebLNProvider | null;
|
||||||
handleZap: () => void;
|
handleZap: () => void;
|
||||||
handleCopy: () => void;
|
handleCopy: () => void;
|
||||||
openInWallet: () => void;
|
openInWallet: () => void;
|
||||||
@@ -73,7 +73,7 @@ const ZapContent = forwardRef<HTMLDivElement, ZapContentProps>(({
|
|||||||
isZapping,
|
isZapping,
|
||||||
qrCodeUrl,
|
qrCodeUrl,
|
||||||
copied,
|
copied,
|
||||||
hasWebLN,
|
webln,
|
||||||
handleZap,
|
handleZap,
|
||||||
handleCopy,
|
handleCopy,
|
||||||
openInWallet,
|
openInWallet,
|
||||||
@@ -138,7 +138,7 @@ const ZapContent = forwardRef<HTMLDivElement, ZapContentProps>(({
|
|||||||
|
|
||||||
{/* Payment buttons */}
|
{/* Payment buttons */}
|
||||||
<div className="space-y-3 mt-4">
|
<div className="space-y-3 mt-4">
|
||||||
{hasWebLN && (
|
{webln && (
|
||||||
<Button
|
<Button
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
const finalAmount = typeof amount === 'string' ? parseInt(amount, 10) : amount;
|
const finalAmount = typeof amount === 'string' ? parseInt(amount, 10) : amount;
|
||||||
@@ -239,7 +239,7 @@ export function ZapDialog({ target, children, className }: ZapDialogProps) {
|
|||||||
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 } = useWallet();
|
const { webln, activeNWC } = 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>('');
|
||||||
@@ -337,7 +337,7 @@ export function ZapDialog({ target, children, className }: ZapDialogProps) {
|
|||||||
isZapping,
|
isZapping,
|
||||||
qrCodeUrl,
|
qrCodeUrl,
|
||||||
copied,
|
copied,
|
||||||
hasWebLN,
|
webln,
|
||||||
handleZap,
|
handleZap,
|
||||||
handleCopy,
|
handleCopy,
|
||||||
openInWallet,
|
openInWallet,
|
||||||
|
|||||||
+16
-16
@@ -97,7 +97,7 @@ async function createNIP98Token(
|
|||||||
tags,
|
tags,
|
||||||
created_at: Math.floor(Date.now() / 1000)
|
created_at: Math.floor(Date.now() / 1000)
|
||||||
});
|
});
|
||||||
|
|
||||||
// Return the token (base64 encoded event)
|
// Return the token (base64 encoded event)
|
||||||
return btoa(JSON.stringify(event));
|
return btoa(JSON.stringify(event));
|
||||||
}
|
}
|
||||||
@@ -122,7 +122,7 @@ async function handleAPIError(response: Response) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw new Error(`Invalid request: ${error.error?.message || error.details || error.error || 'Please check your request parameters.'}`);
|
throw new Error(`Invalid request: ${error.error?.message || error.details || error.error || 'Please check your request parameters.'}`);
|
||||||
} catch (parseError) {
|
} catch {
|
||||||
throw new Error('Invalid request. Please check your parameters and try again.');
|
throw new Error('Invalid request. Please check your parameters and try again.');
|
||||||
}
|
}
|
||||||
} else if (response.status === 404) {
|
} else if (response.status === 404) {
|
||||||
@@ -133,7 +133,7 @@ async function handleAPIError(response: Response) {
|
|||||||
try {
|
try {
|
||||||
const errorData = await response.json();
|
const errorData = await response.json();
|
||||||
throw new Error(`API error: ${errorData.error?.message || errorData.details || errorData.error || response.statusText}`);
|
throw new Error(`API error: ${errorData.error?.message || errorData.details || errorData.error || response.statusText}`);
|
||||||
} catch (parseError) {
|
} catch {
|
||||||
throw new Error(`Network error: ${response.statusText}. Please check your connection and try again.`);
|
throw new Error(`Network error: ${response.statusText}. Please check your connection and try again.`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -151,7 +151,7 @@ export function useShakespeare() {
|
|||||||
|
|
||||||
// Chat completion function
|
// Chat completion function
|
||||||
const sendChatMessage = useCallback(async (
|
const sendChatMessage = useCallback(async (
|
||||||
messages: ChatMessage[],
|
messages: ChatMessage[],
|
||||||
model: string = 'shakespeare',
|
model: string = 'shakespeare',
|
||||||
options?: Partial<ChatCompletionRequest>
|
options?: Partial<ChatCompletionRequest>
|
||||||
): Promise<ChatCompletionResponse> => {
|
): Promise<ChatCompletionResponse> => {
|
||||||
@@ -189,20 +189,20 @@ export function useShakespeare() {
|
|||||||
return await response.json();
|
return await response.json();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
let errorMessage = 'An unexpected error occurred';
|
let errorMessage = 'An unexpected error occurred';
|
||||||
|
|
||||||
if (err instanceof Error) {
|
if (err instanceof Error) {
|
||||||
errorMessage = err.message;
|
errorMessage = err.message;
|
||||||
} else if (typeof err === 'string') {
|
} else if (typeof err === 'string') {
|
||||||
errorMessage = err;
|
errorMessage = err;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add context for common issues
|
// Add context for common issues
|
||||||
if (errorMessage.includes('Failed to fetch') || errorMessage.includes('Network')) {
|
if (errorMessage.includes('Failed to fetch') || errorMessage.includes('Network')) {
|
||||||
errorMessage = 'Network error: Please check your internet connection and try again.';
|
errorMessage = 'Network error: Please check your internet connection and try again.';
|
||||||
} else if (errorMessage.includes('signer')) {
|
} else if (errorMessage.includes('signer')) {
|
||||||
errorMessage = 'Authentication error: Please make sure you are logged in with a Nostr account that supports signing.';
|
errorMessage = 'Authentication error: Please make sure you are logged in with a Nostr account that supports signing.';
|
||||||
}
|
}
|
||||||
|
|
||||||
setError(errorMessage);
|
setError(errorMessage);
|
||||||
throw new Error(errorMessage);
|
throw new Error(errorMessage);
|
||||||
} finally {
|
} finally {
|
||||||
@@ -212,7 +212,7 @@ export function useShakespeare() {
|
|||||||
|
|
||||||
// Streaming chat completion function
|
// Streaming chat completion function
|
||||||
const sendStreamingMessage = useCallback(async (
|
const sendStreamingMessage = useCallback(async (
|
||||||
messages: ChatMessage[],
|
messages: ChatMessage[],
|
||||||
model: string = 'shakespeare',
|
model: string = 'shakespeare',
|
||||||
onChunk: (chunk: string) => void,
|
onChunk: (chunk: string) => void,
|
||||||
options?: Partial<ChatCompletionRequest>
|
options?: Partial<ChatCompletionRequest>
|
||||||
@@ -269,7 +269,7 @@ export function useShakespeare() {
|
|||||||
if (line.startsWith('data: ')) {
|
if (line.startsWith('data: ')) {
|
||||||
const data = line.slice(6);
|
const data = line.slice(6);
|
||||||
if (data === '[DONE]') return;
|
if (data === '[DONE]') return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const parsed = JSON.parse(data);
|
const parsed = JSON.parse(data);
|
||||||
const content = parsed.choices?.[0]?.delta?.content;
|
const content = parsed.choices?.[0]?.delta?.content;
|
||||||
@@ -287,20 +287,20 @@ export function useShakespeare() {
|
|||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
let errorMessage = 'An unexpected error occurred';
|
let errorMessage = 'An unexpected error occurred';
|
||||||
|
|
||||||
if (err instanceof Error) {
|
if (err instanceof Error) {
|
||||||
errorMessage = err.message;
|
errorMessage = err.message;
|
||||||
} else if (typeof err === 'string') {
|
} else if (typeof err === 'string') {
|
||||||
errorMessage = err;
|
errorMessage = err;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add context for common issues
|
// Add context for common issues
|
||||||
if (errorMessage.includes('Failed to fetch') || errorMessage.includes('Network')) {
|
if (errorMessage.includes('Failed to fetch') || errorMessage.includes('Network')) {
|
||||||
errorMessage = 'Network error: Please check your internet connection and try again.';
|
errorMessage = 'Network error: Please check your internet connection and try again.';
|
||||||
} else if (errorMessage.includes('signer')) {
|
} else if (errorMessage.includes('signer')) {
|
||||||
errorMessage = 'Authentication error: Please make sure you are logged in with a Nostr account that supports signing.';
|
errorMessage = 'Authentication error: Please make sure you are logged in with a Nostr account that supports signing.';
|
||||||
}
|
}
|
||||||
|
|
||||||
setError(errorMessage);
|
setError(errorMessage);
|
||||||
throw new Error(errorMessage);
|
throw new Error(errorMessage);
|
||||||
} finally {
|
} finally {
|
||||||
@@ -336,20 +336,20 @@ export function useShakespeare() {
|
|||||||
return await response.json();
|
return await response.json();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
let errorMessage = 'An unexpected error occurred';
|
let errorMessage = 'An unexpected error occurred';
|
||||||
|
|
||||||
if (err instanceof Error) {
|
if (err instanceof Error) {
|
||||||
errorMessage = err.message;
|
errorMessage = err.message;
|
||||||
} else if (typeof err === 'string') {
|
} else if (typeof err === 'string') {
|
||||||
errorMessage = err;
|
errorMessage = err;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add context for common issues
|
// Add context for common issues
|
||||||
if (errorMessage.includes('Failed to fetch') || errorMessage.includes('Network')) {
|
if (errorMessage.includes('Failed to fetch') || errorMessage.includes('Network')) {
|
||||||
errorMessage = 'Network error: Please check your internet connection and try again.';
|
errorMessage = 'Network error: Please check your internet connection and try again.';
|
||||||
} else if (errorMessage.includes('signer')) {
|
} else if (errorMessage.includes('signer')) {
|
||||||
errorMessage = 'Authentication error: Please make sure you are logged in with a Nostr account that supports signing.';
|
errorMessage = 'Authentication error: Please make sure you are logged in with a Nostr account that supports signing.';
|
||||||
}
|
}
|
||||||
|
|
||||||
setError(errorMessage);
|
setError(errorMessage);
|
||||||
throw new Error(errorMessage);
|
throw new Error(errorMessage);
|
||||||
} finally {
|
} finally {
|
||||||
@@ -362,7 +362,7 @@ export function useShakespeare() {
|
|||||||
isLoading,
|
isLoading,
|
||||||
error,
|
error,
|
||||||
isAuthenticated: !!user,
|
isAuthenticated: !!user,
|
||||||
|
|
||||||
// Actions
|
// Actions
|
||||||
sendChatMessage,
|
sendChatMessage,
|
||||||
sendStreamingMessage,
|
sendStreamingMessage,
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
import { useState, useEffect, useCallback, useMemo } from 'react';
|
import { useMemo } from 'react';
|
||||||
import { useNWC } from '@/hooks/useNWCContext';
|
import { useNWC } from '@/hooks/useNWCContext';
|
||||||
import type { WebLNProvider } from '@webbtc/webln-types';
|
import type { WebLNProvider } from '@webbtc/webln-types';
|
||||||
|
|
||||||
export interface WalletStatus {
|
export interface WalletStatus {
|
||||||
hasWebLN: boolean;
|
|
||||||
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;
|
||||||
@@ -11,17 +10,13 @@ export interface WalletStatus {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function useWallet() {
|
export function useWallet() {
|
||||||
const [webln, setWebln] = useState<WebLNProvider | null>(null);
|
|
||||||
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 synchronously on mount
|
// Access WebLN directly from browser global scope
|
||||||
useEffect(() => {
|
const webln = (globalThis as { webln?: WebLNProvider }).webln || null;
|
||||||
const webLn = (globalThis as { webln?: WebLNProvider }).webln;
|
|
||||||
setWebln(webLn || null);
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
// Calculate status values reactively
|
// Calculate status values reactively
|
||||||
const hasNWC = useMemo(() => {
|
const hasNWC = useMemo(() => {
|
||||||
@@ -36,7 +31,6 @@ export function useWallet() {
|
|||||||
: 'manual';
|
: 'manual';
|
||||||
|
|
||||||
const status: WalletStatus = {
|
const status: WalletStatus = {
|
||||||
hasWebLN: !!webln,
|
|
||||||
hasNWC,
|
hasNWC,
|
||||||
webln,
|
webln,
|
||||||
activeNWC,
|
activeNWC,
|
||||||
|
|||||||
Reference in new issue
Block a user