211 lines
7.5 KiB
Vue
211 lines
7.5 KiB
Vue
<template>
|
|
<aside class="list">
|
|
<div class="list-head">
|
|
<span class="list-title">设备</span>
|
|
<span class="spacer"></span>
|
|
<n-button size="tiny" @click="openAdd">添加设备</n-button>
|
|
<n-button size="tiny" @click="openGroup">新建分组</n-button>
|
|
</div>
|
|
<div class="list-search">
|
|
<n-input v-model:value="query" size="small" clearable placeholder="搜索名称或端编号" />
|
|
</div>
|
|
<div class="list-scroll">
|
|
<div v-if="!store.running" class="empty">被控没有在运行。启动后可以看见设备。</div>
|
|
<div v-else-if="!store.accountUser" class="empty">
|
|
登录账号后可以看见设备。
|
|
<div style="margin-top: 8px"><n-button size="small" type="primary" @click="emitLogin">登录</n-button></div>
|
|
</div>
|
|
<div v-else-if="store.deviceError" class="empty">{{ store.deviceError }}</div>
|
|
<div v-else-if="store.devices.length === 0" class="empty">
|
|
还没有设备。
|
|
<div style="margin-top: 8px"><n-button size="small" type="primary" @click="openAdd">添加设备</n-button></div>
|
|
</div>
|
|
<template v-else>
|
|
<div v-if="ungrouped.length || !query.trim()" class="group-head"><span class="group-name">未分组</span></div>
|
|
<button
|
|
v-for="item in ungrouped"
|
|
:key="item.id"
|
|
type="button"
|
|
class="device-row"
|
|
:class="{ active: store.selectedId === item.id }"
|
|
@click="choose(item.id)"
|
|
>
|
|
<span class="device-main">
|
|
<span class="device-name">{{ item.name }}</span>
|
|
<span class="device-id">{{ item.endpoint_id }}</span>
|
|
</span>
|
|
<span class="pill" :class="item.presence">{{ presenceText(item.presence) }}</span>
|
|
</button>
|
|
<section v-for="group in visibleGroups" :key="group.id">
|
|
<div class="group-head">
|
|
<span class="group-name">{{ group.name }}</span>
|
|
<span class="spacer"></span>
|
|
<n-button text size="tiny" @click="rename(group)">重命名</n-button>
|
|
<n-button text size="tiny" @click="remove(group)">删除</n-button>
|
|
</div>
|
|
<button
|
|
v-for="item in group.devices"
|
|
:key="item.id"
|
|
type="button"
|
|
class="device-row"
|
|
:class="{ active: store.selectedId === item.id }"
|
|
@click="choose(item.id)"
|
|
>
|
|
<span class="device-main">
|
|
<span class="device-name">{{ item.name }}</span>
|
|
<span class="device-id">{{ item.endpoint_id }}</span>
|
|
</span>
|
|
<span class="pill" :class="item.presence">{{ presenceText(item.presence) }}</span>
|
|
</button>
|
|
</section>
|
|
<div v-if="query.trim() && ungrouped.length === 0 && visibleGroups.length === 0" class="empty">没有匹配的设备</div>
|
|
</template>
|
|
</div>
|
|
|
|
<n-modal v-model:show="addShow" preset="card" title="添加设备" style="width: 440px" :segmented="{ content: true, footer: 'soft' }">
|
|
<div class="modal-body">
|
|
<n-form label-placement="left" label-width="72">
|
|
<n-form-item label="名称"><n-input v-model:value="addName" maxlength="40" /></n-form-item>
|
|
<n-form-item label="端编号">
|
|
<n-input v-model:value="addEndpoint" placeholder="对方的端编号,留空则登记这台电脑" />
|
|
</n-form-item>
|
|
</n-form>
|
|
</div>
|
|
<template #footer>
|
|
<div class="modal-actions">
|
|
<n-button @click="addShow = false">取消</n-button>
|
|
<n-button type="primary" :disabled="!addName.trim()" :loading="saving" @click="add">添加</n-button>
|
|
</div>
|
|
</template>
|
|
</n-modal>
|
|
|
|
<n-modal v-model:show="groupShow" preset="card" :title="groupId ? '重命名分组' : '新建分组'" style="width: 400px" :segmented="{ content: true, footer: 'soft' }">
|
|
<div class="modal-body">
|
|
<n-form label-placement="left" label-width="72">
|
|
<n-form-item label="名称"><n-input v-model:value="groupName" maxlength="40" /></n-form-item>
|
|
</n-form>
|
|
</div>
|
|
<template #footer>
|
|
<div class="modal-actions">
|
|
<n-button @click="groupShow = false">取消</n-button>
|
|
<n-button type="primary" :disabled="!groupName.trim()" :loading="saving" @click="saveGroup">保存</n-button>
|
|
</div>
|
|
</template>
|
|
</n-modal>
|
|
</aside>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import { computed, ref } from 'vue'
|
|
import { useRouter } from 'vue-router'
|
|
import { NButton, NForm, NFormItem, NInput, NModal, useDialog, useMessage } from 'naive-ui'
|
|
import { api } from '../api'
|
|
import { useAppStore } from '../stores/app'
|
|
import { presenceText, type Group } from '../types'
|
|
|
|
const store = useAppStore()
|
|
const router = useRouter()
|
|
const message = useMessage()
|
|
const dialog = useDialog()
|
|
const query = ref('')
|
|
const addShow = ref(false)
|
|
const addName = ref('')
|
|
const addEndpoint = ref('')
|
|
const groupShow = ref(false)
|
|
const groupName = ref('')
|
|
const groupId = ref('')
|
|
const saving = ref(false)
|
|
|
|
const knownGroups = computed(() => new Set(store.groups.map((item) => item.id)))
|
|
const filtered = computed(() => {
|
|
const text = query.value.trim().toLowerCase()
|
|
return store.devices.filter((item) => !text || item.name.toLowerCase().includes(text) || item.endpoint_id.toLowerCase().includes(text))
|
|
})
|
|
const ungrouped = computed(() => filtered.value.filter((item) => !item.group_id || !knownGroups.value.has(item.group_id)))
|
|
const visibleGroups = computed(() =>
|
|
store.groups
|
|
.map((group) => ({ ...group, devices: filtered.value.filter((item) => item.group_id === group.id) }))
|
|
.filter((group) => group.devices.length > 0 || !query.value.trim()),
|
|
)
|
|
|
|
function choose(id: string) {
|
|
store.selectedId = id
|
|
router.push('/')
|
|
}
|
|
|
|
function emitLogin() {
|
|
window.dispatchEvent(new CustomEvent('tapremly-open-host'))
|
|
}
|
|
|
|
function openAdd() {
|
|
addName.value = ''
|
|
addEndpoint.value = ''
|
|
addShow.value = true
|
|
}
|
|
|
|
function openGroup() {
|
|
groupId.value = ''
|
|
groupName.value = ''
|
|
groupShow.value = true
|
|
}
|
|
|
|
function rename(group: Group) {
|
|
groupId.value = group.id
|
|
groupName.value = group.name
|
|
groupShow.value = true
|
|
}
|
|
|
|
function remove(group: Group) {
|
|
dialog.warning({
|
|
title: '删除分组',
|
|
content: '删除后,这个分组里的设备会回到未分组。',
|
|
positiveText: '删除',
|
|
negativeText: '取消',
|
|
onPositiveClick: async () => {
|
|
try {
|
|
await api(`/v1/groups/${encodeURIComponent(group.id)}`, { method: 'DELETE' })
|
|
await store.refreshDevices()
|
|
} catch (err) {
|
|
message.error(err instanceof Error ? err.message : '没有删除')
|
|
}
|
|
},
|
|
})
|
|
}
|
|
|
|
async function add() {
|
|
saving.value = true
|
|
try {
|
|
await api('/v1/devices', {
|
|
method: 'POST',
|
|
body: JSON.stringify({ name: addName.value.trim(), endpoint_id: addEndpoint.value.trim() }),
|
|
})
|
|
addShow.value = false
|
|
await store.tick()
|
|
} catch (err) {
|
|
message.error(err instanceof Error ? err.message : '没有添加')
|
|
} finally {
|
|
saving.value = false
|
|
}
|
|
}
|
|
|
|
async function saveGroup() {
|
|
saving.value = true
|
|
try {
|
|
if (groupId.value) {
|
|
await api(`/v1/groups/${encodeURIComponent(groupId.value)}`, {
|
|
method: 'PATCH',
|
|
body: JSON.stringify({ name: groupName.value.trim() }),
|
|
})
|
|
} else {
|
|
await api('/v1/groups', { method: 'POST', body: JSON.stringify({ name: groupName.value.trim() }) })
|
|
}
|
|
groupShow.value = false
|
|
await store.refreshDevices()
|
|
} catch (err) {
|
|
message.error(err instanceof Error ? err.message : '没有保存')
|
|
} finally {
|
|
saving.value = false
|
|
}
|
|
}
|
|
</script>
|