forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatPanelFeature.swift
More file actions
132 lines (113 loc) · 4.81 KB
/
ChatPanelFeature.swift
File metadata and controls
132 lines (113 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import ActiveApplicationMonitor
import AppKit
import ChatTab
import ComposableArchitecture
import SwiftUI
public struct ChatPanelFeature: ReducerProtocol {
public struct ChatTabGroup: Equatable {
public var tabs: [BaseChatTab]
public var tabTypes: [String]
public var tabInfo: [ChatTabInfo]
public var selectedTabId: String?
init(tabs: [BaseChatTab] = [], tabTypes: [String] = [], tabInfo: [ChatTabInfo] = [], selectedTabId: String? = nil) {
self.tabs = tabs
self.tabTypes = tabTypes
self.tabInfo = tabInfo
self.selectedTabId = selectedTabId
}
public var activeChatTab: BaseChatTab? {
guard let id = selectedTabId else { return tabs.first }
guard let tab = tabs.first(where: { $0.id == id }) else { return tabs.first }
return tab
}
}
public struct State: Equatable {
public var chatTapGroup = ChatTabGroup()
var colorScheme: ColorScheme = .light
var isPanelDisplayed = false
var chatPanelInASeparateWindow = false
}
public enum Action: Equatable {
// Window
case hideButtonClicked
case toggleChatPanelDetachedButtonClicked
case detachChatPanel
case attachChatPanel
case presentChatPanel(forceDetach: Bool)
// Tabs
case updateChatTabInfo([ChatTabInfo])
case closeTabButtonClicked(id: String)
case createNewTapButtonClicked(type: String)
case tabClicked(id: String)
case appendAndSelectTab(BaseChatTab)
}
@Dependency(\.suggestionWidgetControllerDependency) var suggestionWidgetControllerDependency
@Dependency(\.xcodeInspector) var xcodeInspector
@Dependency(\.activatePreviouslyActiveXcode) var activatePreviouslyActiveXcode
@Dependency(\.activateExtensionService) var activateExtensionService
public var body: some ReducerProtocol<State, Action> {
Reduce { state, action in
switch action {
case .hideButtonClicked:
state.isPanelDisplayed = false
return .run { _ in
await activatePreviouslyActiveXcode()
}
case .toggleChatPanelDetachedButtonClicked:
state.chatPanelInASeparateWindow.toggle()
return .none
case .detachChatPanel:
state.chatPanelInASeparateWindow = true
return .none
case .attachChatPanel:
state.chatPanelInASeparateWindow = false
return .none
case let .presentChatPanel(forceDetach):
if forceDetach {
state.chatPanelInASeparateWindow = true
}
state.isPanelDisplayed = true
return .run { _ in
await activateExtensionService()
}
case let .updateChatTabInfo(chatTabInfo):
let previousSelectedIndex = state.chatTapGroup.tabInfo
.firstIndex(where: { $0.id == state.chatTapGroup.selectedTabId })
state.chatTapGroup.tabInfo = chatTabInfo
if !chatTabInfo.contains(where: { $0.id == state.chatTapGroup.selectedTabId }) {
if let previousSelectedIndex {
let proposedSelectedIndex = previousSelectedIndex - 1
if proposedSelectedIndex >= 0,
proposedSelectedIndex < chatTabInfo.endIndex
{
state.chatTapGroup.selectedTabId = chatTabInfo[proposedSelectedIndex].id
} else {
state.chatTapGroup.selectedTabId = chatTabInfo.first?.id
}
} else {
state.chatTapGroup.selectedTabId = nil
}
}
return .none
case let .closeTabButtonClicked(id):
state.chatTapGroup.tabs.removeAll { $0.id == id }
return .none
case .createNewTapButtonClicked:
return .none // handled elsewhere
case let .tabClicked(id):
guard state.chatTapGroup.tabInfo.contains(where: { $0.id == id }) else {
state.chatTapGroup.selectedTabId = nil
return .none
}
state.chatTapGroup.selectedTabId = id
return .none
case let .appendAndSelectTab(tab):
guard !state.chatTapGroup.tabInfo.contains(where: { $0.id == tab.id })
else { return .none }
state.chatTapGroup.tabs.append(tab)
state.chatTapGroup.selectedTabId = tab.id
return .none
}
}
}
}