@grafana/faro-react
Version:
Faro package that enables easier integration in projects built with React.
29 lines • 1.83 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
import { useEffect, useMemo, useRef } from 'react';
import { EVENT_ROUTE_CHANGE, globalObject } from '@grafana/faro-web-sdk';
import { api } from '../../dependencies';
import { NavigationType } from '../types';
import { createRoutesFromChildren, isInitialized, Routes, useLocation, useNavigationType } from './routerDependencies';
import { getRouteFromLocation } from './utils';
export function FaroRoutes(props) {
var _a;
const location = useLocation === null || useLocation === void 0 ? void 0 : useLocation();
const navigationType = useNavigationType === null || useNavigationType === void 0 ? void 0 : useNavigationType();
const routes = useMemo(() => { var _a; return (_a = createRoutesFromChildren === null || createRoutesFromChildren === void 0 ? void 0 : createRoutesFromChildren(props.children)) !== null && _a !== void 0 ? _a : []; }, [props.children]);
const lastRouteRef = useRef({});
useEffect(() => {
var _a, _b;
if (isInitialized && (navigationType === NavigationType.Push || navigationType === NavigationType.Pop)) {
const route = getRouteFromLocation(routes, location);
const url = (_a = globalObject.location) === null || _a === void 0 ? void 0 : _a.href;
api.pushEvent(EVENT_ROUTE_CHANGE, Object.assign({ toRoute: route, toUrl: (_b = globalObject.location) === null || _b === void 0 ? void 0 : _b.href }, lastRouteRef.current));
lastRouteRef.current = {
fromRoute: route,
fromUrl: url,
};
}
}, [location, navigationType, routes]);
const ActualRoutes = (_a = props.routesComponent) !== null && _a !== void 0 ? _a : Routes;
return _jsx(ActualRoutes, Object.assign({}, props));
}
//# sourceMappingURL=FaroRoutes.js.map