aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Vijay A <vraravam@users.noreply.github.com>2024-04-19 09:02:56 +0530
committerLibravatar Vijay Aravamudhan <vraravam@users.noreply.github.com>2024-04-19 03:52:01 +0000
commit03a12413d632a2a1613091e44c52ce46b4adc5b8 (patch)
tree0086e0f98a644df91644b9cdde5ca1515188113a
parent6.7.3-nightly.13 [skip ci] (diff)
downloadferdium-app-03a12413d632a2a1613091e44c52ce46b4adc5b8.tar.gz
ferdium-app-03a12413d632a2a1613091e44c52ce46b4adc5b8.tar.zst
ferdium-app-03a12413d632a2a1613091e44c52ce46b4adc5b8.zip
Minor refactoring to move repeated code into utility class
-rw-r--r--src/components/downloadManager/DownloadManagerLayout.tsx4
-rw-r--r--src/components/services/tabs/TabItem.tsx6
-rw-r--r--src/components/settings/SettingsLayout.tsx4
-rw-r--r--src/components/settings/releaseNotes/ReleaseNotesLayout.tsx4
-rw-r--r--src/components/ui/input/index.tsx3
-rw-r--r--src/components/ui/select/index.tsx13
-rw-r--r--src/features/webControls/components/WebControls.tsx5
-rw-r--r--src/features/workspaces/components/WorkspaceDrawer.tsx2
-rw-r--r--src/jsUtils.ts6
-rw-r--r--test/jsUtils.test.ts54
10 files changed, 80 insertions, 21 deletions
diff --git a/src/components/downloadManager/DownloadManagerLayout.tsx b/src/components/downloadManager/DownloadManagerLayout.tsx
index 43670355c..09a1b2096 100644
--- a/src/components/downloadManager/DownloadManagerLayout.tsx
+++ b/src/components/downloadManager/DownloadManagerLayout.tsx
@@ -10,7 +10,7 @@ import {
10import { mdiClose } from '@mdi/js'; 10import { mdiClose } from '@mdi/js';
11import { Outlet } from 'react-router-dom'; 11import { Outlet } from 'react-router-dom';
12import type { Actions } from '../../actions/lib/actions'; 12import type { Actions } from '../../actions/lib/actions';
13import { isEscKeyPress } from '../../jsUtils'; 13import { isEscapeKeyPress } from '../../jsUtils';
14import Appear from '../ui/effects/Appear'; 14import Appear from '../ui/effects/Appear';
15import Icon from '../ui/icon'; 15import Icon from '../ui/icon';
16import ErrorBoundary from '../util/ErrorBoundary'; 16import ErrorBoundary from '../util/ErrorBoundary';
@@ -45,7 +45,7 @@ class DownloadManagerLayout extends Component<IProps> {
45 } 45 }
46 46
47 handleKeyDown(e: KeyboardEvent) { 47 handleKeyDown(e: KeyboardEvent) {
48 if (isEscKeyPress(e.key)) { 48 if (isEscapeKeyPress(e.key)) {
49 this.props.actions!.ui.closeSettings(); 49 this.props.actions!.ui.closeSettings();
50 } 50 }
51 } 51 }
diff --git a/src/components/services/tabs/TabItem.tsx b/src/components/services/tabs/TabItem.tsx
index bf193e136..0e999506a 100644
--- a/src/components/services/tabs/TabItem.tsx
+++ b/src/components/services/tabs/TabItem.tsx
@@ -19,7 +19,7 @@ import globalMessages from '../../../i18n/globalMessages';
19import type Service from '../../../models/Service'; 19import type Service from '../../../models/Service';
20import Icon from '../../ui/icon'; 20import Icon from '../../ui/icon';
21import MenuItemConstructorOptions = Electron.MenuItemConstructorOptions; 21import MenuItemConstructorOptions = Electron.MenuItemConstructorOptions;
22import { acceleratorString } from '../../../jsUtils'; 22import { acceleratorString, isShiftKeyPress } from '../../../jsUtils';
23 23
24const IS_SERVICE_DEBUGGING_ENABLED = ( 24const IS_SERVICE_DEBUGGING_ENABLED = (
25 localStorage.getItem('debug') || '' 25 localStorage.getItem('debug') || ''
@@ -171,8 +171,8 @@ class TabItem extends Component<IProps, IState> {
171 ); 171 );
172 } 172 }
173 173
174 handleShortcutIndex = (event, showShortcutIndex = true) => { 174 handleShortcutIndex = (event: KeyboardEvent, showShortcutIndex = true) => {
175 if (event.key === 'Shift') { 175 if (isShiftKeyPress(event.key)) {
176 this.setState({ showShortcutIndex }); 176 this.setState({ showShortcutIndex });
177 } 177 }
178 }; 178 };
diff --git a/src/components/settings/SettingsLayout.tsx b/src/components/settings/SettingsLayout.tsx
index a5fa13d71..3b5d81cdd 100644
--- a/src/components/settings/SettingsLayout.tsx
+++ b/src/components/settings/SettingsLayout.tsx
@@ -7,7 +7,7 @@ import {
7 injectIntl, 7 injectIntl,
8} from 'react-intl'; 8} from 'react-intl';
9import { Outlet } from 'react-router-dom'; 9import { Outlet } from 'react-router-dom';
10import { isEscKeyPress } from '../../jsUtils'; 10import { isEscapeKeyPress } from '../../jsUtils';
11import Appear from '../ui/effects/Appear'; 11import Appear from '../ui/effects/Appear';
12import Icon from '../ui/icon'; 12import Icon from '../ui/icon';
13import ErrorBoundary from '../util/ErrorBoundary'; 13import ErrorBoundary from '../util/ErrorBoundary';
@@ -41,7 +41,7 @@ class SettingsLayout extends Component<PropsWithChildren<IProps>> {
41 } 41 }
42 42
43 handleKeyDown(e: KeyboardEvent): void { 43 handleKeyDown(e: KeyboardEvent): void {
44 if (isEscKeyPress(e.key)) { 44 if (isEscapeKeyPress(e.key)) {
45 this.props.closeSettings(); 45 this.props.closeSettings();
46 } 46 }
47 } 47 }
diff --git a/src/components/settings/releaseNotes/ReleaseNotesLayout.tsx b/src/components/settings/releaseNotes/ReleaseNotesLayout.tsx
index 4fb31d053..1743bb5a1 100644
--- a/src/components/settings/releaseNotes/ReleaseNotesLayout.tsx
+++ b/src/components/settings/releaseNotes/ReleaseNotesLayout.tsx
@@ -9,7 +9,7 @@ import {
9import { mdiClose } from '@mdi/js'; 9import { mdiClose } from '@mdi/js';
10import { Outlet } from 'react-router-dom'; 10import { Outlet } from 'react-router-dom';
11import type { Actions } from '../../../actions/lib/actions'; 11import type { Actions } from '../../../actions/lib/actions';
12import { isEscKeyPress } from '../../../jsUtils'; 12import { isEscapeKeyPress } from '../../../jsUtils';
13import Appear from '../../ui/effects/Appear'; 13import Appear from '../../ui/effects/Appear';
14import Icon from '../../ui/icon'; 14import Icon from '../../ui/icon';
15import ErrorBoundary from '../../util/ErrorBoundary'; 15import ErrorBoundary from '../../util/ErrorBoundary';
@@ -44,7 +44,7 @@ class ReleaseNotesLayout extends Component<IProps> {
44 } 44 }
45 45
46 handleKeyDown(e: KeyboardEvent) { 46 handleKeyDown(e: KeyboardEvent) {
47 if (isEscKeyPress(e.key)) { 47 if (isEscapeKeyPress(e.key)) {
48 this.props.actions!.ui.closeSettings(); 48 this.props.actions!.ui.closeSettings();
49 } 49 }
50 } 50 }
diff --git a/src/components/ui/input/index.tsx b/src/components/ui/input/index.tsx
index 225517ba8..7e3179baf 100644
--- a/src/components/ui/input/index.tsx
+++ b/src/components/ui/input/index.tsx
@@ -16,6 +16,7 @@ import {
16 injectIntl, 16 injectIntl,
17} from 'react-intl'; 17} from 'react-intl';
18import withStyles, { type WithStylesProps } from 'react-jss'; 18import withStyles, { type WithStylesProps } from 'react-jss';
19import { isEnterKeyPress } from '../../../jsUtils';
19// biome-ignore lint/suspicious/noShadowRestrictedNames: <explanation> 20// biome-ignore lint/suspicious/noShadowRestrictedNames: <explanation>
20import Error from '../error'; 21import Error from '../error';
21import Icon from '../icon'; 22import Icon from '../icon';
@@ -96,7 +97,7 @@ class Input extends Component<IProps, IState> {
96 } 97 }
97 98
98 onInputKeyPress(e: KeyboardEvent<HTMLInputElement>): void { 99 onInputKeyPress(e: KeyboardEvent<HTMLInputElement>): void {
99 if (e.key === 'Enter') { 100 if (isEnterKeyPress(e.key)) {
100 const { onEnterKey = noop } = this.props; 101 const { onEnterKey = noop } = this.props;
101 onEnterKey(); 102 onEnterKey();
102 } 103 }
diff --git a/src/components/ui/select/index.tsx b/src/components/ui/select/index.tsx
index 397fd97ed..c19c12417 100644
--- a/src/components/ui/select/index.tsx
+++ b/src/components/ui/select/index.tsx
@@ -13,6 +13,11 @@ import {
13 createRef, 13 createRef,
14} from 'react'; 14} from 'react';
15import withStyles, { type WithStylesProps } from 'react-jss'; 15import withStyles, { type WithStylesProps } from 'react-jss';
16import {
17 isArrowDownKeyPress,
18 isArrowUpKeyPress,
19 isEnterKeyPress,
20} from '../../../jsUtils';
16import type { Theme } from '../../../themes'; 21import type { Theme } from '../../../themes';
17// biome-ignore lint/suspicious/noShadowRestrictedNames: <explanation> 22// biome-ignore lint/suspicious/noShadowRestrictedNames: <explanation>
18import Error from '../error'; 23import Error from '../error';
@@ -288,23 +293,23 @@ class SelectComponent extends Component<IProps, IState> {
288 293
289 if (!open) return; 294 if (!open) return;
290 295
291 if (e.key === 'ArrowUp' || e.key === 'ArrowDown') { 296 if (isArrowUpKeyPress(e.key) || isArrowDownKeyPress(e.key)) {
292 e.preventDefault(); 297 e.preventDefault();
293 } 298 }
294 299
295 if (this.componentRef?.current) { 300 if (this.componentRef?.current) {
296 if (e.key === 'ArrowUp' && selected > 0) { 301 if (isArrowUpKeyPress(e.key) && selected > 0) {
297 this.setState((state: IState) => ({ 302 this.setState((state: IState) => ({
298 selected: state.selected - 1, 303 selected: state.selected - 1,
299 })); 304 }));
300 } else if ( 305 } else if (
301 e.key === 'ArrowDown' && 306 isArrowDownKeyPress(e.key) &&
302 selected < Object.keys(options!).length - 1 307 selected < Object.keys(options!).length - 1
303 ) { 308 ) {
304 this.setState((state: IState) => ({ 309 this.setState((state: IState) => ({
305 selected: state.selected + 1, 310 selected: state.selected + 1,
306 })); 311 }));
307 } else if (e.key === 'Enter') { 312 } else if (isEnterKeyPress(e.key)) {
308 this.select(Object.keys(options!)[selected]); 313 this.select(Object.keys(options!)[selected]);
309 } 314 }
310 315
diff --git a/src/features/webControls/components/WebControls.tsx b/src/features/webControls/components/WebControls.tsx
index aee568a7d..25b23cbee 100644
--- a/src/features/webControls/components/WebControls.tsx
+++ b/src/features/webControls/components/WebControls.tsx
@@ -15,6 +15,7 @@ import {
15import withStyles, { type WithStylesProps } from 'react-jss'; 15import withStyles, { type WithStylesProps } from 'react-jss';
16import { Tooltip as ReactTooltip } from 'react-tooltip'; 16import { Tooltip as ReactTooltip } from 'react-tooltip';
17import Icon from '../../../components/ui/icon'; 17import Icon from '../../../components/ui/icon';
18import { isEnterKeyPress, isEscapeKeyPress } from '../../../jsUtils';
18 19
19const messages = defineMessages({ 20const messages = defineMessages({
20 goHome: { 21 goHome: {
@@ -215,7 +216,7 @@ class WebControls extends Component<IProps, IState> {
215 }); 216 });
216 }} 217 }}
217 onKeyDown={event => { 218 onKeyDown={event => {
218 if (event.key === 'Enter') { 219 if (isEnterKeyPress(event.key)) {
219 this.setState({ 220 this.setState({
220 editUrl: false, 221 editUrl: false,
221 }); 222 });
@@ -224,7 +225,7 @@ class WebControls extends Component<IProps, IState> {
224 if (this.inputRef?.current) { 225 if (this.inputRef?.current) {
225 this.inputRef.current.blur(); 226 this.inputRef.current.blur();
226 } 227 }
227 } else if (event.key === 'Escape') { 228 } else if (isEscapeKeyPress(event.key)) {
228 this.setState({ 229 this.setState({
229 editUrl: false, 230 editUrl: false,
230 inputUrl: url, 231 inputUrl: url,
diff --git a/src/features/workspaces/components/WorkspaceDrawer.tsx b/src/features/workspaces/components/WorkspaceDrawer.tsx
index e92bc4b60..4621482bc 100644
--- a/src/features/workspaces/components/WorkspaceDrawer.tsx
+++ b/src/features/workspaces/components/WorkspaceDrawer.tsx
@@ -9,7 +9,7 @@ import {
9} from 'react-intl'; 9} from 'react-intl';
10import withStyles, { type WithStylesProps } from 'react-jss'; 10import withStyles, { type WithStylesProps } from 'react-jss';
11import { Tooltip as ReactTooltip } from 'react-tooltip'; 11import { Tooltip as ReactTooltip } from 'react-tooltip';
12import type { StoresProps } from 'src/@types/ferdium-components.types'; 12import type { StoresProps } from '../../../@types/ferdium-components.types';
13import { H1 } from '../../../components/ui/headline'; 13import { H1 } from '../../../components/ui/headline';
14import Icon from '../../../components/ui/icon'; 14import Icon from '../../../components/ui/icon';
15import workspaceActions from '../actions'; 15import workspaceActions from '../actions';
diff --git a/src/jsUtils.ts b/src/jsUtils.ts
index 72e8111e6..0befb8d56 100644
--- a/src/jsUtils.ts
+++ b/src/jsUtils.ts
@@ -9,7 +9,11 @@ export const convertToJSON = (data?: string | any | null) =>
9export const cleanseJSObject = (data?: any | null) => 9export const cleanseJSObject = (data?: any | null) =>
10 JSON.parse(JSON.stringify(data)); 10 JSON.parse(JSON.stringify(data));
11 11
12export const isEscKeyPress = (key: string) => key === 'Escape'; 12export const isArrowUpKeyPress = (key: string) => key === 'ArrowUp';
13export const isArrowDownKeyPress = (key: string) => key === 'ArrowDown';
14export const isEnterKeyPress = (key: string) => key === 'Enter';
15export const isEscapeKeyPress = (key: string) => key === 'Escape';
16export const isShiftKeyPress = (key: string) => key === 'Shift';
13 17
14export const safeParseInt = (text?: string | number | null) => { 18export const safeParseInt = (text?: string | number | null) => {
15 if (text === undefined || text === null) { 19 if (text === undefined || text === null) {
diff --git a/test/jsUtils.test.ts b/test/jsUtils.test.ts
index cdded7aca..508f5dd9f 100644
--- a/test/jsUtils.test.ts
+++ b/test/jsUtils.test.ts
@@ -96,14 +96,62 @@ describe('jsUtils', () => {
96 }); 96 });
97 }); 97 });
98 98
99 describe('isEscKeyPress', () => { 99 describe('isArrowUpKeyPress', () => {
100 it('returns true if the key is "ArrowUp"', () => {
101 const result = jsUtils.isArrowUpKeyPress('ArrowUp');
102 expect(result).toEqual(true);
103 });
104
105 it('returns false if the key is some other key', () => {
106 const result = jsUtils.isArrowUpKeyPress('Backspace');
107 expect(result).toEqual(false);
108 });
109 });
110
111 describe('isArrowDownKeyPress', () => {
112 it('returns true if the key is "ArrowDown"', () => {
113 const result = jsUtils.isArrowDownKeyPress('ArrowDown');
114 expect(result).toEqual(true);
115 });
116
117 it('returns false if the key is some other key', () => {
118 const result = jsUtils.isArrowDownKeyPress('Backspace');
119 expect(result).toEqual(false);
120 });
121 });
122
123 describe('isEnterKeyPress', () => {
124 it('returns true if the key is "Enter"', () => {
125 const result = jsUtils.isEnterKeyPress('Enter');
126 expect(result).toEqual(true);
127 });
128
129 it('returns false if the key is some other key', () => {
130 const result = jsUtils.isEnterKeyPress('Backspace');
131 expect(result).toEqual(false);
132 });
133 });
134
135 describe('isEscapeKeyPress', () => {
100 it('returns true if the key is "Escape"', () => { 136 it('returns true if the key is "Escape"', () => {
101 const result = jsUtils.isEscKeyPress('Escape'); 137 const result = jsUtils.isEscapeKeyPress('Escape');
138 expect(result).toEqual(true);
139 });
140
141 it('returns false if the key is some other key', () => {
142 const result = jsUtils.isEscapeKeyPress('Backspace');
143 expect(result).toEqual(false);
144 });
145 });
146
147 describe('isShiftKeyPress', () => {
148 it('returns true if the key is "Shift"', () => {
149 const result = jsUtils.isShiftKeyPress('Shift');
102 expect(result).toEqual(true); 150 expect(result).toEqual(true);
103 }); 151 });
104 152
105 it('returns false if the key is some other key', () => { 153 it('returns false if the key is some other key', () => {
106 const result = jsUtils.isEscKeyPress('Backspace'); 154 const result = jsUtils.isShiftKeyPress('Backspace');
107 expect(result).toEqual(false); 155 expect(result).toEqual(false);
108 }); 156 });
109 }); 157 });