BlueGit

ict rusty-teams

Files

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