client/apps/tauri/src/deeplinkHandler.tsx
Alois 25c7ee7c26
Some checks failed
/ build-web (push) Failing after 4m33s
/ build-desktop (linux) (push) Failing after 4m36s
/ build-mobile (push) Failing after 7m7s
/ release (push) Has been skipped
(fix): actually migrate all the imports from tensamin/ui to methanium/ui
(feat): update legal loading screen
2026-07-25 18:12:54 +02:00

72 lines
1.4 KiB
TypeScript

import {
createContext,
useContext,
useEffect,
useState,
type ReactNode,
} from "react";
import { getCurrent, onOpenUrl } from "@tauri-apps/plugin-deep-link";
import { isTauri } from "@tauri-apps/api/core";
import { useIsMobile } from "@methanium/ui";
type DeeplinkContextValue = {
deeplinks: readonly string[];
};
export const deeplinkContext = createContext<DeeplinkContextValue | undefined>(
undefined,
);
export function useDeeplinks() {
const value = useContext(deeplinkContext);
if (!value) {
throw new Error("useDeeplinks must be used within DeeplinkProvider");
}
return value;
}
export default function DeeplinkProvider({
children,
}: {
children: ReactNode;
}) {
const [deeplinks, setDeeplinks] = useState<string[]>([]);
const isMobile = useIsMobile();
useEffect(() => {
if (!isTauri() || !isMobile) return;
let mounted = true;
let unlisten: (() => void) | undefined;
getCurrent().then((links) => {
if (mounted && links) {
setDeeplinks((prev) => [...prev, ...links]);
}
});
onOpenUrl((links) => {
if (mounted) {
setDeeplinks((prev) => [...prev, ...links]);
}
}).then((fn) => {
unlisten = fn;
});
return () => {
mounted = false;
unlisten?.();
};
}, [isMobile]);
return (
<deeplinkContext.Provider
value={{
deeplinks,
}}
>
{children}
</deeplinkContext.Provider>
);
}