Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #278

Merged
merged 34 commits into from
Apr 3, 2024
Merged

Dev #278

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
9ef6e80
build: change eslint configuration
michalkvasnicak Apr 2, 2024
790bbac
fix: some eslint stuff
michalkvasnicak Apr 2, 2024
31364b3
build: do not require strict check
michalkvasnicak Apr 2, 2024
0bb5ee3
fix: more errors
michalkvasnicak Apr 2, 2024
9b7ca5c
build: disable implicing coercion warnings
michalkvasnicak Apr 3, 2024
63b9a71
fix: correctly type Frame and FrameButton
michalkvasnicak Apr 3, 2024
7d1cafe
fix: render package errors
michalkvasnicak Apr 3, 2024
5144c30
fix: debugger issues
michalkvasnicak Apr 3, 2024
1753737
fix: type errors in test
michalkvasnicak Apr 3, 2024
d7a3edc
build: build before linting
michalkvasnicak Apr 3, 2024
7d22df3
chore: add changeset
michalkvasnicak Apr 3, 2024
45204ac
fix: issues in tests
michalkvasnicak Apr 3, 2024
13e0bdd
fix: add missing basePath to next template
michalkvasnicak Apr 3, 2024
9d4a8c5
fix: typo
michalkvasnicak Apr 3, 2024
84283a8
chore: update changeset
michalkvasnicak Apr 3, 2024
1202a40
chore: remove key
michalkvasnicak Apr 3, 2024
445cf3a
chore: changeset
michalkvasnicak Apr 3, 2024
0865ea1
fix: bump template versions (#274)
michalkvasnicak Apr 3, 2024
f43396c
Merge pull request #273 from framesjs/fix/next-vanilla-template-missi…
stephancill Apr 3, 2024
e5f8f02
Merge pull request #266 from framesjs/build/eslint
stephancill Apr 3, 2024
d6256cf
fix: properly split dev dependencies in debugger
michalkvasnicak Apr 3, 2024
bc63b4f
fix: all debugger dependencies to dev except the bin required ones
michalkvasnicak Apr 3, 2024
3202ae6
fix: use correct version of debugger in templates and also upgrade va…
michalkvasnicak Apr 3, 2024
b4c6de9
docs: state management
stephancill Apr 3, 2024
93f8ffa
Merge pull request #276 from framesjs/fix/templates-debugger-dependency
stephancill Apr 3, 2024
1cff0c8
Merge pull request #275 from framesjs/fix/properly-use-dev-dependenci…
stephancill Apr 3, 2024
469905b
Merge pull request #277 from framesjs/docs/state-management
stephancill Apr 3, 2024
d261b07
fix: correctly mark frame ui image as loading
michalkvasnicak Apr 3, 2024
36ebb84
fix: correctly manipulate url in debugger
michalkvasnicak Apr 3, 2024
bdfad03
chore: changeset
michalkvasnicak Apr 3, 2024
141d346
fix: show error if request to URL returns nok ok response
michalkvasnicak Apr 3, 2024
121a61c
fix: add missing response check
michalkvasnicak Apr 3, 2024
69572df
fix: automatically load only url passed as query param
michalkvasnicak Apr 3, 2024
baeb33e
Merge pull request #279 from framesjs/fix/debugger-load-url-on-init
stephancill Apr 3, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/calm-carpets-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@frames.js/render": minor
---

feat: rename types
6 changes: 6 additions & 0 deletions .changeset/chilly-parrots-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"frames.js": patch
"@frames.js/debugger": patch
---

fix: minor bugs and code cleanup
5 changes: 5 additions & 0 deletions .changeset/clever-impalas-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-frames": patch
---

fix: missing basePath in next template
5 changes: 5 additions & 0 deletions .changeset/curly-badgers-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"docs": patch
---

docs: state management
5 changes: 5 additions & 0 deletions .changeset/curly-queens-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@frames.js/debugger": patch
---

fix: correctly manipulate url on load and also on change
5 changes: 5 additions & 0 deletions .changeset/early-turkeys-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@frames.js/render": patch
---

fix: correctly detect image change in order to properly indicate loading state
5 changes: 5 additions & 0 deletions .changeset/fast-wasps-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"framesjs-starter": patch
---

fix: state management generic type demo
5 changes: 5 additions & 0 deletions .changeset/orange-owls-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-frames": patch
---

fix: debugger dependency version
5 changes: 5 additions & 0 deletions .changeset/rotten-taxis-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-frames": patch
---

fix: bump template versions
5 changes: 5 additions & 0 deletions .changeset/silver-timers-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@frames.js/debugger": patch
---

fix: properly split dev dependencies in debugger
5 changes: 1 addition & 4 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
/** @type {import("eslint").Linter.Config} */
module.exports = {
extends: ["@framesjs/eslint-config/library.js"],
parser: "@typescript-eslint/parser",
parserOptions: {
project: true,
},
env: {
jest: true,
},
ignorePatterns: ["**/farcaster/generated/*.ts"],
};
3 changes: 3 additions & 0 deletions .github/workflows/github-actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ jobs:
- name: Install dependencies
run: yarn --frozen-lockfile

- name: Build
run: yarn build:ci

- name: Lint
run: yarn lint

Expand Down
72 changes: 72 additions & 0 deletions docs/pages/guides/state-management.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
title: "Guide: Frames.js State"
description: "Frames.js is the react based framework for making frames. Debugger included."
---

# Guide: State Management in Frames.js

State in Frames.js is a way for you to store data that you want to use in between frames requests. The frames spec allows up to 4kb of data to be stored in the state object.

## Setup

To use state in frames.js, you should declare the initial state in your `createFrames` call. If your state type is not declared explicitly it will be derived from the initial state.

```ts [frames.ts]
import { createFrames } from "@framesjs/next";

export type State = {
count: number;
};

export const frames = createFrames<State>({
initialState: {
count: 0,
},
});
```

## Accessing state

You can access the state object in your frame on the `ctx` parameter. The initial frame will have the state object with the initial values.

```ts [route.tsx]
import { frames } from "./frames";

const handler = frames(async ({ ctx }) => {
return {
image: <div tw="flex">Count: {ctx.state.count}</div>, // [!code focus]
};
});

export const GET = handler;
export const POST = handler;
```

## Updating state

To update the state, you just include the updated state object in your handler's [`FrameDefinition`](/reference/core/createFrames#framedefinition) return value.

```ts [route.tsx]
import { frames, Button } from "./frames";

const handler = frames(async ({ ctx }) => {
const currentState = ctx.state;

// Update the state // [!code focus]
const updatedState = { // [!code focus]
...currentState, // [!code focus]
count: currentState.count + 1, // [!code focus]
}; // [!code focus]

return {
image: <div tw="flex">Count: {updatedState.count}`</div>, // [!code focus],
buttons: [
<Button action="post">Increment</Button>
],
state: updatedState, // [!code focus]
};
});
export const GET = handler;
export const POST = handler;
```
10 changes: 7 additions & 3 deletions docs/vocs.config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ const sidebar = [
text: "Multi-page Frames",
link: "/guides/multiple-frames",
},
{
text: "State Management",
link: "/guides/state-management",
},
{
text: "Deploying your Frame",
link: "/guides/deployment",
Expand Down Expand Up @@ -87,9 +91,9 @@ const sidebar = [
link: "/reference/core/remix",
},
{
text: 'Cloudflare Workers',
link: '/reference/core/cloudflare-workers'
}
text: "Cloudflare Workers",
link: "/reference/core/cloudflare-workers",
},
],
},
{
Expand Down
6 changes: 5 additions & 1 deletion examples/framesjs-starter/app/frames/frames.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { createFrames } from "frames.js/next";

export const frames = createFrames({
type State = {
counter: number;
};

export const frames = createFrames<State>({
basePath: "/frames",
initialState: { counter: 0 },
});
41 changes: 26 additions & 15 deletions packages/debugger/app/components/frame-debugger.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getFrameHtmlHead, getFrameFlattened } from "frames.js";
import { Dispatch, SetStateAction, useEffect, useMemo, useState } from "react";
import React from "react";
import { FrameState, FrameRequest, FrameStackSuccess } from "frames.js/render";
import { FrameState, FrameStackSuccess } from "@frames.js/render";
import { Table, TableBody, TableCell, TableRow } from "@/components/table";
import {
AlertTriangle,
Expand All @@ -10,7 +10,6 @@ import {
HomeIcon,
ListIcon,
LoaderIcon,
MessageCircle,
MessageCircleHeart,
RefreshCwIcon,
XCircle,
Expand Down Expand Up @@ -190,20 +189,22 @@ export function FrameDebugger({

const [openAccordions, setOpenAccordions] = useState<string[]>([]);

const [latestFrame] = frameState.framesStack;

useEffect(() => {
if (!frameState.isLoading) {
// make sure the first frame is open
if (
!openAccordions.includes(
String(frameState.framesStack[0]?.timestamp.getTime())
String(latestFrame?.timestamp.getTime())
)
)
setOpenAccordions((v) => [
...v,
String(frameState.framesStack[0]?.timestamp.getTime()),
String(latestFrame?.timestamp.getTime()),
]);
}
}, [frameState.isLoading]);
}, [frameState.isLoading, latestFrame?.timestamp, openAccordions]);

return (
<div className="flex flex-row items-start p-4 gap-4 bg-slate-50 max-w-full w-full h-full">
Expand All @@ -219,7 +220,6 @@ export function FrameDebugger({
frameState.fetchFrame({
url: frameState?.homeframeUrl,
method: "GET",
request: {},
});
}}
>
Expand All @@ -235,7 +235,6 @@ export function FrameDebugger({
frameState.fetchFrame({
url: frameState?.homeframeUrl,
method: "GET",
request: {},
});
}
}}
Expand All @@ -246,12 +245,21 @@ export function FrameDebugger({
className="flex flex-row gap-3 items-center shadow-sm border"
variant={"outline"}
onClick={() => {
if (frameState?.framesStack[0]?.request) {
frameState.fetchFrame({
url: frameState?.framesStack[0].url,
method: frameState?.framesStack[0].method,
request: frameState.framesStack[0].request,
} as FrameRequest);
const [latestFrame] = frameState.framesStack;

if (latestFrame) {
frameState.fetchFrame(
latestFrame.method === "GET"
? {
method: "GET",
url: latestFrame.url,
}
: {
method: "POST",
request: latestFrame.request,
url: latestFrame.url,
}
);
}
}}
>
Expand All @@ -269,11 +277,14 @@ export function FrameDebugger({
className={`px-4 py-3 flex flex-col gap-2 ${i !== 0 ? "border-t" : "bg-slate-50"} hover:bg-slate-50 w-full`}
key={frameStackItem.timestamp.getTime()}
onClick={() => {
frameState.fetchFrame({
frameState.fetchFrame(frameStackItem.method === 'GET' ? {
method: 'GET',
url: frameStackItem.url,
} : {
url: frameStackItem.url,
method: frameStackItem.method,
request: frameStackItem.request,
} as FrameRequest);
});
}}
>
<span className="flex text-left flex-row w-full">
Expand Down
Loading