From 9a167aa688c1f22531dbfb589fb985783636fbca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E4=BC=9F=E5=BC=BA?= Date: Thu, 18 May 2023 16:50:42 +0800 Subject: [PATCH] feat: text generaion support both url --- .../explore/installed-app/index.tsx | 8 +++- .../share/text-generation/index.tsx | 38 +++++++++++++++---- web/service/share.ts | 2 +- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/web/app/components/explore/installed-app/index.tsx b/web/app/components/explore/installed-app/index.tsx index cc786f9e10..a945ceff89 100644 --- a/web/app/components/explore/installed-app/index.tsx +++ b/web/app/components/explore/installed-app/index.tsx @@ -2,6 +2,7 @@ import React, { FC, useEffect } from 'react' import { App } from '@/types/app' import ChatApp from '@/app/components/share/chat' +import TextGenerationApp from '@/app/components/share/text-generation' import { fetchAppDetail } from '@/service/explore' import Loading from '@/app/components/base/loading' @@ -35,7 +36,12 @@ const InstalledApp: FC = ({ return (
- + {app?.mode === 'chat' ? ( + + ): ( + + )} +
) } diff --git a/web/app/components/share/text-generation/index.tsx b/web/app/components/share/text-generation/index.tsx index 011143ed72..18942ee9e3 100644 --- a/web/app/components/share/text-generation/index.tsx +++ b/web/app/components/share/text-generation/index.tsx @@ -1,5 +1,5 @@ 'use client' -import React, { useEffect, useState, useRef } from 'react' +import React, { FC, useEffect, useState, useRef } from 'react' import { useTranslation } from 'react-i18next' import useBreakpoints, { MediaType } from '@/hooks/use-breakpoints' import cn from 'classnames' @@ -22,8 +22,17 @@ import TabHeader from '../../base/tab-header' import { XMarkIcon } from '@heroicons/react/24/outline' import s from './style.module.css' import Button from '../../base/button' +import { App } from '@/types/app' -const TextGeneration = () => { +export type IMainProps = { + isInstalledApp?: boolean, + installedAppInfo? : App +} + +const TextGeneration: FC = ({ + isInstalledApp = false, + installedAppInfo +}) => { const { t } = useTranslation() const media = useBreakpoints() const isPC = media === MediaType.pc @@ -49,14 +58,14 @@ const TextGeneration = () => { }) const handleFeedback = async (feedback: Feedbacktype) => { - await updateFeedback({ url: `/messages/${messageId}/feedbacks`, body: { rating: feedback.rating } }) + await updateFeedback({ url: `/messages/${messageId}/feedbacks`, body: { rating: feedback.rating } }, isInstalledApp, installedAppInfo?.id) setFeedback(feedback) } const [savedMessages, setSavedMessages] = useState([]) const fetchSavedMessage = async () => { - const res: any = await doFetchSavedMessage() + const res: any = await doFetchSavedMessage(isInstalledApp, installedAppInfo?.id) setSavedMessages(res.data) } @@ -65,13 +74,13 @@ const TextGeneration = () => { }, []) const handleSaveMessage = async (messageId: string) => { - await saveMessage(messageId) + await saveMessage(messageId, isInstalledApp, installedAppInfo?.id) notify({ type: 'success', message: t('common.api.saved') }) fetchSavedMessage() } const handleRemoveSavedMessage = async (messageId: string) => { - await removeMessage(messageId) + await removeMessage(messageId, isInstalledApp, installedAppInfo?.id) notify({ type: 'success', message: t('common.api.remove') }) fetchSavedMessage() } @@ -151,12 +160,25 @@ const TextGeneration = () => { onError() { setResponsingFalse() } - }) + }, isInstalledApp, installedAppInfo?.id) + } + + const fetchInitData = () => { + return Promise.all([isInstalledApp ? { + app_id: installedAppInfo?.id, + site: { + title: installedAppInfo?.name, + prompt_public: false, + copyright: '' + }, + model_config: installedAppInfo?.app_model_config, + plan: 'basic', + }: fetchAppInfo(), fetchAppParams(isInstalledApp, installedAppInfo?.id)]) } useEffect(() => { (async () => { - const [appData, appParams]: any = await Promise.all([fetchAppInfo(), fetchAppParams()]) + const [appData, appParams]: any = await fetchInitData() const { app_id: appId, site: siteInfo } = appData setAppId(appId) setSiteInfo(siteInfo as SiteInfo) diff --git a/web/service/share.ts b/web/service/share.ts index dea35f36be..adb23caeb5 100644 --- a/web/service/share.ts +++ b/web/service/share.ts @@ -17,7 +17,7 @@ function getAction(action: 'get' | 'post' | 'del', isInstalledApp: boolean) { } function getUrl(url: string, isInstalledApp: boolean, installedAppId: string) { - return isInstalledApp ? `installed-apps/${installedAppId}/${url}` : url + return isInstalledApp ? `installed-apps/${installedAppId}/${url.startsWith('/') ? url.slice(1) : url}` : url } export const sendChatMessage = async (body: Record, { onData, onCompleted, onError, getAbortController }: {