Samad14 commited on
Commit
1dc60a8
·
1 Parent(s): a64e3d2

Fix loading overlay: detect Molstar via canvas element, add event listener, remove blocking z-index

Browse files
bioai-platform/frontend/src/components/DockingViewer.tsx CHANGED
@@ -139,29 +139,20 @@ export function DockingViewer({
139
  container.appendChild(el);
140
  viewerRef.current = el;
141
 
142
- // Wait for plugin to be ready, then load ligand + interactions
143
- let attempts = 0;
144
- checkInterval = setInterval(async () => {
145
- if (cancelled) {
146
- if (checkInterval) clearInterval(checkInterval);
147
- return;
148
- }
149
 
150
  const plugin = el.plugin;
151
- if (!plugin?.canvas3d) {
152
- attempts++;
153
- if (attempts > 150) { // 45 seconds max
154
- if (checkInterval) clearInterval(checkInterval);
155
- if (!cancelled) {
156
- setStatus('error');
157
- setError('Molstar failed to initialize');
158
- }
159
- }
160
  return;
161
  }
162
 
163
- if (checkInterval) clearInterval(checkInterval);
164
-
165
  // Load ligand as additional structure
166
  if (ligandPdb) {
167
  try {
@@ -179,11 +170,38 @@ export function DockingViewer({
179
  if (!cancelled) {
180
  setStatus('ready');
181
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
182
  }, 300);
183
 
184
  return () => {
185
  cancelled = true;
 
186
  if (checkInterval) clearInterval(checkInterval);
 
187
  };
188
  }, [pdbId, ligandPdb, backgroundColor]); // NO interactions here — avoids re-init
189
 
@@ -245,12 +263,12 @@ export function DockingViewer({
245
  <div className="relative" style={{ height }}>
246
  <div ref={containerRef} className="absolute inset-0" style={{ width: '100%', height: '100%' }} />
247
  {status === 'loading' && (
248
- <div className="absolute inset-0 flex items-center justify-center bg-[#0d1117]/80 text-sm text-white/60 z-10">
249
  Loading structure...
250
  </div>
251
  )}
252
  {status === 'error' && (
253
- <div className="absolute inset-0 flex items-center justify-center bg-[#0d1117]/90 px-6 text-center text-sm text-red-400 z-10">
254
  {error}
255
  </div>
256
  )}
 
139
  container.appendChild(el);
140
  viewerRef.current = el;
141
 
142
+ // Wait for pdbe-molstar to finish loading models via its custom event
143
+ let cancelled_after_event = false;
144
+
145
+ const onModelsLoaded = async () => {
146
+ if (cancelled || cancelled_after_event) return;
147
+ cancelled_after_event = true;
148
+ if (checkInterval) clearInterval(checkInterval);
149
 
150
  const plugin = el.plugin;
151
+ if (!plugin) {
152
+ if (!cancelled) { setStatus('ready'); }
 
 
 
 
 
 
 
153
  return;
154
  }
155
 
 
 
156
  // Load ligand as additional structure
157
  if (ligandPdb) {
158
  try {
 
170
  if (!cancelled) {
171
  setStatus('ready');
172
  }
173
+ };
174
+
175
+ el.addEventListener('molstar-models-loaded', onModelsLoaded);
176
+
177
+ // Fallback polling: check if plugin exists at all (not canvas3d which may be renamed)
178
+ let attempts = 0;
179
+ checkInterval = setInterval(() => {
180
+ if (cancelled || cancelled_after_event) {
181
+ if (checkInterval) clearInterval(checkInterval);
182
+ return;
183
+ }
184
+ // Check if the web component has rendered anything (canvas element present)
185
+ const hasCanvas = el.querySelector('canvas');
186
+ if (hasCanvas) {
187
+ onModelsLoaded();
188
+ return;
189
+ }
190
+ attempts++;
191
+ if (attempts > 100) { // 30 seconds max
192
+ if (checkInterval) clearInterval(checkInterval);
193
+ // Even if we can't detect it, mark ready — the viewer may be working
194
+ if (!cancelled) {
195
+ setStatus('ready');
196
+ }
197
+ }
198
  }, 300);
199
 
200
  return () => {
201
  cancelled = true;
202
+ cancelled_after_event = true;
203
  if (checkInterval) clearInterval(checkInterval);
204
+ el.removeEventListener('molstar-models-loaded', onModelsLoaded);
205
  };
206
  }, [pdbId, ligandPdb, backgroundColor]); // NO interactions here — avoids re-init
207
 
 
263
  <div className="relative" style={{ height }}>
264
  <div ref={containerRef} className="absolute inset-0" style={{ width: '100%', height: '100%' }} />
265
  {status === 'loading' && (
266
+ <div className="absolute inset-0 flex items-center justify-center bg-[#0d1117]/80 text-sm text-white/60 pointer-events-none">
267
  Loading structure...
268
  </div>
269
  )}
270
  {status === 'error' && (
271
+ <div className="absolute inset-0 flex items-center justify-center bg-[#0d1117]/90 px-6 text-center text-sm text-red-400">
272
  {error}
273
  </div>
274
  )}