Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/components/modals/information.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React from 'react';
import styled from 'styled-components';
import { AccentButtonLarge } from '../inputs/accent-button';

interface InformationModalProps {
children?: React.ReactNode;
closeModal: Function;
}

const ModalBackgroundDiv = styled.div`
background-color: rgba(0,0,0,0.7);
display: flex;
flex-wrap: wrap;
position: fixed;
right: 0;
top: 0;
justify-content: center;
align-content: center;
height: 100%;
width: 100%;
z-index: 2;
padding: 0;
margin: 0;
`;

const ModalDiv = styled.div`
background: var(--bg_gradient);
display: flex;
flex-direction: column;
justify-content: center;
align-content: center;
height: wrap-content;
max-width: 80em;
border: 0.5em solid var(--border_color_cell);
border-radius: 0.5em;
padding: 1em;
`;

export const InformationModal: React.FC<InformationModalProps> = ({ children, closeModal }) => (
<ModalBackgroundDiv>
<ModalDiv>
{children}
<AccentButtonLarge onClick={() => closeModal()}>Okay</AccentButtonLarge>
</ModalDiv>
</ModalBackgroundDiv>
);
58 changes: 58 additions & 0 deletions src/components/monospace-text/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import React from 'react';
import styled from 'styled-components';
import { IconButtonContainer } from '../inputs/icon-button';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { IconButtonTooltip } from '../inputs/tooltip';
import { faCopy } from '@fortawesome/free-solid-svg-icons';

interface MonospaceTextProps {
text: string;
}

const TextComponent = styled.div`
font-family: monospace;
position: relative;
`;

const ClipboardButtonDiv = styled.div`
position: absolute;
top: 0;
right: 0;
`;

export const MonospaceText: React.FC<MonospaceTextProps> = ({ text }) => {
return (
<TextComponent>
<ul>
{text?.split('\n').map((line, i) => (
<li key={i}>
{line.split('').map((char, i) => {
if (char === '\t') {
return <>
<span>&nbsp;</span>
<span>&nbsp;</span>
<span>&nbsp;</span>
<span>&nbsp;</span>
</>
}
if (char === ' ') {
return <span>&nbsp;</span>;
}
return <span>{char}</span>
})}
</li>
))}
</ul>
<ClipboardButtonDiv>
<IconButtonContainer onClick={() => navigator.clipboard.writeText(text)}>
<FontAwesomeIcon
size={'sm'}
color="var(--color_label)"
icon={faCopy}
/>
<IconButtonTooltip>Copy to clipboard</IconButtonTooltip>
</IconButtonContainer>
</ClipboardButtonDiv>
</TextComponent>
)
};
77 changes: 50 additions & 27 deletions src/components/panes/errors.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
faComputer,
faDownload,
faKeyboard,
faMagicWandSparkles,
faWarning,
} from '@fortawesome/free-solid-svg-icons';
import {FontAwesomeIcon} from '@fortawesome/react-fontawesome';
Expand Down Expand Up @@ -31,6 +32,7 @@ import {
CategoryIconContainer,
} from './grid';
import {Pane} from './pane';
import { InformationModal } from '../modals/information';

const Container = styled.div`
display: flex;
Expand Down Expand Up @@ -81,34 +83,55 @@ const ErrorListContainer: React.FC<
const AppErrors: React.FC<{}> = ({}) => {
const errors = useAppSelector(getAppErrors);
const dispatch = useDispatch();
const [fixDialog, setFixDialog] = useState<any>(undefined);
return (
<ErrorListContainer
clear={() => dispatch(clearAppErrors())}
save={() => saveAppErrors(errors)}
hasErrors={!!errors.length}
>
{errors.map(
({
timestamp,
deviceInfo: {productId, productName, vendorId},
message: error,
}) => (
<Container key={timestamp}>
{timestamp}
<ul>
{error?.split('\n').map((line) => (
<li>{line}</li>
))}
</ul>
<ul>
<li>Device: {productName}</li>
<li>Vid: {printId(vendorId)}</li>
<li>Pid: {printId(productId)}</li>
</ul>
</Container>
),
)}
</ErrorListContainer>
<>
{fixDialog !== undefined && fixDialog}
<ErrorListContainer
clear={() => dispatch(clearAppErrors())}
save={() => saveAppErrors(errors)}
hasErrors={!!errors.length}
>
{errors.map(
({
timestamp,
deviceInfo: {productId, productName, vendorId},
message: error,
isPotentiallyUserFixable: isPotentiallyUserFixable,
userFix: userFix,
}) => (
<Container key={timestamp}>
{timestamp}
<ul>
{error?.split('\n').map((line, i) => (
<li key={i}>{line}</li>
))}
</ul>
<ul>
<li>Device: {productName}</li>
<li>Vid: {printId(vendorId)}</li>
<li>Pid: {printId(productId)}</li>
</ul>
{isPotentiallyUserFixable && userFix !== undefined &&
<IconButtonContainer onClick={() => setFixDialog(
<InformationModal closeModal={() => setFixDialog(undefined)}>
{userFix()}
</InformationModal>
)}
>
<FontAwesomeIcon
size={'sm'}
color="var(--color_label)"
icon={faMagicWandSparkles}
/>
<IconButtonTooltip>Fix</IconButtonTooltip>
</IconButtonContainer>
}
</Container>
),
)}
</ErrorListContainer>
</>
);
};

Expand Down
2 changes: 2 additions & 0 deletions src/store/errorsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export type AppError = {
timestamp: string;
message: string;
deviceInfo: DeviceInfo;
isPotentiallyUserFixable?: boolean;
userFix?: () => JSX.Element | undefined;
};

export const extractDeviceInfo = (device: DeviceInfo): DeviceInfo => ({
Expand Down
4 changes: 4 additions & 0 deletions src/utils/keyboard-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
logAppError,
logKeyboardAPIError,
} from 'src/store/errorsSlice';
import { getUserFixForError } from './user-fixable-errors';

// VIA Command IDs

Expand Down Expand Up @@ -625,10 +626,13 @@ export class KeyboardAPI {
res(ans);
} catch (e: any) {
const deviceInfo = extractDeviceInfo(this.getHID());
const [isPotentiallyUserFixable, userFix] = getUserFixForError(e, deviceInfo);
store.dispatch(
logAppError({
message: getMessageFromError(e),
deviceInfo,
isPotentiallyUserFixable,
userFix,
}),
);
rej(e);
Expand Down
49 changes: 49 additions & 0 deletions src/utils/user-fixable-errors.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { MonospaceText } from 'src/components/monospace-text';
import { DeviceInfo } from 'src/types/types';

const LinuxHidrawFix = (deviceInfo: DeviceInfo): () => JSX.Element => {
const textUdev = `SUBSYSTEM=="usb", ATTR{idVendor}=="${deviceInfo.vendorId.toString(16).toUpperCase().padStart(4, '0')}", ATTR{idProduct}=="${deviceInfo.productId.toString(16).toUpperCase().padStart(4, '0')}", TAG+="uaccess""
Copy link
Copy Markdown

@Forage Forage Jul 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've got a double quote too many at the end of line 5

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching!

KERNEL=="hidraw*", MODE="0660", TAG+="uaccess", TAG+="udev-acl"`

const textDevHidraw = `#!/bin/bash
HID_NAME='${deviceInfo.productName}'
# loop over possible devices
for f in /dev/hidraw*
do
DEVICE_NAME=$(basename \${f})
if grep "$HID_NAME" "/sys/class/hidraw/\${DEVICE_NAME}/device/uevent";
then
# device matches product name
echo Running sudo chmod a+rw "$f"
sudo chmod a+rw "$f"
fi
done`;

const textRunDevHidrawScript = `bash script.sh`;

return () => (
<div>
<p>This error can happen on Linux when the browser is not allowed to access the keyboard HID device. You can fix this either permanently or temporarily.</p>
<p>Create udev rule to fix it permanently. To do this, create a file called</p>
{/* Rule has to precede /usr/lib/udev/rules.d/73-seat-late.rules, see https://wiki.archlinux.org/title/Udev#Allowing_regular_users_to_use_devices */}
<MonospaceText text="/etc/udev/rules.d/50-qmk.rules" />
<p>with the following content:</p>
<MonospaceText text={textUdev} />
<p>Unplug and plug your keyboard back in. Reload this website and it should work.</p>
<p>If you want to temporarily allow the browser access to the keyboard device, create a script with the following content:</p>
<MonospaceText text={textDevHidraw} />
<p>And run it:</p>
<MonospaceText text={textRunDevHidrawScript} />
<p>Reload this website and it should work.</p>
</div>
)
};

export function getUserFixForError(e: any, deviceInfo: DeviceInfo): [boolean, () => JSX.Element | undefined] {
if (e instanceof DOMException) {
if (e.name === 'NotAllowedError' && e.message.includes('Failed to open the device')) {
return [true, LinuxHidrawFix(deviceInfo)];
}
}
return [false, () => undefined];
}