1use context_server::{ContextServerSettings, ServerCommand, ServerConfig};
2use gpui::{DismissEvent, Entity, EventEmitter, FocusHandle, Focusable, WeakEntity, prelude::*};
3use serde_json::json;
4use settings::update_settings_file;
5use ui::{Modal, ModalFooter, ModalHeader, Section, Tooltip, prelude::*};
6use ui_input::SingleLineInput;
7use workspace::{ModalView, Workspace};
8
9use crate::AddContextServer;
10
11pub struct AddContextServerModal {
12 workspace: WeakEntity<Workspace>,
13 name_editor: Entity<SingleLineInput>,
14 command_editor: Entity<SingleLineInput>,
15}
16
17impl AddContextServerModal {
18 pub fn register(
19 workspace: &mut Workspace,
20 _window: Option<&mut Window>,
21 _cx: &mut Context<Workspace>,
22 ) {
23 workspace.register_action(|workspace, _: &AddContextServer, window, cx| {
24 let workspace_handle = cx.entity().downgrade();
25 workspace.toggle_modal(window, cx, |window, cx| {
26 Self::new(workspace_handle, window, cx)
27 })
28 });
29 }
30
31 pub fn new(
32 workspace: WeakEntity<Workspace>,
33 window: &mut Window,
34 cx: &mut Context<Self>,
35 ) -> Self {
36 let name_editor =
37 cx.new(|cx| SingleLineInput::new(window, cx, "Your server name").label("Name"));
38 let command_editor = cx.new(|cx| {
39 SingleLineInput::new(window, cx, "Command").label("Command to run the context server")
40 });
41
42 Self {
43 name_editor,
44 command_editor,
45 workspace,
46 }
47 }
48
49 fn confirm(&mut self, cx: &mut Context<Self>) {
50 let name = self
51 .name_editor
52 .read(cx)
53 .editor()
54 .read(cx)
55 .text(cx)
56 .trim()
57 .to_string();
58 let command = self
59 .command_editor
60 .read(cx)
61 .editor()
62 .read(cx)
63 .text(cx)
64 .trim()
65 .to_string();
66
67 if name.is_empty() || command.is_empty() {
68 return;
69 }
70
71 let mut command_parts = command.split(' ').map(|part| part.trim().to_string());
72 let Some(path) = command_parts.next() else {
73 return;
74 };
75 let args = command_parts.collect::<Vec<_>>();
76
77 if let Some(workspace) = self.workspace.upgrade() {
78 workspace.update(cx, |workspace, cx| {
79 let fs = workspace.app_state().fs.clone();
80 update_settings_file::<ContextServerSettings>(fs.clone(), cx, |settings, _| {
81 settings.context_servers.insert(
82 name.into(),
83 ServerConfig {
84 command: Some(ServerCommand {
85 path,
86 args,
87 env: None,
88 }),
89 settings: Some(json!({})),
90 },
91 );
92 });
93 });
94 }
95
96 cx.emit(DismissEvent);
97 }
98
99 fn cancel(&mut self, cx: &mut Context<Self>) {
100 cx.emit(DismissEvent);
101 }
102}
103
104impl ModalView for AddContextServerModal {}
105
106impl Focusable for AddContextServerModal {
107 fn focus_handle(&self, cx: &App) -> FocusHandle {
108 self.name_editor.focus_handle(cx).clone()
109 }
110}
111
112impl EventEmitter<DismissEvent> for AddContextServerModal {}
113
114impl Render for AddContextServerModal {
115 fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
116 let is_name_empty = self.name_editor.read(cx).is_empty(cx);
117 let is_command_empty = self.command_editor.read(cx).is_empty(cx);
118
119 div()
120 .elevation_3(cx)
121 .w(rems(34.))
122 .key_context("AddContextServerModal")
123 .on_action(cx.listener(|this, _: &menu::Cancel, _window, cx| this.cancel(cx)))
124 .on_action(cx.listener(|this, _: &menu::Confirm, _window, cx| this.confirm(cx)))
125 .capture_any_mouse_down(cx.listener(|this, _, window, cx| {
126 this.focus_handle(cx).focus(window);
127 }))
128 .on_mouse_down_out(cx.listener(|_this, _, _, cx| cx.emit(DismissEvent)))
129 .child(
130 Modal::new("add-context-server", None)
131 .header(ModalHeader::new().headline("Add Context Server"))
132 .section(
133 Section::new()
134 .child(self.name_editor.clone())
135 .child(self.command_editor.clone()),
136 )
137 .footer(
138 ModalFooter::new()
139 .start_slot(
140 Button::new("cancel", "Cancel").on_click(
141 cx.listener(|this, _event, _window, cx| this.cancel(cx)),
142 ),
143 )
144 .end_slot(
145 Button::new("add-server", "Add Server")
146 .disabled(is_name_empty || is_command_empty)
147 .map(|button| {
148 if is_name_empty {
149 button.tooltip(Tooltip::text("Name is required"))
150 } else if is_command_empty {
151 button.tooltip(Tooltip::text("Command is required"))
152 } else {
153 button
154 }
155 })
156 .on_click(
157 cx.listener(|this, _event, _window, cx| this.confirm(cx)),
158 ),
159 ),
160 ),
161 )
162 }
163}