@@ -14,26 +14,25 @@ interface AppUrlParams {
1414 visualRefresh : boolean ;
1515 motionDisabled : boolean ;
1616 appLayoutWidget : boolean ;
17- mode ? : Mode ;
17+ mode : Mode ;
1818}
1919
2020export interface AppContextType < T = unknown > {
21- mode : Mode ;
2221 pageId ?: string ;
2322 urlParams : AppUrlParams & T ;
2423 setUrlParams : ( newParams : Partial < AppUrlParams & T > ) => void ;
2524 setMode : ( newMode : Mode ) => void ;
2625}
2726
2827const appContextDefaults : AppContextType = {
29- mode : Mode . Light ,
3028 pageId : undefined ,
3129 urlParams : {
3230 density : Density . Comfortable ,
3331 direction : 'ltr' ,
3432 visualRefresh : THEME === 'default' ,
3533 motionDisabled : false ,
3634 appLayoutWidget : false ,
35+ mode : Mode . Light ,
3736 } ,
3837 setMode : ( ) => { } ,
3938 setUrlParams : ( ) => { } ,
@@ -74,34 +73,21 @@ function formatQuery(params: AppUrlParams) {
7473export function AppContextProvider ( { children } : { children : React . ReactNode } ) {
7574 const history = useHistory ( ) ;
7675 const location = useLocation ( ) ;
77- const matchWithVisualMode = useRouteMatch < { mode : Mode ; pageId : string } > ( '/:mode(light|dark)/:pageId*' ) ;
78- const matchWithoutVisualMode = useRouteMatch < { pageId : string } > ( '/:pageId*' ) ;
79- const pageId = ( matchWithVisualMode ?? matchWithoutVisualMode ) ?. params . pageId ?? undefined ;
76+ const pageId = useRouteMatch < { pageId : string } > ( '/:pageId*' ) ?. params . pageId ?? undefined ;
8077 const urlParams = parseQuery ( location . search ) as AppUrlParams ;
81- const mode = matchWithVisualMode ?. params . mode ?? urlParams . mode ?? Mode . Light ;
8278
8379 function setUrlParams ( newParams : Partial < AppUrlParams > ) {
8480 const formattedQuery = formatQuery ( { ...urlParams , ...newParams } ) ;
85- if ( matchWithVisualMode ) {
86- const pathname = [ matchWithVisualMode . params . mode , pageId ] . filter ( segment => ! ! segment ) . join ( '/' ) + '/' ;
87- history . replace ( `/${ pathname } ${ formatQuery ( { ...urlParams , ...newParams } ) } ` ) ;
88- } else {
89- const newUrl = pageId ? `/${ pageId } ${ formattedQuery } ` : formattedQuery ;
90- history . replace ( newUrl ) ;
91- }
81+ const newUrl = pageId ? `/${ pageId } ${ formattedQuery } ` : formattedQuery ;
82+ history . replace ( newUrl ) ;
9283 }
9384
9485 function updateMode ( newMode : Mode ) {
95- if ( matchWithVisualMode ) {
96- const pathname = [ newMode , pageId ] . filter ( segment => ! ! segment ) . join ( '/' ) + '/' ;
97- history . replace ( '/' + pathname + location . search + location . hash ) ;
98- } else {
99- setUrlParams ( { mode : newMode } ) ;
100- }
86+ setUrlParams ( { mode : newMode } ) ;
10187 }
10288
10389 return (
104- < AppContext . Provider value = { { mode , pageId, urlParams, setUrlParams : setUrlParams , setMode : updateMode } } >
90+ < AppContext . Provider value = { { pageId, urlParams, setUrlParams : setUrlParams , setMode : updateMode } } >
10591 { children }
10692 </ AppContext . Provider >
10793 ) ;
0 commit comments