Skip to content

Commit 2503a07

Browse files
authored
fix(profile): add profile key length verification(#45)
2 parents 335020b + 67d70f7 commit 2503a07

File tree

2 files changed

+45
-13
lines changed

2 files changed

+45
-13
lines changed

app/src/store/profile.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import { PROFILE_API } from '@/apis'
33
import { useFetch } from '@/hooks'
44
import { useMessageStore } from './message'
55

6-
type State = {
7-
profile: Record<Key, ProfileItem>
6+
export type ProfileState = {
7+
profile: Record<ProfileKey, ProfileItem>
88
confirmLoading: boolean
99
}
1010

11-
type Key =
11+
export type ProfileKey =
1212
| 'questionAmount'
1313
| 'currentRole'
1414
| 'currentModel'
@@ -25,13 +25,13 @@ type Key =
2525

2626
type Model = 'OpenAI' | 'Azure'
2727

28-
type ProfileItem = {
28+
export type ProfileItem = {
2929
value: string | Model
3030
show?: boolean // show or hide key
3131
error?: boolean // error field
3232
}
3333

34-
const state: State = {
34+
const state: ProfileState = {
3535
profile: {
3636
questionAmount: {
3737
value: '',
@@ -104,7 +104,7 @@ export const useProfileStore = defineStore('profileStore', {
104104

105105
for (const key in data) {
106106
if (Object.prototype.hasOwnProperty.call(this.$state.profile, key)) {
107-
const profileKey = key as Key
107+
const profileKey = key as ProfileKey
108108
this.$state.profile[profileKey].value = data[key]
109109
}
110110
}
@@ -116,7 +116,7 @@ export const useProfileStore = defineStore('profileStore', {
116116

117117
const data: any = {}
118118
for (const key in this.$state.profile) {
119-
const profileKey = key as Key
119+
const profileKey = key as ProfileKey
120120
data[key] = this.$state.profile[profileKey].value || ''
121121
}
122122

@@ -183,7 +183,7 @@ export const useProfileStore = defineStore('profileStore', {
183183
const { profile } = this.$state
184184
for (const key in profile) {
185185
if (Object.prototype.hasOwnProperty.call(profile, key)) {
186-
const profileKey = key as Key
186+
const profileKey = key as ProfileKey
187187
if (profile[profileKey].error) profile[profileKey].error = false
188188
}
189189
}

app/src/views/Profile.vue

+37-5
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
label="KEY"
113113
variant="outlined"
114114
density="compact"
115+
:counter="51"
115116
:append-inner-icon="
116117
formData.openaiKey.show ? 'mdi-eye' : 'mdi-eye-off'
117118
"
@@ -132,6 +133,7 @@
132133
label="ORGANIZATION"
133134
variant="outlined"
134135
density="compact"
136+
:counter="28"
135137
:append-inner-icon="
136138
formData.openaiOrganization.show ? 'mdi-eye' : 'mdi-eye-off'
137139
"
@@ -196,6 +198,7 @@
196198
label="KEY"
197199
variant="outlined"
198200
density="compact"
201+
:counter="32"
199202
:append-inner-icon="
200203
formData.azureKey.show ? 'mdi-eye' : 'mdi-eye-off'
201204
"
@@ -280,7 +283,9 @@
280283
elevation="0"
281284
:block="true"
282285
:loading="PROFILE_STORE.confirmLoading"
283-
:disabled="PROFILE_STORE.confirmLoading || !isUpdateFormData"
286+
:disabled="
287+
PROFILE_STORE.confirmLoading || !isUpdateFormData || isFormError
288+
"
284289
@click="handleConfirm"
285290
>
286291
{{ $t('button.submit') }}
@@ -301,7 +306,8 @@ import { ref, onUnmounted } from 'vue'
301306
import { watchDeep } from '@vueuse/core'
302307
import { useI18n } from 'vue-i18n'
303308
import { orangeBgColor, greenBgColor } from '@/utils'
304-
import { useProfileStore } from '@/store'
309+
import { useProfileStore, ProfileItem, ProfileKey } from '@/store'
310+
import { computed } from 'vue'
305311
306312
const { locale } = useI18n()
307313
const PROFILE_STORE = useProfileStore()
@@ -316,12 +322,35 @@ const handleConfirm = async () => {
316322
await PROFILE_STORE.setProfile()
317323
isUpdateFormData.value = false
318324
}
325+
const isFormError = computed(() => {
326+
for (const key in formData) {
327+
if (Object.prototype.hasOwnProperty.call(formData, key)) {
328+
const profileKey = key as ProfileKey
329+
const element: ProfileItem = formData[profileKey]
330+
if (element.error !== undefined && element.error === true) return true
331+
}
332+
}
333+
return false
334+
})
319335
watchDeep(formData, () => {
320336
isUpdateFormData.value = true
337+
// Valid openai key length
338+
if (formData.openaiKey.value && formData.openaiKey.value.length !== 51)
339+
formData.openaiKey.error = true
340+
else formData.openaiKey.error = false
341+
// Valid openai organization length
342+
if (
343+
formData.openaiOrganization.value &&
344+
formData.openaiOrganization.value.length !== 28
345+
)
346+
formData.openaiOrganization.error = true
347+
else formData.openaiOrganization.error = false
348+
// Valid azure key length
349+
if (formData.azureKey.value && formData.azureKey.value.length !== 32)
350+
formData.azureKey.error = true
351+
else formData.azureKey.error = false
321352
322-
if (formData.notionKey) {
323-
PROFILE_STORE.profile.notionKey.error = false
324-
}
353+
if (formData.notionKey) PROFILE_STORE.profile.notionKey.error = false
325354
})
326355
327356
onUnmounted(() => {
@@ -344,4 +373,7 @@ onUnmounted(() => {
344373
margin: 0px 5px 0px -5px;
345374
padding-right: 13px !important;
346375
}
376+
:deep(.v-counter) {
377+
font-size: 10px !important;
378+
}
347379
</style>

0 commit comments

Comments
 (0)