Skip to content

Commit 25bac01

Browse files
committed
feat: enhance deep link handling in useDeepLink hook and Tauri integration
- Updated the useDeepLink hook to register both plugin and event listeners for deep links, improving URL handling. - Integrated deep link event emission in the Tauri app's single instance plugin for better deep link management. - Added error handling for deep link registration to ensure robustness.
1 parent 5a0e11c commit 25bac01

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

src-tauri/src/lib.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ mod services;
88
mod state;
99
mod utils;
1010

11+
#[cfg(any(windows, target_os = "linux"))]
12+
use tauri_plugin_deep_link::DeepLinkExt;
13+
1114
use commands::{
1215
approve_execution, approve_patch, check_codex_version, close_session, delete_session_file,
1316
find_rollout_path_for_session, get_latest_session_id, get_running_sessions, get_session_files,
@@ -31,14 +34,21 @@ use filesystem::{
3134
};
3235
use mcp::{add_mcp_server, delete_mcp_server, read_mcp_servers};
3336
use state::CodexState;
37+
use tauri::Manager;
3438

3539
#[cfg_attr(mobile, tauri::mobile_entry_point)]
3640
pub fn run() {
3741
let mut builder = tauri::Builder::default();
3842
#[cfg(desktop)]
3943
{
40-
builder = builder.plugin(tauri_plugin_single_instance::init(|_app, argv, _cwd| {
44+
builder = builder.plugin(tauri_plugin_single_instance::init(|app, argv, _cwd| {
4145
println!("a new app instance was opened with {argv:?} and the deep link event was already triggered");
46+
47+
if let Some(deep_link_url) = argv.iter().find(|arg| arg.starts_with("codexia://")) {
48+
if let Err(err) = app.emit_all("deep-link-received", deep_link_url.clone()) {
49+
eprintln!("Failed to emit deep link event: {err}");
50+
}
51+
}
4252
}));
4353
}
4454

@@ -111,7 +121,14 @@ pub fn run() {
111121
add_or_update_model_provider,
112122
ensure_default_providers,
113123
])
114-
.setup(|_app| {
124+
.setup(|app| {
125+
#[cfg(any(windows, target_os = "linux"))]
126+
{
127+
if let Err(err) = app.deep_link().register_all() {
128+
eprintln!("Failed to register deep link schemes: {err}");
129+
}
130+
}
131+
115132
tauri::async_runtime::spawn(async {
116133
let _ = ensure_default_providers().await;
117134
});

src/hooks/useDeepLink.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import supabase from "@/lib/supabase";
22
import { ensureProfileRecord, mapProfileRow } from "@/lib/profile";
3-
import type { UnlistenFn } from "@tauri-apps/api/event";
3+
import { listen, type UnlistenFn } from "@tauri-apps/api/event";
44
import { getCurrent, onOpenUrl } from "@tauri-apps/plugin-deep-link";
55
import { useEffect, useState } from "react";
66
import { useNavigate } from "react-router-dom";
@@ -74,16 +74,32 @@ export const useDeepLink = () => {
7474
console.error("Failed to read current deep link", err);
7575
}
7676

77-
let unlisten: UnlistenFn | null = null;
77+
let pluginUnlisten: UnlistenFn | null = null;
7878
try {
79-
unlisten = await onOpenUrl((incoming) => {
79+
pluginUnlisten = await onOpenUrl((incoming) => {
8080
void handleUrl(incoming);
8181
});
8282
} catch (err) {
8383
console.error("Failed to register deep link handler", err);
8484
}
8585

86-
return unlisten;
86+
let eventUnlisten: UnlistenFn | null = null;
87+
try {
88+
eventUnlisten = await listen<string>("deep-link-received", (event) => {
89+
void handleUrl(event.payload);
90+
});
91+
} catch (err) {
92+
console.error("Failed to register native deep link listener", err);
93+
}
94+
95+
if (!pluginUnlisten && !eventUnlisten) {
96+
return null;
97+
}
98+
99+
return () => {
100+
pluginUnlisten?.();
101+
eventUnlisten?.();
102+
};
87103
};
88104

89105
let dispose: UnlistenFn | null = null;

0 commit comments

Comments
 (0)