1- ' use client' ;
1+ " use client" ;
22
3- import React , { createContext , useContext , useState , useEffect , ReactNode } from ' react' ;
4- import { useRouter , usePathname , useSearchParams } from ' next/navigation' ;
3+ import React , { createContext , useContext , useState , useEffect , ReactNode } from " react" ;
4+ import { useRouter , usePathname , useSearchParams } from " next/navigation" ;
55import { View } from "@/types/interface" ;
66
77interface URLParamsState {
@@ -11,6 +11,7 @@ interface URLParamsState {
1111 viewPickerHidden : boolean ;
1212 featurePickerHidden : boolean ;
1313 file ?: string ;
14+ codeLayout : "sidebar" | "tabs" ;
1415}
1516
1617interface URLParamsContextType extends URLParamsState {
@@ -20,6 +21,7 @@ interface URLParamsContextType extends URLParamsState {
2021 setViewPickerHidden : ( disabled : boolean ) => void ;
2122 setFeaturePickerHidden : ( disabled : boolean ) => void ;
2223 setCodeFile : ( fileName : string ) => void ;
24+ setCodeLayout : ( layout : "sidebar" | "tabs" ) => void ;
2325}
2426
2527const URLParamsContext = createContext < URLParamsContextType | undefined > ( undefined ) ;
@@ -40,6 +42,7 @@ export function URLParamsProvider({ children }: URLParamsProviderProps) {
4042 frameworkPickerHidden : searchParams . get ( "frameworkPicker" ) === "false" ,
4143 viewPickerHidden : searchParams . get ( "viewPicker" ) === "false" ,
4244 featurePickerHidden : searchParams . get ( "featurePicker" ) === "false" ,
45+ codeLayout : ( searchParams . get ( "codeLayout" ) as "sidebar" | "tabs" ) || "sidebar" ,
4346 } ) ) ;
4447
4548 // Update URL when state changes
@@ -90,8 +93,22 @@ export function URLParamsProvider({ children }: URLParamsProviderProps) {
9093 }
9194 }
9295
96+ // Update codeLayout param
97+ if ( newState . codeLayout !== undefined ) {
98+ if ( newState . codeLayout === "sidebar" ) {
99+ params . delete ( "codeLayout" ) ;
100+ } else {
101+ params . set ( "codeLayout" , newState . codeLayout ) ;
102+ }
103+ }
104+
105+ // Update file param
106+ if ( newState . file !== undefined ) {
107+ params . set ( "file" , newState . file ) ;
108+ }
109+
93110 const queryString = params . toString ( ) ;
94- router . push ( pathname + ( queryString ? '?' + queryString : '' ) ) ;
111+ router . push ( pathname + ( queryString ? "?" + queryString : "" ) ) ;
95112 } ;
96113
97114 // Sync state with URL changes (e.g., browser back/forward)
@@ -102,6 +119,8 @@ export function URLParamsProvider({ children }: URLParamsProviderProps) {
102119 frameworkPickerHidden : searchParams . get ( "frameworkPicker" ) === "false" ,
103120 viewPickerHidden : searchParams . get ( "viewPicker" ) === "false" ,
104121 featurePickerHidden : searchParams . get ( "featurePicker" ) === "false" ,
122+ file : searchParams . get ( "file" ) || undefined ,
123+ codeLayout : ( searchParams . get ( "codeLayout" ) as "sidebar" | "tabs" ) || "sidebar" ,
105124 } ;
106125
107126 setState ( newState ) ;
@@ -144,6 +163,12 @@ export function URLParamsProvider({ children }: URLParamsProviderProps) {
144163 updateURL ( { file : fileName } ) ;
145164 } ;
146165
166+ const setCodeLayout = ( codeLayout : "sidebar" | "tabs" ) => {
167+ const newState = { ...state , codeLayout } ;
168+ setState ( newState ) ;
169+ updateURL ( { codeLayout } ) ;
170+ } ;
171+
147172 const contextValue : URLParamsContextType = {
148173 ...state ,
149174 setView,
@@ -152,19 +177,16 @@ export function URLParamsProvider({ children }: URLParamsProviderProps) {
152177 setViewPickerHidden,
153178 setFeaturePickerHidden,
154179 setCodeFile,
180+ setCodeLayout,
155181 } ;
156182
157- return (
158- < URLParamsContext . Provider value = { contextValue } >
159- { children }
160- </ URLParamsContext . Provider >
161- ) ;
183+ return < URLParamsContext . Provider value = { contextValue } > { children } </ URLParamsContext . Provider > ;
162184}
163185
164186export function useURLParams ( ) : URLParamsContextType {
165187 const context = useContext ( URLParamsContext ) ;
166188 if ( context === undefined ) {
167- throw new Error ( ' useURLParams must be used within a URLParamsProvider' ) ;
189+ throw new Error ( " useURLParams must be used within a URLParamsProvider" ) ;
168190 }
169191 return context ;
170192}
0 commit comments