@@ -5,61 +5,23 @@ import { Sidebar } from "@/components/sidebar"
55import { SampleViewer } from "@/components/viewer/sample-viewer"
66import { getDefaultSample , getSampleById , getAllSamples } from "@/samples"
77
8- // Enable debug logging
9- const DEBUG = true
10-
11- // Debug helper to log localStorage state
12- function logStorageState ( context : string ) {
13- if ( ! DEBUG || typeof window === 'undefined' ) return
14-
15- const keys = Object . keys ( localStorage ) . filter ( k =>
16- k . includes ( 'document' ) || k . includes ( 'sample' ) || k . includes ( 'demo' )
17- )
18- const state : Record < string , string | null > = { }
19- keys . forEach ( k => state [ k ] = localStorage . getItem ( k ) )
20-
21- console . log ( `[Page] localStorage (${ context } ):` , state )
22- }
23-
248export default function Page ( ) {
259 const [ sidebarOpen , setSidebarOpen ] = useState ( true )
2610 // Always initialize with default to match server render
2711 const [ currentSampleId , setCurrentSampleId ] = useState < string > ( getDefaultSample ( ) . metadata . id )
2812 const [ documentId , setDocumentId ] = useState < string > ( '' )
2913 const [ isMounted , setIsMounted ] = useState ( false )
3014 const isInitialized = useRef ( false )
31- const renderCount = useRef ( 0 )
32-
33- renderCount . current ++
3415
3516 const currentSample = getSampleById ( currentSampleId ) || getDefaultSample ( )
3617
37- // Debug: Track renders
38- useEffect ( ( ) => {
39- if ( DEBUG ) {
40- console . log ( `[Page] Render #${ renderCount . current } ` , {
41- currentSampleId,
42- documentId,
43- isMounted,
44- isInitialized : isInitialized . current ,
45- } )
46- }
47- } )
48-
4918 // Helper function to get document ID for a specific demo
5019 const getDocumentIdForDemo = useCallback ( ( demoId : string ) : string => {
5120 if ( typeof window === 'undefined' ) return ''
5221
5322 const storageKey = `demo-${ demoId } -document-id`
5423 const stored = localStorage . getItem ( storageKey )
5524
56- if ( DEBUG ) {
57- console . log ( `[Page] getDocumentIdForDemo("${ demoId } ")` , {
58- storageKey,
59- storedValue : stored ,
60- } )
61- }
62-
6325 if ( stored ) {
6426 return stored
6527 }
@@ -68,10 +30,6 @@ export default function Page() {
6830 const newDocId = `doc-${ demoId } -${ Date . now ( ) } -${ Math . random ( ) . toString ( 36 ) . substring ( 2 , 9 ) } `
6931 localStorage . setItem ( storageKey , newDocId )
7032
71- if ( DEBUG ) {
72- console . log ( `[Page] Generated new documentId:` , newDocId )
73- }
74-
7533 return newDocId
7634 } , [ ] )
7735
@@ -80,12 +38,6 @@ export default function Page() {
8038 if ( isInitialized . current ) return
8139 if ( typeof window === 'undefined' ) return
8240
83- if ( DEBUG ) {
84- console . log ( '[Page] === INITIALIZATION START ===' )
85- console . log ( '[Page] URL:' , window . location . href )
86- logStorageState ( 'before init' )
87- }
88-
8941 try {
9042 // 1. Determine which sample to load based on URL path
9143 const currentPath = window . location . pathname
@@ -94,13 +46,6 @@ export default function Page() {
9446
9547 let targetSampleId = getDefaultSample ( ) . metadata . id
9648
97- if ( DEBUG ) {
98- console . log ( '[Page] Path resolution:' , {
99- currentPath,
100- sampleFromPath : sampleFromPath ?. metadata . id ,
101- } )
102- }
103-
10449 // Priority order: URL path > localStorage > default
10550 if ( sampleFromPath ) {
10651 targetSampleId = sampleFromPath . metadata . id
@@ -115,82 +60,43 @@ export default function Page() {
11560 }
11661 }
11762
118- if ( DEBUG ) {
119- console . log ( '[Page] Target sample:' , targetSampleId )
120- }
121-
12263 // Store the selection for persistence
12364 localStorage . setItem ( 'last-selected-sample-id' , targetSampleId )
12465
12566 // 2. Check URL for documentId parameter
12667 const urlParams = new URLSearchParams ( window . location . search )
12768 let docId = urlParams . get ( 'documentId' )
12869
129- if ( DEBUG ) {
130- console . log ( '[Page] URL documentId param:' , docId )
131- }
132-
13370 if ( docId ) {
13471 // Use document ID from URL (shareable link)
13572 localStorage . setItem ( `demo-${ targetSampleId } -document-id` , docId )
136- if ( DEBUG ) {
137- console . log ( '[Page] Using documentId from URL:' , docId )
138- }
13973 } else {
14074 // Get or generate document ID for this specific demo
14175 docId = getDocumentIdForDemo ( targetSampleId )
142- if ( DEBUG ) {
143- console . log ( '[Page] Got/generated documentId:' , docId )
144- }
14576 }
14677
14778 // Mark as initialized BEFORE setting state to prevent sample change effect from running
14879 isInitialized . current = true
14980
150- if ( DEBUG ) {
151- console . log ( '[Page] Setting state:' , { targetSampleId, docId } )
152- logStorageState ( 'after init' )
153- }
154-
15581 // Update state atomically - both sampleId and documentId together
15682 // This ensures the iframe never renders with mismatched sample/document
15783 if ( targetSampleId !== currentSampleId ) {
15884 setCurrentSampleId ( targetSampleId )
15985 }
16086 setDocumentId ( docId )
16187 setIsMounted ( true )
162-
163- if ( DEBUG ) {
164- console . log ( '[Page] === INITIALIZATION COMPLETE ===' )
165- }
16688 } catch ( error ) {
16789 console . error ( 'Error initializing:' , error )
16890 setIsMounted ( true )
16991 }
17092 } , [ ] )
17193
172- // Debug: Periodic storage check
173- useEffect ( ( ) => {
174- if ( ! DEBUG || ! isMounted ) return
175-
176- const interval = setInterval ( ( ) => {
177- logStorageState ( 'periodic check' )
178- } , 5000 ) // Every 5 seconds
179-
180- return ( ) => clearInterval ( interval )
181- } , [ isMounted ] )
182-
18394 // Handle sample selection: atomically update both sample and document ID
18495 // to prevent race conditions where iframe renders with mismatched data
18596 const handleSampleSelect = useCallback ( ( newSampleId : string ) => {
18697 if ( newSampleId === currentSampleId ) return
18798 if ( typeof window === 'undefined' ) return
18899
189- if ( DEBUG ) {
190- console . log ( '[Page] === SAMPLE SELECT ===' , newSampleId )
191- logStorageState ( 'before sample select' )
192- }
193-
194100 try {
195101 const sample = getSampleById ( newSampleId )
196102 if ( ! sample ) return
@@ -201,10 +107,6 @@ export default function Page() {
201107 // Get or generate document ID for this demo BEFORE updating state
202108 const docId = getDocumentIdForDemo ( newSampleId )
203109
204- if ( DEBUG ) {
205- console . log ( '[Page] Sample select - setting state:' , { newSampleId, docId } )
206- }
207-
208110 // ATOMIC UPDATE: Set both sample and document ID together
209111 // This prevents the iframe from rendering with mismatched data
210112 setCurrentSampleId ( newSampleId )
@@ -216,13 +118,6 @@ export default function Page() {
216118
217119 if ( window . location . href !== window . location . origin + newUrl ) {
218120 window . history . pushState ( { } , '' , newUrl )
219- if ( DEBUG ) {
220- console . log ( '[Page] Updated URL to:' , newUrl )
221- }
222- }
223-
224- if ( DEBUG ) {
225- logStorageState ( 'after sample select' )
226121 }
227122 } catch ( error ) {
228123 console . error ( 'Error changing sample:' , error )
@@ -233,22 +128,13 @@ export default function Page() {
233128 const handleReset = useCallback ( ( ) => {
234129 if ( typeof window === 'undefined' ) return
235130
236- if ( DEBUG ) {
237- console . log ( '[Page] === RESET ===' )
238- logStorageState ( 'before reset' )
239- }
240-
241131 try {
242132 const sample = getSampleById ( currentSampleId )
243133 if ( ! sample ) return
244134
245135 // Generate a new document ID for this specific demo
246136 const newDocId = `doc-${ currentSampleId } -${ Date . now ( ) } -${ Math . random ( ) . toString ( 36 ) . substring ( 2 , 9 ) } `
247137
248- if ( DEBUG ) {
249- console . log ( '[Page] Generated new documentId for reset:' , newDocId )
250- }
251-
252138 // Update localStorage for this demo
253139 localStorage . setItem ( `demo-${ currentSampleId } -document-id` , newDocId )
254140
@@ -261,13 +147,6 @@ export default function Page() {
261147
262148 if ( window . location . href !== window . location . origin + newUrl ) {
263149 window . history . pushState ( { } , '' , newUrl )
264- if ( DEBUG ) {
265- console . log ( '[Page] Updated URL to:' , newUrl )
266- }
267- }
268-
269- if ( DEBUG ) {
270- logStorageState ( 'after reset' )
271150 }
272151 } catch ( error ) {
273152 console . error ( 'Error resetting document:' , error )
0 commit comments