diff --git a/src/components/ScrollRoutes.tsx b/src/components/ScrollRoutes.tsx deleted file mode 100644 index 61bafdf..0000000 --- a/src/components/ScrollRoutes.tsx +++ /dev/null @@ -1,23 +0,0 @@ -import React from "react" -import { Routes, useLocation, type RoutesProps } from "react-router-dom" - -export interface ScrollRoutesProps extends RoutesProps { - x?: number - y?: number -} - -const ScrollRoutes: React.FC = ({ - x = 0, - y = 0, - ...routesProps -}) => { - const { pathname } = useLocation() - - React.useEffect(() => { - window.scroll(x, y) - }, [pathname, x, y]) - - return -} - -export default ScrollRoutes diff --git a/src/components/index.ts b/src/components/index.ts index 737b769..b54e67c 100644 --- a/src/components/index.ts +++ b/src/components/index.ts @@ -14,8 +14,6 @@ export * from "./ItemizedList" export { default as ItemizedList } from "./ItemizedList" export * from "./OrderedGrid" export { default as OrderedGrid } from "./OrderedGrid" -export * from "./ScrollRoutes" -export { default as ScrollRoutes } from "./ScrollRoutes" export * from "./SyncError" export { default as SyncError } from "./SyncError" export * from "./TablePagination" diff --git a/src/components/page/Page.tsx b/src/components/page/Page.tsx index d12285a..9d82274 100644 --- a/src/components/page/Page.tsx +++ b/src/components/page/Page.tsx @@ -1,4 +1,4 @@ -import { Children } from "react" +import { Children, useEffect } from "react" import { useLocation, type Location } from "react-router-dom" import { @@ -15,6 +15,7 @@ export type PageState = { index?: number props: NotificationProps }> + scroll: { x: number; y: number } } export interface PageProps< @@ -30,7 +31,15 @@ const Page = < children, session, }: PageProps): JSX.Element => { - const { state } = useLocation() as Location + const { state } = useLocation() as Location> + + let { scroll, notifications } = state || {} + scroll = scroll || { x: 0, y: 0 } + notifications = notifications || [] + + useEffect(() => { + window.scroll(scroll.x, scroll.y) + }, [scroll.x, scroll.y]) return ( <> @@ -41,32 +50,21 @@ const Page = < : (children as UseSessionChildrenFunction)(metadata) } - const childrenArray = Children.toArray(children) + if (notifications.length) { + const childrenArray = Children.toArray(children) + + notifications.forEach((notification, index) => { + childrenArray.splice( + notification.index ?? index, + 0, + , + ) + }) - if ( - typeof state === "object" && - state !== null && - "notifications" in state && - Array.isArray(state.notifications) && - state.notifications.every( - (notification: unknown) => - typeof notification === "object" && - notification !== null && - "props" in notification, - ) - ) { - ;(state.notifications as PageState["notifications"]).forEach( - (notification, index) => { - childrenArray.splice( - notification.index ?? index, - 0, - , - ) - }, - ) + return childrenArray } - return childrenArray + return children }, session)} )