client/packages/mtp/src/context.tsx
Alois 0a304e44f2
All checks were successful
Dependency builds / Build web (pull_request) Has been skipped
Dependency builds / Build desktop (pull_request) Has been skipped
Dependency builds / Test native MTP (pull_request) Has been skipped
Dependency builds / Build mobile (pull_request) Has been skipped
/ build-web (push) Successful in 6m14s
/ build-desktop (linux) (push) Successful in 11m36s
/ build-mobile (push) Successful in 24m26s
/ release (push) Successful in 1m39s
feat(mtp): move useful stuff over to mtp directly
2026-08-27 23:30:33 +02:00

41 lines
1.2 KiB
TypeScript

import { type ReactNode, useContext, useEffect, useState } from "react";
import { isTauri } from "@tauri-apps/api/core";
import { MTPClient } from "mtp";
import { BrowserProvider } from "./browser";
import { MTPContext, type MTPContextType } from "./mtpContext";
import { TauriProvider } from "./tauri";
export function Provider(props: {
children: ReactNode;
blockConnection?: boolean;
}) {
if (isTauri()) return <TauriProvider {...props} />;
return <BrowserWasmProvider {...props} />;
}
function BrowserWasmProvider(props: {
children: ReactNode;
blockConnection?: boolean;
}) {
const [wasmReady, setWasmReady] = useState(false);
const [wasmError, setWasmError] = useState<unknown>();
useEffect(() => {
let active = true;
void MTPClient.init().then(
() => active && setWasmReady(true),
(error: unknown) => active && setWasmError(() => error),
);
return () => {
active = false;
};
}, []);
if (wasmError) throw wasmError;
return wasmReady ? <BrowserProvider {...props} /> : null;
}
export function useMTP(): MTPContextType {
const context = useContext(MTPContext);
if (!context) throw new Error("useMTP must be used within an MTPProvider");
return context;
}