Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor UI to components #498

Merged
merged 15 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
207 changes: 207 additions & 0 deletions assets/js/components/multiStageOperation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
// Multi-Stage Operation Component
// A reusable Alpine.js component for tracking multi-stage operations with progress reporting

document.addEventListener('alpine:init', () => {
Alpine.data('multiStageOperation', (config = {}) => {
return {
// Configurable properties with defaults
apiEndpoint: config.apiEndpoint || '',
csrfToken: config.csrfToken || '',
timeoutDuration: config.timeoutDuration || 15000,
operationName: config.operationName || 'Operation',
stageOrder: config.stageOrder || ['Starting'],

// Component state
isRunning: false,
results: [],
currentStage: null,

// Helper methods
isProgressMessage(message) {
if (!message) return false;
const lowerMsg = message.toLowerCase();
return lowerMsg.includes('running') ||
lowerMsg.includes('testing') ||
lowerMsg.includes('establishing') ||
lowerMsg.includes('initializing') ||
lowerMsg.includes('attempting') ||
lowerMsg.includes('processing');
},

// Start the operation
start(payload = {}, options = {}) {
const initialStage = options.initialStage || this.stageOrder[0];
const initialMessage = options.initialMessage || `Initializing ${this.operationName}...`;

this.isRunning = true;
this.currentStage = initialStage;
this.results = [{
success: true,
stage: initialStage,
message: initialMessage,
state: 'running'
}];

// Create a timeout promise
const timeoutPromise = new Promise((_, reject) => {
setTimeout(() => reject(new Error(`Operation timeout after ${this.timeoutDuration/1000} seconds`)), this.timeoutDuration);
});

if (!this.apiEndpoint) {
console.error('No API endpoint specified for the operation');
this.results = [{
success: false,
stage: 'Error',
message: 'No API endpoint specified for the operation',
state: 'failed'
}];
this.isRunning = false;
return Promise.reject(new Error('No API endpoint specified'));
}

// Create the fetch promise
const fetchPromise = fetch(this.apiEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRF-Token': this.csrfToken
},
body: JSON.stringify(payload)
});

// Race between fetch and timeout
return Promise.race([fetchPromise, timeoutPromise])
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = '';

return new ReadableStream({
start: (controller) => {
const push = () => {
reader.read().then(({done, value}) => {
if (done) {
controller.close();
return;
}

buffer += decoder.decode(value, {stream: true});
const lines = buffer.split('\n');
buffer = lines.pop(); // Keep the incomplete line

lines.forEach(line => {
if (line.trim()) {
try {
const result = JSON.parse(line);
this.currentStage = result.stage;

// Find existing result for this stage
const existingIndex = this.results.findIndex(r => r.stage === result.stage);

// Determine if this is a progress message
const isProgress = this.isProgressMessage(result.message);

// Set the state based on the result
const state = result.state ? result.state : // Use existing state if provided
result.error ? 'failed' :
isProgress ? 'running' :
result.success ? 'completed' :
'failed';

const updatedResult = {
...result,
isProgress: isProgress && !result.error, // Progress state is false if there's an error
state,
success: result.error ? false : result.success
};

if (existingIndex >= 0) {
// Update existing result
this.results[existingIndex] = updatedResult;
} else {
// Add new result
this.results.push(updatedResult);
}

// Also update previous stages to completed if this is a new stage
if (!isProgress && result.success && !result.error) {
const currentStageIndex = this.stageOrder.indexOf(result.stage);
this.results.forEach((r, idx) => {
const stageIndex = this.stageOrder.indexOf(r.stage);
if (stageIndex < currentStageIndex && r.state === 'running') {
this.results[idx] = {
...r,
state: 'completed',
isProgress: false
};
}
});
}

// Sort results according to stage order
this.results.sort((a, b) =>
this.stageOrder.indexOf(a.stage) - this.stageOrder.indexOf(b.stage)
);
} catch (e) {
console.error('Failed to parse result:', e);
}
}
});

controller.enqueue(value);
push();
}).catch(error => {
controller.error(error);
});
};

push();
}
});
})
.catch(error => {
const errorMessage = error.message.includes('timeout')
? `The operation took too long to complete. Please try again.`
: `Failed to perform ${this.operationName}`;

this.results = [{
success: false,
stage: 'Error',
message: errorMessage,
error: error.message,
state: 'failed'
}];
this.currentStage = null;
return Promise.reject(error);
})
.finally(() => {
this.isRunning = false;
this.currentStage = null;
});
},

// Check if operation was completely successful
isCompleteSuccess() {
if (this.results.length === 0 || this.isRunning) return false;

// Every result must be successful
if (!this.results.every(result => result.success)) return false;

// Must have reached the final stage
const finalStage = this.stageOrder[this.stageOrder.length - 1];
return this.results.some(result => result.stage === finalStage);
},

// Reset the operation state
reset() {
this.isRunning = false;
this.results = [];
this.currentStage = null;
}
};
});
});
Loading
Loading