more refactoring

This commit is contained in:
pavel 2026-02-11 01:41:07 +01:00
commit 7268d49b4a
12 changed files with 319 additions and 189 deletions

View file

@ -18,7 +18,6 @@ const state = {
isEditing: false,
isAuthenticated: false
};
// DOM elements
const loginOverlay = document.getElementById('login-overlay');
const callbackOverlay = document.getElementById('callback-overlay');
@ -55,6 +54,25 @@ const toggleCustomCronBtn = document.getElementById('toggle-custom-cron');
const customCronContainer = document.getElementById('custom-cron-container');
const presetBtns = document.querySelectorAll('.btn-preset');
function updateState(newState) {
Object.assign(state, newState);
renderApp();
}
function renderApp() {
renderTaskList();
if (state.currentView === 'dashboard') {
fetchRecentRuns();
} else if (state.selectedTaskId) {
const task = state.tasks.find(t => t.id === state.selectedTaskId);
if (task) {
renderRunHistory(task);
showTaskView(task);
}
}
}
// Wrapper for fetch to include Authorization header
async function fetchWithAuth(url, options = {}) {
let response = await fetch(url, { ...options, credentials: 'include' });
@ -97,46 +115,32 @@ async function attemptTokenRefresh() {
}
return false;
}
// I'll replace the fetchTasks function and add updateState
async function fetchTasks() {
try {
const response = await fetchWithAuth(`${API_URL}/tasks`);
const newTasks = await response.json();
// Check if we should follow the latest run (if we were already watching it)
let shouldFollowLatest = false;
// Check if we should follow the latest run
let newSelectedRunId = state.selectedRunId;
if (state.selectedTaskId) {
const currentTask = state.tasks.find(t => t.id === state.selectedTaskId);
const currentTask = newTasks.find(t => t.id === state.selectedTaskId);
if (currentTask && currentTask.runs && currentTask.runs.length > 0) {
const latestRunId = currentTask.runs[currentTask.runs.length - 1].id;
if (state.selectedRunId === latestRunId) {
shouldFollowLatest = true;
// If we don't have a selected run or the runs changed, we might want to update
if (!state.selectedRunId || (state.tasks.find(t => t.id === state.selectedTaskId)?.runs?.length !== currentTask.runs.length)) {
// Only auto-switch if we are "following" the latest
const wasFollowingLatest = state.tasks.find(t => t.id === state.selectedTaskId)?.runs?.[0]?.id === state.selectedRunId;
if (wasFollowingLatest || !state.selectedRunId) {
newSelectedRunId = currentTask.runs[0].id;
}
}
} else if (!state.selectedRunId) {
shouldFollowLatest = true;
}
}
state.tasks = newTasks;
renderTaskList();
// If we are on the dashboard, refresh it too
if (state.currentView === 'dashboard') {
fetchRecentRuns();
}
// If a task is selected, update it
if (state.selectedTaskId) {
const task = state.tasks.find((t) => t.id === state.selectedTaskId);
if (task) {
if (shouldFollowLatest && task.runs && task.runs.length > 0) {
state.selectedRunId = task.runs[0].id;
}
renderRunHistory(task);
showTaskView(task);
}
}
updateState({
tasks: newTasks,
selectedRunId: newSelectedRunId
});
} catch (error) {
console.error('Error fetching tasks:', error);
}