mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-30 03:46:29 -05:00
controller: Desktop start/stop toggle; rename hotspot/bluetooth actions to what they do
Add a Desktop toggle to the Controller (both UIs) that starts/stops display-manager.service. Fail open: the unit is never enabled/disabled, so the desktop always returns on reboot; users who want it disabled across reboots can set the default target themselves. Rename the hotspot and bluetooth actions to reflect their real behavior: - enable/disable_hotspot -> start/stop_hotspot (/api/hotspot/start|stop) - enable/disable_bluetooth -> unblock/block_bluetooth (rfkill semantics, /api/bluetooth/unblock|block) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
parent
a4eeba14ee
commit
3e559e1bf8
14 changed files with 712 additions and 132 deletions
|
|
@ -21,7 +21,7 @@ import {
|
|||
} from 'semantic-ui-react';
|
||||
import {Link, NavLink, useNavigate} from "react-router";
|
||||
import Message from "semantic-ui-react/dist/commonjs/collections/Message";
|
||||
import {useBluetooth, useHotspot, useSearchDirectories, useSearchOrder, useThrottle, useWROLMode} from "../hooks/customHooks";
|
||||
import {useBluetooth, useDesktop, useHotspot, useSearchDirectories, useSearchOrder, useThrottle, useWROLMode} from "../hooks/customHooks";
|
||||
import {Media, SettingsContext, StatusContext, ThemeContext} from "../contexts/contexts";
|
||||
import {
|
||||
Accordion,
|
||||
|
|
@ -1019,6 +1019,49 @@ export function BluetoothToggle() {
|
|||
</div>;
|
||||
}
|
||||
|
||||
export function DesktopToggle() {
|
||||
let {on, setDesktop} = useDesktop();
|
||||
const [confirmOpen, setConfirmOpen] = React.useState(false);
|
||||
const disabled = on === null;
|
||||
|
||||
const handleChange = (checked) => {
|
||||
if (checked) {
|
||||
setDesktop(true);
|
||||
} else {
|
||||
// Stopping the desktop kills any session on this WROLPi's own screen.
|
||||
setConfirmOpen(true);
|
||||
}
|
||||
}
|
||||
|
||||
const handleConfirmStop = (e) => {
|
||||
if (e) {
|
||||
e.preventDefault()
|
||||
}
|
||||
setConfirmOpen(false);
|
||||
setDesktop(false);
|
||||
}
|
||||
|
||||
return <div style={{margin: '0.5em'}}>
|
||||
<Confirm
|
||||
open={confirmOpen}
|
||||
onCancel={() => setConfirmOpen(false)}
|
||||
onClose={() => setConfirmOpen(false)}
|
||||
onConfirm={handleConfirmStop}
|
||||
header='Stop the desktop'
|
||||
content={"Anyone using this WROLPi's own screen will lose their session."
|
||||
+ ' The desktop will return on the next reboot. Are you sure?'}
|
||||
confirmButton='Stop'
|
||||
/>
|
||||
<Toggle
|
||||
label='Desktop'
|
||||
disabled={disabled}
|
||||
checked={on === true}
|
||||
onChange={handleChange}
|
||||
/>
|
||||
{disabled && <InfoPopup content='The Desktop is not supported on this server'/>}
|
||||
</div>;
|
||||
}
|
||||
|
||||
export function Toggle({label, checked, disabled, onChange, icon, popupContent = null, info = null}) {
|
||||
// Custom toggle because Semantic UI does not handle inverted labels correctly.
|
||||
const {t, inverted} = useContext(ThemeContext);
|
||||
|
|
@ -1352,14 +1395,14 @@ export function HotspotStatusIcon() {
|
|||
const {on, inUse, setHotspot, dockerized, hotspotSsid} = useHotspot();
|
||||
const {modal: unsupportedModal, doOpen: openUnsupportedModal} =
|
||||
UnsupportedModal('Unsupported', 'You cannot toggle the hotspot on this machine.');
|
||||
const [disableHotspotOpen, setDisableHotspotOpen] = React.useState(false);
|
||||
const [stopHotspotOpen, setStopHotspotOpen] = React.useState(false);
|
||||
const [inUseOpen, setInUseOpen] = React.useState(false);
|
||||
|
||||
const handleConfirmDisable = (e) => {
|
||||
const handleConfirmStop = (e) => {
|
||||
if (e) {
|
||||
e.preventDefault()
|
||||
}
|
||||
setDisableHotspotOpen(false);
|
||||
setStopHotspotOpen(false);
|
||||
setHotspot(false);
|
||||
}
|
||||
|
||||
|
|
@ -1372,8 +1415,8 @@ export function HotspotStatusIcon() {
|
|||
}
|
||||
|
||||
if (inUse === true) {
|
||||
const content = hotspotSsid ? `Wifi device is in use for ${hotspotSsid}. Disconnect and enable hotspot?`
|
||||
: 'Wifi device is in use. Disconnect and enable hotspot?'
|
||||
const content = hotspotSsid ? `Wifi device is in use for ${hotspotSsid}. Disconnect and start hotspot?`
|
||||
: 'Wifi device is in use. Disconnect and start hotspot?'
|
||||
return <>
|
||||
<Confirm open={inUseOpen}
|
||||
onCancel={() => setInUseOpen(false)}
|
||||
|
|
@ -1381,7 +1424,7 @@ export function HotspotStatusIcon() {
|
|||
onConfirm={handleConfirmInUse}
|
||||
header='Wifi is in-use'
|
||||
content={content}
|
||||
confirmButton='Enable Hotspot'
|
||||
confirmButton='Start Hotspot'
|
||||
/>
|
||||
<a href='#' onClick={() => setInUseOpen(true)}>
|
||||
<IconGroup size='large'>
|
||||
|
|
@ -1393,15 +1436,15 @@ export function HotspotStatusIcon() {
|
|||
} else if (dockerized === false && on === true) {
|
||||
return <>
|
||||
<Confirm
|
||||
open={disableHotspotOpen}
|
||||
onCancel={() => setDisableHotspotOpen(false)}
|
||||
onClose={() => setDisableHotspotOpen(false)}
|
||||
onConfirm={handleConfirmDisable}
|
||||
header='Disable the hotspot'
|
||||
open={stopHotspotOpen}
|
||||
onCancel={() => setStopHotspotOpen(false)}
|
||||
onClose={() => setStopHotspotOpen(false)}
|
||||
onConfirm={handleConfirmStop}
|
||||
header='Stop the hotspot'
|
||||
content='You will be disconnected when using the hotspot. Are you sure?'
|
||||
confirmButton='Disable'
|
||||
confirmButton='Stop'
|
||||
/>
|
||||
<a href='#' onClick={() => setDisableHotspotOpen(true)}>
|
||||
<a href='#' onClick={() => setStopHotspotOpen(true)}>
|
||||
<IconGroup size='large'>
|
||||
<Icon name='wifi' disabled={on !== true}/>
|
||||
{on === false && <Icon corner name='x'/>}
|
||||
|
|
|
|||
58
app/src/components/DesktopToggle.test.js
Normal file
58
app/src/components/DesktopToggle.test.js
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
import React from 'react';
|
||||
import {render, screen} from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import {DesktopToggle} from './Common';
|
||||
|
||||
const mockSetDesktop = jest.fn();
|
||||
let mockDesktopState = {on: true};
|
||||
|
||||
jest.mock('../hooks/customHooks', () => ({
|
||||
...jest.requireActual('../hooks/customHooks'),
|
||||
useDesktop: () => ({...mockDesktopState, setDesktop: mockSetDesktop}),
|
||||
}));
|
||||
|
||||
describe('DesktopToggle', () => {
|
||||
beforeEach(() => {
|
||||
mockSetDesktop.mockClear();
|
||||
mockDesktopState = {on: true};
|
||||
});
|
||||
|
||||
it('renders checked when the desktop is running', () => {
|
||||
render(<DesktopToggle/>);
|
||||
expect(screen.getByTestId('toggle')).toBeChecked();
|
||||
});
|
||||
|
||||
it('starts the desktop immediately when toggled on', async () => {
|
||||
mockDesktopState = {on: false};
|
||||
render(<DesktopToggle/>);
|
||||
await userEvent.click(screen.getByTestId('toggle'));
|
||||
expect(mockSetDesktop).toHaveBeenCalledWith(true);
|
||||
});
|
||||
|
||||
it('asks for confirmation before stopping the desktop', async () => {
|
||||
render(<DesktopToggle/>);
|
||||
await userEvent.click(screen.getByTestId('toggle'));
|
||||
// Not stopped yet; the Confirm modal is shown instead.
|
||||
expect(mockSetDesktop).not.toHaveBeenCalled();
|
||||
expect(screen.getByText('Stop the desktop')).toBeInTheDocument();
|
||||
|
||||
await userEvent.click(screen.getByText('Stop'));
|
||||
expect(mockSetDesktop).toHaveBeenCalledWith(false);
|
||||
});
|
||||
|
||||
it('cancelling the confirmation leaves the desktop running', async () => {
|
||||
render(<DesktopToggle/>);
|
||||
await userEvent.click(screen.getByTestId('toggle'));
|
||||
await userEvent.click(screen.getByText('Cancel'));
|
||||
expect(mockSetDesktop).not.toHaveBeenCalled();
|
||||
expect(screen.getByTestId('toggle')).toBeChecked();
|
||||
});
|
||||
|
||||
it('does nothing when the desktop is not supported', async () => {
|
||||
mockDesktopState = {on: null};
|
||||
render(<DesktopToggle/>);
|
||||
await userEvent.click(screen.getByTestId('toggle'));
|
||||
expect(mockSetDesktop).not.toHaveBeenCalled();
|
||||
expect(screen.queryByText('Stop the desktop')).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
import React from 'react';
|
||||
import {Checkbox, Container, Dropdown, Form, Icon, Input} from "semantic-ui-react";
|
||||
import {Button, Confirm, Header, Loader, Modal, Segment, Table} from "../Theme";
|
||||
import {APIButton, BluetoothToggle, DirectorySearch, HandPointMessage, HotspotToggle, InfoMessage, ThrottleToggle, Toggle,} from "../Common";
|
||||
import {APIButton, BluetoothToggle, DesktopToggle, DirectorySearch, HandPointMessage, HotspotToggle, InfoMessage, ThrottleToggle, Toggle,} from "../Common";
|
||||
import {useDockerized, useMediaDirectory} from "../../hooks/customHooks";
|
||||
import {Media} from "../../contexts/contexts";
|
||||
import {
|
||||
|
|
@ -1589,6 +1589,7 @@ function AdminControlsSection() {
|
|||
|
||||
<HotspotToggle/>
|
||||
<BluetoothToggle/>
|
||||
<DesktopToggle/>
|
||||
<ThrottleToggle/>
|
||||
|
||||
<HotspotSettingsForm/>
|
||||
|
|
|
|||
|
|
@ -79,6 +79,7 @@ jest.mock('../../hooks/customHooks', () => ({
|
|||
useDockerized: jest.fn().mockReturnValue(false),
|
||||
useMediaDirectory: jest.fn().mockReturnValue('/media/wrolpi'),
|
||||
useBluetooth: jest.fn().mockReturnValue({on: false, setBluetooth: jest.fn()}),
|
||||
useDesktop: jest.fn().mockReturnValue({on: true, setDesktop: jest.fn()}),
|
||||
useHotspot: jest.fn().mockReturnValue({on: true, setHotspot: jest.fn()}),
|
||||
useThrottle: jest.fn().mockReturnValue({on: false, setThrottle: jest.fn()}),
|
||||
}));
|
||||
|
|
@ -89,6 +90,7 @@ jest.mock('../Common', () => {
|
|||
return {
|
||||
...actual,
|
||||
BluetoothToggle: () => <div data-testid="bluetooth-toggle">BluetoothToggle</div>,
|
||||
DesktopToggle: () => <div data-testid="desktop-toggle">DesktopToggle</div>,
|
||||
HotspotToggle: () => <div data-testid="hotspot-toggle">HotspotToggle</div>,
|
||||
ThrottleToggle: () => <div data-testid="throttle-toggle">ThrottleToggle</div>,
|
||||
Toggle: ({label, checked, onChange}) => (
|
||||
|
|
@ -174,6 +176,10 @@ describe('ControllerPage', () => {
|
|||
await waitFor(() => {
|
||||
expect(screen.getByText('Hardware Controls')).toBeInTheDocument();
|
||||
});
|
||||
expect(screen.getByTestId('hotspot-toggle')).toBeInTheDocument();
|
||||
expect(screen.getByTestId('bluetooth-toggle')).toBeInTheDocument();
|
||||
expect(screen.getByTestId('desktop-toggle')).toBeInTheDocument();
|
||||
expect(screen.getByTestId('throttle-toggle')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
test('calls getServices on mount', async () => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue