2023-04-09 14:34:33 -06:00
|
|
|
import React from "react";
|
2024-08-15 21:45:06 -06:00
|
|
|
import {Link, NavLink} from "react-router-dom";
|
|
|
|
|
import {Dropdown, Icon as SIcon, Menu} from "semantic-ui-react";
|
|
|
|
|
import {Media, SettingsContext, StatusContext, ThemeContext} from "../contexts/contexts";
|
2025-01-10 11:33:21 -07:00
|
|
|
import {DarkModeToggle, HotspotStatusIcon, isReactChildNull, useLocalStorage} from "./Common";
|
2023-04-10 13:15:34 -06:00
|
|
|
import {ShareButton} from "./Share";
|
2025-01-10 11:33:21 -07:00
|
|
|
import {useCPUTemperature, useIOStats, useLoad, useMemoryStats, usePowerStats, useWROLMode} from "../hooks/customHooks";
|
2024-01-01 11:31:17 -07:00
|
|
|
import {SearchIconButton} from "./Search";
|
2025-01-10 11:33:21 -07:00
|
|
|
import {Icon, Popup} from "./Theme";
|
2024-11-02 15:14:02 -06:00
|
|
|
import {HELP_VIEWER_URI, NAME} from "./Vars";
|
2024-12-18 16:39:50 -07:00
|
|
|
import _ from "lodash";
|
2021-11-01 16:42:52 -06:00
|
|
|
|
2023-02-21 15:49:11 -07:00
|
|
|
const links = [
|
|
|
|
|
{text: 'Videos', to: '/videos', key: 'videos'},
|
|
|
|
|
{text: 'Archive', to: '/archive', key: 'archive'},
|
|
|
|
|
{text: 'Map', to: '/map', key: 'map'},
|
2023-05-29 18:04:03 -06:00
|
|
|
{text: 'Zim', to: '/zim', key: 'zim'},
|
2023-02-21 15:49:11 -07:00
|
|
|
{text: 'Files', to: '/files', key: 'files'},
|
2023-03-29 13:46:39 -06:00
|
|
|
{
|
|
|
|
|
text: 'More', key: 'more',
|
2023-02-21 15:49:11 -07:00
|
|
|
links: [
|
2023-05-29 18:04:03 -06:00
|
|
|
{text: 'Inventory', to: '/inventory', key: 'inventory'},
|
2023-02-21 15:49:11 -07:00
|
|
|
{to: '/more/otp', text: 'One Time Pad', end: true},
|
2023-06-11 12:31:56 -06:00
|
|
|
{to: '/more/vin', text: 'VIN Decoder', end: true},
|
2024-05-31 23:53:30 -06:00
|
|
|
{to: '/more/calculators', text: 'Calculators', end: true},
|
2023-07-17 17:32:29 -06:00
|
|
|
{to: '/more/statistics', text: 'Statistics', end: true},
|
2023-02-21 15:49:11 -07:00
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
];
|
2024-10-14 14:44:35 -06:00
|
|
|
const help = {to: HELP_VIEWER_URI, text: 'Help', key: 'help', target: '_blank'};
|
2022-11-04 09:19:51 -06:00
|
|
|
const admin = {to: '/admin', text: 'Admin', key: 'admin'};
|
2023-10-15 17:18:33 -06:00
|
|
|
const rightLinks = [help, admin];
|
2021-11-01 16:42:52 -06:00
|
|
|
|
2023-10-15 17:19:51 -06:00
|
|
|
const collapsedLinks = links.concat([help, admin]);
|
2021-11-01 16:42:52 -06:00
|
|
|
|
2023-02-21 15:32:11 -07:00
|
|
|
function DropdownLinks({link}) {
|
|
|
|
|
return <Dropdown item text={link.text} direction='left'>
|
2023-01-07 18:31:39 -07:00
|
|
|
<Dropdown.Menu>
|
2023-02-21 15:32:11 -07:00
|
|
|
{link.links.map(l => <MenuLink key={l.to} link={l}/>)}
|
2023-01-07 18:31:39 -07:00
|
|
|
</Dropdown.Menu>
|
2023-02-17 16:09:24 -07:00
|
|
|
</Dropdown>
|
2021-11-01 16:42:52 -06:00
|
|
|
}
|
|
|
|
|
|
2022-11-04 09:19:51 -06:00
|
|
|
function MenuLink({link}) {
|
2022-01-29 22:25:35 -07:00
|
|
|
// Wrapper around NavLink to handle Navlink/Dropdown change.
|
2023-02-21 15:49:11 -07:00
|
|
|
if (link.links) {
|
|
|
|
|
return <DropdownLinks link={link}/>
|
|
|
|
|
} else {
|
2022-11-04 09:19:51 -06:00
|
|
|
const end = link.end ? {end: true} : {end: undefined};
|
2024-10-14 14:44:35 -06:00
|
|
|
const target = link.target ? {target: link.target} : {};
|
2023-02-17 16:09:24 -07:00
|
|
|
return <NavLink
|
2023-02-21 15:49:11 -07:00
|
|
|
className='item'
|
2023-01-07 18:31:39 -07:00
|
|
|
to={link.to}
|
|
|
|
|
{...end}
|
2024-10-14 14:44:35 -06:00
|
|
|
{...target}
|
2023-01-07 18:31:39 -07:00
|
|
|
>
|
|
|
|
|
{link.text}
|
2023-02-17 16:09:24 -07:00
|
|
|
</NavLink>
|
2021-11-01 16:42:52 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-10 11:33:21 -07:00
|
|
|
function NavIconWrapper({children}) {
|
|
|
|
|
// Do not add margins around empty react element to avoid filling up navbar.
|
|
|
|
|
const emptyChild = !children || isReactChildNull(children);
|
|
|
|
|
const style = emptyChild ? {} : {marginTop: '0.8em', marginLeft: '1.5em'};
|
|
|
|
|
return <div style={style}>{children}</div>
|
2022-10-19 09:18:53 -06:00
|
|
|
}
|
|
|
|
|
|
2024-10-05 14:06:13 -06:00
|
|
|
function useNavColorSetting() {
|
|
|
|
|
// Use localstorage to avoid flickering navbar color on startup.
|
|
|
|
|
const {settings} = React.useContext(SettingsContext);
|
|
|
|
|
const [navColor, setNavColor] = useLocalStorage('nav_color', 'violet');
|
|
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
2024-12-18 17:31:50 -07:00
|
|
|
if (!_.isEmpty(settings)) {
|
|
|
|
|
setNavColor(settings.nav_color);
|
|
|
|
|
}
|
2024-10-05 14:06:13 -06:00
|
|
|
}, [settings.nav_color]);
|
|
|
|
|
|
|
|
|
|
return navColor
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-01 16:42:52 -06:00
|
|
|
export function NavBar() {
|
2023-07-21 15:14:09 -06:00
|
|
|
const wrolModeEnabled = useWROLMode();
|
2024-08-15 21:45:06 -06:00
|
|
|
const {status} = React.useContext(StatusContext);
|
2024-10-05 14:06:13 -06:00
|
|
|
const navColor = useNavColorSetting();
|
2023-02-09 13:47:25 -07:00
|
|
|
const wrolpiIcon = <img src='/icon.svg' height='32px' width='32px' alt='WROLPi Home Icon'/>;
|
2024-05-18 17:51:06 -06:00
|
|
|
const name = <i>{NAME || wrolpiIcon}</i>;
|
2024-08-15 21:45:06 -06:00
|
|
|
const topNavText = wrolModeEnabled ? <>{name} <SIcon name='lock'/></> : name;
|
2023-04-06 11:16:45 -06:00
|
|
|
const {i} = React.useContext(ThemeContext);
|
2022-08-06 05:22:07 +00:00
|
|
|
|
2023-02-09 13:47:25 -07:00
|
|
|
const homeLink = <NavLink className='item' to='/' style={{paddingTop: 0, paddingBottom: 0}}>
|
|
|
|
|
{topNavText}
|
|
|
|
|
</NavLink>;
|
2023-10-06 12:48:20 -06:00
|
|
|
|
2025-01-10 11:33:21 -07:00
|
|
|
// Red/Yellow colors will blend in to some navbar colors, swap the colors for high contrast.
|
|
|
|
|
const conflictingColors = ['red', 'orange', 'yellow', 'olive', 'pink'];
|
|
|
|
|
const lowWarningColor = conflictingColors.includes(navColor) ? 'white' : 'yellow';
|
|
|
|
|
const highWarningColor = conflictingColors.includes(navColor) ? 'black' : 'red';
|
|
|
|
|
|
|
|
|
|
// Generic system load, the least important warning.
|
|
|
|
|
const {minute_1, mediumLoad, highLoad} = useLoad();
|
|
|
|
|
let systemLoadIcon;
|
|
|
|
|
if (mediumLoad || highLoad) {
|
|
|
|
|
// System load is high, display a warning icon.
|
|
|
|
|
const color = highLoad ? highWarningColor : lowWarningColor;
|
|
|
|
|
const icon = <Link to='/admin/status'>
|
|
|
|
|
<Icon name='tachometer alternate' size='large' color={color}/>
|
|
|
|
|
</Link>;
|
|
|
|
|
systemLoadIcon = <Popup content={`Load: ${minute_1}`} trigger={icon}/>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RAM consumption.
|
|
|
|
|
const {percent: memoryPercent} = useMemoryStats();
|
|
|
|
|
let memoryIcon;
|
|
|
|
|
if (memoryPercent > 80) {
|
|
|
|
|
const color = memoryPercent > 90 ? highWarningColor : lowWarningColor;
|
|
|
|
|
const icon = <Link to='/admin/status' color={color}>
|
|
|
|
|
<Icon name='microchip' size='large'/>
|
|
|
|
|
</Link>;
|
|
|
|
|
memoryIcon = <Popup content={`System Memory: ${memoryPercent}%`} trigger={icon}/>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Any disk is busy and processes are waiting.
|
|
|
|
|
const {percentIOWait} = useIOStats();
|
|
|
|
|
let diskWaitIcon;
|
|
|
|
|
if (percentIOWait >= 50) {
|
|
|
|
|
// Processes are waiting on disk, display a warning icon.
|
|
|
|
|
const color = percentIOWait > 75 ? highWarningColor : lowWarningColor;
|
|
|
|
|
const icon = <Link to='/admin/status' color={color}>
|
|
|
|
|
<Icon name='disk' size='large'/>
|
|
|
|
|
</Link>;
|
|
|
|
|
diskWaitIcon = <Popup content={`Processes waiting on disk: ${percentIOWait}`} trigger={icon}/>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CPU temperature.
|
|
|
|
|
const {temperature, highTemperature, criticalTemperature} = useCPUTemperature();
|
|
|
|
|
let temperatureIcon;
|
|
|
|
|
if (temperature && temperature >= highTemperature) {
|
|
|
|
|
// CPU temperature is high, display a warning icon.
|
|
|
|
|
const color = temperature >= criticalTemperature ? highWarningColor : lowWarningColor;
|
|
|
|
|
const name = temperature >= criticalTemperature ? 'thermometer' : 'thermometer half';
|
|
|
|
|
const icon = <Icon data-testid='cpuTemperatureIcon' name={name} size='large' color={color}/>
|
|
|
|
|
const link = <Link to='/admin/status'>{icon}</Link>;
|
|
|
|
|
temperatureIcon = <Popup content={`CPU: ${temperature}°C`} trigger={link}/>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Power issues, this is always displayed if detected.
|
|
|
|
|
const {underVoltage, overCurrent} = usePowerStats();
|
|
|
|
|
let powerIcon;
|
|
|
|
|
if (underVoltage || overCurrent) {
|
|
|
|
|
const name = underVoltage ? 'power cord' : 'lightning';
|
|
|
|
|
const icon = <Icon name={name} size='large' color={highWarningColor}/>;
|
|
|
|
|
const message = underVoltage
|
|
|
|
|
? 'Under-voltage detected! Your power supply is insufficient!'
|
|
|
|
|
: 'Over-current detected! Your peripherals are using too much power!';
|
|
|
|
|
powerIcon = <Popup content={message} trigger={icon}/>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Display the temperature icon first because it can cause the system to throttle. The rest are in order of effects
|
|
|
|
|
// that will slow down the system and the user should address. Generic load is last because it is probably not an
|
|
|
|
|
// issue.
|
|
|
|
|
const warningIcon = temperatureIcon || diskWaitIcon || memoryIcon || systemLoadIcon;
|
2024-08-15 21:45:06 -06:00
|
|
|
|
2024-12-18 16:39:50 -07:00
|
|
|
let processingLink;
|
|
|
|
|
if (status && status.flags) {
|
|
|
|
|
if (status.flags.refreshing) {
|
|
|
|
|
processingLink = '/files';
|
|
|
|
|
} else if (status.flags.map_importing) {
|
|
|
|
|
processingLink = '/map/manage';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const processingIcon = processingLink &&
|
|
|
|
|
<Link to={processingLink}>
|
|
|
|
|
<Icon loading name='circle notch' size='large'/>
|
|
|
|
|
</Link>;
|
2023-10-06 12:48:20 -06:00
|
|
|
|
2023-01-07 18:31:39 -07:00
|
|
|
const icons = <React.Fragment>
|
2025-01-10 11:33:21 -07:00
|
|
|
<NavIconWrapper>{processingIcon}</NavIconWrapper>
|
|
|
|
|
<NavIconWrapper>{powerIcon}</NavIconWrapper>
|
|
|
|
|
<NavIconWrapper>{warningIcon}</NavIconWrapper>
|
|
|
|
|
<NavIconWrapper><ShareButton/></NavIconWrapper>
|
|
|
|
|
<NavIconWrapper><HotspotStatusIcon/></NavIconWrapper>
|
|
|
|
|
<NavIconWrapper><DarkModeToggle/></NavIconWrapper>
|
2023-01-07 18:31:39 -07:00
|
|
|
</React.Fragment>;
|
2021-11-01 16:42:52 -06:00
|
|
|
|
2023-01-07 18:31:39 -07:00
|
|
|
return <>
|
|
|
|
|
<Media at='mobile'>
|
2024-10-14 14:20:50 -06:00
|
|
|
<Menu {...i} attached='top' color={navColor} id='global_navbar'>
|
2023-01-07 18:31:39 -07:00
|
|
|
{homeLink}
|
|
|
|
|
<Menu.Menu position='right'>
|
|
|
|
|
{icons}
|
2024-12-18 16:39:50 -07:00
|
|
|
<SearchIconButton/>
|
2023-01-07 18:31:39 -07:00
|
|
|
<Dropdown item icon="bars">
|
|
|
|
|
<Dropdown.Menu>
|
2023-01-31 17:26:43 -07:00
|
|
|
{collapsedLinks.map(i => <MenuLink link={i} key={i.key}/>)}
|
2023-01-07 18:31:39 -07:00
|
|
|
</Dropdown.Menu>
|
|
|
|
|
</Dropdown>
|
|
|
|
|
</Menu.Menu>
|
|
|
|
|
</Menu>
|
|
|
|
|
</Media>
|
|
|
|
|
<Media greaterThanOrEqual='tablet'>
|
2024-10-14 14:20:50 -06:00
|
|
|
<Menu {...i} attached='top' color={navColor} id='global_navbar'>
|
2023-01-07 18:31:39 -07:00
|
|
|
{homeLink}
|
|
|
|
|
{links.map(i => <MenuLink link={i} key={i.key}/>)}
|
2021-11-01 16:42:52 -06:00
|
|
|
|
2023-01-07 18:31:39 -07:00
|
|
|
<Menu.Menu position='right'>
|
|
|
|
|
{icons}
|
2024-12-18 16:39:50 -07:00
|
|
|
<SearchIconButton/>
|
2023-01-07 18:31:39 -07:00
|
|
|
{rightLinks.map(i => <MenuLink link={i} key={i.key}/>)}
|
|
|
|
|
</Menu.Menu>
|
|
|
|
|
</Menu>
|
|
|
|
|
</Media>
|
|
|
|
|
</>
|
2021-11-01 16:42:52 -06:00
|
|
|
}
|