From 3a432f545f3ae081cd58dea4b04935711622aec5 Mon Sep 17 00:00:00 2001 From: arvinxx Date: Mon, 23 Oct 2023 22:25:29 +0800 Subject: [PATCH] :sparkles: feat: add standalone type --- package.json | 6 ++-- public/manifest-standalone.json | 32 +++++++++++++++++++ src/components/Render.tsx | 2 +- src/pages/api/clothes.ts | 2 +- src/pages/iframe/index.tsx | 55 +++++++++++++++++++++++++++++++++ src/services/clothes.ts | 10 ++++++ 6 files changed, 101 insertions(+), 6 deletions(-) create mode 100644 public/manifest-standalone.json create mode 100644 src/pages/iframe/index.tsx create mode 100644 src/services/clothes.ts diff --git a/package.json b/package.json index b68e0dd..ec0c897 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,7 @@ "not ie <= 10" ], "dependencies": { - "@lobehub/chat-plugin-sdk": "^1.17.7", + "@lobehub/chat-plugin-sdk": "^1.18.0", "@lobehub/chat-plugins-gateway": "^1", "@lobehub/ui": "latest", "antd": "^5", @@ -66,8 +66,7 @@ "devDependencies": { "@commitlint/cli": "^17", "@lobehub/lint": "latest", - "@types/react": "18", - "@types/react-dom": "18", + "@types/react": "18.2.28", "@vercel/node": "^2", "@vitest/coverage-v8": "latest", "commitlint": "^17", @@ -75,7 +74,6 @@ "eslint": "^8", "father": "4.3.1", "husky": "^8", - "lerna": "^7", "lint-staged": "^13", "prettier": "^3", "remark": "^14", diff --git a/public/manifest-standalone.json b/public/manifest-standalone.json new file mode 100644 index 0000000..d73d699 --- /dev/null +++ b/public/manifest-standalone.json @@ -0,0 +1,32 @@ +{ + "api": [ + { + "url": "http://localhost:3400/api/clothes", + "name": "recommendClothes", + "description": "根据用户的心情,给用户推荐他有的衣服", + "parameters": { + "properties": { + "mood": { + "description": "用户当前的心情,可选值有:开心(happy), 难过(sad),生气 (anger),害怕(fear),惊喜( surprise),厌恶 (disgust)", + "enums": ["happy", "sad", "anger", "fear", "surprise", "disgust"], + "type": "string" + }, + "gender": { + "type": "string", + "enum": ["man", "woman"], + "description": "对话用户的性别,需要询问用户后才知道这个信息" + } + }, + "required": ["mood", "gender"], + "type": "object" + } + } + ], + "identifier": "plugin-identifier-standalone", + "type": "standalone", + "ui": { + "url": "http://localhost:3400/iframe", + "height": 200 + }, + "version": "1" +} diff --git a/src/components/Render.tsx b/src/components/Render.tsx index aba4de1..312d411 100644 --- a/src/components/Render.tsx +++ b/src/components/Render.tsx @@ -23,7 +23,7 @@ const Render = memo>(({ mood, clothes, today }) => { 推荐衣物 - + {clothes?.map((item) => ( {item.description} diff --git a/src/pages/api/clothes.ts b/src/pages/api/clothes.ts index af9e0c9..df97c99 100644 --- a/src/pages/api/clothes.ts +++ b/src/pages/api/clothes.ts @@ -15,7 +15,7 @@ export default async (req: Request) => { const clothes = gender === 'man' ? manClothes : womanClothes; const result: ResponseData = { - clothes: clothes[mood] || [], + clothes: mood ? clothes[mood] : Object.values(clothes).flat(), mood, today: Date.now(), }; diff --git a/src/pages/iframe/index.tsx b/src/pages/iframe/index.tsx new file mode 100644 index 0000000..2dc0c2c --- /dev/null +++ b/src/pages/iframe/index.tsx @@ -0,0 +1,55 @@ +import { + fetchPluginMessage, + postToFillPluginContent, + useOnStandalonePluginInit, +} from '@lobehub/chat-plugin-sdk/client'; +import { Button } from 'antd'; +import { memo, useEffect, useState } from 'react'; +import { Center } from 'react-layout-kit'; + +import Data from '@/components/Render'; +import { fetchClothes } from '@/services/clothes'; +import { ResponseData } from '@/type'; + +const Render = memo(() => { + // 初始化渲染状态 + const [data, setData] = useState(); + + // 初始化时从主应用同步状态 + useEffect(() => { + fetchPluginMessage().then(setData); + }, []); + + // 记录请求参数 + const [payload, setPayload] = useState(); + + useOnStandalonePluginInit((payload) => { + if (payload.func === 'recommendClothes') { + setPayload(payload.args); + } + }); + + const fetchData = async () => { + const data = await fetchClothes(payload); + setData(data); + postToFillPluginContent(data); + }; + + return data ? ( + + ) : ( +
+ +
+ ); +}); + +export default Render; diff --git a/src/services/clothes.ts b/src/services/clothes.ts new file mode 100644 index 0000000..ba3e2b2 --- /dev/null +++ b/src/services/clothes.ts @@ -0,0 +1,10 @@ +import { RequestData } from '@/type'; + +export const fetchClothes = async (params: RequestData) => { + const res = await fetch('/api/clothes', { + body: JSON.stringify(params), + method: 'POST', + }); + + return res.json(); +};