11<script setup lang="ts">
22import { computed , onMounted , onUnmounted , ref , watch } from ' vue'
3- import { ChatView , ServerPicker , WebSocketTransport } from ' @sbot/chat-ui'
3+ import { ServerChatShell , WebSocketTransport , useServerSelection } from ' @sbot/chat-ui'
44import type { RemoteEntry } from ' @sbot/chat-ui'
55import { SConfirm , SToast } from ' sbot-ui'
66import ThemeMenu from ' ./ThemeMenu.vue'
@@ -11,7 +11,6 @@ import darkThemeCSS from '@sbot/chat-ui/themes/theme-dark.css?inline'
1111import ' @sbot/chat-ui/themes/theme-pwa.css'
1212import ' @sbot/chat-ui/themes/sbot-ui-bridge.css'
1313
14- const DEFAULT_PORT = 5500
1514const REMOTES_KEY = ' sbot-app-remotes'
1615const THEME_KEY = ' sbot-app-theme'
1716
@@ -38,12 +37,36 @@ function saveRemotes(list: RemoteEntry[]) {
3837 writeStorage (REMOTES_KEY , JSON .stringify (list ))
3938}
4039
41- const remotes = ref <RemoteEntry []>(loadRemotes ())
42- const phase = ref <' server-pick' | ' chat' >(' server-pick' )
43- const transport = ref <WebSocketTransport | null >(null )
44- const currentBaseUrl = ref (' ' )
45- const connectError = ref (' ' )
46- const connecting = ref (false )
40+ const {
41+ remotes,
42+ phase,
43+ transport,
44+ currentBaseUrl,
45+ connectError,
46+ connecting,
47+ selectLocal,
48+ selectRemote,
49+ addRemote,
50+ updateRemote,
51+ removeRemote,
52+ switchServer,
53+ } = useServerSelection <WebSocketTransport >({
54+ initialRemotes: loadRemotes (),
55+ adapter: {
56+ saveRemotes ,
57+ connect : async (baseUrl ) => {
58+ const nextTransport = new WebSocketTransport (baseUrl )
59+ try {
60+ await nextTransport .getSettings ()
61+ return nextTransport
62+ } catch (error ) {
63+ nextTransport .disconnect ()
64+ throw error
65+ }
66+ },
67+ disconnect : (currentTransport ) => currentTransport .disconnect (),
68+ },
69+ })
4770
4871const themeMode = ref <ThemeMode >(((): ThemeMode => {
4972 const v = readStorage (THEME_KEY )
@@ -88,93 +111,35 @@ function removeSystemThemeListener() {
88111onMounted (addSystemThemeListener )
89112onUnmounted (removeSystemThemeListener )
90113
91- async function selectServer(baseUrl : string ) {
92- if (connecting .value ) return
93- connectError .value = ' '
94- connecting .value = true
95- const nextTransport = new WebSocketTransport (baseUrl )
96- try {
97- await nextTransport .getSettings ()
98- transport .value = nextTransport
99- currentBaseUrl .value = baseUrl
100- phase .value = ' chat'
101- } catch {
102- nextTransport .disconnect ()
103- connectError .value = ` 无法连接服务器 ${baseUrl } `
104- } finally {
105- connecting .value = false
106- }
107- }
108-
109- function switchServer() {
110- transport .value ?.disconnect ()
111- transport .value = null
112- connectError .value = ' '
113- phase .value = ' server-pick'
114- }
115-
116- function selectLocal() {
117- selectServer (` http://localhost:${DEFAULT_PORT } ` )
118- }
119-
120- function selectRemote(index : number ) {
121- const r = remotes .value [index ]
122- if (r ) {
123- const proto = r .secure ? ' https' : ' http'
124- selectServer (` ${proto }://${r .host }:${r .port } ` )
125- }
126- }
127-
128- function addRemote(name : string , host : string , port : number , secure : boolean ) {
129- remotes .value .push ({ name , host , port , secure })
130- saveRemotes (remotes .value )
131- const proto = secure ? ' https' : ' http'
132- selectServer (` ${proto }://${host }:${port } ` )
133- }
134-
135- function updateRemote(index : number , patch : { name? : string ; host? : string ; port? : number ; secure? : boolean }) {
136- const r = remotes .value [index ]
137- if (r ) {
138- Object .assign (r , patch )
139- saveRemotes (remotes .value )
140- }
141- }
142-
143- function removeRemote(index : number ) {
144- remotes .value .splice (index , 1 )
145- saveRemotes (remotes .value )
146- }
147114 </script >
148115
149116<template >
150117 <div class =" desktop-app" >
151118 <UpdaterDialog />
152119 <SToast />
153120 <SConfirm default-confirm-text="确定" default-cancel-text="取消" />
154- <div v-if =" phase === 'server-pick'" class =" theme-menu-floating" >
155- <ThemeMenu :mode =" themeMode " @update =" (m ) => themeMode = m " />
156- </div >
157-
158- <template v-if =" phase === ' server-pick' " >
159- <div v-if =" connectError" class =" desktop-connect-error" >{{ connectError }}</div >
160- <div v-if =" connecting" class =" desktop-connect-status" >正在连接服务器...</div >
161- <ServerPicker
162- :remotes =" remotes "
163- @select-local =" selectLocal "
164- @select-remote =" selectRemote "
165- @add-remote =" addRemote "
166- @update-remote =" updateRemote "
167- @remove-remote =" removeRemote "
168- />
169- </template >
170- <template v-else-if =" transport " >
171- <div class =" desktop-server-bar" >
172- <span class =" desktop-server-url" >{{ currentBaseUrl }}</span >
121+ <ServerChatShell
122+ :phase =" phase "
123+ :remotes =" remotes "
124+ :transport =" transport "
125+ :current-base-url =" currentBaseUrl "
126+ :connect-error =" connectError "
127+ :connecting =" connecting "
128+ :show-attachments =" true "
129+ @select-local =" selectLocal "
130+ @select-remote =" selectRemote "
131+ @add-remote =" addRemote "
132+ @update-remote =" updateRemote "
133+ @remove-remote =" removeRemote "
134+ @switch-server =" switchServer "
135+ >
136+ <template #picker-actions >
173137 <ThemeMenu :mode =" themeMode " @update =" (m ) => themeMode = m " />
174- <button class =" desktop-server-switch" @click =" switchServer" >切换服务器</button >
175- </div >
176- <ChatView :transport =" transport " :show-attachments =" true " />
177- </template >
138+ </template >
139+ <template #server-actions >
140+ <ThemeMenu :mode =" themeMode " @update =" (m ) => themeMode = m " />
141+ </template >
142+ </ServerChatShell >
178143 </div >
179144</template >
180145
@@ -196,59 +161,4 @@ body {
196161 flex-direction : column ;
197162 overflow : hidden ;
198163}
199- .desktop-server-bar {
200- display : flex ;
201- align-items : center ;
202- gap : 8px ;
203- padding : 6px 12px ;
204- background : var (--chatui-bg-surface );
205- border-bottom : 1px solid var (--chatui-border );
206- flex-shrink : 0 ;
207- font-size : 12px ;
208- }
209- .desktop-server-url {
210- flex : 1 ;
211- color : var (--chatui-fg-secondary );
212- font-family : monospace ;
213- overflow : hidden ;
214- text-overflow : ellipsis ;
215- white-space : nowrap ;
216- }
217- .desktop-server-switch {
218- padding : 2px 10px ;
219- border : 1px solid var (--chatui-border );
220- border-radius : 4px ;
221- background : transparent ;
222- cursor : pointer ;
223- font-size : 12px ;
224- color : var (--chatui-fg );
225- flex-shrink : 0 ;
226- }
227- .desktop-server-switch :hover {
228- background : var (--chatui-bg-hover );
229- }
230- .theme-menu-floating {
231- position : fixed ;
232- top : 8px ;
233- right : 12px ;
234- z-index : 10 ;
235- }
236- .desktop-connect-error ,
237- .desktop-connect-status {
238- padding : 8px 12px ;
239- margin : 8px 12px 0 ;
240- border-radius : 4px ;
241- font-size : 12px ;
242- flex-shrink : 0 ;
243- }
244- .desktop-connect-error {
245- color : var (--chatui-btn-danger , #d14343 );
246- background : var (--chatui-bg-surface );
247- border : 1px solid var (--chatui-btn-danger , #d14343 );
248- }
249- .desktop-connect-status {
250- color : var (--chatui-fg-secondary );
251- background : var (--chatui-bg-surface );
252- border : 1px solid var (--chatui-border );
253- }
254164 </style >
0 commit comments