ServerIdentityPanel.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <script setup lang="ts">
  2. import { computed, ref, watch } from 'vue'
  3. import { useAuthStore } from '@/stores/auth'
  4. import { saveServerIdentity } from '@/api/serverIdentity'
  5. const auth = useAuthStore()
  6. const serverId = ref('')
  7. const name = ref(''), revision = ref(0), baseline = ref(''), busy = ref(false), error = ref(''), notice = ref('')
  8. const editable = computed(() => auth.user?.role === 'admin')
  9. const dirty = computed(() => name.value !== baseline.value)
  10. const emit = defineEmits<{ 'dirty-change': [dirty: boolean] }>()
  11. watch(dirty, value => emit('dirty-change', value))
  12. function hydrate() {
  13. if (!auth.serverIdentity) return
  14. name.value = baseline.value = auth.serverIdentity.name
  15. revision.value = auth.serverIdentity.revision
  16. serverId.value = auth.serverIdentity.serverId
  17. }
  18. watch(() => auth.serverIdentity, () => { if (!dirty.value) hydrate() }, { immediate: true })
  19. async function reload() { await auth.refreshServerIdentity(); hydrate(); error.value = '' }
  20. async function save() {
  21. busy.value = true; error.value = ''; notice.value = ''
  22. try {
  23. const value = await saveServerIdentity(name.value, revision.value, serverId.value)
  24. auth.acceptServerIdentity(value); hydrate(); notice.value = '服务器名称已保存,三端同步显示。'
  25. } catch (cause) { error.value = cause instanceof Error ? cause.message : '保存失败' }
  26. finally { busy.value = false }
  27. }
  28. void auth.refreshServerIdentity()
  29. </script>
  30. <template>
  31. <section v-if="auth.serverIdentity" class="server-identity" aria-label="服务器名称">
  32. <h3>服务器名称</h3>
  33. <form v-if="editable" @submit.prevent="save">
  34. <label>统一显示名称<input v-model="name" name="server-display-name" maxlength="100" placeholder="例如:家里的 Mac" :disabled="busy" /></label>
  35. <p>该服务器上的 Agent 共用此名称,Web、iOS 和安卓同步。留空恢复服务器主机名。</p>
  36. <p v-if="error" role="alert">{{ error }}</p><p v-if="notice" role="status">{{ notice }}</p>
  37. <button type="submit" :disabled="busy || !dirty">{{ busy ? '保存中…' : '保存服务器名称' }}</button>
  38. <button v-if="error" type="button" :disabled="busy" @click="reload">重新载入</button>
  39. </form>
  40. <template v-else><strong>{{ auth.serverIdentity.displayName }}</strong><p>由服务器管理员设置,三端同步显示。</p></template>
  41. </section>
  42. </template>
  43. <style scoped>
  44. .server-identity{padding:16px 0;border-top:1px solid var(--line)}h3{margin:0 0 12px;font-size:15px}label{display:grid;gap:8px;font-size:13px}input{box-sizing:border-box;width:100%;padding:10px 12px;border:1px solid var(--line);border-radius:10px;background:var(--surface);color:var(--text)}p{font-size:12px;line-height:1.6;color:var(--text-secondary)}button{min-height:36px;padding:7px 12px;border:1px solid var(--line);border-radius:8px;background:var(--surface);color:var(--text);cursor:pointer}button:disabled{opacity:.5;cursor:default}[role=alert]{color:var(--danger,#dc2626)}
  45. </style>