Skip to content

Commit

Permalink
chore: review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
steveoh committed Jun 13, 2024
1 parent 1339a14 commit a508a05
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 35 deletions.
12 changes: 8 additions & 4 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { BootstrapDartboard as FindAddress } from '@ugrc/dart-board';
import Sherlock, { WebApiProvider } from '@ugrc/sherlock';
import { logEvent } from 'firebase/analytics';
import PropTypes from 'prop-types';
import { useCallback, useMemo, useState } from 'react';
import { useCallback, useEffect, useState } from 'react';
import { ErrorBoundary } from 'react-error-boundary';
import { Button, Card, Collapse } from 'reactstrap';
import Header from './components/Header/Header';
Expand Down Expand Up @@ -37,9 +37,13 @@ ErrorFallback.propTypes = {
export default function App() {
const app = useFirebaseApp();
const analytics = useAnalytics();
useMemo(async () => {
const { getPerformance } = await import('firebase/performance');
return getPerformance(app);
useEffect(() => {
async function initPerformance() {
const { getPerformance } = await import('firebase/performance');

return getPerformance(app);
}
initPerformance();
}, [app]);
const [zoomToGraphic, setZoomToGraphic] = useState({
zoomToGraphic: {
Expand Down
6 changes: 4 additions & 2 deletions src/components/firebase/AnalyticsProvider.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getAnalytics } from 'firebase/analytics';
import PropTypes from 'prop-types';
import { createContext, useContext } from 'react';
import { useFirebaseApp } from './FirebaseAppProvider';
import { useFirebaseApp } from './FirebaseAppProvider.jsx';

const AnalyticsSdkContext = createContext(undefined);

Expand All @@ -20,4 +20,6 @@ AnalyticsProvider.propTypes = {
sdk: PropTypes.object,
};

export const useAnalytics = () => useContext(AnalyticsSdkContext);
export function useAnalytics() {
return useContext(AnalyticsSdkContext);
}
36 changes: 7 additions & 29 deletions src/components/firebase/FirebaseAppProvider.jsx
Original file line number Diff line number Diff line change
@@ -1,41 +1,19 @@
import { getApps, initializeApp, registerVersion } from 'firebase/app';
import { initializeApp, registerVersion } from 'firebase/app';
import PropTypes from 'prop-types';
import { createContext, useContext, useMemo, version } from 'react';

const DEFAULT_APP_NAME = '[DEFAULT]';
const FirebaseAppContext = createContext(undefined);

const shallowEq = (a, b) => a === b || [...Object.keys(a), ...Object.keys(b)].every((key) => a[key] === b[key]);

export function FirebaseAppProvider(props) {
const { firebaseConfig, appName } = props;

const { firebaseConfig } = props;
const firebaseApp = useMemo(() => {
if (props.firebaseApp) {
return props.firebaseApp;
}

const existingApp = getApps().find((app) => app.name === (appName || DEFAULT_APP_NAME));
if (existingApp) {
if (firebaseConfig && shallowEq(existingApp.options, firebaseConfig)) {
return existingApp;
} else {
throw new Error(
`Does not match the options already provided to the ${appName || 'default'} firebase app instance, give this new instance a different appName.`,
);
}
} else {
if (!firebaseConfig) {
throw new Error('No firebaseConfig provided');
}

registerVersion('react', version || 'unknown');
// TODO! add app package.json version
// registerVersion('app', appVersion || 'unknown');
registerVersion('react', version || 'unknown');
// TODO! add app package.json version
// registerVersion('app', appVersion || 'unknown');

return initializeApp(firebaseConfig, appName);
}
}, [props.firebaseApp, appName, firebaseConfig]);
return initializeApp(firebaseConfig, DEFAULT_APP_NAME);
}, [firebaseConfig]);

return <FirebaseAppContext.Provider value={firebaseApp} {...props} />;
}
Expand Down

0 comments on commit a508a05

Please sign in to comment.