Files
myeasycms-v2/packages/monitoring/baselime/src/components/provider.tsx
giancarlo 3b3f50a714 Refactor Baselime monitoring provider for optional API key
The code was adjusted to not require an API key for the Baselime monitoring provider. Now, a warning will be logged if no key is provided. Dependencies in pnpm-lock.yaml and error message in baselime-server-monitoring.service.ts were also updated accordingly to reflect this change, primarily to utilize the variable 'NEXT_PUBLIC_BASELIME_KEY' instead of 'BASELIME_API_KEY'.
2024-04-25 13:32:22 +07:00

51 lines
1.2 KiB
TypeScript

import { useRef } from 'react';
import { BaselimeRum } from '@baselime/react-rum';
import { MonitoringContext } from '@kit/monitoring-core';
import { useBaselime } from '../hooks/use-baselime';
export function BaselimeProvider({
children,
apiKey,
enableWebVitals,
ErrorPage,
}: React.PropsWithChildren<{
apiKey?: string;
enableWebVitals?: boolean;
ErrorPage?: React.ReactElement;
}>) {
const key = apiKey ?? process.env.NEXT_PUBLIC_BASELIME_KEY ?? '';
if (!key) {
console.warn(
'You configured Baselime as monitoring provider but did not provide a key. ' +
'Please provide a key to enable monitoring with Baselime using the variable NEXT_PUBLIC_BASELIME_KEY.',
);
return children;
}
return (
<BaselimeRum
apiKey={key}
enableWebVitals={enableWebVitals}
fallback={ErrorPage ?? null}
>
<MonitoringProvider>{children}</MonitoringProvider>
</BaselimeRum>
);
}
function MonitoringProvider(props: React.PropsWithChildren) {
const service = useBaselime();
const provider = useRef(service);
return (
<MonitoringContext.Provider value={provider.current}>
{props.children}
</MonitoringContext.Provider>
);
}