1 import type { FC, PropsWithChildren } from 'react';
2 import { createContext, useContext } from 'react';
4 import { useApi } from '@proton/components';
5 import useInstance from '@proton/hooks/useInstance';
6 import type { MaybeNull } from '@proton/pass/types';
7 import { getSilentApi } from '@proton/shared/lib/api/helpers/customConfig';
9 import { createPassBridge } from '.';
10 import type { PassBridge } from './types';
12 export const PassBridgeContext = createContext<MaybeNull<PassBridge>>(null);
14 export const PassBridgeProvider: FC<PropsWithChildren> = ({ children }) => {
16 const silentAPI = getSilentApi(api);
17 const bridge = useInstance(() => createPassBridge(silentAPI));
19 return <PassBridgeContext.Provider value={bridge}>{children}</PassBridgeContext.Provider>;
22 export const usePassBridge = () => {
23 const passBridge = useContext(PassBridgeContext);
24 if (!passBridge) throw new Error('PassBridge was not instantiated');