import template with deep link
This commit is contained in:
+35
-4
@@ -1,5 +1,10 @@
|
||||
import { emitTo } from '@tauri-apps/api/event'
|
||||
import { WebviewWindow } from '@tauri-apps/api/webviewWindow'
|
||||
import { ADD_TEMPLATE, type AddTemplatePayload } from './events'
|
||||
import { openWindow } from './window'
|
||||
|
||||
const TEMPLATES_WINDOW = 'Templates'
|
||||
|
||||
export function getDeepLinkUrl(url: string) {
|
||||
let cleanedUrl = url.replace('rclone:', '')
|
||||
while (cleanedUrl.startsWith('/')) {
|
||||
@@ -8,12 +13,38 @@ export function getDeepLinkUrl(url: string) {
|
||||
return cleanedUrl
|
||||
}
|
||||
|
||||
export function handleDeepLinkUrl(url: string) {
|
||||
export async function handleDeepLinkUrl(url: string) {
|
||||
console.log('deep link url', url)
|
||||
|
||||
const domain = url.split('/')[0]
|
||||
// `add-template?cmd=…` — split the query off before matching the route, and tolerate a
|
||||
// trailing slash (`add-template/?cmd=…`) that some platforms add.
|
||||
const queryIndex = url.indexOf('?')
|
||||
const route = queryIndex === -1 ? url : url.slice(0, queryIndex)
|
||||
const params = new URLSearchParams(queryIndex === -1 ? '' : url.slice(queryIndex + 1))
|
||||
const domain = route.split('/')[0]
|
||||
|
||||
if (domain === 'add-template') {
|
||||
return openWindow({ name: 'Templates', url: '/templates?action=add' })
|
||||
try {
|
||||
if (domain === 'add-template') {
|
||||
const payload: AddTemplatePayload = {
|
||||
cmd: params.get('cmd')?.trim() || undefined,
|
||||
name: params.get('name')?.trim() || undefined,
|
||||
}
|
||||
|
||||
// open_window only focuses an existing window (it never re-navigates), so a live
|
||||
// Templates window gets the payload over the event bus instead of the URL.
|
||||
const existing = await WebviewWindow.getByLabel(TEMPLATES_WINDOW)
|
||||
if (existing) {
|
||||
await emitTo(TEMPLATES_WINDOW, ADD_TEMPLATE, payload)
|
||||
await openWindow({ name: TEMPLATES_WINDOW, url: '/templates' })
|
||||
return
|
||||
}
|
||||
|
||||
const search = new URLSearchParams({ action: 'add' })
|
||||
if (payload.cmd) search.set('cmd', payload.cmd)
|
||||
if (payload.name) search.set('name', payload.name)
|
||||
await openWindow({ name: TEMPLATES_WINDOW, url: `/templates?${search}` })
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[handleDeepLinkUrl] failed to handle deep link', error)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,16 @@ export interface RestartRclonePayload {
|
||||
syncConfigLinkTarget?: string | null
|
||||
}
|
||||
|
||||
// Deep-link payload forwarded from the main window to an already-open 'Templates' window
|
||||
// (lib/deep.ts → src/pages/Templates.tsx). Not part of AppEventPayload: it targets a specific
|
||||
// window via emitTo, not the main-window listeners.
|
||||
export const ADD_TEMPLATE = 'add-template'
|
||||
|
||||
export interface AddTemplatePayload {
|
||||
cmd?: string
|
||||
name?: string
|
||||
}
|
||||
|
||||
export type AppEventPayload = {
|
||||
[CLOSE_APP]: undefined
|
||||
[RELAUNCH_APP]: undefined
|
||||
|
||||
Reference in New Issue
Block a user