Jake Vanderwerf
2026-02-08 0442e1186ae642c86947d03961fde7d461ba054d
assets/js/concise/Queue.js
@@ -134,6 +134,7 @@
            actions: {
               cancel: 'button.cancel',
               retry: 'button.retry',
               refresh: 'button.refresh',
               dismiss: 'button.dismiss',
            }
         },
@@ -198,6 +199,13 @@
            return;
         }
         const refreshPage = window.targetCheck(e, this.selectors.actions.refresh);
         if (refreshPage) {
            this.handleRefresh(opId);
            return;
         }
         const clear = window.targetCheck(e, this.selectors.actions.clear);
         if (clear) {
            this.opActions('completed', 'dismiss').then(()=>{});
@@ -280,7 +288,8 @@
            storeName: 'queue',
            keyPath: 'id',
            endpoint: this.endpoint,
            TTL: Infinity,
            // TTL: Infinity,
            TTL: 5000,
            indexes: [
               {name: 'status', keyPath: 'status'},
               {name: 'type', keyPath: 'type'},
@@ -295,22 +304,90 @@
      this.store.subscribe((event, data) => {
         switch (event) {
            case 'data-loaded':
               const serverOps = this.store.getAll();
               serverOps.forEach(serverOp => {
                  const localOp = this.queue.get(serverOp.id);
                  const mapped = this.mapServerOperation(serverOp);
                  this.queue.set(mapped.id, mapped);
                  // Notify if changed
                  if (localOp && localOp.status !== mapped.status) {
                     this.notify('operation-status', mapped);
                  }
               });
               this.maybeStartPolling();
               this.updateUI();
               break;
            case 'items-save':
               this.maybeStartPolling();
               this.updateUI();
               break;
            case 'item-saved':
               if (data.previousItem && data.previousItem.status !== data.item.status) {
                  this.updateOperationStatus(data.item.id, data.item.status);
               if (data.item) {
                  this.queue.set(data.item.id, data.item);
                  if (data.previousItem?.status !== data.item.status) {
                     this.notify('operation-status', data.item);
                  }
               }
               this.maybeStartPolling();
               break;
            default:
               break;
         }
      });
   }
   /**
    * Handle refresh button click - clears cache for the relevant store
    */
   handleRefresh(opId) {
      const op = this.getQueue(opId);
      if (!op) return;
      // Determine which store to refresh based on operation type
      let storeName = null;
      // Map operation types to store names
      const typeToStore = {
         'content_update': op.data?.posts ? Object.values(op.data.posts)[0]?.content : null,
         'batch_creation': op.data?.content,
         'image_upload': 'uploads',
         'video_upload': 'uploads',
         'document_upload': 'uploads',
      };
      storeName = typeToStore[op.type];
      // If we found a store name, clear its cache
      if (storeName && window.jvbStore) {
         const store = window.jvbStore.stores.get(storeName);
         if (store) {
            window.jvbStore.clearCache(storeName);
            window.jvbStore.fetch(storeName);
            // Give visual feedback
            const button = this.items.get(opId)?.ui?.actions?.refresh;
            if (button) {
               const originalText = button.querySelector('span').textContent;
               button.querySelector('span').textContent = 'Refreshed!';
               button.disabled = true;
               setTimeout(() => {
                  button.querySelector('span').textContent = originalText;
                  button.disabled = false;
               }, 2000);
            }
         }
      } else {
         // Fallback: just reload the page if we can't determine the store
         if (confirm('Refresh the page to see changes?')) {
            window.location.reload();
         }
      }
   }
   /****************************************************************************
    OPERATIONS
   ****************************************************************************/
@@ -356,14 +433,16 @@
      const existingOps = Array.from(this.getAllQueue()).filter(op=> {
         return op.status === 'queued' &&
         op.endpoint === item.endpoint &&
         op.canMerge
            op.endpoint === item.endpoint &&
            op.canMerge
      });
      if (existingOps.length > 0) {
         const existing = existingOps[0];
         existing.data = window.deepMerge(existing.data, item.data);
         existing.timestamp = Date.now();
         this.setQueue(existing);
         this.updateOperationStatus(existing.id, existing.status);
         this.updateUI();
         this.trackActivity();
@@ -407,7 +486,7 @@
               },
               body: JSON.stringify({
                  action,
                  ids: statusOrId,
                  ids: Array.isArray(statusOrId) ? statusOrId : [statusOrId],
                  user: this.user
               })
            }
@@ -469,7 +548,13 @@
      }
      this.setProcessing(false);
      this.stopActivityTracking();
      const remainingQueue = this.getQueueByStatus('queued');
      if (remainingQueue.length === 0) {
         this.stopActivityTracking();
      } else {
         // Still have queued items, restart activity tracking
         this.trackActivity();
      }
      this.toggleQueue(this.maybeStartPolling());
   }
@@ -844,21 +929,32 @@
            item.ui.actions['retry'].hidden = op.status !=='failed';
         }
         if (item.ui.actions.dismiss) item.ui.actions.dismiss.hidden = this.pendingStatuses.includes(op.status);
         if (item.ui.actions.refresh) {
            item.ui.actions.refresh.hidden = op.status !== 'completed';
         }
      }
      getProgress(op) {
         if (op.progress) return op.progress;
         if (!this.statuses.includes(op.status)) return 0;
         let statusProgress = {
            'queued': 10,
            'uploading': 25,
            'pending': 40,
            'processing':70,
            'completed':100,
            'failed':0,
            'failed_permanent':0
         };
         return statusProgress[op.status]??0;
   getProgress(op) {
      // Check server-provided percentage first
      if (op.progress_percentage !== undefined) {
         return op.progress_percentage;
      }
      // Legacy: check old 'progress' field
      if (op.progress !== undefined) {
         return op.progress;
      }
      // Fallback to status-based calculation
      if (!this.statuses.includes(op.status)) return 0;
      const statusProgress = {
         'queued': 10,
         'uploading': 25,
         'pending': 40,
         'processing': 70,
         'completed': 100,
         'failed': 0,
         'failed_permanent': 0
      };
      return statusProgress[op.status] ?? 0;
   }
   removeOperationUI(opId) {
      let op = this.items.get(opId);
      if (!op) return;
@@ -883,7 +979,8 @@
         'processing': 'Processing',
         'completed': 'Completed',
         'failed': 'Failed',
         'failed_permanent': 'Failed permanently'
         'failed_permanent': 'Failed permanently',
         'merged': 'Merged'
      };
      return labels[status];
   }
@@ -899,9 +996,24 @@
         case 'pending':
            return item.position ? `Position ${item.position} in queue` : 'In server queue';
         case 'processing':
            return item.progress ? `${item.progress}% complete` : 'Processing...';
            // Show progress count if available
            if (item.count && item.progress_count !== undefined) {
               const processed = item.progress_count;
               const total = item.count;
               const percentage = Math.round((processed / total) * 100);
               return `Processing ${processed}/${total} items (${percentage}%)`;
            }
            // Fallback to percentage only
            if (item.progress_percentage !== undefined) {
               return `${item.progress_percentage}% complete`;
            }
            return 'Processing...';
         case 'completed':
            return 'Successfully completed';
            return 'Successfully completed. Refresh to see changes.';
         case 'merged':
            return item.merged_into
               ? `Merged with another operation (${item.merged_into.substring(0, 8)}...)`
               : 'Merged with another operation';
         case 'failed':
            return `Failed: ${item.lastError || 'Unknown error'} (Retry ${item.retries}/${2})`;
         case 'failed_permanent':
@@ -919,6 +1031,68 @@
      this.isProcessing = on;
      this.ui.toggle.button.classList.toggle('saving', on);
   }
   /**
    * Map server operation format to frontend format
    * Server uses: type, data (requestData), status (from state/outcome)
    * Frontend uses: endpoint, data, status, headers, method, etc.
    */
   mapServerOperation(serverOp) {
      const localOp = this.queue.get(serverOp.id);
      // If we have local operation data, preserve it
      if (localOp && localOp.endpoint) {
         const mappedOp = {
            ...localOp,
            ...serverOp,
            endpoint: localOp.endpoint,
            method: localOp.method,
            headers: localOp.headers,
            progress_percentage: serverOp.progress_percentage,
            progress_count: serverOp.progress_count,
            count: serverOp.count
         };
         if (serverOp.merged_into) {
            this.handleMergedOperation(mappedOp);
         }
      }
      // Minimal mapping for server-only operations
      // Extract endpoint from type if possible, otherwise use type
      const endpoint = serverOp.type ? serverOp.type.replace('_update', '').replace('_', '/') : 'unknown';
      const mappedOp = {
         ...serverOp,
         endpoint: endpoint,
         method: 'POST',
         headers: { ...this.headers },
      };
      if (serverOp.merged_into) {
         this.handleMergedOperation(mappedOp);
      }
      return mappedOp
   }
   /**
    * Handle merged operations
    * The target operation already has merged data from server,
    * so we just need to clean up the merged operation locally
    */
   handleMergedOperation(operation) {
      if (!operation.merged_into) return;
      console.log(`[Queue] Operation ${operation.id} merged into ${operation.merged_into}`);
      // Auto-dismiss merged operation after brief display
      // The target operation already has all the merged data from server
      setTimeout(() => {
         this.store.delete(operation.id);
         this.removeOperationFromUI(operation.id);
      }, 3000);
   }
   /****************************************************************************
    SUBSCRIPTION
    ****************************************************************************/