don't throw error

This commit is contained in:
goenning 2023-09-01 17:57:07 +01:00
parent 4966ef28ec
commit b1062af145
2 changed files with 13 additions and 7 deletions

View file

@ -1,7 +1,7 @@
'use client';
"use client";
import { init, trackEvent, type AptabaseOptions } from '@aptabase/web';
import { createContext, useContext, useEffect } from 'react';
import { init, trackEvent, type AptabaseOptions } from "@aptabase/web";
import { createContext, useContext, useEffect } from "react";
globalThis.__APTABASE_SDK_VERSION__ = `aptabase-react@${process.env.PKG_VERSION}`;
@ -27,15 +27,20 @@ export function AptabaseProvider({ appKey, options, children }: Props) {
init(appKey, options);
}, [appKey, options]);
return <AptabaseContext.Provider value={{ appKey, options }}>{children}</AptabaseContext.Provider>;
return (
<AptabaseContext.Provider value={{ appKey, options }}>
{children}
</AptabaseContext.Provider>
);
}
export function useAptabase(): AptabaseClient {
const ctx = useContext(AptabaseContext);
if (!ctx) {
throw new Error(
'useAptabase must be used within AptabaseProvider. Did you forget to wrap your app in <AptabaseProvider>?',
if (!ctx.appKey) {
console.error(
"Aptabase: useAptabase must be used within AptabaseProvider. Did you forget to wrap your app in <AptabaseProvider>?"
);
return { trackEvent: () => Promise.resolve() };
}
return { trackEvent };