diff --git a/gui/src/components/gui/Shortcut.test.tsx b/gui/src/components/gui/Shortcut.test.tsx index f92c6e7f1..17f1d009f 100644 --- a/gui/src/components/gui/Shortcut.test.tsx +++ b/gui/src/components/gui/Shortcut.test.tsx @@ -1,8 +1,8 @@ -import React from "react"; -import { describe, it, expect, vi, beforeEach } from "vitest"; import { render } from "@testing-library/react"; -import Shortcut from "./Shortcut"; +import { beforeEach, describe, expect, it, vi } from "vitest"; import * as util from "../../util"; +import { Platform } from "../../util"; +import Shortcut from "./Shortcut"; vi.mock("../../util", () => ({ ...vi.importActual("../../util"), @@ -15,13 +15,12 @@ vi.mock("../../util", () => ({ describe("Shortcut component", () => { beforeEach(() => { vi.resetAllMocks(); - vi.mocked(util.getPlatform).mockReturnValue('windows'); + vi.mocked(util.getPlatform).mockReturnValue("windows"); vi.mocked(util.getFontSize).mockReturnValue(14); }); - describe("Meta key rendering across platforms", () => { const metaKeyInputs = ["meta", "cmd", "ctrl", "^", "⌘"]; - const platforms = ["windows", "mac", "linux"]; + const platforms: Platform[] = ["windows", "mac", "linux"]; platforms.forEach((platform) => { metaKeyInputs.forEach((metaKey) => { @@ -36,7 +35,7 @@ describe("Shortcut component", () => { const { container } = render({metaKey}); const kbdElement = container.querySelector("kbd"); expect(kbdElement?.textContent).toBe( - platform === "mac" ? "⌘" : "Ctrl" + platform === "mac" ? "⌘" : "Ctrl", ); }); }); @@ -59,7 +58,7 @@ describe("Shortcut component", () => { describe("Delete key rendering across platforms", () => { const deleteKeyInputs = ["backspace", "delete", "⌫"]; - const platforms = ["windows", "mac", "linux"]; + const platforms: Platform[] = ["windows", "mac", "linux"]; platforms.forEach((platform) => { deleteKeyInputs.forEach((deleteKey) => { @@ -67,8 +66,7 @@ describe("Shortcut component", () => { vi.mocked(util.getPlatform).mockReturnValue(platform); const { container } = render({deleteKey}); const kbdElement = container.querySelector("kbd"); - const expectedText = - platform === "mac" ? "Delete ⌫" : "Backspace ⌫"; + const expectedText = platform === "mac" ? "Delete ⌫" : "Backspace ⌫"; expect(kbdElement?.textContent).toBe(expectedText); expect(kbdElement).toHaveClass("keyboard-key-special"); }); @@ -95,7 +93,7 @@ describe("Shortcut component", () => { describe("Option/Alt key rendering across platforms", () => { const optionKeyInputs = ["⌥", "option", "opt", "alt"]; - const platforms = ["windows", "mac"]; + const platforms: util.Platform[] = ["windows", "mac"]; platforms.forEach((platform) => { optionKeyInputs.forEach((optionKey) => { @@ -118,12 +116,12 @@ describe("Shortcut component", () => { describe("Font class application for option/alt key", () => { it("should apply 'keyboard-key-special' class for option/alt key", () => { - vi.mocked(util.getPlatform).mockReturnValue('windows'); + vi.mocked(util.getPlatform).mockReturnValue("windows"); vi.mocked(util.getFontSize).mockReturnValue(14); - vi.mocked(util.getAltKeyLabel).mockImplementation(() => 'Alt'); + vi.mocked(util.getAltKeyLabel).mockImplementation(() => "Alt"); const { container } = render(alt); const kbdElement = container.querySelector("kbd"); expect(kbdElement).toHaveClass("keyboard-key-special"); }); }); -}); \ No newline at end of file +}); diff --git a/gui/src/components/gui/Shortcut.tsx b/gui/src/components/gui/Shortcut.tsx index 8fa09fa2f..33c7cd91d 100644 --- a/gui/src/components/gui/Shortcut.tsx +++ b/gui/src/components/gui/Shortcut.tsx @@ -1,11 +1,11 @@ import React from "react"; -import { - getFontSize, - getPlatform, - getMetaKeyLabel, - getAltKeyLabel, -} from "../../util"; import "../../../src/index.css"; +import { + getAltKeyLabel, + getFontSize, + getMetaKeyLabel, + getPlatform, +} from "../../util"; import "./Shortcut.css"; interface ShortcutProps { @@ -18,7 +18,7 @@ const metaKeys = ["meta", "⌘", "ctrl", "cmd", "^"]; const altKeys = ["alt", "option", "opt", "⌥"]; const modifierKeys = [...metaKeys, ...altKeys]; -const getSpecialKeyMap = (platform: string) => ({ +const getSpecialKeyMap = (platform: string): Record => ({ uparrow: "UpArrow ↑", downarrow: "DownArrow ↓", leftarrow: "LeftArrow ←", @@ -33,7 +33,7 @@ const getSpecialKeyMap = (platform: string) => ({ const parseShortcut = (shortcut: string, platform: string) => { if (!shortcut || typeof shortcut !== "string") { console.warn("Invalid shortcut provided:", shortcut); - return []; + return []; } const specialKeyMap = getSpecialKeyMap(platform); @@ -43,15 +43,15 @@ const parseShortcut = (shortcut: string, platform: string) => { combo .trim() .split(" ") - .filter((key) => key) + .filter((key) => key) .map((key) => { const lowerKey = key.toLowerCase(); if (metaKeys.includes(lowerKey)) return getMetaKeyLabel(); if (altKeys.includes(lowerKey)) return getAltKeyLabel(); return specialKeyMap[lowerKey] || capitalizeKey(key); - }) + }), ) - .filter((combo) => combo.length > 0); + .filter((combo) => combo.length > 0); }; const capitalizeKey = (key: string) => @@ -82,7 +82,8 @@ const Shortcut: React.FC = ({ children }) => { }`} style={{ fontSize: - key && modifierKeys.includes(key.toLowerCase()) && + key && + modifierKeys.includes(key.toLowerCase()) && platform === "mac" ? `${fontSize - 2}px` : `${fontSize - 3}px`, @@ -104,4 +105,4 @@ const Shortcut: React.FC = ({ children }) => { ); }; -export default Shortcut; \ No newline at end of file +export default Shortcut; diff --git a/gui/src/util/index.ts b/gui/src/util/index.ts index cb00d2680..eb5fc2324 100644 --- a/gui/src/util/index.ts +++ b/gui/src/util/index.ts @@ -1,9 +1,9 @@ -import _ from "lodash"; -import { getLocalStorage } from "./localStorage"; -import { KeyboardEvent } from "react"; import { ProfileDescription } from "core/config/ProfileLifecycleManager"; +import _ from "lodash"; +import { KeyboardEvent } from "react"; +import { getLocalStorage } from "./localStorage"; -type Platform = "mac" | "linux" | "windows" | "unknown"; +export type Platform = "mac" | "linux" | "windows" | "unknown"; export function getPlatform(): Platform { const platform = window.navigator.platform.toUpperCase();