Skip to content

Commit

Permalink
feat: handle falsy intents
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Feb 8, 2024
1 parent 238c2c1 commit 5371211
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
19 changes: 18 additions & 1 deletion example/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ app.frame('/', ({ status }) => {
</div>
</div>
),
intents: [
intents: status === 'initial' && [
<Button>Apples</Button>,
<Button>Oranges</Button>,
<Button>Bananas</Button>,
Expand All @@ -58,6 +58,23 @@ app.frame('/no-intents', () => {
}
})

app.frame('/falsy-intents', () => {
return {
image: (
<div style={{ backgroundColor: 'red', width: '100%', height: '100%' }}>
foo
</div>
),
intents: [
null,
undefined,
false,
<Button>Apples</Button>,
false && <Button>Oranges</Button>,
],
}
})

export default {
port: 3001,
fetch: app.fetch,
Expand Down
17 changes: 12 additions & 5 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,19 @@ type FrameContext = {
url: Context['req']['url']
}

type FrameReturnType = {
type Intent = JSX.Element | false | null | undefined
type Intents = Intent | Intent[]
type FrameHandlerReturnType = {
image: JSX.Element
intents?: JSX.Element | JSX.Element[]
intents?: Intents
}

export class Framework extends Hono {
frame(
path: string,
handler: (c: FrameContext) => FrameReturnType | Promise<FrameReturnType>,
handler: (
c: FrameContext,
) => FrameHandlerReturnType | Promise<FrameHandlerReturnType>,
) {
// Frame Route (implements GET & POST).
this.use(path, async (c) => {
Expand Down Expand Up @@ -395,7 +399,7 @@ async function getFrameContext(ctx: Context): Promise<FrameContext> {
}
}

function parseIntents(intents_: JSX.Element | JSX.Element[]) {
function parseIntents(intents_: Intents) {
const intents = intents_ as unknown as JSXNode
const counter: Counter = {
button: 1,
Expand All @@ -410,7 +414,10 @@ function parseIntents(intents_: JSX.Element | JSX.Element[]) {
return parseIntent(intents, counter)
}

function parseIntent(node: JSXNode, counter: Counter) {
function parseIntent(node_: JSXNode, counter: Counter) {
// Check if the node is a "falsy" node (ie. `null`, `undefined`, `false`, etc).
const node = (!node_ ? { tag() {} } : node_) as JSXNode

const props = (() => {
if ((node.tag as any).__type === 'button')
return { ...node.props, children: node.children, index: counter.button++ }
Expand Down

0 comments on commit 5371211

Please sign in to comment.