Skip to content

Commit 73ae1ee

Browse files
committed
Initial push
0 parents  commit 73ae1ee

File tree

13 files changed

+3879
-0
lines changed

13 files changed

+3879
-0
lines changed

.eslintrc.cjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
root: true,
3+
extends: ['custom'],
4+
rules: {
5+
'@next/next/no-img-element': 'off',
6+
'@next/next/no-html-link-for-pages': 'off',
7+
},
8+
}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
dist

.npmignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
src
2+
.env.local.example
3+
.eslintignore
4+
.eslintrc.cjs
5+
rollup.config.js
6+
tsconfig.json

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<!-- markdownlint-disable MD030 -->
2+
3+
# Flowise Embed React
4+
5+
React library to display flowise chatbot on your website
6+
7+
![Flowise](https://github.com/FlowiseAI/FlowiseChatEmbed/blob/main/images/ChatEmbed.gif?raw=true)
8+
9+
## How to Use
10+
11+
In your project, install:
12+
13+
```bash
14+
npm install flowise-embed flowise-embed-react
15+
```
16+
17+
or
18+
19+
```bash
20+
yarn add flowise-embed flowise-embed-react
21+
```
22+
23+
Then use it:
24+
25+
```tsx
26+
import { FullPageChat } from "flowise-embed-react";
27+
28+
const App = () => {
29+
return (
30+
<FullPageChat
31+
chatflowid="your-chatflow-id"
32+
apiHost="http://localhost:3000"
33+
style={{ width: "100%", height: "600px" }}
34+
/>
35+
);
36+
};
37+
```

base.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"$schema": "https://json.schemastore.org/tsconfig",
3+
"display": "Default",
4+
"compilerOptions": {
5+
"composite": false,
6+
"declaration": true,
7+
"declarationMap": true,
8+
"esModuleInterop": true,
9+
"forceConsistentCasingInFileNames": true,
10+
"inlineSources": false,
11+
"isolatedModules": true,
12+
"moduleResolution": "node",
13+
"noUnusedLocals": false,
14+
"noUnusedParameters": false,
15+
"preserveWatchOutput": true,
16+
"skipLibCheck": true,
17+
"strict": true,
18+
"downlevelIteration": true
19+
},
20+
"exclude": ["node_modules"]
21+
}

package.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "flowise-embed-react",
3+
"version": "1.0.0",
4+
"description": "React library to display flowise chatbot on your website",
5+
"main": "dist/index.js",
6+
"types": "dist/index.d.ts",
7+
"type": "module",
8+
"scripts": {
9+
"dev": "rollup --watch --config rollup.config.js",
10+
"build": "rollup --config rollup.config.js",
11+
"lint": "eslint --fix \"src/**/*.ts*\""
12+
},
13+
"keywords": [],
14+
"license": "MIT",
15+
"dependencies": {
16+
"@ladle/react": "2.5.1"
17+
},
18+
"devDependencies": {
19+
"@babel/preset-react": "7.18.6",
20+
"@babel/preset-typescript": "7.21.4",
21+
"@rollup/plugin-babel": "6.0.3",
22+
"@rollup/plugin-node-resolve": "15.0.1",
23+
"@rollup/plugin-terser": "0.4.0",
24+
"@rollup/plugin-typescript": "11.0.0",
25+
"@types/node": "18.15.11",
26+
"@types/react": "18.0.32",
27+
"eslint": "8.37.0",
28+
"flowise-embed": "*",
29+
"react": "18.2.0",
30+
"rollup": "3.20.2",
31+
"rollup-plugin-typescript-paths": "1.4.0",
32+
"tslib": "2.5.0",
33+
"tsx": "3.12.6",
34+
"typescript": "5.0.3"
35+
},
36+
"peerDependencies": {
37+
"flowise-embed": "*",
38+
"react": "18.x"
39+
}
40+
}

react-library.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"$schema": "https://json.schemastore.org/tsconfig",
3+
"display": "React Library",
4+
"extends": "./base.json",
5+
"compilerOptions": {
6+
"jsx": "react-jsx",
7+
"lib": ["ES2015", "dom", "dom.iterable"],
8+
"module": "ESNext",
9+
"target": "es6"
10+
}
11+
}
12+

rollup.config.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import resolve from "@rollup/plugin-node-resolve";
2+
import terser from "@rollup/plugin-terser";
3+
import { babel } from "@rollup/plugin-babel";
4+
import typescript from "@rollup/plugin-typescript";
5+
import { typescriptPaths } from "rollup-plugin-typescript-paths";
6+
7+
const extensions = [".ts", ".tsx"];
8+
9+
const indexConfig = {
10+
input: "./src/index.ts",
11+
output: {
12+
file: "./dist/index.js",
13+
format: "es",
14+
},
15+
external: ["react", "react/jsx-runtime", "flowise-embed"],
16+
plugins: [
17+
resolve({ extensions }),
18+
babel({
19+
babelHelpers: "bundled",
20+
exclude: "node_modules/**",
21+
presets: ["@babel/preset-react", "@babel/preset-typescript"],
22+
extensions,
23+
}),
24+
typescript(),
25+
typescriptPaths({ preserveExtensions: true }),
26+
terser({ output: { comments: false } }),
27+
],
28+
};
29+
30+
const configs = [indexConfig];
31+
32+
export default configs;

src/BubbleChat.tsx

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { useCallback, useEffect, useRef, useState } from 'react'
2+
import type { BubbleProps } from 'flowise-embed'
3+
4+
type Props = BubbleProps
5+
6+
declare global {
7+
namespace JSX {
8+
interface IntrinsicElements {
9+
'flowise-bubble': React.DetailedHTMLProps<
10+
React.HTMLAttributes<HTMLElement>,
11+
HTMLElement
12+
>
13+
}
14+
}
15+
}
16+
17+
type BubbleElement = HTMLElement & Props
18+
19+
export const BubbleChat = (props: Props) => {
20+
const ref = useRef<BubbleElement | null>(null)
21+
const [isInitialized, setIsInitialized] = useState(false)
22+
23+
useEffect(() => {
24+
;(async () => {
25+
await import('flowise-embed/dist/web')
26+
setIsInitialized(true)
27+
})()
28+
return () => {
29+
ref.current?.remove()
30+
}
31+
}, [])
32+
33+
const attachBubbleToDom = useCallback((props: Props) => {
34+
const bubbleElement = document.createElement(
35+
'flowise-bubble'
36+
) as BubbleElement
37+
ref.current = bubbleElement
38+
injectPropsToElement(ref.current, props)
39+
document.body.append(ref.current)
40+
}, [])
41+
42+
useEffect(() => {
43+
if (!isInitialized) return
44+
if (!ref.current) attachBubbleToDom(props)
45+
injectPropsToElement(ref.current as BubbleElement, props)
46+
}, [attachBubbleToDom, isInitialized, props])
47+
48+
const injectPropsToElement = (element: BubbleElement, props: Props) => {
49+
Object.assign(element, props)
50+
}
51+
52+
return null
53+
}

src/FullPageChat.tsx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { useEffect, useRef } from 'react'
2+
import type { BotProps } from 'flowise-embed'
3+
4+
type Props = BotProps & {
5+
style?: React.CSSProperties
6+
className?: string
7+
}
8+
9+
declare global {
10+
namespace JSX {
11+
interface IntrinsicElements {
12+
'flowise-fullchatbot': React.DetailedHTMLProps<
13+
React.HTMLAttributes<HTMLElement>,
14+
HTMLElement
15+
> & { class?: string }
16+
}
17+
}
18+
}
19+
20+
type FullPageChatElement = HTMLElement & Props
21+
22+
export const FullPageChat = ({ style, className, ...assignableProps }: Props) => {
23+
const ref = useRef<FullPageChatElement | null>(null)
24+
25+
useEffect(() => {
26+
;(async () => {
27+
await import('flowise-embed/dist/web')
28+
})()
29+
}, [])
30+
31+
useEffect(() => {
32+
if (!ref.current) return
33+
Object.assign(ref.current, assignableProps)
34+
}, [assignableProps])
35+
36+
return <flowise-fullchatbot ref={ref} style={style} class={className} />
37+
}

0 commit comments

Comments
 (0)