Skip to content

Commit

Permalink
fix: state handling
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasc committed Mar 7, 2024
1 parent cdbeaf5 commit 95492d2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
13 changes: 6 additions & 7 deletions framegear/components/Frame/Frame.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ export function Frame() {

const latestFrame = results[results.length - 1];

if (!latestFrame.isValid) {
return <ErrorFrame />;
}

return <ValidFrame metadata={latestFrame.metadata} />;
}

Expand Down Expand Up @@ -58,6 +54,7 @@ function ValidFrame({ metadata }: { metadata: FrameMetadataWithImageObject }) {
key={button.label}
index={index + 1}
button={button}
state={metadata.state}
>
{button.label}
</FrameButton>
Expand All @@ -81,7 +78,7 @@ function PlaceholderFrame() {
<div className="flex flex-col">
<div className="bg-farcaster flex aspect-[1.91/1] w-full rounded-t-xl"></div>
<div className="bg-button-gutter-light dark:bg-content-light flex flex-wrap gap-2 rounded-b-xl px-4 py-2">
<FrameButton index={1} inputText="">
<FrameButton state={{}} index={1} inputText="">
Get Started
</FrameButton>
</div>
Expand All @@ -94,10 +91,12 @@ function FrameButton({
button,
index,
inputText,
state,
}: PropsWithChildren<{
button?: NonNullable<FrameMetadataWithImageObject['buttons']>[0];
index: number;
inputText: string;
state: any;
}>) {
const { openModal } = useRedirectModal();
const [isLoading, setIsLoading] = useState(false);
Expand All @@ -110,6 +109,7 @@ function FrameButton({
const result = await postFrame({
buttonIndex: index,
url: button.target!,
state: JSON.stringify(state),
// TODO: make these user-input-driven
castId: {
fid: 0,
Expand All @@ -120,7 +120,6 @@ function FrameButton({
messageHash: '0xthisisnotreal',
network: 0,
timestamp: 0,
state: '',
});
// TODO: handle when result is not defined
if (result) {
Expand All @@ -140,7 +139,7 @@ function FrameButton({
openModal(onConfirm);
}
// TODO: implement other actions (mint, etc.)
}, [button?.action, button?.target, index, inputText, openModal, setResults]);
}, [button, index, inputText, openModal, setResults, state]);

const buttonIcon = useMemo(() => {
switch (button?.action) {
Expand Down
8 changes: 4 additions & 4 deletions framegear/utils/frameResultToFrameMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ export type FrameMetadataWithImageObject = FrameMetadataType & {
export function frameResultToFrameMetadata(
result: Record<string, string>,
): FrameMetadataWithImageObject {
const postUrl = result['fc:frame:post_url'];
const buttons = [1, 2, 3, 4].map((idx) =>
result[`fc:frame:button:${idx}`]
? {
action: result[`fc:frame:button:${idx}:action`],
action: result[`fc:frame:button:${idx}:action`] || 'post',
label: result[`fc:frame:button:${idx}`],
target: result[`fc:frame:button:${idx}:target`],
target: result[`fc:frame:button:${idx}:target`] || postUrl,
}
: undefined,
);
const imageSrc = result['fc:frame:image'];
const imageAspectRatio = result['fc:frame:image:aspect_ratio'];
const inputText = result['fc:frame:input'];
const input = inputText ? { text: inputText } : undefined;
const postUrl = result['fc:frame:post_url'];
const rawState = result['fc:frame:state'];
const rawRefreshPeriod = result['fc:frame:refresh_period'];
const refreshPeriod = rawRefreshPeriod ? parseInt(rawRefreshPeriod, 10) : undefined;
const state = rawState ? JSON.parse(result['fc:frame:state']) : undefined;
const state = rawState ? JSON.parse(decodeURIComponent(result['fc:frame:state'])) : undefined;

return {
buttons: buttons as any,
Expand Down

0 comments on commit 95492d2

Please sign in to comment.