forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMCPRegistryURLInputField.swift
More file actions
160 lines (145 loc) · 5.46 KB
/
MCPRegistryURLInputField.swift
File metadata and controls
160 lines (145 loc) · 5.46 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import GitHubCopilotService
import SwiftUI
import SharedUIComponents
struct MCPRegistryURLInputField: View {
@Binding var urlText: String
@AppStorage(\.mcpRegistryBaseURLHistory) private var urlHistory
@State private var showHistory: Bool = false
@FocusState private var isFocused: Bool
let defaultMCPRegistryBaseURL = "https://api.mcp.github.com"
let maxURLLength: Int
let isSheet: Bool
let mcpRegistryEntry: MCPRegistryEntry?
let onValidationChange: ((Bool) -> Void)?
let onCommit: (() -> Void)?
private var isRegistryOnly: Bool {
mcpRegistryEntry?.registryAccess == .registryOnly
}
init(
urlText: Binding<String>,
maxURLLength: Int = 2048,
isSheet: Bool = false,
mcpRegistryEntry: MCPRegistryEntry? = nil,
onValidationChange: ((Bool) -> Void)? = nil,
onCommit: (() -> Void)? = nil
) {
self._urlText = urlText
self.maxURLLength = maxURLLength
self.isSheet = isSheet
self.mcpRegistryEntry = mcpRegistryEntry
self.onValidationChange = onValidationChange
self.onCommit = onCommit
}
var body: some View {
VStack(alignment: .leading, spacing: 8) {
HStack(spacing: 8) {
if isSheet {
TextFieldsContainer {
TextField("MCP Registry Base URL", text: $urlText)
.focused($isFocused)
.disabled(isRegistryOnly)
.onChange(of: urlText) { newValue in
handleURLChange(newValue)
}
.onSubmit {
onCommit?()
}
}
} else {
TextField("MCP Registry Base URL:", text: $urlText)
.textFieldStyle(.roundedBorder)
.focused($isFocused)
.disabled(isRegistryOnly)
.onChange(of: urlText) { newValue in
handleURLChange(newValue)
}
.onSubmit {
onCommit?()
}
}
Menu {
ForEach(urlHistory, id: \.self) { url in
Button(url) {
urlText = url
isFocused = false
onCommit?()
}
}
Divider()
Button("Reset to Default") {
urlText = defaultMCPRegistryBaseURL
onCommit?()
}
if !urlHistory.isEmpty {
Button("Clear History") {
urlHistory = []
}
}
} label: {
Image(systemName: "chevron.down")
.resizable()
.scaledToFit()
.frame(width: 11, height: 11)
.padding(isSheet ? 9 : 3)
}
.labelStyle(.iconOnly)
.menuIndicator(.hidden)
.buttonStyle(
HoverButtonStyle(
hoverColor: SecondarySystemFillColor,
backgroundColor: SecondarySystemFillColor,
cornerRadius: isSheet ? 12 : 6
)
)
.opacity(isRegistryOnly ? 0.5 : 1)
.disabled(isRegistryOnly)
}
if isRegistryOnly {
Badge(
text: "This URL is managed by \(mcpRegistryEntry!.owner.login) and cannot be modified",
level: .info,
icon: "info.circle.fill"
)
}
}
.onAppear {
if isRegistryOnly, let entryURL = mcpRegistryEntry?.url {
urlText = entryURL
}
}
.onChange(of: mcpRegistryEntry) { newEntry in
if newEntry?.registryAccess == .registryOnly, let entryURL = newEntry?.url {
urlText = entryURL
}
}
}
private func handleURLChange(_ newValue: String) {
// If registryOnly, force the URL back to the registry entry URL
if isRegistryOnly, let entryURL = mcpRegistryEntry?.url {
urlText = entryURL
return
}
let limitedText = String(newValue.prefix(maxURLLength))
if limitedText != newValue {
urlText = limitedText
}
let isValid = limitedText.isEmpty || isValidURL(limitedText)
onValidationChange?(isValid)
}
private func isValidURL(_ string: String) -> Bool {
guard !string.isEmpty else { return true }
return URL(string: string) != nil && (string.hasPrefix("http://") || string.hasPrefix("https://"))
}
}
extension Array where Element == String {
mutating func addToHistory(_ url: String, maxItems: Int = 10) {
// Remove if already exists
removeAll { $0 == url }
// Add to beginning
insert(url, at: 0)
// Keep only maxItems
if count > maxItems {
removeLast(count - maxItems)
}
}
}