|
| 1 | +import { useIsInMiniApp } from "@coinbase/onchainkit/minikit"; |
| 2 | +import sdk from "@farcaster/frame-sdk"; |
| 3 | +import { useQuery } from "@tanstack/react-query"; |
| 4 | + |
| 5 | +function useUserInfo() { |
| 6 | + const { isInMiniApp } = useIsInMiniApp(); |
| 7 | + |
| 8 | + return useQuery({ |
| 9 | + queryKey: ["useQuickAuth", isInMiniApp], |
| 10 | + queryFn: async () => { |
| 11 | + // If we're in a mini app context, all we have to do to make an authenticated |
| 12 | + // request is to use `sdk.quickAuth.fetch`. This will automatically include the |
| 13 | + // necessary `Authorization` header for the backend to verify. |
| 14 | + const result = await sdk.quickAuth.fetch("/api/me"); |
| 15 | + |
| 16 | + const userInfo = await result.json(); |
| 17 | + return { |
| 18 | + displayName: userInfo.display_name, |
| 19 | + pfpUrl: userInfo.pfp_url, |
| 20 | + bio: userInfo.profile?.bio?.text, |
| 21 | + followerCount: userInfo.follower_count, |
| 22 | + followingCount: userInfo.following_count, |
| 23 | + }; |
| 24 | + }, |
| 25 | + enabled: isInMiniApp, |
| 26 | + }); |
| 27 | +} |
| 28 | + |
| 29 | +export function UserInfo() { |
| 30 | + const { data, isLoading, error } = useUserInfo(); |
| 31 | + |
| 32 | + if (isLoading) { |
| 33 | + return ( |
| 34 | + <div className="bg-[var(--app-card-bg)] border border-[var(--app-card-border)] rounded-lg p-4 animate-pulse"> |
| 35 | + <div className="flex items-center space-x-4"> |
| 36 | + <div className="w-16 h-16 bg-[var(--app-gray)] rounded-full"></div> |
| 37 | + <div className="space-y-2 flex-1"> |
| 38 | + <div className="h-5 bg-[var(--app-gray)] rounded w-32"></div> |
| 39 | + <div className="h-4 bg-[var(--app-gray)] rounded w-24"></div> |
| 40 | + </div> |
| 41 | + </div> |
| 42 | + </div> |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + if (error || !data) { |
| 47 | + return ( |
| 48 | + <div className="bg-[var(--app-card-bg)] border border-[var(--app-card-border)] rounded-lg p-4"> |
| 49 | + <p className="text-[var(--app-foreground-muted)] text-center"> |
| 50 | + {error ? "Failed to load user info" : "No user info available"} |
| 51 | + </p> |
| 52 | + </div> |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + return ( |
| 57 | + <div className="bg-[var(--app-card-bg)] border border-[var(--app-card-border)] rounded-lg p-6 backdrop-blur-sm"> |
| 58 | + <div className="flex items-start space-x-4"> |
| 59 | + {/* Profile Picture */} |
| 60 | + {data.pfpUrl && ( |
| 61 | + <img |
| 62 | + src={data.pfpUrl} |
| 63 | + alt={`${data.displayName}'s profile picture`} |
| 64 | + className="w-16 h-16 rounded-full object-cover border-2 border-[var(--app-card-border)]" |
| 65 | + /> |
| 66 | + )} |
| 67 | + |
| 68 | + {/* User Info */} |
| 69 | + <div className="flex-1 min-w-0"> |
| 70 | + {/* Display Name */} |
| 71 | + <h2 className="text-xl font-bold text-[var(--app-foreground)] mb-2 truncate"> |
| 72 | + {data.displayName} |
| 73 | + </h2> |
| 74 | + |
| 75 | + {/* Bio */} |
| 76 | + {data.bio && ( |
| 77 | + <p className="text-[var(--app-foreground-muted)] text-sm mb-3 leading-relaxed"> |
| 78 | + {data.bio} |
| 79 | + </p> |
| 80 | + )} |
| 81 | + |
| 82 | + {/* Follower Stats */} |
| 83 | + <div className="flex space-x-6 text-sm"> |
| 84 | + <div className="flex flex-col items-center"> |
| 85 | + <span className="font-semibold text-[var(--app-foreground)]"> |
| 86 | + {data.followerCount?.toLocaleString() || "0"} |
| 87 | + </span> |
| 88 | + <span className="text-[var(--app-foreground-muted)]"> |
| 89 | + Followers |
| 90 | + </span> |
| 91 | + </div> |
| 92 | + <div className="flex flex-col items-center"> |
| 93 | + <span className="font-semibold text-[var(--app-foreground)]"> |
| 94 | + {data.followingCount?.toLocaleString() || "0"} |
| 95 | + </span> |
| 96 | + <span className="text-[var(--app-foreground-muted)]"> |
| 97 | + Following |
| 98 | + </span> |
| 99 | + </div> |
| 100 | + </div> |
| 101 | + </div> |
| 102 | + </div> |
| 103 | + </div> |
| 104 | + ); |
| 105 | +} |
0 commit comments