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
This commit is contained in:
João Nuno Mota
2023-04-15 17:54:33 +01:00
committed by GitHub
parent 648634d376
commit 2c328a4540
7 changed files with 57 additions and 30 deletions

View File

@ -16,6 +16,7 @@ interface State {
disablePublicMemos: boolean;
additionalStyle: string;
additionalScript: string;
openAIConfig: OpenAIConfig;
}
const SystemSection = () => {
@ -29,10 +30,7 @@ const SystemSection = () => {
additionalStyle: systemStatus.additionalStyle,
additionalScript: systemStatus.additionalScript,
disablePublicMemos: systemStatus.disablePublicMemos,
});
const [openAIConfig, setOpenAIConfig] = useState<OpenAIConfig>({
key: "",
host: "",
openAIConfig: systemStatus.openAIConfig,
});
useEffect(() => {
@ -47,18 +45,10 @@ const SystemSection = () => {
additionalStyle: systemStatus.additionalStyle,
additionalScript: systemStatus.additionalScript,
disablePublicMemos: systemStatus.disablePublicMemos,
openAIConfig: systemStatus.openAIConfig,
});
}, [systemStatus]);
useEffect(() => {
api.getSystemSetting().then(({ data: { data: systemSettings } }) => {
const openAIConfigSetting = systemSettings.find((setting) => setting.name === "openai-config");
if (openAIConfigSetting) {
setOpenAIConfig(JSON.parse(openAIConfigSetting.value));
}
});
}, []);
const handleAllowSignUpChanged = async (value: boolean) => {
setState({
...state,
@ -97,9 +87,12 @@ const SystemSection = () => {
};
const handleOpenAIConfigKeyChanged = (value: string) => {
setOpenAIConfig({
...openAIConfig,
key: value,
setState({
...state,
openAIConfig: {
...state.openAIConfig,
key: value,
},
});
};
@ -107,8 +100,9 @@ const SystemSection = () => {
try {
await api.upsertSystemSetting({
name: "openai-config",
value: JSON.stringify(openAIConfig),
value: JSON.stringify(state.openAIConfig),
});
globalStore.setSystemStatus({ openAIConfig: state.openAIConfig });
} catch (error) {
console.error(error);
return;
@ -117,9 +111,12 @@ const SystemSection = () => {
};
const handleOpenAIConfigHostChanged = (value: string) => {
setOpenAIConfig({
...openAIConfig,
host: value,
setState({
...state,
openAIConfig: {
...state.openAIConfig,
host: value,
},
});
};
@ -225,7 +222,7 @@ const SystemSection = () => {
fontSize: "14px",
}}
placeholder={t("setting.system-section.openai-api-key-placeholder")}
value={openAIConfig.key}
value={state.openAIConfig.key}
onChange={(event) => handleOpenAIConfigKeyChanged(event.target.value)}
/>
<div className="form-label mt-2">
@ -238,7 +235,7 @@ const SystemSection = () => {
fontSize: "14px",
}}
placeholder={t("setting.system-section.openai-api-host-placeholder")}
value={openAIConfig.host}
value={state.openAIConfig.host}
onChange={(event) => handleOpenAIConfigHostChanged(event.target.value)}
/>
<Divider className="!mt-3 !my-4" />