From 7dcf3f51d9a10a595ab9dfcfe42a41f534d5ddd0 Mon Sep 17 00:00:00 2001 From: Stephan Cilliers <5469870+stephancill@users.noreply.github.com> Date: Thu, 28 Mar 2024 15:01:06 +0200 Subject: [PATCH] feat: update starter frame with counter + state example --- .changeset/tidy-cheetahs-cover.md | 5 + .../framesjs-starter/app/frames/frames.ts | 6 ++ examples/framesjs-starter/app/frames/route.ts | 1 - .../framesjs-starter/app/frames/route.tsx | 31 ++++++ examples/framesjs-starter/app/page.tsx | 102 +++--------------- 5 files changed, 59 insertions(+), 86 deletions(-) create mode 100644 .changeset/tidy-cheetahs-cover.md create mode 100644 examples/framesjs-starter/app/frames/frames.ts delete mode 100644 examples/framesjs-starter/app/frames/route.ts create mode 100644 examples/framesjs-starter/app/frames/route.tsx diff --git a/.changeset/tidy-cheetahs-cover.md b/.changeset/tidy-cheetahs-cover.md new file mode 100644 index 000000000..0565a005b --- /dev/null +++ b/.changeset/tidy-cheetahs-cover.md @@ -0,0 +1,5 @@ +--- +"framesjs-starter": patch +--- + +feat: update starter frame to use the new api diff --git a/examples/framesjs-starter/app/frames/frames.ts b/examples/framesjs-starter/app/frames/frames.ts new file mode 100644 index 000000000..1428a50f7 --- /dev/null +++ b/examples/framesjs-starter/app/frames/frames.ts @@ -0,0 +1,6 @@ +import { createFrames } from "frames.js/next"; + +export const frames = createFrames({ + basePath: "/frames", + initialState: { totalButtonPresses: 0 }, +}); diff --git a/examples/framesjs-starter/app/frames/route.ts b/examples/framesjs-starter/app/frames/route.ts deleted file mode 100644 index 2b10a621f..000000000 --- a/examples/framesjs-starter/app/frames/route.ts +++ /dev/null @@ -1 +0,0 @@ -export { POST } from "frames.js/next/server"; diff --git a/examples/framesjs-starter/app/frames/route.tsx b/examples/framesjs-starter/app/frames/route.tsx new file mode 100644 index 000000000..697985255 --- /dev/null +++ b/examples/framesjs-starter/app/frames/route.tsx @@ -0,0 +1,31 @@ +/* eslint-disable react/jsx-key */ +import { Button } from "frames.js/next"; +import { frames } from "./frames"; + +const frameHandler = frames(async (ctx) => { + const counter = ctx.message ? ctx.searchParams.op === "+" ? ctx.state.totalButtonPresses + 1 : ctx.state.totalButtonPresses - 1 : ctx.state.totalButtonPresses + + return { + image:
+ {ctx.message?.inputText &&
+ {`Input: ${ctx.message.inputText}`} +
} +
Counter {counter}
, + textInput: "Say something", + buttons: [ + , + , + + ], + state: { totalButtonPresses: counter} + } +}) + +export const GET = frameHandler; +export const POST = frameHandler; diff --git a/examples/framesjs-starter/app/page.tsx b/examples/framesjs-starter/app/page.tsx index c34727389..4168a5e83 100644 --- a/examples/framesjs-starter/app/page.tsx +++ b/examples/framesjs-starter/app/page.tsx @@ -1,16 +1,7 @@ import { - FrameButton, - FrameContainer, - FrameImage, - FrameInput, - FrameReducer, - NextServerPageProps, - getFrameMessage, - getPreviousFrame, - useFramesReducer, + NextServerPageProps } from "frames.js/next/server"; import Link from "next/link"; -import { DEFAULT_DEBUGGER_HUB_URL, createDebugUrl } from "./debug"; import { currentURL } from "./utils"; type State = { @@ -18,40 +9,29 @@ type State = { total_button_presses: number; }; -const initialState = { active: "1", total_button_presses: 0 }; +import { fetchMetadata } from "frames.js/next"; +import type { Metadata } from "next"; +import { createDebugUrl } from "./debug"; +import { vercelURL } from "./utils"; -const reducer: FrameReducer = (state, action) => { +export async function generateMetadata(): Promise { return { - total_button_presses: state.total_button_presses + 1, - active: action.postBody?.untrustedData.buttonIndex - ? String(action.postBody?.untrustedData.buttonIndex) - : "1", + title: "frames.js starter", + description: "This is a frames.js starter template", + other: { + ...(await fetchMetadata( + new URL( + "/frames", + vercelURL() || "http://localhost:3000" + ) + )), + }, }; -}; +} // This is a react server component only export default async function Home({ searchParams }: NextServerPageProps) { const url = currentURL("/"); - const previousFrame = getPreviousFrame(searchParams); - - const frameMessage = await getFrameMessage(previousFrame.postBody, { - hubHttpUrl: DEFAULT_DEBUGGER_HUB_URL, - }); - - if (frameMessage && !frameMessage?.isValid) { - throw new Error("Invalid frame payload"); - } - - const [state, dispatch] = useFramesReducer( - reducer, - initialState, - previousFrame - ); - - // Here: do a server side side effect either sync or async (using await), such as minting an NFT if you want. - // example: load the users credentials & check they have an NFT - - console.log("info: state is:", state); // then, when done, return next frame return ( @@ -65,54 +45,6 @@ export default async function Home({ searchParams }: NextServerPageProps) { other examples - - {/* */} - -
-
- {frameMessage?.inputText ? frameMessage.inputText : "Hello world"} -
- {frameMessage && ( -
-
- Requester is @{frameMessage.requesterUserData?.username}{" "} -
-
- Requester follows caster:{" "} - {frameMessage.requesterFollowsCaster ? "true" : "false"} -
-
- Caster follows requester:{" "} - {frameMessage.casterFollowsRequester ? "true" : "false"} -
-
- Requester liked cast:{" "} - {frameMessage.likedCast ? "true" : "false"} -
-
- Requester recasted cast:{" "} - {frameMessage.recastedCast ? "true" : "false"} -
-
- )} -
-
- - - {state?.active === "1" ? "Active" : "Inactive"} - - - {state?.active === "2" ? "Active" : "Inactive"} - - - External - -
); }