Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
yuvalotem1 committed Nov 6, 2023
1 parent 66dfe7f commit c8723c3
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 21 deletions.
1 change: 1 addition & 0 deletions packages/react/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const cjsPlugins = [
const isExternal = (id) => {
return (
id !== './FronteggProvider' &&
id !== './AlwaysRenderInProvider' &&
id !== './AuthorizedContent' &&
id !== './sdkVersion' &&
id !== './routerProxy' &&
Expand Down
41 changes: 41 additions & 0 deletions packages/react/src/AlwaysRenderInProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React, { FC } from 'react';
import { useAuthRoutes, CustomComponentRegister } from '@frontegg/react-hooks';
import { useQueryKeeper } from './queryKeeper';
import { FronteggApp } from '@frontegg/js';
import { FronteggThemeOptions } from '@frontegg/types';

export type HistoryObject = {
push: (path: string) => void;
replace: (path: string) => void;
};

type QueryKeeperWrapperProps = {
history: HistoryObject;
};

export const QueryKeeperWrapper: FC<QueryKeeperWrapperProps> = ({ history }) => {
const { signUpUrl } = useAuthRoutes();
useQueryKeeper({ routes: { signUpUrl }, history });
return <></>;
};

type AlwaysRenderInProviderProps = {
isExternalHistory: boolean;
app: FronteggApp;
themeOptions?: FronteggThemeOptions;
history: HistoryObject;
};

export const AlwaysRenderInProvider = ({
isExternalHistory,
app,
themeOptions,
history,
}: AlwaysRenderInProviderProps) => {
return (
<>
<CustomComponentRegister app={app} themeOptions={themeOptions} />
{!isExternalHistory && <QueryKeeperWrapper history={history} />}
</>
);
};
29 changes: 8 additions & 21 deletions packages/react/src/FronteggProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,46 +1,31 @@
import React, { FC, ReactNode, useCallback, useEffect, useMemo, useRef } from 'react';
import { initialize } from '@frontegg/js';
import { FronteggAppOptions } from '@frontegg/types';
import { FronteggStoreProvider, useAuthRoutes, CustomComponentRegister } from '@frontegg/react-hooks';
import { FronteggStoreProvider } from '@frontegg/react-hooks';
import { BrowserRouter, useHistory, UseHistory } from './routerProxy';
import { ContextHolder, RedirectOptions, FronteggFrameworks } from '@frontegg/rest-api';
import { AppHolder } from '@frontegg/js/AppHolder';
import { useQueryKeeper } from './queryKeeper';
import { isAuthRoute } from '@frontegg/redux-store';
import sdkVersion from './sdkVersion';
import ReactPkg from 'react/package.json';
import { AlwaysRenderInProvider, HistoryObject } from './AlwaysRenderInProvider';

export type FronteggProviderProps = FronteggAppOptions & {
appName?: string;
history?: UseHistory;
children?: ReactNode;
};

type HistoryObject = {
push: (path: string) => void;
replace: (path: string) => void;
};

type ConnectorProps = Omit<FronteggProviderProps, 'history'> & {
history: HistoryObject;
isExternalHistory?: boolean;
};

type QueryKeeperWrapperProps = {
history: HistoryObject;
};

export const ConnectorHistory: FC<Omit<ConnectorProps, 'history'>> = (props) => {
const history = useHistory();
return <Connector history={history} {...props} />;
};

export const QueryKeeperWrapper: FC<QueryKeeperWrapperProps> = ({ history }) => {
const { signUpUrl } = useAuthRoutes();
useQueryKeeper({ routes: { signUpUrl }, history });
return <></>;
};

export const Connector: FC<ConnectorProps> = ({ history, appName, isExternalHistory = false, ...props }) => {
const isSSR = typeof window === 'undefined';
const version = `@frontegg/react@${sdkVersion.version}`;
Expand Down Expand Up @@ -100,10 +85,12 @@ export const Connector: FC<ConnectorProps> = ({ history, appName, isExternalHist
<FronteggStoreProvider
{...({ ...props, app } as any)}
alwaysVisibleChildren={
<>
<CustomComponentRegister app={app} themeOptions={props.themeOptions} />
{!isExternalHistory && <QueryKeeperWrapper history={history} />}
</>
<AlwaysRenderInProvider
app={app}
themeOptions={props.themeOptions}
history={history}
isExternalHistory={isExternalHistory}
/>
}
/>
);
Expand Down

0 comments on commit c8723c3

Please sign in to comment.