auto_update_ui.rs

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