/**
* userStates.js - User state management
*
* This module handles all functionality related to user working states.
*/
import AppState from './state.js';
import { fetchUserStates } from './api.js';
/**
* Get user state display HTML
* @param {string} username - Username to get state for
* @returns {string} - HTML string for displaying the user state
*/
export function getUserStateDisplay(username) {
const state = getUserActualState(username);
if (state === 'working') {
return 'Working';
} else if (state === 'not_working') {
return 'Not Working';
} else {
return 'Unknown';
}
}
/**
* Get the actual state for a user (from API or simulation)
* @param {string} username - Username to get state for
* @returns {string} - User state ('working', 'not_working', or 'unknown')
*/
export function getUserActualState(username) {
// const userStates = AppState.getUserStates(); // This line is now inside the AppState check
// Removed obsolete simulation logic:
// [Multi-line commented out block that was here previously]
// Return state from API if available, otherwise 'unknown'
// Ensure AppState and getUserStates are available before calling
if (AppState && typeof AppState.getUserStates === 'function') {
const states = AppState.getUserStates();
return states[username] || 'unknown';
}
console.warn("AppState.getUserStates is not available in getUserActualState. Returning 'unknown'.");
return 'unknown'; // Fallback if AppState is not ready
}
/**
* Get state value for sorting (numeric value)
* @param {string} username - Username to get state value for
* @returns {number} - Numeric state value for sorting
*/
export function getUserStateValue(username) {
// Return a numeric value for sorting states
// Working (2) > Unknown (1) > Not Working (0)
const state = getUserActualState(username);
if (state === 'working') {
return 2;
} else if (state === 'unknown') {
return 1;
} else {
return 0;
}
}
/**
* Update displayed states in the table
*/
export function updateTableStates() {
// Get all user rows
const userRows = document.querySelectorAll('.user-link');
userRows.forEach(userLink => {
const username = userLink.getAttribute('data-user');
const row = userLink.closest('tr');
const stateCell = row.querySelector('.user-state-cell');
if (stateCell) {
stateCell.innerHTML = getUserStateDisplay(username);
}
});
}
/**
* Refresh user states from server
* @param {boolean} forceRefresh - Force refresh even if cache is valid
* @returns {Promise