Skip to content

Commit 7b32460

Browse files
committedJul 14, 2023
feat: edit key name
1 parent 389ec31 commit 7b32460

File tree

4 files changed

+55
-14
lines changed

4 files changed

+55
-14
lines changed
 

‎pages/index.vue

+28-13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { useStorage } from '@vueuse/core'
55
const toast = useToast()
66
const key = ref('')
77
const columns = [
8+
{ key: 'name', label: 'Name' },
89
{ key: 'key', label: 'Key' },
910
{ key: 'totalLimit', label: 'Limit' },
1011
{ key: 'totalUsage', label: 'Usage' },
@@ -25,13 +26,12 @@ function showToast(msg: string, { color = 'primary' } = {}) {
2526
}
2627
2728
function checkKeyAdded (key: string) {
28-
const index = keyStore.value.findIndex(item => item.key === key.trim())
29-
return index > -1
29+
return checkRow(keyStore, key)
3030
}
3131
3232
async function getUsages (key: string) {
3333
loading.value = true
34-
const { data, pending } = await useFetch<KeyData>('/api/openai/usage', {
34+
const { data, pending } = await useFetch('/api/openai/usage', {
3535
query: {
3636
key
3737
},
@@ -54,24 +54,31 @@ async function doQuery () {
5454
}
5555
const res = await getUsages(validatedKey)
5656
res && keyStore.value.push({
57+
name: `Key${keyStore.value.length + 1}`,
5758
...res
5859
})
60+
// cleanup input
61+
key.value = ''
62+
}
63+
64+
function onEditName (row: KeyData) {
65+
const name = prompt('Enter your Name', row.name || 'Key')
66+
name && updateRow(keyStore, row.key, {
67+
name,
68+
})
5969
}
6070
6171
async function onRefetchKey(row: KeyData) {
6272
const res = await getUsages(row.key)
63-
const index = keyStore.value.findIndex(item => item.key === row.key.trim())
64-
if (index > -1) {
65-
res && keyStore.value.splice(index, 1, res)
66-
}
73+
updateRow(keyStore, row.key, {
74+
name: row.name,
75+
...res
76+
})
6777
}
6878
6979
function onDeleteKey(row: KeyData) {
70-
const index = keyStore.value.findIndex(item => item.key === row.key.trim())
71-
if (index > -1) {
72-
keyStore.value.splice(index, 1)
73-
showToast('Key has deleted')
74-
}
80+
const deleted = deleteRow(keyStore, row.key)
81+
deleted && showToast('Key has deleted')
7582
}
7683
</script>
7784

@@ -100,8 +107,16 @@ function onDeleteKey(row: KeyData) {
100107
:columns="columns"
101108
:rows="keyStore"
102109
>
110+
<template #name-data="{ row }">
111+
<div class="flex items-center justify-center">
112+
<span>{{ row.name }}</span>
113+
<UButton class="ml-1" size="2xs" color="blue" variant="ghost" icon="i-heroicons-pencil-square" @click="onEditName(row)" />
114+
</div>
115+
</template>
103116
<template #key-data="{ row }">
104-
<span>{{ hideApiKey(row.key) }}</span>
117+
<span>
118+
{{ hideApiKey(row.key) }}
119+
</span>
105120
</template>
106121
<template #totalLimit-data="{ row }">
107122
<span class="font-bold">${{ row.totalLimit }}</span>

‎server/api/openai/usage.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export default eventHandler(async (event) => {
3333
const totalUsage = usage.total_usage / 100
3434

3535
return {
36-
key,
36+
key: key as string,
3737
totalLimit,
3838
totalUsage,
3939
remaining: totalLimit - totalUsage,

‎types/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export type KeyData = {
2+
name?: string
23
key: string
34
totalLimit: number
45
totalUsage: number

‎utils/index.ts

+25
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { KeyData } from 'types'
2+
13
export function hideApiKey (key: string) {
24
return key.replace(/^(sk-[a-zA-Z0-9]{4})[a-zA-Z0-9]*(.{6})$/, '$1***$2')
35
}
@@ -10,3 +12,26 @@ export function validateKey (key: string) {
1012
return trimed
1113
}
1214

15+
export function checkRow (rows: Ref<KeyData[]>, key: string) {
16+
const index = rows.value.findIndex(item => item.key === key.trim())
17+
return index > -1
18+
}
19+
20+
export function updateRow (rows: Ref<KeyData[]>, key: string, partialRow: Partial<KeyData>) {
21+
const index = rows.value.findIndex(item => item.key === key.trim())
22+
if (index > -1) {
23+
rows.value.splice(index, 1, {
24+
...rows.value[index],
25+
...partialRow
26+
})
27+
}
28+
return index > -1
29+
}
30+
31+
export function deleteRow (rows: Ref<KeyData[]>,key: string) {
32+
const index = rows.value.findIndex(item => item.key === key.trim())
33+
if (index > -1) {
34+
rows.value.splice(index, 1)
35+
}
36+
return index > -1
37+
}

0 commit comments

Comments
 (0)