Skip to content

Commit

Permalink
fix(projects): Fixed redirection when switching roles & init tab no c…
Browse files Browse the repository at this point in the history
…ache
  • Loading branch information
mufeng889 committed Sep 12, 2024
1 parent 32628df commit 58d1feb
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 17 deletions.
2 changes: 2 additions & 0 deletions packages/simple-router/src/router.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck
import { createBrowserRouter, createHashRouter, createMemoryRouter } from 'react-router-dom';
import type { Location, RouteObject } from 'react-router-dom';
import type { ElegantConstRoute } from '@ohh-889/react-auto-route';
Expand Down
6 changes: 5 additions & 1 deletion src/hooks/common/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ export function useLogin() {

if (userName) {
await dispatch(initAuthRoute());
await redirectFromLogin(redirect);

if (redirect) {
await redirectFromLogin(redirect);
}

window.$notification?.success({
message: t('page.login.common.loginSuccess'),
description: t('page.login.common.welcomeBack', { userName })
Expand Down
6 changes: 4 additions & 2 deletions src/hooks/common/routerPush.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ interface RouterPushOptions {
export function useRouterPush() {
const router = useRouter();

const route = useRoute();

const [searchParams] = useSearchParams();

const routerPush = router.push;
Expand Down Expand Up @@ -77,7 +79,7 @@ export function useRouterPush() {

const options: RouterPushOptions = {};

const redirect = redirectUrl || router.currentRoute.fullPath;
const redirect = redirectUrl || route.fullPath;

options.query = {
redirect
Expand All @@ -92,7 +94,7 @@ export function useRouterPush() {
* @param module
*/
async function toggleLoginModule(module: UnionKey.LoginModule) {
const query = router.currentRoute.query as Record<string, string>;
const query = route.query as Record<string, string>;

return routerPushByKey(`login_${module}`, { query });
}
Expand Down
6 changes: 5 additions & 1 deletion src/layouts/modules/global-tab/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useUpdateEffect } from 'ahooks';
import DarkModeContainer from '@/components/stateless/common/DarkModeContainer';
import BetterScroll from '@/components/stateless/custom/BetterScroll';
import { getDarkMode, getThemeSettings } from '@/store/slice/theme';
import { addTabByRoute, getActiveTabId, isTabRetain, removeTab, selectAllTabs } from '@/store/slice/tab';
import { addTabByRoute, getActiveTabId, initTabStore, isTabRetain, removeTab, selectAllTabs } from '@/store/slice/tab';
import {
getFullContent,
getIsMobile,
Expand Down Expand Up @@ -126,6 +126,10 @@ const GlobalTab = memo(() => {
}
});

useLayoutEffect(() => {
dispatch(initTabStore());
}, [dispatch]);

return (
<DarkModeContainer className="size-full flex-y-center px-16px shadow-tab">
<div
Expand Down
4 changes: 1 addition & 3 deletions src/pages/function/toggle-auth/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ export function Component() {

await toLogin({ userName: account.userName, password: account.password }, false);

const routeNames = router.getAllRouteNames();

dispatch(initTabStore(routeNames));
dispatch(initTabStore());

endLoading();

Expand Down
20 changes: 10 additions & 10 deletions src/store/slice/tab/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,15 @@ export const selectAllTabs = createSelector([getTabs, getHomeTab], (tabs, homeTa
*
* @param currentRoute Current route
*/
export const initTabStore =
(routeNames: string[]): AppThunk =>
(dispatch, getState) => {
const storageTabs = localStg.get('globalTabs');
const themeSettings = getThemeSettings(getState());
if (themeSettings.tab.cache && storageTabs) {
const tabs = extractTabsByAllRoutes(routeNames, storageTabs);
dispatch(setTabs(tabs));
}
};
export const initTabStore = (): AppThunk => (dispatch, getState) => {
const storageTabs = localStg.get('globalTabs');
const themeSettings = getThemeSettings(getState());

if (themeSettings.tab.cache && storageTabs) {
const tabs = extractTabsByAllRoutes(router.getAllRouteNames(), storageTabs);
dispatch(setTabs(tabs));
}
};

/**
* Remove tab
Expand Down Expand Up @@ -201,6 +200,7 @@ export const cacheTabs = (): AppThunk => (_, getState) => {
const themeSettings = getThemeSettings(getState());
if (!themeSettings.tab.cache) return;
const tabs = getTabs(getState());

localStg.set('globalTabs', tabs);
};

Expand Down

0 comments on commit 58d1feb

Please sign in to comment.