From 2c328a45405c0fca28f15fb15285ed86a5b89e06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Nuno=20Mota?= Date: Sat, 15 Apr 2023 17:54:33 +0100 Subject: [PATCH] feat: hide ask ai button when key is empty (#1515) * Add option to hide Ask AI and update dev version * Fix formatting according to eslint * Replace option to hide Ask AI with auto hiding based on config * Fix golangci-lint errors * Remove showAskAI logic from OpenAPI --- api/system.go | 2 + server/system.go | 11 +++++ web/src/components/Header.tsx | 20 +++++---- web/src/components/Settings/SystemSection.tsx | 41 +++++++++---------- web/src/store/module/global.ts | 8 ++++ web/src/store/reducer/global.ts | 4 ++ web/src/types/modules/system.d.ts | 1 + 7 files changed, 57 insertions(+), 30 deletions(-) diff --git a/api/system.go b/api/system.go index 82e886586..da94161e7 100644 --- a/api/system.go +++ b/api/system.go @@ -24,4 +24,6 @@ type SystemStatus struct { StorageServiceID int `json:"storageServiceId"` // Local storage path LocalStoragePath string `json:"localStoragePath"` + // Local storage path + OpenAIConfig OpenAIConfig `json:"openAIConfig"` } diff --git a/server/system.go b/server/system.go index ac5214571..5581a17bb 100644 --- a/server/system.go +++ b/server/system.go @@ -56,6 +56,10 @@ func (s *Server) registerSystemRoutes(g *echo.Group) { }, StorageServiceID: api.DatabaseStorage, LocalStoragePath: "", + OpenAIConfig: api.OpenAIConfig{ + Key: "", + Host: "", + }, } systemSettingList, err := s.Store.FindSystemSettingList(ctx, &api.SystemSettingFind{}) @@ -95,6 +99,13 @@ func (s *Server) registerSystemRoutes(g *echo.Group) { systemStatus.StorageServiceID = int(baseValue.(float64)) } else if systemSetting.Name == api.SystemSettingLocalStoragePathName { systemStatus.LocalStoragePath = baseValue.(string) + } else if systemSetting.Name == api.SystemSettingOpenAIConfigName { + openAIConfig := api.OpenAIConfig{} + err := json.Unmarshal([]byte(systemSetting.Value), &openAIConfig) + if err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, "Failed to unmarshal system setting open ai config value").SetInternal(err) + } + systemStatus.OpenAIConfig = openAIConfig } } diff --git a/web/src/components/Header.tsx b/web/src/components/Header.tsx index f4ff0031b..6df7b50f2 100644 --- a/web/src/components/Header.tsx +++ b/web/src/components/Header.tsx @@ -1,7 +1,7 @@ import { useEffect } from "react"; import { NavLink, useLocation } from "react-router-dom"; import { useTranslation } from "react-i18next"; -import { useLayoutStore, useUserStore } from "@/store/module"; +import { useGlobalStore, useLayoutStore, useUserStore } from "@/store/module"; import { resolution } from "@/utils/layout"; import Icon from "./Icon"; import showSettingDialog from "./SettingDialog"; @@ -14,8 +14,10 @@ const Header = () => { const { t } = useTranslation(); const location = useLocation(); const userStore = useUserStore(); + const globalStore = useGlobalStore(); const layoutStore = useLayoutStore(); const showHeader = layoutStore.state.showHeader; + const showAskAI = globalStore.showAskAI(); const isVisitorMode = userStore.isVisitorMode() && !userStore.state.user; useEffect(() => { @@ -107,13 +109,15 @@ const Header = () => { {!isVisitorMode && ( <> - + {showAskAI && ( + + )}