134 lines
3.6 KiB
TypeScript
134 lines
3.6 KiB
TypeScript
'use client'
|
|
|
|
import React from 'react'
|
|
import { WagmiProvider } from 'wagmi'
|
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
|
import { config } from '@/lib/wagmi-config'
|
|
import { useEffect, useState } from 'react'
|
|
import sdk from '@farcaster/miniapp-sdk'
|
|
|
|
const queryClient = new QueryClient({
|
|
defaultOptions: {
|
|
queries: {
|
|
retry: 1,
|
|
refetchOnWindowFocus: false,
|
|
},
|
|
},
|
|
})
|
|
|
|
export default function Providers({ children }: { children: React.ReactNode }) {
|
|
const [isSDKReady, setIsSDKReady] = useState(false)
|
|
const [userContext, setUserContext] = useState<any>(null)
|
|
|
|
useEffect(() => {
|
|
const initSDK = async () => {
|
|
try {
|
|
// Initialize Farcaster SDK
|
|
await sdk.actions.ready()
|
|
|
|
// Get user context if available
|
|
try {
|
|
const context = await sdk.context
|
|
setUserContext(context)
|
|
console.log('✅ Farcaster SDK initialized with user context:', {
|
|
fid: context?.user?.fid,
|
|
username: context?.user?.username,
|
|
})
|
|
} catch (contextError) {
|
|
console.log('📱 Running outside Farcaster environment:', contextError)
|
|
}
|
|
|
|
setIsSDKReady(true)
|
|
} catch (error) {
|
|
console.error('❌ Failed to initialize Farcaster SDK:', error)
|
|
// Continue anyway for development/testing
|
|
setIsSDKReady(true)
|
|
}
|
|
}
|
|
|
|
initSDK()
|
|
}, [])
|
|
|
|
// Show loading screen while SDK initializes
|
|
if (!isSDKReady) {
|
|
return (
|
|
<div className="min-h-screen flex items-center justify-center bg-gradient-to-br from-celo-green to-celo-yellow">
|
|
<div className="text-center">
|
|
<div className="text-6xl mb-6 animate-bounce">📊</div>
|
|
<div className="text-2xl font-bold text-white mb-2">PollCelo</div>
|
|
<div className="text-white/80">Loading prediction markets...</div>
|
|
<div className="mt-4">
|
|
<div className="w-8 h-8 border-2 border-white/30 border-t-white rounded-full animate-spin mx-auto"></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
return (
|
|
<WagmiProvider config={config}>
|
|
<QueryClientProvider client={queryClient}>
|
|
{/* Global user context provider */}
|
|
<UserContextProvider userContext={userContext}>
|
|
{children}
|
|
</UserContextProvider>
|
|
</QueryClientProvider>
|
|
</WagmiProvider>
|
|
)
|
|
}
|
|
|
|
// User context provider component
|
|
const UserContext = React.createContext<any>(null)
|
|
|
|
function UserContextProvider({
|
|
children,
|
|
userContext
|
|
}: {
|
|
children: React.ReactNode
|
|
userContext: any
|
|
}) {
|
|
const [context, setContext] = useState(userContext)
|
|
|
|
useEffect(() => {
|
|
// Listen for context updates - simplified version
|
|
const handleContextUpdate = (newContext: any) => {
|
|
setContext(newContext)
|
|
}
|
|
|
|
if (sdk) {
|
|
// sdk.onContextUpdate(handleContextUpdate) - Commented out due to API differences
|
|
}
|
|
|
|
return () => {
|
|
// Cleanup removed due to SDK API differences
|
|
}
|
|
}, [])
|
|
|
|
return (
|
|
<UserContext.Provider value={context}>
|
|
{children}
|
|
</UserContext.Provider>
|
|
)
|
|
}
|
|
|
|
// User context types and hooks
|
|
export function useUserContext() {
|
|
const context = React.useContext(UserContext)
|
|
return context
|
|
}
|
|
|
|
export function useFarcasterUser() {
|
|
const context = useUserContext()
|
|
|
|
return {
|
|
fid: context?.user?.fid,
|
|
username: context?.user?.username,
|
|
displayName: context?.user?.displayName,
|
|
pfpUrl: context?.user?.pfpUrl,
|
|
custodyAddress: context?.user?.custodyAddress,
|
|
verifiedAddresses: context?.user?.verifiedAddresses,
|
|
isAuthenticated: !!context?.user?.fid,
|
|
rawContext: context,
|
|
}
|
|
}
|