Skip to content

Commit b3692ff

Browse files
committed
refactor(main): node config component
1 parent c412a44 commit b3692ff

File tree

3 files changed

+135
-21
lines changed

3 files changed

+135
-21
lines changed

app/components/common/client-modal.vue renamed to app/components/common/node-config.vue

+54-14
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
<script lang="ts" setup>
22
import { fetch } from '@tauri-apps/plugin-http'
3-
import { z } from 'zod'
43
import { useStorage } from '@vueuse/core'
5-
import type { FormSubmitEvent } from '#ui/types'
6-
7-
const runtimeConfig = useRuntimeConfig()
8-
9-
const form = ref()
10-
11-
const clientData = useStorage<{ [key: string]: Schema }>('client-data', {})
4+
import { z } from 'zod'
5+
import type { FormSubmitEvent, Form } from '#ui/types'
126
137
const schema = z.object({
148
name: z.string().min(1),
@@ -17,11 +11,13 @@ const schema = z.object({
1711
username: z.string().min(1),
1812
password: z.string().min(1),
1913
https: z.boolean(),
20-
entrance: z.string().min(1),
14+
entrance: z.string(),
2115
token: z.string().min(1),
2216
})
2317
24-
type Schema = z.output<typeof schema>
18+
export type Schema = z.output<typeof schema>
19+
20+
const runtimeConfig = useRuntimeConfig()
2521
2622
const state = reactive<Schema>({
2723
name: runtimeConfig.public.dev.name || '',
@@ -34,6 +30,8 @@ const state = reactive<Schema>({
3430
token: '',
3531
})
3632
33+
const form = ref<Form<Schema>>()
34+
3735
const loading = ref(false)
3836
3937
const getToken = async (loginInfo: Schema) => {
@@ -71,21 +69,63 @@ const getToken = async (loginInfo: Schema) => {
7169
}
7270
}
7371
72+
const visible = defineModel<boolean>()
73+
74+
const nodeConfig = useStorage<Schema[]>('node-config', [])
75+
7476
const onSubmit = async (event: FormSubmitEvent<Schema>) => {
7577
const { name } = event.data
76-
clientData.value[name] = event.data
78+
const index = nodeConfig.value.findIndex(item => item.name === name)
79+
80+
if (index === -1) {
81+
nodeConfig.value.push(event.data)
82+
} else {
83+
nodeConfig.value[index] = event.data
84+
}
85+
86+
visible.value = false
7787
}
88+
89+
watch(visible, (value) => {
90+
if (!value) {
91+
state.token = ''
92+
state.name = ''
93+
state.host = ''
94+
state.port = 10000
95+
state.username = ''
96+
state.password = ''
97+
state.https = false
98+
state.entrance = ''
99+
state.token = ''
100+
}
101+
})
78102
</script>
79103

80104
<template>
81-
<UModal>
105+
<UModal
106+
v-model="visible"
107+
prevent-close
108+
>
82109
<UCard
83110
:ui="{
84111
ring: '',
85112
divide: 'divide-y divide-gray-100 dark:divide-gray-800',
86113
}"
87114
>
88-
<template #header />
115+
<template #header>
116+
<div class="flex items-center justify-between">
117+
<h2 class="font-bold">
118+
{{ $t('label.create-client') }}
119+
</h2>
120+
<UButton
121+
color="gray"
122+
variant="ghost"
123+
size="xs"
124+
icon="i-heroicons-x-mark-20-solid"
125+
@click="visible = false"
126+
/>
127+
</div>
128+
</template>
89129

90130
<UForm
91131
ref="form"
@@ -160,7 +200,7 @@ const onSubmit = async (event: FormSubmitEvent<Schema>) => {
160200
</UFormGroup>
161201
</UForm>
162202
<template #footer>
163-
<UButton @click="form.submit()">
203+
<UButton @click="form?.submit()">
164204
{{ $t('button.submit') }}
165205
</UButton>
166206
</template>

app/pages/index.vue

+70-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,70 @@
11
<script lang="ts" setup>
2+
import { useStorage } from '@vueuse/core'
3+
import { fetch } from '@tauri-apps/plugin-http'
4+
import type { Schema as NodeConfigSchema } from '~/components/common/node-config.vue'
5+
26
const visible = ref(false)
7+
8+
const nodeConfig = useStorage<NodeConfigSchema[]>('node-config', [])
9+
10+
const nodeStatus = useStorage<any>('node-status', [])
11+
12+
useIntervalFn(async () => {
13+
for (const item in nodeConfig.value) {
14+
if (!nodeConfig.value[item]) {
15+
return
16+
}
17+
const { name, host, port, https, token } = nodeConfig.value[item]
18+
const response = await fetch(
19+
`${
20+
https ? 'https' : 'http'
21+
}://${host}:${port}/api/v1/dashboard/current/all/all`,
22+
{
23+
headers: {
24+
PanelAuthorization: token,
25+
},
26+
},
27+
)
28+
const result = await response.json()
29+
if (result.code !== 200) {
30+
console.log(result.message)
31+
} else {
32+
const index = nodeStatus.value.findIndex(
33+
(item: any) => item.name === name,
34+
)
35+
36+
const assignData = Object.assign(result.data, { name })
37+
38+
if (index === -1) {
39+
nodeStatus.value.push(assignData)
40+
} else {
41+
nodeStatus.value[index] = assignData
42+
}
43+
}
44+
}
45+
}, 1000)
46+
47+
const columns = [
48+
{
49+
key: 'id',
50+
label: '运行状态',
51+
},
52+
{
53+
key: 'name',
54+
label: '节点名',
55+
},
56+
{
57+
key: 'title',
58+
label: 'IP',
59+
},
60+
{
61+
key: 'email',
62+
label: '在线时间',
63+
},
64+
{
65+
key: 'role',
66+
},
67+
]
368
</script>
469

570
<template>
@@ -15,13 +80,16 @@ const visible = ref(false)
1580
:label="$t('label.create-client')"
1681
@click="visible = true"
1782
/>
18-
<CommonClientModal v-model="visible" />
83+
<CommonNodeConfig v-model="visible" />
1984

2085
<CommonSetLocale />
2186
</header>
2287
<main
2388
class="mx-auto px-4 sm:px-6 lg:px-8 max-w-7xl gap-16 sm:gap-y-24 flex flex-col"
2489
>
25-
<UTable />
90+
<UTable
91+
:columns
92+
:rows="nodeStatus"
93+
/>
2694
</main>
2795
</template>

eslint.config.mjs

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
// @ts-check
22
import withNuxt from './.nuxt/eslint.config.mjs'
33

4-
export default withNuxt().override('nuxt/rules', {
5-
rules: {
6-
'vue/no-multiple-template-root': 'off',
7-
},
8-
})
4+
export default withNuxt()
5+
.override('nuxt/rules', {
6+
rules: {
7+
'vue/no-multiple-template-root': 'off',
8+
},
9+
})
10+
.override('nuxt/typescript/rules', {
11+
rules: {
12+
'@typescript-eslint/no-explicit-any': 'off',
13+
},
14+
})

0 commit comments

Comments
 (0)