| 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, DialogMode, 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 | /// KVT-fork: the WebView2 host, for opening the sleutels.kvt.nl connect page. |
| 21 | pub fn host() -> Option<Arc<WebViewTransport>> { |
| 22 | HOST.get().cloned() |
| 23 | } |
| 24 | |
| 25 | pub fn open(dialog: UrlDialog, engine: Option<Arc<Engine>>, cx: &mut App) { |
| 26 | let Some(host) = HOST.get().cloned() else { |
| 27 | app_base::open_link::open_url(cx, &dialog.fallback_url); |
| 28 | return; |
| 29 | }; |
| 30 | let state = cx.global::<AppHandle>().0.clone(); |
| 31 | let owner = cx |
| 32 | .active_window() |
| 33 | .and_then(|window| { |
| 34 | window |
| 35 | .update(cx, |_, window, _| notify::native_handle(window)) |
| 36 | .ok() |
| 37 | }) |
| 38 | .flatten(); |
| 39 | cx.spawn(async move |cx| run(host, dialog, state, engine, owner, cx).await) |
| 40 | .detach(); |
| 41 | } |
| 42 | |
| 43 | async fn run( |
| 44 | host: Arc<WebViewTransport>, |
| 45 | dialog: UrlDialog, |
| 46 | state: Entity<AppState>, |
| 47 | engine: Option<Arc<Engine>>, |
| 48 | owner: Option<isize>, |
| 49 | cx: &mut AsyncApp, |
| 50 | ) { |
| 51 | let identity = fetch_identity(engine, &dialog.scope.conversation_id).await; |
| 52 | let context = HostContext { |
| 53 | user_id: identity.user_id, |
| 54 | user_display_name: identity.display_name, |
| 55 | tenant_id: identity.tenant_id, |
| 56 | conversation_id: dialog.scope.conversation_id.clone(), |
| 57 | app_id: dialog.app_id.clone(), |
| 58 | session_id: uuid::Uuid::new_v4().to_string(), |
| 59 | dark: true, |
| 60 | web_application_resource: dialog.web_application_resource.clone(), |
| 61 | }; |
| 62 | let page_html = HostPage { |
| 63 | task_url: &dialog.url, |
| 64 | background: theme::background_hex(), |
| 65 | token_acquirer_script: &session::token_acquirer_script(), |
| 66 | } |
| 67 | .html(); |
| 68 | let (handle, mut events) = host.open_dialog(DialogSpec { |
| 69 | mode: DialogMode::Hosted, |
| 70 | title: dialog.title.clone(), |
| 71 | width: dialog.width, |
| 72 | height: dialog.height, |
| 73 | background: theme::background_hex(), |
| 74 | owner, |
| 75 | page_url: sdk::host_page_url(), |
| 76 | page_html, |
| 77 | }); |
| 78 | while let Some(DialogEvent::Message(raw)) = events.recv().await { |
| 79 | let handled = sdk::handle(&raw, &context); |
| 80 | for directive in &handled.directives { |
| 81 | handle.post(directive.to_string()); |
| 82 | } |
| 83 | if let Some(effect) = handled.effect { |
| 84 | apply(effect, &dialog, &state, &handle, cx).await; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | async fn fetch_identity(engine: Option<Arc<Engine>>, conversation_id: &str) -> DialogIdentity { |
| 90 | let empty = DialogIdentity { |
| 91 | user_id: String::new(), |
| 92 | display_name: String::new(), |
| 93 | tenant_id: String::new(), |
| 94 | }; |
| 95 | let Some(engine) = engine else { |
| 96 | return empty; |
| 97 | }; |
| 98 | let conversation_id = conversation_id.to_owned(); |
| 99 | let lookup = runtime::spawn(async move { engine.dialog_identity(&conversation_id).await.ok() }); |
| 100 | lookup.await.ok().flatten().unwrap_or(empty) |
| 101 | } |
| 102 | |
| 103 | async fn apply( |
| 104 | effect: Effect, |
| 105 | dialog: &UrlDialog, |
| 106 | state: &Entity<AppState>, |
| 107 | handle: &DialogHandle, |
| 108 | cx: &mut AsyncApp, |
| 109 | ) { |
| 110 | match effect { |
| 111 | Effect::Resize { width, height } => handle.resize(width, height), |
| 112 | Effect::OpenLink(link) => { |
| 113 | cx.update(|cx| app_base::open_link::open_url(cx, &link)); |
| 114 | } |
| 115 | Effect::Submit(result) => { |
| 116 | let scope = dialog.scope.clone(); |
| 117 | let next = state |
| 118 | .update(cx, |state, cx| state.submit_url_dialog(scope, result, cx)) |
| 119 | .await; |
| 120 | match next { |
| 121 | UrlDialogNext::Close => handle.close(), |
| 122 | UrlDialogNext::Navigate { url, width, height } => { |
| 123 | handle.resize(Some(width), Some(height)); |
| 124 | handle.post(json!({"kind": "load", "url": url}).to_string()); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | } |