[verified] refactor: harden operations and redesign device console
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
export function fleetStatusKind(status) {
|
||||
if (!status) return 'unknown'
|
||||
if (!status.reachable) return 'offline'
|
||||
if (status.authenticated === false) return 'auth'
|
||||
return 'online'
|
||||
}
|
||||
|
||||
export function filterAndSortFleet(instances, statuses, { filter = 'all', query = '', sort = 'name' } = {}) {
|
||||
const needle = String(query).trim().toLocaleLowerCase()
|
||||
const rank = { online: 0, auth: 1, offline: 2, unknown: 3 }
|
||||
return instances.filter(item => {
|
||||
const status = statuses.get(item.id)
|
||||
if (filter !== 'all' && fleetStatusKind(status) !== filter) return false
|
||||
if (!needle) return true
|
||||
return [item.id, item.name, item.url, item.description, ...(item.tags || []), JSON.stringify(status?.summary || {})]
|
||||
.join(' ').toLocaleLowerCase().includes(needle)
|
||||
}).slice().sort((a, b) => {
|
||||
const sa = statuses.get(a.id); const sb = statuses.get(b.id)
|
||||
if (sort === 'latency') return (sa?.latencyMs ?? Infinity) - (sb?.latencyMs ?? Infinity) || String(a.name || a.id).localeCompare(String(b.name || b.id))
|
||||
if (sort === 'status') return rank[fleetStatusKind(sa)] - rank[fleetStatusKind(sb)] || String(a.name || a.id).localeCompare(String(b.name || b.id))
|
||||
return String(a.name || a.id).localeCompare(String(b.name || b.id))
|
||||
})
|
||||
}
|
||||
|
||||
export function emptyFleetReason({ total = 0, query = '', filter = 'all', visible = 0 } = {}) {
|
||||
if (!total) return 'config'
|
||||
if (!visible && String(query).trim()) return 'search'
|
||||
if (!visible && filter !== 'all') return 'filter'
|
||||
return null
|
||||
}
|
||||
Reference in New Issue
Block a user