Markishime commited on
Commit
da06b3a
·
1 Parent(s): 267453f

Fix history page routing - handle multiple LOAD_ANALYSIS message formats and improve URL-based routing

Browse files
Files changed (2) hide show
  1. app.py +15 -3
  2. modules/history.py +55 -12
app.py CHANGED
@@ -571,20 +571,32 @@ def main():
571
  # Initialize the application
572
  initialize_app()
573
 
574
- # Check for analysisId in query params - if present, show history page
 
575
  query_params = st.query_params
576
  analysis_id = query_params.get('analysisId', None)
577
 
578
- if analysis_id and show_history_page:
 
 
 
 
 
 
 
 
 
579
  # Show header
580
  show_header()
581
 
582
  # Show sidebar
583
  show_sidebar()
584
 
585
- # Show history page
586
  show_history_page()
587
  return
 
 
588
 
589
  # Show header
590
  show_header()
 
571
  # Initialize the application
572
  initialize_app()
573
 
574
+ # CRITICAL: Check for analysisId in query params FIRST - if present, show history page
575
+ # This must happen before showing header/sidebar to ensure proper routing
576
  query_params = st.query_params
577
  analysis_id = query_params.get('analysisId', None)
578
 
579
+ # Also check for analysisData in URL (from postMessage)
580
+ analysis_data_encoded = query_params.get('analysisData', None)
581
+
582
+ # Debug logging
583
+ if analysis_id or analysis_data_encoded:
584
+ print(f"🔍 Routing to history page - analysisId: {analysis_id}, has analysisData: {bool(analysis_data_encoded)}")
585
+
586
+ # If we have analysisId or analysisData, route to history page
587
+ if (analysis_id or analysis_data_encoded) and show_history_page:
588
+ print("✅ Routing to history page")
589
  # Show header
590
  show_header()
591
 
592
  # Show sidebar
593
  show_sidebar()
594
 
595
+ # Show history page (it will handle loading the data)
596
  show_history_page()
597
  return
598
+ elif analysis_id or analysis_data_encoded:
599
+ print(f"⚠️ analysisId/analysisData found but show_history_page is not available")
600
 
601
  # Show header
602
  show_header()
modules/history.py CHANGED
@@ -80,13 +80,37 @@ def inject_load_analysis_listener():
80
 
81
  const data = event.data;
82
 
 
 
 
 
 
 
83
  if (data && data.type === 'LOAD_ANALYSIS') {
84
- console.log('📥 Received LOAD_ANALYSIS message:', data);
85
- console.log('📥 analysisId:', data.analysisId);
86
- console.log('📥 analysisData keys:', data.analysisData ? Object.keys(data.analysisData) : 'none');
87
-
88
- const analysisId = data.analysisId;
89
- const analysisData = data.analysisData;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
 
91
  if (!analysisId && !analysisData) {
92
  console.error('❌ LOAD_ANALYSIS message missing both analysisId and analysisData');
@@ -132,8 +156,10 @@ def inject_load_analysis_listener():
132
  }
133
  }
134
 
 
135
  window.history.replaceState({}, '', url);
136
  console.log('✅ Updated URL:', url.toString());
 
137
 
138
  // Trigger Streamlit rerun to load the analysis
139
  console.log('🔄 Reloading page to display analysis...');
@@ -145,19 +171,36 @@ def inject_load_analysis_listener():
145
  }
146
  });
147
 
148
- // Also check for analysisId in URL on page load
149
- window.addEventListener('load', function() {
 
150
  const urlParams = new URLSearchParams(window.location.search);
151
  const analysisId = urlParams.get('analysisId');
152
- if (analysisId) {
153
- console.log('📋 Found analysisId in URL on load:', analysisId);
 
 
 
 
154
  // Store in sessionStorage for consistency
155
- sessionStorage.setItem('load_analysis_id', analysisId);
 
 
 
 
 
156
  }
157
- });
 
 
 
 
 
 
158
 
159
  // Log that listener is ready
160
  console.log('✅ LOAD_ANALYSIS listener ready and waiting for messages');
 
161
  })();
162
  </script>
163
  """
 
80
 
81
  const data = event.data;
82
 
83
+ // Handle multiple message formats
84
+ let analysisData = null;
85
+ let analysisId = null;
86
+ let messageReceived = false;
87
+
88
+ // Format 1: Direct LOAD_ANALYSIS
89
  if (data && data.type === 'LOAD_ANALYSIS') {
90
+ console.log('📥 Received LOAD_ANALYSIS (Format 1):', data);
91
+ analysisData = data.analysisData;
92
+ analysisId = data.analysisId;
93
+ messageReceived = true;
94
+ }
95
+ // Format 2: Nested MESSAGE
96
+ else if (data && data.type === 'MESSAGE' && data.message && data.message.type === 'LOAD_ANALYSIS') {
97
+ console.log('📥 Received nested LOAD_ANALYSIS (Format 2):', data.message);
98
+ analysisData = data.message.analysisData;
99
+ analysisId = data.message.analysisId;
100
+ messageReceived = true;
101
+ }
102
+ // Format 3: Action format
103
+ else if (data && data.action === 'load_analysis') {
104
+ console.log('📥 Received action load_analysis (Format 3):', data);
105
+ analysisData = data.data;
106
+ analysisId = data.analysisId || (data.data && data.data.id);
107
+ messageReceived = true;
108
+ }
109
+
110
+ if (messageReceived) {
111
+ console.log('📥 analysisId:', analysisId);
112
+ console.log('📥 analysisData type:', analysisData ? typeof analysisData : 'none');
113
+ console.log('📥 analysisData keys:', analysisData ? Object.keys(analysisData) : 'none');
114
 
115
  if (!analysisId && !analysisData) {
116
  console.error('❌ LOAD_ANALYSIS message missing both analysisId and analysisData');
 
156
  }
157
  }
158
 
159
+ // CRITICAL: Update URL and reload to trigger Streamlit routing
160
  window.history.replaceState({}, '', url);
161
  console.log('✅ Updated URL:', url.toString());
162
+ console.log('✅ URL will route to history page with analysisId:', finalAnalysisId);
163
 
164
  // Trigger Streamlit rerun to load the analysis
165
  console.log('🔄 Reloading page to display analysis...');
 
171
  }
172
  });
173
 
174
+ // Also check for analysisId in URL on page load (immediate check)
175
+ // This handles cases where the URL was already updated before page load
176
+ function checkUrlForAnalysisId() {
177
  const urlParams = new URLSearchParams(window.location.search);
178
  const analysisId = urlParams.get('analysisId');
179
+ const analysisData = urlParams.get('analysisData');
180
+ if (analysisId || analysisData) {
181
+ console.log('📋 Found analysisId/analysisData in URL on load:', {
182
+ analysisId: analysisId,
183
+ hasAnalysisData: !!analysisData
184
+ });
185
  // Store in sessionStorage for consistency
186
+ if (analysisId) {
187
+ sessionStorage.setItem('load_analysis_id', analysisId);
188
+ }
189
+ // The page should already be routing to history page via Streamlit
190
+ // But we log this for debugging
191
+ console.log('✅ URL contains analysis data - Streamlit should route to history page');
192
  }
193
+ }
194
+
195
+ // Check immediately (in case DOM is already loaded)
196
+ checkUrlForAnalysisId();
197
+
198
+ // Also check on load event
199
+ window.addEventListener('load', checkUrlForAnalysisId);
200
 
201
  // Log that listener is ready
202
  console.log('✅ LOAD_ANALYSIS listener ready and waiting for messages');
203
+ console.log('✅ Current URL:', window.location.href);
204
  })();
205
  </script>
206
  """