add viewProfile example

This commit is contained in:
Tony D'Addeo
2025-01-06 16:54:40 -06:00
committed by lucas-neynar
parent 7cde86a049
commit 805036bdaf
9 changed files with 266 additions and 38 deletions

View File

@@ -1,10 +1,13 @@
"use client";
import { useEffect, useCallback, useState, useMemo } from "react";
import { Input } from "../components/ui/input"
import { signIn, signOut, getCsrfToken } from "next-auth/react";
import sdk, {
AddFrame,
FrameNotificationDetails,
type FrameContext,
SignIn as SignInCore,
type Context,
} from "@farcaster/frame-sdk";
import {
useAccount,
@@ -24,14 +27,15 @@ import { truncateAddress } from "~/lib/truncateAddress";
import { base, optimism } from "wagmi/chains";
import { BaseError, UserRejectedRequestError } from "viem";
import { useSession } from "next-auth/react"
import { SignIn as SignInCore } from "@farcaster/frame-core";
import { SignInResult } from "@farcaster/frame-core/dist/actions/signIn";
import { createStore } from 'mipd'
import { Label } from "~/components/ui/label";
export default function Demo(
{ title }: { title?: string } = { title: "Frames v2 Demo" }
) {
const [isSDKLoaded, setIsSDKLoaded] = useState(false);
const [context, setContext] = useState<FrameContext>();
const [context, setContext] = useState<Context.FrameContext>();
const [isContextOpen, setIsContextOpen] = useState(false);
const [txHash, setTxHash] = useState<string | null>(null);
@@ -124,9 +128,21 @@ export default function Demo(
console.log("primaryButtonClicked");
});
console.log("Calling ready");
sdk.actions.ready({});
// Set up a MIPD Store, and request Providers.
const store = createStore()
// Subscribe to the MIPD Store.
store.subscribe(providerDetails => {
console.log("PROVIDER DETAILS", providerDetails)
// => [EIP6963ProviderDetail, EIP6963ProviderDetail, ...]
})
};
if (sdk && !isSDKLoaded) {
console.log("Calling load");
setIsSDKLoaded(true);
load();
return () => {
@@ -153,19 +169,23 @@ export default function Demo(
const result = await sdk.actions.addFrame();
if (result.added) {
if (result.notificationDetails) {
setNotificationDetails(result.notificationDetails);
}
setAddFrameResult(
result.notificationDetails
? `Added, got notificaton token ${result.notificationDetails.token} and url ${result.notificationDetails.url}`
: "Added, got no notification details"
);
} else {
setAddFrameResult(`Not added: ${result.reason}`);
if (result.notificationDetails) {
setNotificationDetails(result.notificationDetails);
}
setAddFrameResult(
result.notificationDetails
? `Added, got notificaton token ${result.notificationDetails.token} and url ${result.notificationDetails.url}`
: "Added, got no notification details"
);
} catch (error) {
if (error instanceof AddFrame.RejectedByUser) {
setAddFrameResult(`Not added: ${error.message}`);
}
if (error instanceof AddFrame.InvalidDomainManifest) {
setAddFrameResult(`Not added: ${error.message}`);
}
setAddFrameResult(`Error: ${error}`);
}
}, []);
@@ -307,6 +327,15 @@ export default function Demo(
<Button onClick={openWarpcastUrl}>Open Warpcast Link</Button>
</div>
<div className="mb-4">
<div className="p-2 bg-gray-100 dark:bg-gray-800 rounded-lg my-2">
<pre className="font-mono text-xs whitespace-pre-wrap break-words max-w-[260px] overflow-x-">
sdk.actions.viewProfile
</pre>
</div>
<ViewProfile />
</div>
<div className="mb-4">
<div className="p-2 bg-gray-100 dark:bg-gray-800 rounded-lg my-2">
<pre className="font-mono text-xs whitespace-pre-wrap break-words max-w-[260px] overflow-x-">
@@ -553,7 +582,7 @@ function SendEth() {
function SignIn() {
const [signingIn, setSigningIn] = useState(false);
const [signingOut, setSigningOut] = useState(false);
const [signInResult, setSignInResult] = useState<SignInResult>();
const [signInResult, setSignInResult] = useState<SignInCore.SignInResult>();
const [signInFailure, setSignInFailure] = useState<string>();
const { data: session, status } = useSession()
@@ -638,6 +667,33 @@ function SignIn() {
);
}
function ViewProfile() {
const [fid, setFid] = useState('3');
return (
<>
<div>
<Label className="text-xs font-semibold text-gray-500 mb-1" htmlFor="view-profile-fid">Fid</Label>
<Input
id="view-profile-fid"
type="number"
value={fid}
className="mb-2"
onChange={(e) => {
setFid(e.target.value)
}}
step="1"
min="1"
/>
</div>
<Button
onClick={() => { sdk.actions.viewProfile({ fid: parseInt(fid) }) }}
>
View Profile
</Button>
</>
);
}
const renderError = (error: Error | null) => {
if (!error) return null;

View File

@@ -0,0 +1,22 @@
import * as React from "react"
import { cn } from "~/lib/utils"
const Input = React.forwardRef<HTMLInputElement, React.ComponentProps<"input">>(
({ className, type, ...props }, ref) => {
return (
<input
type={type}
className={cn(
"flex h-10 w-full rounded-md border border-neutral-200 bg-white px-3 py-2 text-base ring-offset-white file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-neutral-950 placeholder:text-neutral-500 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-neutral-950 focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 md:text-sm dark:border-neutral-800 dark:bg-neutral-950 dark:ring-offset-neutral-950 dark:file:text-neutral-50 dark:placeholder:text-neutral-400 dark:focus-visible:ring-neutral-300",
className
)}
ref={ref}
{...props}
/>
)
}
)
Input.displayName = "Input"
export { Input }

View File

@@ -0,0 +1,26 @@
"use client"
import * as React from "react"
import * as LabelPrimitive from "@radix-ui/react-label"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "~/lib/utils"
const labelVariants = cva(
"text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
)
const Label = React.forwardRef<
React.ElementRef<typeof LabelPrimitive.Root>,
React.ComponentPropsWithoutRef<typeof LabelPrimitive.Root> &
VariantProps<typeof labelVariants>
>(({ className, ...props }, ref) => (
<LabelPrimitive.Root
ref={ref}
className={cn(labelVariants(), className)}
{...props}
/>
))
Label.displayName = LabelPrimitive.Root.displayName
export { Label }