Files
eranos/src/components/DeepLinkHandler.tsx
T
Chad Curtis 947bbfccb0 Add deep linking for ditto.pub Android App Links
- Add Android intent-filter with autoVerify for https://ditto.pub/*
- Create DeepLinkHandler component to handle appUrlOpen events at runtime
- Mount DeepLinkHandler in AppRouter inside BrowserRouter context
- Add public/.well-known/assetlinks.json with pub.ditto.app package and signing cert fingerprint
2026-03-16 01:39:56 -05:00

46 lines
1.1 KiB
TypeScript

import { useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import { Capacitor } from '@capacitor/core';
/**
* Handles deep links from ditto.pub on native platforms.
* Listens for appUrlOpen events and navigates to the corresponding route.
* Must be rendered inside a <BrowserRouter>.
*/
export function DeepLinkHandler() {
const navigate = useNavigate();
useEffect(() => {
if (!Capacitor.isNativePlatform()) return;
let cleanup: (() => void) | undefined;
async function setup() {
const { App } = await import('@capacitor/app');
// Handle URLs opened while the app is already running
const listener = await App.addListener('appUrlOpen', (event) => {
try {
const url = new URL(event.url);
const path = url.pathname + url.search + url.hash;
if (path) {
navigate(path);
}
} catch {
// Invalid URL, ignore
}
});
cleanup = () => listener.remove();
}
setup();
return () => {
cleanup?.();
};
}, [navigate]);
return null;
}