1use auto_update::AutoUpdater;
2use client::proto::UpdateNotification;
3use editor::{Editor, MultiBuffer};
4use gpui::{App, Context, DismissEvent, Entity, SharedString, Window, actions, prelude::*};
5use http_client::HttpClient;
6use markdown_preview::markdown_preview_view::{MarkdownPreviewMode, MarkdownPreviewView};
7use release_channel::{AppVersion, ReleaseChannel};
8use serde::Deserialize;
9use smol::io::AsyncReadExt;
10use util::ResultExt as _;
11use workspace::Workspace;
12use workspace::notifications::simple_message_notification::MessageNotification;
13use workspace::notifications::{NotificationId, show_app_notification};
14
15actions!(auto_update, [ViewReleaseNotesLocally]);
16
17pub fn init(cx: &mut App) {
18 notify_if_app_was_updated(cx);
19 cx.observe_new(|workspace: &mut Workspace, _window, _cx| {
20 workspace.register_action(|workspace, _: &ViewReleaseNotesLocally, window, cx| {
21 view_release_notes_locally(workspace, window, cx);
22 });
23 })
24 .detach();
25}
26
27#[derive(Deserialize)]
28struct ReleaseNotesBody {
29 title: String,
30 release_notes: String,
31}
32
33fn view_release_notes_locally(
34 workspace: &mut Workspace,
35 window: &mut Window,
36 cx: &mut Context<Workspace>,
37) {
38 let release_channel = ReleaseChannel::global(cx);
39
40 let url = match release_channel {
41 ReleaseChannel::Nightly => Some("https://github.com/zed-industries/zed/commits/nightly/"),
42 ReleaseChannel::Dev => Some("https://github.com/zed-industries/zed/commits/main/"),
43 _ => None,
44 };
45
46 if let Some(url) = url {
47 cx.open_url(url);
48 return;
49 }
50
51 let version = AppVersion::global(cx).to_string();
52
53 let client = client::Client::global(cx).http_client();
54 let url = client.build_url(&format!(
55 "/api/release_notes/v2/{}/{}",
56 release_channel.dev_name(),
57 version
58 ));
59
60 let markdown = workspace
61 .app_state()
62 .languages
63 .language_for_name("Markdown");
64
65 workspace
66 .with_local_workspace(window, cx, move |_, window, cx| {
67 cx.spawn_in(window, async move |workspace, cx| {
68 let markdown = markdown.await.log_err();
69 let response = client.get(&url, Default::default(), true).await;
70 let Some(mut response) = response.log_err() else {
71 return;
72 };
73
74 let mut body = Vec::new();
75 response.body_mut().read_to_end(&mut body).await.ok();
76
77 let body: serde_json::Result<ReleaseNotesBody> =
78 serde_json::from_slice(body.as_slice());
79
80 if let Ok(body) = body {
81 workspace
82 .update_in(cx, |workspace, window, cx| {
83 let project = workspace.project().clone();
84 let buffer = project.update(cx, |project, cx| {
85 project.create_local_buffer("", markdown, cx)
86 });
87 buffer.update(cx, |buffer, cx| {
88 buffer.edit([(0..0, body.release_notes)], None, cx)
89 });
90 let language_registry = project.read(cx).languages().clone();
91
92 let buffer = cx.new(|cx| MultiBuffer::singleton(buffer, cx));
93
94 let tab_content = Some(SharedString::from(body.title.to_string()));
95 let editor = cx.new(|cx| {
96 Editor::for_multibuffer(buffer, Some(project), window, cx)
97 });
98 let workspace_handle = workspace.weak_handle();
99 let markdown_preview: Entity<MarkdownPreviewView> =
100 MarkdownPreviewView::new(
101 MarkdownPreviewMode::Default,
102 editor,
103 workspace_handle,
104 language_registry,
105 tab_content,
106 window,
107 cx,
108 );
109 workspace.add_item_to_active_pane(
110 Box::new(markdown_preview.clone()),
111 None,
112 true,
113 window,
114 cx,
115 );
116 cx.notify();
117 })
118 .log_err();
119 }
120 })
121 .detach();
122 })
123 .detach();
124}
125
126/// Shows a notification across all workspaces if an update was previously automatically installed
127/// and this notification had not yet been shown.
128pub fn notify_if_app_was_updated(cx: &mut App) {
129 let Some(updater) = AutoUpdater::get(cx) else {
130 return;
131 };
132 let should_show_notification = updater.read(cx).should_show_update_notification(cx);
133 cx.spawn(async move |cx| {
134 let should_show_notification = should_show_notification.await?;
135 if should_show_notification {
136 cx.update(|cx| {
137 let version = updater.read(cx).current_version();
138 let app_name = ReleaseChannel::global(cx).display_name();
139 show_app_notification(
140 NotificationId::unique::<UpdateNotification>(),
141 cx,
142 move |cx| {
143 let workspace_handle = cx.entity().downgrade();
144 cx.new(|cx| {
145 MessageNotification::new(
146 format!("Updated to {app_name} {}", version),
147 cx,
148 )
149 .primary_message("View Release Notes")
150 .primary_on_click(move |window, cx| {
151 if let Some(workspace) = workspace_handle.upgrade() {
152 workspace.update(cx, |workspace, cx| {
153 crate::view_release_notes_locally(workspace, window, cx);
154 })
155 }
156 cx.emit(DismissEvent);
157 })
158 .show_suppress_button(false)
159 })
160 },
161 );
162 updater.update(cx, |updater, cx| {
163 updater
164 .set_should_show_update_notification(false, cx)
165 .detach_and_log_err(cx);
166 })
167 })?;
168 }
169 anyhow::Ok(())
170 })
171 .detach();
172}