mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-08-24 04:13:59 +02:00
Command handlers took Tauri types, so running them anywhere else meant rewriting them. `yaak_commands::Host` is what a handler actually needs from its surroundings: who the client is, what it's looking at, and the shared managers. Handlers are generic over it and keep the router's shape, so another host registers one with rpc_handler_async! and no adapter at all. 28 commands move: models, deletes, response reads, encryption, plugin info, export. ClientCtx implements Host, so the desktop adapters are one line and behavior is unchanged. PluginHost is separate because PluginManager is "spawn a Node sidecar and talk to it" — a browser host runs plugins in a worker and can't hand one back. Only 4 of the 28 need it; the rest work without a plugin runtime, and the compiler says which is which. Also drops yaak_core::AppContext, an earlier sketch of this that was never implemented.
54 lines
1.7 KiB
Rust
54 lines
1.7 KiB
Rust
use regex::Regex;
|
|
use tauri::{Runtime, Url, WebviewWindow};
|
|
use yaak_core::WorkspaceContext;
|
|
|
|
pub trait WorkspaceWindowTrait {
|
|
fn workspace_id(&self) -> Option<String>;
|
|
fn cookie_jar_id(&self) -> Option<String>;
|
|
fn environment_id(&self) -> Option<String>;
|
|
fn request_id(&self) -> Option<String>;
|
|
/// All four at once, from a single read of the window URL.
|
|
fn workspace_context(&self) -> WorkspaceContext;
|
|
}
|
|
|
|
impl<R: Runtime> WorkspaceWindowTrait for WebviewWindow<R> {
|
|
fn workspace_id(&self) -> Option<String> {
|
|
workspace_id_from_url(&self.url().unwrap())
|
|
}
|
|
|
|
fn cookie_jar_id(&self) -> Option<String> {
|
|
query_param(&self.url().unwrap(), "cookie_jar_id")
|
|
}
|
|
|
|
fn environment_id(&self) -> Option<String> {
|
|
query_param(&self.url().unwrap(), "environment_id")
|
|
}
|
|
|
|
fn request_id(&self) -> Option<String> {
|
|
query_param(&self.url().unwrap(), "request_id")
|
|
}
|
|
|
|
fn workspace_context(&self) -> WorkspaceContext {
|
|
let url = self.url().unwrap();
|
|
WorkspaceContext {
|
|
workspace_id: workspace_id_from_url(&url),
|
|
environment_id: query_param(&url, "environment_id"),
|
|
cookie_jar_id: query_param(&url, "cookie_jar_id"),
|
|
request_id: query_param(&url, "request_id"),
|
|
}
|
|
}
|
|
}
|
|
|
|
fn workspace_id_from_url(url: &Url) -> Option<String> {
|
|
let re = Regex::new(r"/workspaces/(?<id>\w+)").unwrap();
|
|
match re.captures(url.as_str()) {
|
|
None => None,
|
|
Some(captures) => captures.name("id").map(|c| c.as_str().to_string()),
|
|
}
|
|
}
|
|
|
|
fn query_param(url: &Url, key: &str) -> Option<String> {
|
|
let mut query_pairs = url.query_pairs();
|
|
query_pairs.find(|(k, _v)| k == key).map(|(_k, v)| v.to_string())
|
|
}
|