@@ -56,7 +56,10 @@ struct GUI {
5656 case start
5757 case openChatPanel( forceDetach: Bool )
5858 case createAndSwitchToChatGPTChatTabIfNeeded
59- case createAndSwitchToBrowserTabIfNeeded( url: URL )
59+ case createAndSwitchToChatTabIfNeededMatching(
60+ check: ( any ChatTab ) -> Bool ,
61+ kind: ChatTabKind ?
62+ )
6063 case sendCustomCommandToActiveChat( CustomCommand )
6164 case toggleWidgetsHotkeyPressed
6265
@@ -147,77 +150,42 @@ struct GUI {
147150 }
148151
149152 case . createAndSwitchToChatGPTChatTabIfNeeded:
150- if let selectedTabInfo = state. chatTabGroup. selectedTabInfo,
151- chatTabPool. getTab ( of: selectedTabInfo. id) is ChatGPTChatTab
152- {
153- // Already in ChatGPT tab
154- return . none
155- }
156-
157- if let firstChatGPTTabInfo = state. chatTabGroup. tabInfo. first ( where: {
158- chatTabPool. getTab ( of: $0. id) is ChatGPTChatTab
159- } ) {
160- return . run { send in
161- await send ( . suggestionWidget( . chatPanel( . tabClicked(
162- id: firstChatGPTTabInfo. id
163- ) ) ) )
164- }
165- }
166153 return . run { send in
167- if let ( _, chatTabInfo) = await chatTabPool. createTab ( for: nil ) {
168- await send (
169- . suggestionWidget( . chatPanel( . appendAndSelectTab( chatTabInfo) ) )
170- )
171- }
172- }
173-
174- case let . createAndSwitchToBrowserTabIfNeeded( url) :
175- #if canImport(BrowserChatTab)
176- func match( _ tabURL: URL ? ) -> Bool {
177- guard let tabURL else { return false }
178- return tabURL == url
179- || tabURL. absoluteString. hasPrefix ( url. absoluteString)
154+ await send ( . createAndSwitchToChatTabIfNeededMatching(
155+ check: { $0 is ChatGPTChatTab } ,
156+ kind: nil
157+ ) )
180158 }
181159
160+ case let . createAndSwitchToChatTabIfNeededMatching( check, kind) :
182161 if let selectedTabInfo = state. chatTabGroup. selectedTabInfo,
183- let tab = chatTabPool. getTab ( of: selectedTabInfo. id) as? BrowserChatTab ,
184- match ( tab. url )
162+ let tab = chatTabPool. getTab ( of: selectedTabInfo. id) ,
163+ check ( tab)
185164 {
186- // Already in the target Browser tab
165+ // Already in ChatGPT tab
187166 return . none
188167 }
189168
190169 if let firstChatGPTTabInfo = state. chatTabGroup. tabInfo. first ( where: {
191- guard let tab = chatTabPool. getTab ( of: $0. id) as? BrowserChatTab ,
192- match ( tab. url )
193- else { return false }
194- return true
170+ if let tab = chatTabPool. getTab ( of: $0. id) {
171+ return check ( tab)
172+ }
173+ return false
195174 } ) {
196175 return . run { send in
197176 await send ( . suggestionWidget( . chatPanel( . tabClicked(
198177 id: firstChatGPTTabInfo. id
199178 ) ) ) )
200179 }
201180 }
202-
203181 return . run { send in
204- if let ( _, chatTabInfo) = await chatTabPool. createTab (
205- for: . init( BrowserChatTab . urlChatBuilder (
206- url: url,
207- externalDependency: ChatTabFactory
208- . externalDependenciesForBrowserChatTab ( )
209- ) )
210- ) {
182+ if let ( _, chatTabInfo) = await chatTabPool. createTab ( for: kind) {
211183 await send (
212184 . suggestionWidget( . chatPanel( . appendAndSelectTab( chatTabInfo) ) )
213185 )
214186 }
215187 }
216188
217- #else
218- return . none
219- #endif
220-
221189 case let . sendCustomCommandToActiveChat( command) :
222190 @Sendable func stopAndHandleCommand( _ tab: ChatGPTChatTab ) async {
223191 if tab. service. isReceivingMessage {
0 commit comments