fix: #589 improve unauthorized tips

This commit is contained in:
Yidadaa 2023-04-09 23:51:12 +08:00
parent 4a492264a1
commit 0e05733bbb
6 changed files with 66 additions and 48 deletions

View File

@ -19,6 +19,7 @@ import {
BOT_HELLO, BOT_HELLO,
ROLES, ROLES,
createMessage, createMessage,
useAccessStore,
} from "../store"; } from "../store";
import { import {
@ -485,11 +486,17 @@ export function Chat(props: {
const context: RenderMessage[] = session.context.slice(); const context: RenderMessage[] = session.context.slice();
const accessStore = useAccessStore();
if ( if (
context.length === 0 && context.length === 0 &&
session.messages.at(0)?.content !== BOT_HELLO.content session.messages.at(0)?.content !== BOT_HELLO.content
) { ) {
context.push(BOT_HELLO); const copiedHello = Object.assign({}, BOT_HELLO);
if (!accessStore.isAuthorized()) {
copiedHello.content = Locale.Error.Unauthorized;
}
context.push(copiedHello);
} }
// preview messages // preview messages

View File

@ -124,8 +124,7 @@ export function Settings(props: { closeSettings: () => void }) {
const builtinCount = SearchService.count.builtin; const builtinCount = SearchService.count.builtin;
const customCount = promptStore.prompts.size ?? 0; const customCount = promptStore.prompts.size ?? 0;
const showUsage = !!accessStore.token || !!accessStore.accessCode; const showUsage = accessStore.isAuthorized();
useEffect(() => { useEffect(() => {
checkUpdate(); checkUpdate();
showUsage && checkUsage(); showUsage && checkUsage();
@ -346,37 +345,7 @@ export function Settings(props: { closeSettings: () => void }) {
></input> ></input>
</SettingItem> </SettingItem>
</List> </List>
<List>
<SettingItem
title={Locale.Settings.Prompt.Disable.Title}
subTitle={Locale.Settings.Prompt.Disable.SubTitle}
>
<input
type="checkbox"
checked={config.disablePromptHint}
onChange={(e) =>
updateConfig(
(config) =>
(config.disablePromptHint = e.currentTarget.checked),
)
}
></input>
</SettingItem>
<SettingItem
title={Locale.Settings.Prompt.List}
subTitle={Locale.Settings.Prompt.ListCount(
builtinCount,
customCount,
)}
>
<IconButton
icon={<EditIcon />}
text={Locale.Settings.Prompt.Edit}
onClick={() => showToast(Locale.WIP)}
/>
</SettingItem>
</List>
<List> <List>
{enabledAccessControl ? ( {enabledAccessControl ? (
<SettingItem <SettingItem
@ -473,6 +442,38 @@ export function Settings(props: { closeSettings: () => void }) {
</SettingItem> </SettingItem>
</List> </List>
<List>
<SettingItem
title={Locale.Settings.Prompt.Disable.Title}
subTitle={Locale.Settings.Prompt.Disable.SubTitle}
>
<input
type="checkbox"
checked={config.disablePromptHint}
onChange={(e) =>
updateConfig(
(config) =>
(config.disablePromptHint = e.currentTarget.checked),
)
}
></input>
</SettingItem>
<SettingItem
title={Locale.Settings.Prompt.List}
subTitle={Locale.Settings.Prompt.ListCount(
builtinCount,
customCount,
)}
>
<IconButton
icon={<EditIcon />}
text={Locale.Settings.Prompt.Edit}
onClick={() => showToast(Locale.WIP)}
/>
</SettingItem>
</List>
<List> <List>
<SettingItem title={Locale.Settings.Model}> <SettingItem title={Locale.Settings.Model}>
<select <select

View File

@ -156,7 +156,7 @@ export function showToast(
}; };
setTimeout(() => { setTimeout(() => {
// close(); close();
}, delay); }, delay);
root.render(<Toast content={content} action={action} />); root.render(<Toast content={content} action={action} />);

View File

@ -3,7 +3,7 @@ import { SubmitKey } from "../store/app";
const cn = { const cn = {
WIP: "该功能仍在开发中……", WIP: "该功能仍在开发中……",
Error: { Error: {
Unauthorized: "现在是未授权状态,请在设置页输入访问密码。", Unauthorized: "现在是未授权状态,请点击左下角设置按钮输入访问密码。",
}, },
ChatItem: { ChatItem: {
ChatItemCount: (count: number) => `${count} 条对话`, ChatItemCount: (count: number) => `${count} 条对话`,
@ -90,7 +90,7 @@ const cn = {
}, },
SendKey: "发送键", SendKey: "发送键",
Theme: "主题", Theme: "主题",
TightBorder: "紧凑边框", TightBorder: "无边框模式",
SendPreviewBubble: "发送预览气泡", SendPreviewBubble: "发送预览气泡",
Prompt: { Prompt: {
Disable: { Disable: {

View File

@ -9,6 +9,7 @@ export interface AccessControlStore {
updateToken: (_: string) => void; updateToken: (_: string) => void;
updateCode: (_: string) => void; updateCode: (_: string) => void;
enabledAccessControl: () => boolean; enabledAccessControl: () => boolean;
isAuthorized: () => boolean;
} }
export const ACCESS_KEY = "access-control"; export const ACCESS_KEY = "access-control";
@ -27,10 +28,13 @@ export const useAccessStore = create<AccessControlStore>()(
updateToken(token: string) { updateToken(token: string) {
set((state) => ({ token })); set((state) => ({ token }));
}, },
isAuthorized() {
return !!get().token || !!get().accessCode;
},
}), }),
{ {
name: ACCESS_KEY, name: ACCESS_KEY,
version: 1, version: 1,
} },
) ),
); );

View File

@ -333,17 +333,23 @@ export const useChatStore = create<ChatStore>()(
if (!isMobileScreen() || confirm(Locale.Home.DeleteChat)) { if (!isMobileScreen() || confirm(Locale.Home.DeleteChat)) {
get().removeSession(index); get().removeSession(index);
showToast(Locale.Home.DeleteToast, { showToast(
Locale.Home.DeleteToast,
{
text: Locale.Home.Revert, text: Locale.Home.Revert,
onClick() { onClick() {
set((state) => ({ set((state) => ({
sessions: state.sessions sessions: state.sessions
.slice(0, index) .slice(0, index)
.concat([deletedSession]) .concat([deletedSession])
.concat(state.sessions.slice(index + Number(isLastSession))), .concat(
state.sessions.slice(index + Number(isLastSession)),
),
})); }));
}, },
}); },
5000,
);
} }
}, },