| 1 | use std::sync::{Arc, OnceLock}; |
| 2 | |
| 3 | use gpui_kit::*; |
| 4 | use serde_json::json; |
| 5 | use teams_core::DialogIdentity; |
| 6 | use webview::{DialogEvent, DialogHandle, DialogSpec, WebViewTransport}; |
| 7 | |
| 8 | use super::sdk::{self, Effect, HostContext, HostPage}; |
| 9 | use super::{UrlDialog, UrlDialogNext}; |
| 10 | use crate::app_state::{AppHandle, AppState}; |
| 11 | use crate::backend::Engine; |
| 12 | use crate::{notify, runtime, theme}; |
| 13 | |
| 14 | static HOST: OnceLock<Arc<WebViewTransport>> = OnceLock::new(); |
| 15 | |
| 16 | pub fn install_host(transport: Arc<WebViewTransport>) { |
| 17 | let _ = HOST.set(transport); |
| 18 | } |
| 19 | |
| 20 | pub fn open(dialog: UrlDialog, engine: Option<Arc<Engine>>, cx: &mut App) { |
| 21 | let Some(host) = HOST.get().cloned() else { |
| 22 | cx.open_url(&dialog.fallback_url); |
| 23 | return; |
| 24 | }; |
| 25 | let state = cx.global::<AppHandle>().0.clone(); |
| 26 | let owner = cx |
| 27 | .active_window() |
| 28 | .and_then(|window| { |
| 29 | window |
| 30 | .update(cx, |_, window, _| notify::native_handle(window)) |
| 31 | .ok() |
| 32 | }) |
| 33 | .flatten(); |
| 34 | cx.spawn(async move |cx| run(host, dialog, state, engine, owner, cx).await) |
| 35 | .detach(); |
| 36 | } |
| 37 | |
| 38 | async fn run( |
| 39 | host: Arc<WebViewTransport>, |
| 40 | dialog: UrlDialog, |
| 41 | state: Entity<AppState>, |
| 42 | engine: Option<Arc<Engine>>, |
| 43 | owner: Option<isize>, |
| 44 | cx: &mut AsyncApp, |
| 45 | ) { |
| 46 | let identity = fetch_identity(engine, &dialog.scope.conversation_id).await; |
| 47 | let context = HostContext { |
| 48 | user_id: identity.user_id, |
| 49 | user_display_name: identity.display_name, |
| 50 | tenant_id: identity.tenant_id, |
| 51 | conversation_id: dialog.scope.conversation_id.clone(), |
| 52 | app_id: dialog.app_id.clone(), |
| 53 | session_id: uuid::Uuid::new_v4().to_string(), |
| 54 | dark: true, |
| 55 | web_application_resource: dialog.web_application_resource.clone(), |
| 56 | }; |
| 57 | let page_html = HostPage { |
| 58 | task_url: &dialog.url, |
| 59 | background: theme::BACKGROUND, |
| 60 | token_acquirer_script: &session::token_acquirer_script(), |
| 61 | } |
| 62 | .html(); |
| 63 | let (handle, mut events) = host.open_dialog(DialogSpec { |
| 64 | title: dialog.title.clone(), |
| 65 | width: dialog.width, |
| 66 | height: dialog.height, |
| 67 | background: theme::BACKGROUND, |
| 68 | owner, |
| 69 | page_url: sdk::host_page_url(), |
| 70 | page_html, |
| 71 | }); |
| 72 | while let Some(DialogEvent::Message(raw)) = events.recv().await { |
| 73 | let handled = sdk::handle(&raw, &context); |
| 74 | for directive in &handled.directives { |
| 75 | handle.post(directive.to_string()); |
| 76 | } |
| 77 | if let Some(effect) = handled.effect { |
| 78 | apply(effect, &dialog, &state, &handle, cx).await; |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | async fn fetch_identity(engine: Option<Arc<Engine>>, conversation_id: &str) -> DialogIdentity { |
| 84 | let empty = DialogIdentity { |
| 85 | user_id: String::new(), |
| 86 | display_name: String::new(), |
| 87 | tenant_id: String::new(), |
| 88 | }; |
| 89 | let Some(engine) = engine else { |
| 90 | return empty; |
| 91 | }; |
| 92 | let conversation_id = conversation_id.to_owned(); |
| 93 | let lookup = runtime::spawn(async move { engine.dialog_identity(&conversation_id).await.ok() }); |
| 94 | lookup.await.ok().flatten().unwrap_or(empty) |
| 95 | } |
| 96 | |
| 97 | async fn apply( |
| 98 | effect: Effect, |
| 99 | dialog: &UrlDialog, |
| 100 | state: &Entity<AppState>, |
| 101 | handle: &DialogHandle, |
| 102 | cx: &mut AsyncApp, |
| 103 | ) { |
| 104 | match effect { |
| 105 | Effect::Resize { width, height } => handle.resize(width, height), |
| 106 | Effect::OpenLink(link) => { |
| 107 | cx.update(|cx| cx.open_url(&link)); |
| 108 | } |
| 109 | Effect::Submit(result) => { |
| 110 | let scope = dialog.scope.clone(); |
| 111 | let next = state |
| 112 | .update(cx, |state, cx| state.submit_url_dialog(scope, result, cx)) |
| 113 | .await; |
| 114 | match next { |
| 115 | UrlDialogNext::Close => handle.close(), |
| 116 | UrlDialogNext::Navigate { url, width, height } => { |
| 117 | handle.resize(Some(width), Some(height)); |
| 118 | handle.post(json!({"kind": "load", "url": url}).to_string()); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |