Update README and enhance device management logic

- Updated Trendshift repository link in README.md.
- Improved error handling and user feedback in deviceDetails.php:
  - Added warnings for device not found scenarios.
  - Refactored device name and owner retrieval logic with caching and REST API fallback.
This commit is contained in:
Jokob @NetAlertX
2026-05-09 23:35:21 +00:00
parent 300820e6bd
commit 1def218db5
2 changed files with 58 additions and 25 deletions

View File

@@ -157,7 +157,7 @@ Check the [GitHub Issues](https://github.com/netalertx/NetAlertX/issues) for the
## Everything else
<!--- --------------------------------------------------------------------- --->
<a href="https://trendshift.io/repositories/12670" target="_blank"><img src="https://trendshift.io/api/badge/repositories/12670" alt="jokob-sk%2FNetAlertX | Trendshift" style="width: 250px; height: 55px;" width="250" height="55"/></a>
<a href="https://trendshift.io/repositories/19712" target="_blank"><img src="https://trendshift.io/api/badge/repositories/19712" alt="jokob-sk%2FNetAlertX | Trendshift" style="width: 250px; height: 55px;" width="250" height="55"/></a>
### 📧 Get notified what's new

View File

@@ -307,7 +307,10 @@ function updateChevrons(currentMac) {
pos = refreshedList.findIndex(item => item.devMac === currentMac);
if (pos === -1) {
console.error('Still not found after re-cache:', currentMac);
console.warn('Device not found in device list after re-cache — hiding navigation controls:', currentMac);
$('#txtRecord').hide();
$('#btnPrevious').hide();
$('#btnNext').hide();
return;
}
@@ -499,26 +502,9 @@ async function renderSmallBoxes() {
}
}
function updateDevicePageName(mac) {
let name = getDevDataByMac(mac, "devName");
let owner = getDevDataByMac(mac, "devOwner");
// If data is missing, re-cache and retry once
if (mac != 'new' && (name === null|| owner === null)) {
console.warn("Device not found in cache, retrying after re-cache:", mac);
showSpinner();
cacheDevices(true).then(() => {
hideSpinner();
// Retry after successful cache
updateDevicePageName(mac);
}).catch((err) => {
hideSpinner();
console.error("Failed to refresh devices:", err);
});
return; // Exit early to avoid showing bad data
}
// Page title - Name
// ----------------------------------------
// Write device name/owner into page title DOM. Pure DOM side-effect, no data fetching.
function applyDevicePageTitle(mac, name, owner) {
let pageTitleText;
if (mac === "new") {
@@ -530,12 +516,12 @@ function updateDevicePageName(mac) {
`<i class="fa fa-circle-info"></i> ` + getString("Gen_create_new_device_info")
);
$('#devicePageInfoPlc').show();
} else if (!owner || name.toString().includes(owner)) {
pageTitleText = name;
} else if (!owner || (name && name.toString().includes(owner))) {
pageTitleText = name ?? getString("DevDetail_EveandAl_NewDevice");
$('#pageTitle').html(pageTitleText);
$('#devicePageInfoPlc').hide();
} else {
pageTitleText = `${name} (${owner})`;
pageTitleText = `${name ?? getString("DevDetail_EveandAl_NewDevice")} (${owner})`;
$('#pageTitle').html(pageTitleText);
$('#devicePageInfoPlc').hide();
}
@@ -544,6 +530,53 @@ function updateDevicePageName(mac) {
$('title').html(pageTitleText + ' - ' + $('title').html());
}
// ----------------------------------------
// Resolve device name/owner for the page title.
// Stage 1: localStorage cache (synchronous, fast path).
// Stage 2: one forced re-cache from table_devices.json.
// Stage 3: REST API fallback so a direct-link visit never loops.
async function updateDevicePageName(mac) {
let name = getDevDataByMac(mac, "devName");
let owner = getDevDataByMac(mac, "devOwner");
// Stage 2: one re-cache attempt
if (mac !== 'new' && (name === null || owner === null)) {
console.warn("Device not in cache, attempting re-cache:", mac);
showSpinner();
try {
await cacheDevices(true);
} catch (err) {
console.error("Re-cache failed:", err);
} finally {
hideSpinner();
}
name = getDevDataByMac(mac, "devName");
owner = getDevDataByMac(mac, "devOwner");
}
// Stage 3: REST fallback — same endpoint renderSmallBoxes uses, always DB-direct
if (mac !== 'new' && (name === null || owner === null)) {
console.warn("Device not found in cache after re-cache, falling back to REST API:", mac);
try {
const { apiBase, authHeader } = getAuthContext();
const res = await fetch(`${apiBase}/device/${encodeURIComponent(mac)}`, {
headers: authHeader
});
if (res.ok) {
const data = await res.json();
name = data.devName ?? null;
owner = data.devOwner ?? null;
} else {
console.error("REST fallback for device name returned:", res.status);
}
} catch (err) {
console.error("REST fallback error:", err);
}
}
applyDevicePageTitle(mac, name, owner);
}
//-----------------------------------------------------------------------------------