WorkspaceView.vue 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <script setup lang="ts">
  2. import { computed, onBeforeUnmount, onMounted, ref } from 'vue'
  3. import WorkspaceShell from '@/components/app/WorkspaceShell.vue'
  4. import AgentIdentityDialog from '@/components/app/AgentIdentityDialog.vue'
  5. import { updateProfileIdentity } from '@/api/profiles'
  6. import { useAuthStore } from '@/stores/auth'
  7. import { useThemeStore } from '@/stores/theme'
  8. withDefaults(defineProps<{
  9. sidebarTitle: string
  10. sidebarSubtitle?: string
  11. sidebarContextTitle?: string
  12. sidebarFocusMode?: boolean
  13. inspectorOpen?: boolean
  14. inspectorCloseLabel?: string
  15. }>(), { sidebarSubtitle: '', sidebarContextTitle: '', sidebarFocusMode: false, inspectorOpen: false, inspectorCloseLabel: '关闭预览' })
  16. const emit = defineEmits<{ closeInspector: [] }>()
  17. const auth = useAuthStore()
  18. const theme = useThemeStore()
  19. const userName = computed(() => auth.user?.displayName || auth.user?.username || '')
  20. const identityOpen = ref(false)
  21. const identityBusy = ref(false)
  22. const identityError = ref('')
  23. async function logout() { await auth.logout() }
  24. function selectProfile(name: string) { auth.selectProfile(name) }
  25. async function saveIdentity(input: { title: string; avatarDataURL?: string | null }) {
  26. const profile = auth.activeProfile
  27. if (!profile) return
  28. identityBusy.value = true
  29. identityError.value = ''
  30. try {
  31. await updateProfileIdentity(profile, input)
  32. await auth.refreshProfiles()
  33. await auth.refreshProfileAvatars()
  34. identityOpen.value = false
  35. } catch (cause) {
  36. identityError.value = cause instanceof Error ? cause.message : '保存 Agent 身份失败'
  37. } finally {
  38. identityBusy.value = false
  39. }
  40. }
  41. async function refreshOnFocus() {
  42. await auth.refreshProfiles()
  43. await auth.refreshProfileAvatars()
  44. }
  45. function refreshOnWindowFocus() { void refreshOnFocus().catch(() => undefined) }
  46. function openIdentity() {
  47. void auth.refreshProfileAvatars().finally(() => { identityOpen.value = true })
  48. }
  49. onMounted(() => {
  50. window.addEventListener('focus', refreshOnWindowFocus)
  51. void auth.refreshProfileAvatars().catch(() => undefined)
  52. })
  53. onBeforeUnmount(() => window.removeEventListener('focus', refreshOnWindowFocus))
  54. </script>
  55. <template>
  56. <WorkspaceShell
  57. :user-name="userName"
  58. :pairing-user-name="auth.user?.username || ''"
  59. :active-profile="auth.activeProfile"
  60. :profiles="auth.profiles"
  61. :theme="theme.resolvedTheme"
  62. :insecure-transport="auth.insecureLan"
  63. :is-admin="auth.user?.role === 'admin'"
  64. :upstream-ready="auth.upstreamReady"
  65. :upstream-error="auth.upstreamError"
  66. :sidebar-title="sidebarTitle"
  67. :sidebar-subtitle="sidebarSubtitle"
  68. :sidebar-context-title="sidebarContextTitle"
  69. :sidebar-focus-mode="sidebarFocusMode"
  70. :inspector-open="inspectorOpen"
  71. :inspector-close-label="inspectorCloseLabel"
  72. @logout="logout"
  73. @toggle-theme="theme.toggle"
  74. @select-profile="selectProfile"
  75. @edit-profile="openIdentity"
  76. @close-inspector="emit('closeInspector')"
  77. >
  78. <template #sidebar-action><slot name="sidebar-action" /></template>
  79. <template #sidebar-before-heading><slot name="sidebar-before-heading" /></template>
  80. <template #sidebar><slot name="sidebar" /></template>
  81. <template #mobile-sidebar><slot name="sidebar" /></template>
  82. <template #default><slot /></template>
  83. <template #inspector><slot name="inspector" /></template>
  84. </WorkspaceShell>
  85. <AgentIdentityDialog :open="identityOpen" :profile="auth.activeProfile" :busy="identityBusy" :error="identityError" :is-admin="auth.user?.role === 'admin'" @close="identityOpen = false" @save="saveIdentity" />
  86. </template>