|
| 1 | +/* eslint-disable @next/next/no-img-element */ |
| 2 | +import React from "react"; |
| 3 | +import { |
| 4 | + Dialog, |
| 5 | + DialogContent, |
| 6 | + DialogTitle, |
| 7 | + DialogDescription, |
| 8 | +} from "@/components/ui/dialog"; |
| 9 | +import { useQuery } from "@tanstack/react-query"; |
| 10 | +import { z } from "zod"; |
| 11 | +import { Loader2Icon, TriangleAlertIcon } from "lucide-react"; |
| 12 | +import Image from "next/image"; |
| 13 | + |
| 14 | +type UserDetails = { |
| 15 | + username: string; |
| 16 | + pfp_url: string; |
| 17 | + profile: { |
| 18 | + bio: { |
| 19 | + text: string; |
| 20 | + }; |
| 21 | + }; |
| 22 | + follower_count: number; |
| 23 | + following_count: number; |
| 24 | +}; |
| 25 | + |
| 26 | +type FrameAppDebuggerViewProfileDialogProps = { |
| 27 | + fid: number; |
| 28 | + onDismiss: () => void; |
| 29 | +}; |
| 30 | + |
| 31 | +const UserDetailsSchema = z.object({ |
| 32 | + username: z.string(), |
| 33 | + pfp_url: z.string().url(), |
| 34 | + profile: z.object({ |
| 35 | + bio: z.object({ |
| 36 | + text: z.string(), |
| 37 | + }), |
| 38 | + }), |
| 39 | + follower_count: z.number().int(), |
| 40 | + following_count: z.number().int(), |
| 41 | +}); |
| 42 | + |
| 43 | +async function fetchUser(fid: number): Promise<UserDetails> { |
| 44 | + const response = await fetch(`/farcaster/user/${fid}`); |
| 45 | + |
| 46 | + if (!response.ok) { |
| 47 | + throw new Error("Network response was not ok"); |
| 48 | + } |
| 49 | + |
| 50 | + const data = await response.json(); |
| 51 | + |
| 52 | + return UserDetailsSchema.parse(data); |
| 53 | +} |
| 54 | + |
| 55 | +export function FrameAppDebuggerViewProfileDialog({ |
| 56 | + fid, |
| 57 | + onDismiss, |
| 58 | +}: FrameAppDebuggerViewProfileDialogProps) { |
| 59 | + const query = useQuery({ |
| 60 | + queryKey: ["user", fid], |
| 61 | + queryFn: () => fetchUser(fid), |
| 62 | + }); |
| 63 | + |
| 64 | + return ( |
| 65 | + <Dialog open={true} onOpenChange={onDismiss}> |
| 66 | + <DialogContent> |
| 67 | + <DialogTitle>Profile Details</DialogTitle> |
| 68 | + {query.isLoading && ( |
| 69 | + <DialogDescription className="flex items-center justify-center"> |
| 70 | + <Loader2Icon className="animate-spin" /> |
| 71 | + </DialogDescription> |
| 72 | + )} |
| 73 | + {query.isError && ( |
| 74 | + <DialogDescription className="flex flex-col items-center justify-center text-red-500"> |
| 75 | + <TriangleAlertIcon className="mb-2 w-6 h-6" /> |
| 76 | + <span className="font-semibold">Unexpected error occurred</span> |
| 77 | + </DialogDescription> |
| 78 | + )} |
| 79 | + {query.isSuccess && ( |
| 80 | + <DialogDescription className="flex flex-col gap-2 items-center justify-center"> |
| 81 | + <div className="flex items-center"> |
| 82 | + <div className="aspect-square h-[80px] w-[80px] rounded-full overflow-hidden"> |
| 83 | + <img |
| 84 | + className="w-full h-full object-contain" |
| 85 | + src={query.data.pfp_url} |
| 86 | + alt={query.data.username} |
| 87 | + width={80} |
| 88 | + /> |
| 89 | + </div> |
| 90 | + </div> |
| 91 | + <strong className="text-lg">{query.data.username}</strong> |
| 92 | + <div className="grid grid-cols-2 gap-4 text-sm text-gray-500 text-center"> |
| 93 | + <div> |
| 94 | + <strong>{formatCount(query.data.follower_count)}</strong>{" "} |
| 95 | + followers |
| 96 | + </div> |
| 97 | + <div> |
| 98 | + <strong>{formatCount(query.data.following_count)}</strong>{" "} |
| 99 | + following |
| 100 | + </div> |
| 101 | + </div> |
| 102 | + <span>{query.data.profile.bio.text}</span> |
| 103 | + </DialogDescription> |
| 104 | + )} |
| 105 | + </DialogContent> |
| 106 | + </Dialog> |
| 107 | + ); |
| 108 | +} |
| 109 | + |
| 110 | +function formatCount(count: number): string { |
| 111 | + if (count < 1000) { |
| 112 | + return count.toString(); |
| 113 | + } else if (count >= 1000 && count < 1000000) { |
| 114 | + return (count / 1000).toFixed(1) + "K"; |
| 115 | + } |
| 116 | + |
| 117 | + return (count / 1000000).toFixed(1) + "M"; |
| 118 | +} |
0 commit comments