Skip to content

Commit

Permalink
fix(projects): fix route type & remove startTransition
Browse files Browse the repository at this point in the history
  • Loading branch information
Ohh-889 committed Sep 9, 2024
1 parent f3f570b commit fac368b
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 35 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"devDependencies": {
"@iconify/json": "2.2.245",
"@iconify/types": "2.0.0",
"@ohh-889/react-auto-route": "0.3.2",
"@ohh-889/react-auto-route": "0.3.4",
"@sa/scripts": "workspace:*",
"@sa/uno-preset": "workspace:*",
"@soybeanjs/eslint-config": "1.4.1",
Expand Down
3 changes: 2 additions & 1 deletion packages/simple-router/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ export type {
RouteLocationNamedRaw,
AfterEach,
BeforeEach,
RouteLocationNormalizedLoaded as Route
RouteLocationNormalizedLoaded,
Route
} from './types';
26 changes: 15 additions & 11 deletions packages/simple-router/src/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ class CreateRouter {
}: RouterOptions) {
this.matcher = new CreateRouterMatcher(initRoutes);

const initReactRoutes = initRoutes.map(route => {
return getReactRoutes(route);
});
const initReactRoutes = initRoutes.map(route => getReactRoutes(route));
this.reactRouter = historyCreatorMap[history](initReactRoutes, {
basename
});
Expand Down Expand Up @@ -92,14 +90,20 @@ class CreateRouter {

const flattenRoutes = routes.flat();

const reactRoutes = flattenRoutes.map(route => {
// Add route
this.#addRoute(route);
// Transform to react-router route
const reactRoute = this.getReactRoutes(route);
this.reactRoutes.push(reactRoute);
return reactRoute;
});
const reactRoutes = flattenRoutes
.map(route => {
const matcher = this.matcher.getRecordMatcher(route.name);
if (matcher) return null;
// Add route
this.#addRoute(route);
// Transform to react-router route
const reactRoute = this.getReactRoutes(route);
this.reactRoutes.push(reactRoute);
return reactRoute;
})
.filter(Boolean);
console.log(this.reactRouter);

// Add to react-router's routes
this.reactRouter.patchRoutes(parent, reactRoutes);
}
Expand Down
8 changes: 6 additions & 2 deletions packages/simple-router/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck

import type { ElegantConstRoute, RouteMeta } from '@ohh-889/react-auto-route';
import type { Router as RemixRouter } from '@remix-run/router';
import type { RouteObject } from 'react-router-dom';
import type { Router as RemixRouter } from '@remix-run/router';
import type { RouteRecordNormalized } from '../matcher/types';

/** Internal type for common properties among all kind of {@link RouteRecordRaw}. */
Expand Down Expand Up @@ -162,3 +162,7 @@ export interface RouteQueryAndHash {
* @internal
*/
export interface RouteLocationNamedRaw extends RouteQueryAndHash, LocationAsRelativeRaw, RouteLocationOptions {}

export interface Route extends RouteLocationNormalizedLoaded {
params: Record<string, string>;
}
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions src/hooks/common/route.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { useMatches } from 'react-router-dom';
import type { Route } from '@sa/simple-router';
import { router } from '@/router';

export function useRoute() {
export function useRoute(): Route {
const customRoute = useSyncExternalStore(router.subscribe, router.getSnapshot);

const matches = useMatches();

const params = matches[matches.length - 1].params;

const route = Object.assign(customRoute, { params });
const route = Object.assign(customRoute, { params }) as Route;

return route;
}
20 changes: 7 additions & 13 deletions src/layouts/modules/global-tab/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
/* eslint-disable */
import ClassNames from 'classnames';
import type BScroll from '@better-scroll/core';
import { PageTab } from '@sa/materials';
import {useUpdateEffect} from 'ahooks'
import { startTransition } from 'react';
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 { getActiveTabId, isTabRetain, removeTab, selectAllTabs,addTabByRoute } from '@/store/slice/tab';
import { addTabByRoute, getActiveTabId, isTabRetain, removeTab, selectAllTabs } from '@/store/slice/tab';
import {
getFullContent,
getIsMobile,
Expand Down Expand Up @@ -41,6 +39,7 @@ const GlobalTab = memo(() => {
const setBsScroll = (bscroll: BScroll) => {
bsScrollRef.current = bscroll;
};

function scrollByClientX(clientX: number) {
const { left, width } = bsWrapperSizeBounding.current;
const currentX = clientX - left;
Expand Down Expand Up @@ -82,17 +81,13 @@ const GlobalTab = memo(() => {
}

function handleCloseTab(tab: App.Global.Tab) {
dispatch(removeTab(tab.id));
dispatch(removeTab(tab.id));
}


function handleClickTab(tab: App.Global.Tab) {
startTransition(() => {
navigate(tab.fullPath);
});
navigate(tab.fullPath);
}


function getContextMenuDisabledKeys(tabId: string, index: number) {
const disabledKeys: App.Global.DropdownKey[] = [];
const isRetain = dispatch(isTabRetain(tabId));
Expand All @@ -117,10 +112,9 @@ const GlobalTab = memo(() => {
scrollToActiveTab();
}, [activeTabId]);


useEffect(() => {
dispatch(addTabByRoute(route))
},[route,dispatch])
dispatch(addTabByRoute(route));
}, [route, dispatch]);

useMount(() => {
if (bsWrapper.current) {
Expand Down

0 comments on commit fac368b

Please sign in to comment.