Skip to content

Commit

Permalink
fix: Button.Redirect (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm authored Mar 3, 2024
1 parent 71a8c77 commit 79bb79d
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/hip-penguins-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"frog": patch
---

Fixed Button.Redirect behavior
17 changes: 17 additions & 0 deletions playground/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -207,5 +207,22 @@ app.frame('/image-only', (c) => {
})
})

app.frame('/redirect-buttons', (c) => {
return c.res({
image: <div tw="flex">foo</div>,
intents: [
<Button.Redirect location={`https://example.com/${c.frameData?.fid}`}>
FID
</Button.Redirect>,
<Button.Redirect
location={`https://example.com/${c.frameData?.castId?.fid}`}
>
Cast ID
</Button.Redirect>,
<Button.Reset>Reset</Button.Reset>,
],
})
})

app.route('/todos', todoApp)
app.route('/routing', routingApp)
5 changes: 2 additions & 3 deletions src/components/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,8 @@ export function ButtonRedirect({
property={`fc:frame:button:${index}:action`}
content="post_redirect"
/>,
location && (
<meta property={`fc:frame:button:${index}:target`} content={location} />
),
// TODO: Add `target` prop so folks can `'post_redirect'` to a different frame
// <meta property={`fc:frame:button:${index}:target`} content={target} />,
] as unknown as HtmlEscapedString
}

Expand Down
6 changes: 5 additions & 1 deletion src/dev/components/Tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,11 @@ export function Tabs() {

<div class={rowClass} x-cloak x-show="data.response.location">
<div class={labelClass}>Location</div>
<div class={valueClass} x-text="data.response.location" />
<div
class={valueClass}
x-text="data.response.location"
{...{ ':title': 'data.response.location' }}
/>
</div>

<div class={rowClass} x-cloak x-show="data.response.error">
Expand Down
12 changes: 7 additions & 5 deletions src/frog-base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,6 @@ export class FrogBase<
req: c.req,
})

if (context.status === 'redirect') {
const location = context.buttonValue
if (!location) throw new Error('location required to redirect')
return c.redirect(location, 302)
}
if (context.url !== parsePath(c.req.url)) return c.redirect(context.url)

const {
Expand All @@ -256,6 +251,13 @@ export class FrogBase<
} = await handler(context)
const buttonValues = getButtonValues(parseIntents(intents))

if (context.status === 'redirect' && context.buttonIndex) {
const buttonValue = buttonValues[context.buttonIndex - 1]
const location = buttonValue?.replace(/^_r:/, '')
if (!location) throw new Error('location required to redirect')
return c.redirect(location, 302)
}

// If the user is coming from a browser, and a `browserLocation` is set,
// then we will redirect the user to that location.
const browser = detect(c.req.header('user-agent'))
Expand Down

0 comments on commit 79bb79d

Please sign in to comment.