WorkspaceView.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <script setup lang="ts">
  2. import { computed, onBeforeUnmount, onMounted, ref } from 'vue'
  3. import WorkspaceShell from '@/components/app/WorkspaceShell.vue'
  4. import { updateProfileIdentity, type ProfileIdentityInput } from '@/api/profiles'
  5. import { useAuthStore } from '@/stores/auth'
  6. import { useThemeStore } from '@/stores/theme'
  7. withDefaults(defineProps<{
  8. sidebarTitle: string
  9. sidebarSubtitle?: string
  10. sidebarContextTitle?: string
  11. sidebarFocusMode?: boolean
  12. inspectorOpen?: boolean
  13. inspectorCloseLabel?: string
  14. }>(), { sidebarSubtitle: '', sidebarContextTitle: '', sidebarFocusMode: false, inspectorOpen: false, inspectorCloseLabel: '关闭预览' })
  15. const emit = defineEmits<{ closeInspector: [] }>()
  16. const auth = useAuthStore()
  17. const theme = useThemeStore()
  18. const userName = computed(() => auth.user?.displayName || auth.user?.username || '')
  19. const identityBusy = ref(false)
  20. const identityError = ref('')
  21. const identityResetVersion = ref(0)
  22. async function logout() { await auth.logout() }
  23. function selectProfile(name: string) { auth.selectProfile(name) }
  24. async function saveIdentity(input: ProfileIdentityInput) {
  25. const profile = auth.activeProfile
  26. if (!profile) return
  27. identityBusy.value = true
  28. identityError.value = ''
  29. try {
  30. await updateProfileIdentity(profile, input)
  31. await auth.refreshProfiles()
  32. await auth.refreshProfileAvatars()
  33. identityError.value = ''
  34. identityResetVersion.value += 1
  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. onMounted(() => {
  47. window.addEventListener('focus', refreshOnWindowFocus)
  48. void auth.refreshProfileAvatars().catch(() => undefined)
  49. })
  50. onBeforeUnmount(() => window.removeEventListener('focus', refreshOnWindowFocus))
  51. </script>
  52. <template>
  53. <WorkspaceShell
  54. :user-name="userName"
  55. :user-avatar="auth.user?.avatar"
  56. :pairing-user-name="auth.user?.username || ''"
  57. :active-profile="auth.activeProfile"
  58. :profiles="auth.profiles"
  59. :theme="theme.resolvedTheme"
  60. :theme-preference="theme.theme"
  61. :insecure-transport="auth.insecureLan"
  62. :is-admin="auth.user?.role === 'admin'"
  63. :upstream-ready="auth.upstreamReady"
  64. :upstream-error="auth.upstreamError"
  65. :identity-busy="identityBusy"
  66. :identity-error="identityError"
  67. :identity-reset-version="identityResetVersion"
  68. :sidebar-title="sidebarTitle"
  69. :sidebar-subtitle="sidebarSubtitle"
  70. :sidebar-context-title="sidebarContextTitle"
  71. :sidebar-focus-mode="sidebarFocusMode"
  72. :inspector-open="inspectorOpen"
  73. :inspector-close-label="inspectorCloseLabel"
  74. @logout="logout"
  75. @toggle-theme="theme.toggle"
  76. @set-theme="theme.setTheme"
  77. @select-profile="selectProfile"
  78. @save-identity="saveIdentity"
  79. @close-inspector="emit('closeInspector')"
  80. >
  81. <template #sidebar-action><slot name="sidebar-action" /></template>
  82. <template #sidebar-before-heading><slot name="sidebar-before-heading" /></template>
  83. <template #sidebar><slot name="sidebar" /></template>
  84. <template #mobile-sidebar><slot name="sidebar" /></template>
  85. <template #default><slot /></template>
  86. <template #inspector><slot name="inspector" /></template>
  87. </WorkspaceShell>
  88. </template>