Skip to content

Commit

Permalink
Minor fix (#43)
Browse files Browse the repository at this point in the history
* update
  • Loading branch information
swuecho authored Mar 27, 2023
1 parent 8d99464 commit 6b3d798
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 82 deletions.
8 changes: 2 additions & 6 deletions web/src/components/common/Setting/Admin.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@
<div class="p-4 space-y-4">
<div class="p-2 space-y-2 rounded-md bg-neutral-100 dark:bg-neutral-700">
<p>
<a
class="text-blue-600 dark:text-blue-500"
href="/#/admin"
target="_blank"
>
在新标签页中打开(仅限管理员)
<a class="text-blue-600 dark:text-blue-500" href="/#/admin/user" target="_blank">
{{ $t('admin.openPanel') }}
</a>
</p>
</div>
Expand Down
15 changes: 13 additions & 2 deletions web/src/locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@
"signup": "Sign up",
"email_placeholder": "Please enter your email",
"password_placeholder": "Please enter your password",
"unauthorizedTips": "Please sign up or login"
"unauthorizedTips": "Please sign up or login",
"login_success": "Login Successful",
"logout_success": "Logout Successful",
"signup_success": "Registration Successful",
"signup_failed": "Registration Failed",
"login_failed": "Login Failed",
"logout_failed": "Logout Failed",
"please_register": "Please Register First"
},
"chat": {
"new": "New Chat",
Expand Down Expand Up @@ -70,6 +77,7 @@
"admin": "Admin"
},
"admin": {
"openPanel": "Open Admin Panel",
"title": "Admin",
"permission": "Permission",
"rateLimit": "Rate Limit",
Expand All @@ -82,6 +90,9 @@
"rateLimit10Min": "Message Limit (10 minutes)"
},
"error": {
"rateLimit": "The message sending limit has been reached, please contact the administrator."
"rateLimit": "The message sending limit has been reached, please contact the administrator.",
"invalidEmail": "Invalid email address",
"sync_chat_session": "Sync Failed, Please Try Again Later",
"invalidPassword": "password is not valid, it should be length >=6 and include a number, a lowercase letter, an uppercase letter, and a special character"
}
}
5 changes: 1 addition & 4 deletions web/src/locales/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ import { createI18n } from 'vue-i18n'
import enUS from './en-US.json'
import zhCN from './zh-CN.json'
import zhTW from './zh-TW.json'
import { useAppStoreWithOut } from '@/store/modules/app'
import type { Language } from '@/store/modules/app/helper'

const appStore = useAppStoreWithOut()

const defaultLocale = appStore.language || 'zh-CN'
const defaultLocale = 'zh-CN'

const i18n = createI18n({
locale: defaultLocale,
Expand Down
15 changes: 13 additions & 2 deletions web/src/locales/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@
"signup": "注册",
"email_placeholder": "请输入邮箱",
"password_placeholder": "请输入密码",
"unauthorizedTips": "请注册或者登录"
"unauthorizedTips": "请注册或者登录",
"login_success": "登录成功",
"logout_success": "登出成功",
"signup_success": "注册成功",
"signup_failed": "注册失败",
"login_failed": "登录失败",
"logout_failed": "登出失败",
"please_register": "请先注册"
},
"chat": {
"new": "新聊天",
Expand Down Expand Up @@ -53,6 +60,7 @@
"debug": "调试模式"
},
"admin": {
"openPanel": "在新标签页中打开(仅限管理员)",
"title": "管理",
"userEmail": "用户邮箱",
"totalChatMessages": "消息总数",
Expand Down Expand Up @@ -82,6 +90,9 @@
"socks": "Socks"
},
"error": {
"rateLimit": "发送消息数量已经达到上限, 请联系管理员"
"rateLimit": "发送消息数量已经达到上限, 请联系管理员",
"invalidEmail": "邮箱格式不正确",
"invalidPassword": "密码格式不正确, 长度至少为6位, 一个大写字母, 一个小写字母, 一个数字, 一个特殊字符",
"sync_chat_session": "同步失败, 请稍后再试"
}
}
16 changes: 14 additions & 2 deletions web/src/locales/zh-TW.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,20 @@
"signup": "註冊",
"email_placeholder": "請輸入電子郵件",
"password_placeholder": "請輸入密碼",
"unauthorizedTips": "請註冊或登錄"
"unauthorizedTips": "請註冊或登錄",
"login_success": "登入成功",
"logout_success": "登出成功",
"signup_success": "註冊成功",
"signup_failed": "註冊失敗",
"login_failed": "登入失敗",
"logout_failed": "登出失敗",
"please_register": "請先註冊"
},
"error": {"rateLimit": "發送消息數量已經達到上限,請聯繫管理員"
"error": {
"rateLimit": "發送消息數量已經達到上限,請聯繫管理員",
"invalidEmail": "電子郵件格式不正確",
"invalidPassword": "密碼格式不正確,長度至少需為6位,應包含1個大寫字母、1個小寫字母、1個數字、1個特殊字符",
"sync_chat_session": "同步失敗,請稍後再試"
},
"chat": {
"new": "新建聊天",
Expand Down Expand Up @@ -72,6 +83,7 @@
"socks": "Socks"
},
"admin": {
"openPanel": "打開管理面板",
"title": "管理",
"permission": "權限",
"rateLimit": "限流",
Expand Down
3 changes: 2 additions & 1 deletion web/src/store/modules/chat/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { v4 as uuidv4 } from 'uuid'
import { ss } from '@/utils/storage'

import { createChatSession } from '@/api'
import { t } from '@/locales'

const LOCAL_NAME = 'chatStorage'

export function defaultState(): Chat.ChatState {
const uuid = uuidv4()
const new_chat_text = 'New Chat'
const new_chat_text = t('chat.new')
createChatSession(uuid, new_chat_text)
return { active: uuid, history: [{ uuid, title: new_chat_text, isEdit: false }], chat: [{ uuid, data: [] }] }
}
Expand Down
10 changes: 7 additions & 3 deletions web/src/store/modules/chat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {
renameChatSession,
} from '@/api'

import { t } from '@/locales'

export const useChatStore = defineStore('chat-store', {
state: (): Chat.ChatState => getLocalState(),

Expand Down Expand Up @@ -61,6 +63,7 @@ export const useChatStore = defineStore('chat-store', {
},

async syncChatSessions() {
const new_chat_text = t('chat.new')
const sessions = await getChatSessionsByUser()
if (sessions.length <= 0)
return
Expand All @@ -75,7 +78,7 @@ export const useChatStore = defineStore('chat-store', {
})
if (this.history.length === 0) {
const uuid = uuidv4()
this.addChatSession({ title: 'New Chat', isEdit: false, uuid })
this.addChatSession({ title: new_chat_text, isEdit: false, uuid })
}

let active_session_uuid = this.history[0].uuid
Expand Down Expand Up @@ -158,6 +161,7 @@ export const useChatStore = defineStore('chat-store', {
},

addChatByUuid(uuid: string, chat: Chat.Chat) {
const new_chat_text = t('chat.new')
if (!uuid) {
if (this.history.length === 0) {
const uuid = uuidv4()
Expand All @@ -169,7 +173,7 @@ export const useChatStore = defineStore('chat-store', {
}
else {
this.chat[0].data.push(chat)
if (this.history[0].title === 'New Chat') {
if (this.history[0].title === new_chat_text) {
this.history[0].title = chat.text
renameChatSession(this.history[0].uuid, chat.text.substring(0, 20))
}
Expand All @@ -180,7 +184,7 @@ export const useChatStore = defineStore('chat-store', {
const index = this.chat.findIndex(item => item.uuid === uuid)
if (index !== -1) {
this.chat[index].data.push(chat)
if (this.history[0].title === 'New Chat') {
if (this.history[0].title === new_chat_text) {
this.history[0].title = chat.text
renameChatSession(this.history[0].uuid, chat.text.substring(0, 20))
}
Expand Down
29 changes: 12 additions & 17 deletions web/src/views/admin/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import { NIcon, NLayout, NLayoutSider, NMenu } from 'naive-ui'
import type { MenuOption } from 'naive-ui'
import { PulseOutline, ShieldCheckmarkOutline } from '@vicons/ionicons5'
import { RouterLink } from 'vue-router'
import i18n from '@/locales'
import { t } from '@/locales'
import { useBasicLayout } from '@/hooks/useBasicLayout'
import { HoverButton, SvgIcon } from '@/components/common'
const { isMobile } = useBasicLayout()
const collapsed: Ref<boolean> = ref(false)
const activeKey: Ref<string | null> = ref(null)
const activeKey: Ref<string> = ref('rateLimit')
const getMobileClass = computed<CSSProperties>(() => {
if (isMobile.value) {
Expand All @@ -39,9 +39,9 @@ const menuOptions: MenuOption[] = reactive([
name: 'AdminUser',
},
},
{ default: () => i18n.global.t('admin.rateLimit') },
{ default: () => t('admin.rateLimit') },
),
key: 'hear-the-wind-sing',
key: 'rateLimit',
icon: renderIcon(PulseOutline),
},
{
Expand All @@ -52,9 +52,9 @@ const menuOptions: MenuOption[] = reactive([
name: 'AdminSystem',
},
},
{ default: () => i18n.global.t('admin.permission') },
{ default: () => t('admin.permission') },
),
key: 'a-wild-sheep-chase',
key: 'permission',
icon: renderIcon(ShieldCheckmarkOutline),
},
])
Expand All @@ -69,8 +69,7 @@ function handleUpdateCollapsed() {
<div class="h-full dark:bg-[#24272e] transition-all" :class="[isMobile ? 'p-0' : 'p-4']">
<div class="h-full overflow-hidden" :class="getMobileClass">
<header
class="sticky top-0 left-0 right-0 z-30 border-b dark:border-neutral-800 bg-white/80 dark:bg-black/20 backdrop-blur"
>
class="sticky top-0 left-0 right-0 z-30 border-b dark:border-neutral-800 bg-white/80 dark:bg-black/20 backdrop-blur">
<div class="relative flex items-center justify-between min-w-0 overflow-hidden h-14">
<div class="flex items-center">
<button class="flex items-center justify-center w-11 h-11" @click="handleUpdateCollapsed">
Expand All @@ -96,15 +95,11 @@ function handleUpdateCollapsed() {
</div>
</header>
<NLayout has-sider>
<NLayoutSider
bordered :width="240" :collapsed-width="0" :collapsed="collapsed" :show-trigger="isMobile ? false : 'arrow-circle'"
collapse-mode="transform" position="absolute" :style="getMobileClass"
@collapse="collapsed = true" @expand="collapsed = false"
>
<NMenu
v-model:value="activeKey" :collapsed="collapsed" :collapsed-width="64" :collapsed-icon-size="22"
:options="menuOptions"
/>
<NLayoutSider bordered :width="240" :collapsed-width="10" :collapsed="collapsed"
:show-trigger="isMobile ? false : 'arrow-circle'" collapse-mode="transform" position="absolute"
:style="getMobileClass" @collapse="collapsed = true" @expand="collapsed = false">
<NMenu v-model:value="activeKey" :collapsed="collapsed" :collapsed-width="64" :collapsed-icon-size="22"
:options="menuOptions" />
</NLayoutSider>
<NLayout>
<router-view />
Expand Down
10 changes: 5 additions & 5 deletions web/src/views/admin/user/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { h, onMounted, reactive, ref } from 'vue'
import { NDataTable, NInput } from 'naive-ui'
import { GetUserData, UpdateRateLimit } from '@/api'
import i18n from '@/locales'
import { t } from '@/locales'
interface UserData {
email: string
Expand All @@ -19,23 +19,23 @@ const tableData = ref<UserData[]>([])
const columns = [
{
title: i18n.global.t('admin.userEmail'),
title: t('admin.userEmail'),
key: 'email',
width: 400,
},
{
title: i18n.global.t('admin.totalChatMessages'),
title: t('admin.totalChatMessages'),
key: 'totalChatMessages',
width: 100,
},
{
title: i18n.global.t('admin.totalChatMessages3Days'),
title: t('admin.totalChatMessages3Days'),
key: 'totalChatMessages3Days',
width: 100,
},
{
title: i18n.global.t('admin.rateLimit10Min'),
title: t('admin.rateLimit10Min'),
key: 'rateLimit',
width: 100,
render: (row: any, index: number) => {
Expand Down
17 changes: 13 additions & 4 deletions web/src/views/chat/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,7 @@ async function onConversationStream() {
)
}
else {
const lastIndex = responseText.lastIndexOf('\n\ndata:')
// Extract the JSON data chunk from the responseText
const chunk = responseText.slice(lastIndex + 8)
const chunk = getDataFromResponseText(responseText)
// Check if the chunk is not empty
if (chunk) {
// Parse the JSON data chunk
Expand Down Expand Up @@ -169,12 +167,12 @@ async function onConversationStream() {
return response
}
catch (error: any) {
console.log(error)
const response = error.response
if (response.status === 500)
nui_msg.error(response.data.message)
else if (response.status === 429)
nui_msg.error(response.data.message)
throw error
}
finally {
loading.value = false
Expand Down Expand Up @@ -441,6 +439,17 @@ onUnmounted(() => {
if (loading.value)
controller.abort()
})
function getDataFromResponseText(responseText: string): string {
// first data segment
if (responseText.lastIndexOf('data:') === 0)
return responseText.slice(5)
// Find the last occurrence of the data segment
const lastIndex = responseText.lastIndexOf('\n\ndata:')
// Extract the JSON data chunk from the responseText
const chunk = responseText.slice(lastIndex + 8)
return chunk
}
</script>

<template>
Expand Down
Loading

0 comments on commit 6b3d798

Please sign in to comment.