From f2f19484b8f75b228d2ead4b35f20288381aea8c Mon Sep 17 00:00:00 2001 From: Joel Date: Wed, 31 May 2023 16:24:30 +0800 Subject: [PATCH] fix: text generation too long hide the operation btn (#271) --- .../share/text-generation/index.tsx | 160 +++++++++--------- 1 file changed, 83 insertions(+), 77 deletions(-) diff --git a/web/app/components/share/text-generation/index.tsx b/web/app/components/share/text-generation/index.tsx index e45073289f..aa651b697b 100644 --- a/web/app/components/share/text-generation/index.tsx +++ b/web/app/components/share/text-generation/index.tsx @@ -1,39 +1,39 @@ 'use client' -import React, { FC, useEffect, useState, useRef } from 'react' +import type { FC } from 'react' +import React, { useEffect, useRef, useState } from 'react' import { useTranslation } from 'react-i18next' -import useBreakpoints, { MediaType } from '@/hooks/use-breakpoints' import cn from 'classnames' import { useBoolean, useClickAway } from 'ahooks' +import { XMarkIcon } from '@heroicons/react/24/outline' +import TabHeader from '../../base/tab-header' +import Button from '../../base/button' +import s from './style.module.css' +import useBreakpoints, { MediaType } from '@/hooks/use-breakpoints' import ConfigScence from '@/app/components/share/text-generation/config-scence' import NoData from '@/app/components/share/text-generation/no-data' // import History from '@/app/components/share/text-generation/history' -import { fetchAppInfo, fetchAppParams, sendCompletionMessage, updateFeedback, saveMessage, fetchSavedMessage as doFetchSavedMessage, removeMessage } from '@/service/share' +import { fetchSavedMessage as doFetchSavedMessage, fetchAppInfo, fetchAppParams, removeMessage, saveMessage, sendCompletionMessage, updateFeedback } from '@/service/share' import type { SiteInfo } from '@/models/share' -import type { PromptConfig, MoreLikeThisConfig, SavedMessage } from '@/models/debug' +import type { MoreLikeThisConfig, PromptConfig, SavedMessage } from '@/models/debug' import Toast from '@/app/components/base/toast' import AppIcon from '@/app/components/base/app-icon' -import { Feedbacktype } from '@/app/components/app/chat' +import type { Feedbacktype } from '@/app/components/app/chat' import { changeLanguage } from '@/i18n/i18next-config' import Loading from '@/app/components/base/loading' import { userInputsFormToPromptVariables } from '@/utils/model-config' import TextGenerationRes from '@/app/components/app/text-generate/item' import SavedItems from '@/app/components/app/text-generate/saved-items' -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' -import { InstalledApp } from '@/models/explore' +import type { InstalledApp } from '@/models/explore' import { appDefaultIconBackground } from '@/config' export type IMainProps = { - isInstalledApp?: boolean, - installedAppInfo? : InstalledApp + isInstalledApp?: boolean + installedAppInfo?: InstalledApp } const TextGeneration: FC = ({ isInstalledApp = false, - installedAppInfo + installedAppInfo, }) => { const { t } = useTranslation() const media = useBreakpoints() @@ -56,7 +56,7 @@ const TextGeneration: FC = ({ const [messageId, setMessageId] = useState(null) const [feedback, setFeedback] = useState({ - rating: null + rating: null, }) const handleFeedback = async (feedback: Feedbacktype) => { @@ -93,21 +93,20 @@ const TextGeneration: FC = ({ const checkCanSend = () => { const prompt_variables = promptConfig?.prompt_variables - if (!prompt_variables || prompt_variables?.length === 0) { + if (!prompt_variables || prompt_variables?.length === 0) return true - } + let hasEmptyInput = false const requiredVars = prompt_variables?.filter(({ key, name, required }) => { const res = (!key || !key.trim()) || (!name || !name.trim()) || (required || required === undefined || required === null) return res }) || [] // compatible with old version requiredVars.forEach(({ key }) => { - if (hasEmptyInput) { + if (hasEmptyInput) return - } - if (!inputs[key]) { + + if (!inputs[key]) hasEmptyInput = true - } }) if (hasEmptyInput) { @@ -138,16 +137,17 @@ const TextGeneration: FC = ({ setMessageId(null) setFeedback({ - rating: null + rating: null, }) setCompletionRes('') const res: string[] = [] let tempMessageId = '' - if (!isPC) { + if (!isPC) + // eslint-disable-next-line @typescript-eslint/no-use-before-define showResSidebar() - } + setResponsingTrue() sendCompletionMessage(data, { onData: (data: string, _isFirstMessage: boolean, { messageId }: any) => { @@ -161,20 +161,22 @@ const TextGeneration: FC = ({ }, onError() { setResponsingFalse() - } + }, }, isInstalledApp, installedAppInfo?.id) } const fetchInitData = () => { - return Promise.all([isInstalledApp ? { - app_id: installedAppInfo?.id, - site: { - title: installedAppInfo?.app.name, - prompt_public: false, - copyright: '' - }, - plan: 'basic', - }: fetchAppInfo(), fetchAppParams(isInstalledApp, installedAppInfo?.id)]) + return Promise.all([isInstalledApp + ? { + app_id: installedAppInfo?.id, + site: { + title: installedAppInfo?.app.name, + prompt_public: false, + copyright: '', + }, + plan: 'basic', + } + : fetchAppInfo(), fetchAppParams(isInstalledApp, installedAppInfo?.id)]) } useEffect(() => { @@ -195,7 +197,7 @@ const TextGeneration: FC = ({ })() }, []) - // Can Use metadata(https://beta.nextjs.org/docs/api-reference/metadata) to set title. But it only works in server side client. + // Can Use metadata(https://beta.nextjs.org/docs/api-reference/metadata) to set title. But it only works in server side client. useEffect(() => { if (siteInfo?.title) document.title = `${siteInfo.title} - Powered by Dify` @@ -204,7 +206,7 @@ const TextGeneration: FC = ({ const [isShowResSidebar, { setTrue: showResSidebar, setFalse: hideResSidebar }] = useBoolean(false) const resRef = useRef(null) useClickAway(() => { - hideResSidebar(); + hideResSidebar() }, resRef) const renderRes = ( @@ -212,10 +214,10 @@ const TextGeneration: FC = ({ ref={resRef} className={ cn( - "flex flex-col h-full shrink-0", + 'flex flex-col h-full shrink-0', isPC ? 'px-10 py-8' : 'bg-gray-50', isTablet && 'p-6', isMoble && 'p-4') - } + } > <>
@@ -233,32 +235,34 @@ const TextGeneration: FC = ({ )}
-
- {(isResponsing && !completionRes) ? ( -
- -
) : ( - <> - {isNoData - ? - : ( - - ) - } - - )} +
+ {(isResponsing && !completionRes) + ? ( +
+ +
) + : ( + <> + {isNoData + ? + : ( + + ) + } + + )}
@@ -267,19 +271,18 @@ const TextGeneration: FC = ({ if (!appId || !siteInfo || !promptConfig) return - return ( <>
{/* Left */}
@@ -307,12 +310,16 @@ const TextGeneration: FC = ({ items={[ { id: 'create', name: t('share.generation.tabs.create') }, { - id: 'saved', name: t('share.generation.tabs.saved'), extra: savedMessages.length > 0 ? ( -
- {savedMessages.length} -
- ) : null - } + id: 'saved', + name: t('share.generation.tabs.saved'), + extra: savedMessages.length > 0 + ? ( +
+ {savedMessages.length} +
+ ) + : null, + }, ]} value={currTab} onChange={setCurrTab} @@ -340,12 +347,11 @@ const TextGeneration: FC = ({ )}
- {/* copyright */}
+ 'fixed bottom-4 flex space-x-2 text-gray-400 font-normal text-xs', + )}>
© {siteInfo.copyright || siteInfo.title} {(new Date()).getFullYear()}
{siteInfo.privacy_policy && ( <> @@ -373,7 +379,7 @@ const TextGeneration: FC = ({
{renderRes}