LiamKhoaLe commited on
Commit
50706f3
·
1 Parent(s): f1d8e9a

Fix sessions and project identifier

Browse files
Files changed (3) hide show
  1. static/index.html +1 -1
  2. static/projects.js +65 -24
  3. static/sessions.js +7 -1
static/index.html CHANGED
@@ -375,8 +375,8 @@
375
  <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
376
  <script src="/static/auth.js"></script>
377
  <script src="/static/sidebar.js"></script>
 
378
  <script src="/static/sessions.js"></script>
379
  <script src="/static/script.js"></script>
380
- <script src="/static/projects.js"></script>
381
  </body>
382
  </html>
 
375
  <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
376
  <script src="/static/auth.js"></script>
377
  <script src="/static/sidebar.js"></script>
378
+ <script src="/static/projects.js"></script>
379
  <script src="/static/sessions.js"></script>
380
  <script src="/static/script.js"></script>
 
381
  </body>
382
  </html>
static/projects.js CHANGED
@@ -44,20 +44,31 @@
44
 
45
  async function loadProjects() {
46
  const user = window.__sb_get_user();
47
- if (!user) return;
 
 
 
 
 
48
 
49
  try {
50
  const response = await fetch(`/projects?user_id=${user.user_id}`);
 
 
51
  if (response.ok) {
52
  const data = await response.json();
53
  projects = data.projects || [];
 
 
54
  renderProjectList();
55
 
56
  // If no projects, show welcome screen
57
  if (projects.length === 0) {
 
58
  showWelcomeScreen();
59
  } else {
60
  // Select first project by default
 
61
  selectProject(projects[0]);
62
  }
63
 
@@ -65,9 +76,13 @@
65
  if (window.__sb_update_upload_button) {
66
  window.__sb_update_upload_button();
67
  }
 
 
 
 
68
  }
69
  } catch (error) {
70
- console.error('Failed to load projects:', error);
71
  }
72
  }
73
 
@@ -113,6 +128,7 @@
113
  }
114
 
115
  function selectProject(project) {
 
116
  currentProject = project;
117
 
118
  // Update UI
@@ -130,25 +146,12 @@
130
  if (uploadSection) uploadSection.style.display = 'block';
131
  if (chatSection) chatSection.style.display = 'block';
132
 
133
- // Enable chat functionality when project is selected
134
- if (window.__sb_enable_chat) {
135
- window.__sb_enable_chat();
136
- }
137
-
138
  // Update project list
139
  renderProjectList();
140
 
141
- // Load chat history
142
- loadChatHistory();
143
-
144
  // Store current project in localStorage
145
  localStorage.setItem('sb_current_project', JSON.stringify(project));
146
-
147
- // Enable chat if user is authenticated
148
- const user = window.__sb_get_user();
149
- if (user) {
150
- enableChat();
151
- }
152
 
153
  // Update page title to show project name
154
  if (window.__sb_update_page_title) {
@@ -158,15 +161,53 @@
158
  // Dispatch custom event to notify other scripts that project has changed
159
  const event = new CustomEvent('projectChanged', { detail: { project } });
160
  document.dispatchEvent(event);
 
161
 
162
- // Update upload button if the function exists
163
- if (window.__sb_update_upload_button) {
164
- window.__sb_update_upload_button();
165
- }
166
- // Ensure stored files are loaded immediately
167
- if (window.__sb_load_stored_files) {
168
- window.__sb_load_stored_files();
169
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
170
  }
171
 
172
  function showWelcomeScreen() {
 
44
 
45
  async function loadProjects() {
46
  const user = window.__sb_get_user();
47
+ if (!user) {
48
+ console.log('[PROJECTS] No user found, skipping project load');
49
+ return;
50
+ }
51
+
52
+ console.log('[PROJECTS] Loading projects for user:', user.user_id);
53
 
54
  try {
55
  const response = await fetch(`/projects?user_id=${user.user_id}`);
56
+ console.log('[PROJECTS] Projects API response status:', response.status);
57
+
58
  if (response.ok) {
59
  const data = await response.json();
60
  projects = data.projects || [];
61
+ console.log('[PROJECTS] Loaded projects:', projects.length);
62
+
63
  renderProjectList();
64
 
65
  // If no projects, show welcome screen
66
  if (projects.length === 0) {
67
+ console.log('[PROJECTS] No projects found, showing welcome screen');
68
  showWelcomeScreen();
69
  } else {
70
  // Select first project by default
71
+ console.log('[PROJECTS] Selecting first project:', projects[0].name);
72
  selectProject(projects[0]);
73
  }
74
 
 
76
  if (window.__sb_update_upload_button) {
77
  window.__sb_update_upload_button();
78
  }
79
+ } else {
80
+ console.error('[PROJECTS] Failed to load projects, status:', response.status);
81
+ const errorText = await response.text();
82
+ console.error('[PROJECTS] Error response:', errorText);
83
  }
84
  } catch (error) {
85
+ console.error('[PROJECTS] Failed to load projects:', error);
86
  }
87
  }
88
 
 
128
  }
129
 
130
  function selectProject(project) {
131
+ console.log('[PROJECTS] Selecting project:', project.name, project.project_id);
132
  currentProject = project;
133
 
134
  // Update UI
 
146
  if (uploadSection) uploadSection.style.display = 'block';
147
  if (chatSection) chatSection.style.display = 'block';
148
 
 
 
 
 
 
149
  // Update project list
150
  renderProjectList();
151
 
 
 
 
152
  // Store current project in localStorage
153
  localStorage.setItem('sb_current_project', JSON.stringify(project));
154
+ console.log('[PROJECTS] Stored project in localStorage');
 
 
 
 
 
155
 
156
  // Update page title to show project name
157
  if (window.__sb_update_page_title) {
 
161
  // Dispatch custom event to notify other scripts that project has changed
162
  const event = new CustomEvent('projectChanged', { detail: { project } });
163
  document.dispatchEvent(event);
164
+ console.log('[PROJECTS] Dispatched projectChanged event');
165
 
166
+ // Use setTimeout to ensure other scripts are loaded and ready
167
+ setTimeout(() => {
168
+ console.log('[PROJECTS] Executing delayed project selection tasks');
169
+
170
+ // Enable chat functionality when project is selected
171
+ if (window.__sb_enable_chat) {
172
+ console.log('[PROJECTS] Calling __sb_enable_chat');
173
+ window.__sb_enable_chat();
174
+ } else {
175
+ console.log('[PROJECTS] __sb_enable_chat not available');
176
+ }
177
+
178
+ // Load chat history
179
+ if (window.__sb_load_chat_history) {
180
+ console.log('[PROJECTS] Calling __sb_load_chat_history');
181
+ window.__sb_load_chat_history();
182
+ } else {
183
+ console.log('[PROJECTS] __sb_load_chat_history not available');
184
+ }
185
+
186
+ // Enable chat if user is authenticated
187
+ const user = window.__sb_get_user();
188
+ if (user && window.enableChat) {
189
+ console.log('[PROJECTS] Calling enableChat');
190
+ window.enableChat();
191
+ } else {
192
+ console.log('[PROJECTS] enableChat not available or no user');
193
+ }
194
+
195
+ // Update upload button if the function exists
196
+ if (window.__sb_update_upload_button) {
197
+ console.log('[PROJECTS] Calling __sb_update_upload_button');
198
+ window.__sb_update_upload_button();
199
+ } else {
200
+ console.log('[PROJECTS] __sb_update_upload_button not available');
201
+ }
202
+
203
+ // Ensure stored files are loaded immediately
204
+ if (window.__sb_load_stored_files) {
205
+ console.log('[PROJECTS] Calling __sb_load_stored_files');
206
+ window.__sb_load_stored_files();
207
+ } else {
208
+ console.log('[PROJECTS] __sb_load_stored_files not available');
209
+ }
210
+ }, 100);
211
  }
212
 
213
  function showWelcomeScreen() {
static/sessions.js CHANGED
@@ -22,7 +22,10 @@
22
  function init() {
23
  setupEventListeners();
24
  // Load sessions when project changes
25
- document.addEventListener('projectChanged', loadSessions);
 
 
 
26
  }
27
 
28
  function setupEventListeners() {
@@ -55,7 +58,10 @@
55
  const user = window.__sb_get_user();
56
  const currentProject = window.__sb_get_current_project && window.__sb_get_current_project();
57
 
 
 
58
  if (!user || !currentProject) {
 
59
  sessions = [];
60
  updateSessionDropdown();
61
  return;
 
22
  function init() {
23
  setupEventListeners();
24
  // Load sessions when project changes
25
+ document.addEventListener('projectChanged', (event) => {
26
+ console.log('[SESSIONS] Project changed event received:', event.detail);
27
+ loadSessions();
28
+ });
29
  }
30
 
31
  function setupEventListeners() {
 
58
  const user = window.__sb_get_user();
59
  const currentProject = window.__sb_get_current_project && window.__sb_get_current_project();
60
 
61
+ console.log('[SESSIONS] Loading sessions for user:', user?.user_id, 'project:', currentProject?.project_id);
62
+
63
  if (!user || !currentProject) {
64
+ console.log('[SESSIONS] No user or project, clearing sessions');
65
  sessions = [];
66
  updateSessionDropdown();
67
  return;