Skip to content

Commit 3a432f5

Browse files
committed
✨ feat: add standalone type
1 parent 215ced4 commit 3a432f5

File tree

6 files changed

+101
-6
lines changed

6 files changed

+101
-6
lines changed

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
"not ie <= 10"
5353
],
5454
"dependencies": {
55-
"@lobehub/chat-plugin-sdk": "^1.17.7",
55+
"@lobehub/chat-plugin-sdk": "^1.18.0",
5656
"@lobehub/chat-plugins-gateway": "^1",
5757
"@lobehub/ui": "latest",
5858
"antd": "^5",
@@ -66,16 +66,14 @@
6666
"devDependencies": {
6767
"@commitlint/cli": "^17",
6868
"@lobehub/lint": "latest",
69-
"@types/react": "18",
70-
"@types/react-dom": "18",
69+
"@types/react": "18.2.28",
7170
"@vercel/node": "^2",
7271
"@vitest/coverage-v8": "latest",
7372
"commitlint": "^17",
7473
"cross-env": "^7",
7574
"eslint": "^8",
7675
"father": "4.3.1",
7776
"husky": "^8",
78-
"lerna": "^7",
7977
"lint-staged": "^13",
8078
"prettier": "^3",
8179
"remark": "^14",

public/manifest-standalone.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"api": [
3+
{
4+
"url": "http://localhost:3400/api/clothes",
5+
"name": "recommendClothes",
6+
"description": "根据用户的心情,给用户推荐他有的衣服",
7+
"parameters": {
8+
"properties": {
9+
"mood": {
10+
"description": "用户当前的心情,可选值有:开心(happy), 难过(sad),生气 (anger),害怕(fear),惊喜( surprise),厌恶 (disgust)",
11+
"enums": ["happy", "sad", "anger", "fear", "surprise", "disgust"],
12+
"type": "string"
13+
},
14+
"gender": {
15+
"type": "string",
16+
"enum": ["man", "woman"],
17+
"description": "对话用户的性别,需要询问用户后才知道这个信息"
18+
}
19+
},
20+
"required": ["mood", "gender"],
21+
"type": "object"
22+
}
23+
}
24+
],
25+
"identifier": "plugin-identifier-standalone",
26+
"type": "standalone",
27+
"ui": {
28+
"url": "http://localhost:3400/iframe",
29+
"height": 200
30+
},
31+
"version": "1"
32+
}

src/components/Render.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const Render = memo<Partial<ResponseData>>(({ mood, clothes, today }) => {
2323
</Flexbox>
2424
<Flexbox gap={8}>
2525
推荐衣物
26-
<Flexbox gap={12} horizontal>
26+
<Flexbox gap={12} horizontal style={{ overflow: 'scroll' }}>
2727
{clothes?.map((item) => (
2828
<Card key={item.name} size={'small'} title={item.name}>
2929
{item.description}

src/pages/api/clothes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default async (req: Request) => {
1515
const clothes = gender === 'man' ? manClothes : womanClothes;
1616

1717
const result: ResponseData = {
18-
clothes: clothes[mood] || [],
18+
clothes: mood ? clothes[mood] : Object.values(clothes).flat(),
1919
mood,
2020
today: Date.now(),
2121
};

src/pages/iframe/index.tsx

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import {
2+
fetchPluginMessage,
3+
postToFillPluginContent,
4+
useOnStandalonePluginInit,
5+
} from '@lobehub/chat-plugin-sdk/client';
6+
import { Button } from 'antd';
7+
import { memo, useEffect, useState } from 'react';
8+
import { Center } from 'react-layout-kit';
9+
10+
import Data from '@/components/Render';
11+
import { fetchClothes } from '@/services/clothes';
12+
import { ResponseData } from '@/type';
13+
14+
const Render = memo(() => {
15+
// 初始化渲染状态
16+
const [data, setData] = useState<ResponseData>();
17+
18+
// 初始化时从主应用同步状态
19+
useEffect(() => {
20+
fetchPluginMessage().then(setData);
21+
}, []);
22+
23+
// 记录请求参数
24+
const [payload, setPayload] = useState<any>();
25+
26+
useOnStandalonePluginInit<ResponseData>((payload) => {
27+
if (payload.func === 'recommendClothes') {
28+
setPayload(payload.args);
29+
}
30+
});
31+
32+
const fetchData = async () => {
33+
const data = await fetchClothes(payload);
34+
setData(data);
35+
postToFillPluginContent(data);
36+
};
37+
38+
return data ? (
39+
<Data {...data}></Data>
40+
) : (
41+
<Center style={{ height: 150 }}>
42+
<Button
43+
disabled={!payload}
44+
onClick={() => {
45+
fetchData();
46+
}}
47+
type={'primary'}
48+
>
49+
查询衣物
50+
</Button>
51+
</Center>
52+
);
53+
});
54+
55+
export default Render;

src/services/clothes.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { RequestData } from '@/type';
2+
3+
export const fetchClothes = async (params: RequestData) => {
4+
const res = await fetch('/api/clothes', {
5+
body: JSON.stringify(params),
6+
method: 'POST',
7+
});
8+
9+
return res.json();
10+
};

0 commit comments

Comments
 (0)