Skip to content

Commit

Permalink
fix(core): 14176 - broken url routing for all pages except the default
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaIzr authored Jan 11, 2023
1 parent e8a5c17 commit 77fb483
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/core/app/features.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { appConfig } from '~core/app_config';
import { featureFlagsAtom } from '~core/shared_state';
import { createBooleanAtom } from '~utils/atoms';
import { store } from '~core/store/store';
import type { BackendFeature } from '~core/auth/types';
import type { ApiClient } from '~core/api_client';

Expand All @@ -15,11 +17,17 @@ export function loadFeatures(
return featuresResponse;
}

export const featuresWereSetAtom = createBooleanAtom(false);

export function setFeatures(value: BackendFeature[] | null) {
// FIXME: investigate proper app and user feature merge
const newFeatures = {};
value?.forEach((ft) => {
newFeatures[ft.name] = true;
});
featureFlagsAtom.set.dispatch(newFeatures);
store.dispatch([
featureFlagsAtom.set.dispatch(newFeatures),
featuresWereSetAtom.setTrue.dispatch(),
]);
}
2 changes: 1 addition & 1 deletion src/core/postInit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useEffect } from 'react';
import { appConfig } from '~core/app_config';
import { initMetricsOnce } from '~core/metrics';
import { currentRouteAtom } from '~core/router/atoms/currentRoute';
import { featureFlagsAtom, FeatureFlag } from '~core/shared_state';
import { featureFlagsAtom } from '~core/shared_state';
import type { AppFeatureType } from '~core/auth/types';

// Temporary solution till we refactor init and move it to proper place
Expand Down
1 change: 1 addition & 0 deletions src/core/router/atoms/currentRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { availableRoutesAtom } from './availableRoutes';
import { currentLocationAtom } from './currentLocation';
import type { AppRoute } from '../types';

// Describes current route, but cannot change it
export const currentRouteAtom = createAtom(
{
availableRoutesAtom,
Expand Down
5 changes: 4 additions & 1 deletion src/core/router/components/Routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CacheRoute } from 'react-router-cache-route';
import { Route } from 'react-router-dom';
import { useAction, useAtom } from '@reatom/react';
import { LoadingSpinner } from '~components/LoadingSpinner/LoadingSpinner';
import { featuresWereSetAtom } from '~core/app/features';
import { availableRoutesAtom } from '../atoms/availableRoutes';
import { getAbsoluteRoute } from '../getAbsoluteRoute';
import { showAboutForNewUsersAtom } from '../atoms/showAboutForNewUsers';
Expand Down Expand Up @@ -47,7 +48,9 @@ const RouterStateToReactRouter = ({

export function Routes() {
const [availableRoutes] = useAtom(availableRoutesAtom);
use404Redirect(availableRoutes);
const [featuresWereSet] = useAtom(featuresWereSetAtom);

use404Redirect(availableRoutes, featuresWereSet);

if (availableRoutes === null) {
return <FullScreenLoader />;
Expand Down
7 changes: 5 additions & 2 deletions src/core/router/components/user404Redirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { getAbsoluteRoute } from '../getAbsoluteRoute';
import { goTo } from '../goTo';
import type { AppRouterConfig } from '../types';

export function use404Redirect(availableRoutes: AppRouterConfig | null) {
export function use404Redirect(
availableRoutes: AppRouterConfig | null,
featuresWereSet: boolean,
) {
const location = useLocation();
useEffect(() => {
if (!availableRoutes) return;
Expand All @@ -16,7 +19,7 @@ export function use404Redirect(availableRoutes: AppRouterConfig | null) {
});
});

if (!match) {
if (!match && featuresWereSet) {
goTo(availableRoutes.defaultRoute);
}
}, [location, availableRoutes]);
Expand Down
2 changes: 1 addition & 1 deletion src/features/side_bar/components/SideBar/SideBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { AppNameAndIcon } from '../AppNameAndIcon/AppNameAndIcon';
import { SmallIconSlot } from '../SmallIconSlot/SmallIconSlot';
import { routeVisibilityChecker } from './routeVisibilityChecker';
import s from './SideBar.module.css';
import type { AvailableRoutesAtom, CurrentRouteAtom, AppRoute } from '~core/router';
import type { AvailableRoutesAtom, CurrentRouteAtom } from '~core/router';

const wasClosed = 'sidebarClosed';

Expand Down
2 changes: 1 addition & 1 deletion src/views/CommonView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function CommonView({
getAbsoluteRoute: (path: string) => string;
}>) {
const [featureFlags] = useAtom(featureFlagsAtom);
const [currentLanguage] = useAtom(currentLanguageAtom);
useAtom(currentLanguageAtom);
useFavicon(appConfig.faviconUrl);
useTabNameUpdate(appConfig.name);

Expand Down

0 comments on commit 77fb483

Please sign in to comment.