BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-state / src / task_dialog / hosted.rs

hosted.rs

hosted.rs 4.0 KiB Download Raw
1use std::sync::{Arc, OnceLock};
2
3use gpui_kit::*;
4use serde_json::json;
5use teams_core::DialogIdentity;
6use webview::{DialogEvent, DialogHandle, DialogMode, DialogSpec, WebViewTransport};
7
8use super::sdk::{self, Effect, HostContext, HostPage};
9use super::{UrlDialog, UrlDialogNext};
10use crate::app_state::{AppHandle, AppState};
11use crate::backend::Engine;
12use crate::{notify, runtime, theme};
13
14static HOST: OnceLock<Arc<WebViewTransport>> = OnceLock::new();
15
16pub fn install_host(transport: Arc<WebViewTransport>) {
17 let _ = HOST.set(transport);
18}
19
20pub fn open(dialog: UrlDialog, engine: Option<Arc<Engine>>, cx: &mut App) {
21 let Some(host) = HOST.get().cloned() else {
22 app_base::open_link::open_url(cx, &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
38async 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_hex(),
60 token_acquirer_script: &session::token_acquirer_script(),
61 }
62 .html();
63 let (handle, mut events) = host.open_dialog(DialogSpec {
64 mode: DialogMode::Hosted,
65 title: dialog.title.clone(),
66 width: dialog.width,
67 height: dialog.height,
68 background: theme::background_hex(),
69 owner,
70 page_url: sdk::host_page_url(),
71 page_html,
72 });
73 while let Some(DialogEvent::Message(raw)) = events.recv().await {
74 let handled = sdk::handle(&raw, &context);
75 for directive in &handled.directives {
76 handle.post(directive.to_string());
77 }
78 if let Some(effect) = handled.effect {
79 apply(effect, &dialog, &state, &handle, cx).await;
80 }
81 }
82}
83
84async fn fetch_identity(engine: Option<Arc<Engine>>, conversation_id: &str) -> DialogIdentity {
85 let empty = DialogIdentity {
86 user_id: String::new(),
87 display_name: String::new(),
88 tenant_id: String::new(),
89 };
90 let Some(engine) = engine else {
91 return empty;
92 };
93 let conversation_id = conversation_id.to_owned();
94 let lookup = runtime::spawn(async move { engine.dialog_identity(&conversation_id).await.ok() });
95 lookup.await.ok().flatten().unwrap_or(empty)
96}
97
98async fn apply(
99 effect: Effect,
100 dialog: &UrlDialog,
101 state: &Entity<AppState>,
102 handle: &DialogHandle,
103 cx: &mut AsyncApp,
104) {
105 match effect {
106 Effect::Resize { width, height } => handle.resize(width, height),
107 Effect::OpenLink(link) => {
108 cx.update(|cx| app_base::open_link::open_url(cx, &link));
109 }
110 Effect::Submit(result) => {
111 let scope = dialog.scope.clone();
112 let next = state
113 .update(cx, |state, cx| state.submit_url_dialog(scope, result, cx))
114 .await;
115 match next {
116 UrlDialogNext::Close => handle.close(),
117 UrlDialogNext::Navigate { url, width, height } => {
118 handle.resize(Some(width), Some(height));
119 handle.post(json!({"kind": "load", "url": url}).to_string());
120 }
121 }
122 }
123 }
124}