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

QRcode block #1142

Merged
merged 5 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
41 changes: 41 additions & 0 deletions packages/forge/blocks/qrcode/actions/generateQrCodeImage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { createAction, option } from '@typebot.io/forge'
import { toBuffer as generateQrCodeBuffer } from 'qrcode'
import { uploadFileToBucket } from '@typebot.io/lib/s3/uploadFileToBucket'
import { createId } from '@typebot.io/lib/createId'

export const generateQrCode = createAction({
name: 'Generate a QR Code',
options: option.object({
data: option.string.layout({
label: 'Data',
helperText:
'This can be a URL, or any text data you want to encode into a QR code.',
}),
saveQrcodeInVariableId: option.string.layout({
label: 'Save QR code image URL',
inputType: 'variableDropdown',
}),
}),
getSetVariableIds: (options) =>
options.saveQrcodeInVariableId ? [options.saveQrcodeInVariableId] : [],
run: {
server: async ({ options, variables, logs }) => {
if (!options.data)
return logs.add(
'QR code value is empty. Please provide the data to be encoded into the QR code.'
)
if (!options.saveQrcodeInVariableId)
return logs.add(
'QR Code save variable is not specified. Please select a variable to save the generated QR code.'
)

const url = await uploadFileToBucket({
file: await generateQrCodeBuffer(options.data),
key: `tmp/qrcodes/${createId() + createId()}.png`,
mimeType: 'image/png',
})

variables.set(options.saveQrcodeInVariableId, url)
},
},
})
11 changes: 11 additions & 0 deletions packages/forge/blocks/qrcode/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { createBlock } from '@typebot.io/forge'
import { QrCodeLogo } from './logo'
import { generateQrCode } from './actions/generateQrCodeImage'

export const qrCode = createBlock({
id: 'qr-code',
name: 'QR code',
tags: [],
LightLogo: QrCodeLogo,
actions: [generateQrCode],
})
27 changes: 27 additions & 0 deletions packages/forge/blocks/qrcode/logo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from 'react'

export const QrCodeLogo = (props: React.SVGProps<SVGSVGElement>) => (
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
{...props}
>
<rect width="5" height="5" x="3" y="3" rx="1" />
<rect width="5" height="5" x="16" y="3" rx="1" />
<rect width="5" height="5" x="3" y="16" rx="1" />
<path d="M21 16h-3a2 2 0 0 0-2 2v3" />
<path d="M21 21v.01" />
<path d="M12 7v3a2 2 0 0 1-2 2H7" />
<path d="M3 12h.01" />
<path d="M12 3h.01" />
<path d="M12 16v.01" />
<path d="M16 12h1" />
<path d="M21 12v.01" />
<path d="M12 21v-1" />
</svg>
)
19 changes: 19 additions & 0 deletions packages/forge/blocks/qrcode/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "@typebot.io/qrcode-block",
"version": "1.0.0",
"description": "",
"main": "index.ts",
"keywords": [],
"license": "ISC",
"devDependencies": {
"@typebot.io/forge": "workspace:*",
"@typebot.io/lib": "workspace:*",
"@typebot.io/tsconfig": "workspace:*",
"@types/react": "18.2.15",
"@types/qrcode": "^1.5.3",
"typescript": "5.3.2"
},
"dependencies": {
"qrcode": "^1.5.3"
}
}
10 changes: 10 additions & 0 deletions packages/forge/blocks/qrcode/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "@typebot.io/tsconfig/base.json",
"include": ["**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"],
"compilerOptions": {
"lib": ["ESNext", "DOM"],
"noEmit": true,
"jsx": "react"
}
}
5 changes: 3 additions & 2 deletions packages/forge/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ZodRawShape } from 'zod'
import { AuthDefinition, BlockDefinition, ActionDefinition } from './types'
import { z } from './zod'

Expand All @@ -18,8 +19,8 @@ export const createBlock = <

export const createAction = <
A extends AuthDefinition,
BaseOptions extends z.ZodObject<any>,
O extends z.ZodObject<any>
BaseOptions extends z.ZodObject<ZodRawShape> = z.ZodObject<{}>,
O extends z.ZodObject<ZodRawShape> = z.ZodObject<{}>
>(
actionDefinition: {
auth?: A
Expand Down
5 changes: 3 additions & 2 deletions packages/forge/core/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SVGProps } from 'react'
import { z } from './zod'
import { ZodRawShape } from 'zod'

export type VariableStore = {
get: (variableId: string) => string | (string | null)[] | null | undefined
Expand Down Expand Up @@ -28,8 +29,8 @@ export type ReadOnlyVariableStore = Omit<VariableStore, 'set'>

export type ActionDefinition<
A extends AuthDefinition,
BaseOptions extends z.ZodObject<any>,
Options extends z.ZodObject<any> = z.ZodObject<{}>
BaseOptions extends z.ZodObject<ZodRawShape> = z.ZodObject<{}>,
Options extends z.ZodObject<ZodRawShape> = z.ZodObject<{}>
> = {
name: string
fetchers?: FetcherDefinition<A, z.infer<BaseOptions> & z.infer<Options>>[]
Expand Down
1 change: 1 addition & 0 deletions packages/forge/repository/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export const enabledBlocks = [
'zemantic-ai',
'cal-com',
'chat-node',
'qr-code',
] as const
2 changes: 2 additions & 0 deletions packages/forge/schemas/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Do not edit this file manually
import { qrCode } from '@typebot.io/qrcode-block'
import { chatNode } from '@typebot.io/chat-node-block'
import { calCom } from '@typebot.io/cal-com-block'
import { zemanticAi } from '@typebot.io/zemantic-ai-block'
Expand All @@ -16,6 +17,7 @@ export const forgedBlocks = [
zemanticAi,
calCom,
chatNode,
qrCode,
] as BlockDefinition<(typeof enabledBlocks)[number], any, any>[]

export type ForgedBlockDefinition = (typeof forgedBlocks)[number]
Expand Down
3 changes: 2 additions & 1 deletion packages/forge/schemas/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"@typebot.io/openai-block": "workspace:*",
"@typebot.io/zemantic-ai-block": "workspace:*",
"@typebot.io/cal-com-block": "workspace:*",
"@typebot.io/chat-node-block": "workspace:*"
"@typebot.io/chat-node-block": "workspace:*",
"@typebot.io/qrcode-block": "workspace:*"
}
}
Loading