0x0x230x

Untitled

May 12th, 2025
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. ```
  2. 'use client';
  3.  
  4. import { useEffect, useState, createContext, useContext } from 'react';
  5. import sdk, { Context } from '@farcaster/frame-sdk';
  6. import { useLoginToFrame } from '@privy-io/react-auth/farcaster';
  7. import { usePrivy, useWallets } from '@privy-io/react-auth';
  8. import { useIsFrameOnMobile } from '@/lib/hooks/useIsFrameOnMobile';
  9. import { useAccount, useConnect } from 'wagmi';
  10. import farcasterFrame from '@farcaster/frame-wagmi-connector';
  11.  
  12. const FrameContext = createContext<{
  13. isSDKLoaded: boolean;
  14. frameContext: Context.FrameContext | null;
  15. loginIntoFrame: () => Promise<void>;
  16. }>({
  17. isSDKLoaded: false,
  18. frameContext: null,
  19. loginIntoFrame: () => Promise.resolve(),
  20. });
  21.  
  22. export const useFrameContext = () => {
  23. const context = useContext(FrameContext);
  24. if (!context) {
  25. throw new Error('useFrame must be used within a FrameProvider');
  26. }
  27. return context;
  28. };
  29.  
  30. export default function FrameProvider({
  31. children,
  32. }: {
  33. children: React.ReactNode;
  34. }) {
  35. const [isSDKLoaded, setIsSDKLoaded] = useState(false);
  36. const [frameContext, setFrameContext] = useState<Context.FrameContext | null>(
  37. null,
  38. );
  39.  
  40. const { ready, authenticated, user } = usePrivy();
  41. const { initLoginToFrame, loginToFrame } = useLoginToFrame();
  42. const isFrameOnMobile = useIsFrameOnMobile();
  43. const { connect } = useConnect();
  44. const { wallets } = useWallets();
  45.  
  46. const { isConnected } = useAccount();
  47.  
  48. // Setup SDK
  49. useEffect(() => {
  50. const load = async () => {
  51. const frameContext = await sdk.context;
  52. // await setTimeout(10000);
  53. setFrameContext(frameContext);
  54. sdk.actions.ready();
  55. };
  56.  
  57. if (sdk && !isSDKLoaded) {
  58. setIsSDKLoaded(true);
  59. load();
  60. }
  61. }, [isSDKLoaded]);
  62.  
  63. const login = async () => {
  64. const { nonce } = await initLoginToFrame();
  65. const result = await sdk.actions.signIn({ nonce: nonce });
  66.  
  67. await loginToFrame({
  68. message: result.message,
  69. signature: result.signature,
  70. });
  71. };
  72.  
  73. // Login to Frame
  74. // useEffect(() => {
  75. // if (!frameContext) {
  76. // return;
  77. // }
  78.  
  79. // if (ready && !authenticated) {
  80. // login();
  81. // }
  82. // }, [ready, authenticated, frameContext]);
  83.  
  84. // Add Frame
  85. useEffect(() => {
  86. if (!frameContext || !authenticated) {
  87. return;
  88. }
  89.  
  90. const addFrame = async () => {
  91. await sdk.actions.addFrame();
  92. };
  93.  
  94. if (!frameContext?.client.added) {
  95. addFrame();
  96. }
  97. }, [frameContext, authenticated, ready, user]);
  98.  
  99. // If we are on mobile and have frame context, connect to warpcast wallet
  100. useEffect(() => {
  101. if (isConnected) {
  102. return;
  103. }
  104.  
  105. if (frameContext && isFrameOnMobile) {
  106. connect({ connector: farcasterFrame() });
  107. }
  108. }, [frameContext, isFrameOnMobile, isConnected, wallets]);
  109.  
  110. return (
  111. <FrameContext.Provider
  112. value={{ isSDKLoaded, frameContext, loginIntoFrame: login }}
  113. >
  114. {children}
  115. </FrameContext.Provider>
  116. );
  117. }
  118.  
  119. ```
Advertisement
Add Comment
Please, Sign In to add comment