import React, { useCallback, useEffect, useMemo, useState } from "react"; import { Divider, Button } from "antd"; import LeftSVG from "@assets/second/left.svg?react"; import { isDesktop, isMobile } from "react-device-detect"; import { CloseOutlined } from '@ant-design/icons'; import { useReactAt } from "i18n-auto-extractor/react"; import ScrollThrottlingSelect, { Option } from "@components/ScrollThrottlingSelect"; import { layouts, keyboards } from "@/keyboardLayouts"; import { KeyboardLedSync, useSettingsStore } from "@/hooks/stores"; import { useJsonRpc } from "@/hooks/useJsonRpc"; import notifications from "@/notifications"; import KeyboardLayoutModal, { KeyboardLayoutContent } from "@/layout/components_bottom/keyboard/KeyboardLayoutModal"; import { dark_bg2_style, dark_font_style, dark_bd_style, dark_line_style } from "@/layout/theme_color"; import { useThemeSettings } from "@routes/login_page/useLocalAuth"; const KeyboardPanel: React.FC = () => { const { $at } = useReactAt(); const { isDark } = useThemeSettings(); const [showMore, setShowMore] = useState(false); const keyboardLayout = useSettingsStore(state => state.keyboardLayout); const setKeyboardLayout = useSettingsStore(state => state.setKeyboardLayout); const [layoutOptions, setLayoutOptions] = useState(); const [maxShowCount, setMaxShowCount] = useState(3); const layoutAbbrevMap = useMemo(() => { const map: Record = {}; keyboards.forEach(kb => { const oldCode = kb.isoCode.replace("-", "_"); map[oldCode] = oldCode; }); return map; }, []); useEffect(() => { const curLayoutOptions = (() => { const options = Object.entries(layouts).map(([code, language]) => ({ value: code, label: `${language} (${layoutAbbrevMap[code] || code})`, })); const currentLayout = keyboardLayout ?? ""; if (!currentLayout) { return options; } const currentIndex = options.findIndex(option => option.value === currentLayout); if (currentIndex === -1 || currentIndex < 3) { setMaxShowCount(3); return options; } setMaxShowCount(4); const [movedItem] = options.splice(currentIndex, 1); options.splice(3, 0, movedItem); return options; })(); setLayoutOptions(curLayoutOptions); }, [layouts, keyboardLayout, layoutAbbrevMap]); const safeKeyboardLayout = useMemo(() => { if (keyboardLayout && keyboardLayout.length > 0) return keyboardLayout; return "en_US"; }, [keyboardLayout]); const [send] = useJsonRpc(); useEffect(() => { send("getKeyboardLayout", {}, resp => { if ("error" in resp) return; setKeyboardLayout(resp.result as string); }); }, []); const onKeyboardLayoutChange = useCallback( (layout: string[] | string) => { send("setKeyboardLayout", { layout }, resp => { if ("error" in resp) { notifications.error( `Failed to set keyboard layout: ${resp.error.data || "Unknown error"}`, ); } notifications.success("Keyboard layout set successfully"); setKeyboardLayout(layout as string); }); }, [send, setKeyboardLayout], ); const keysOptionsList: Option[] = [ { label: "Show Pressed Keys", value: "show-pressed-keys" }, ]; const showPressedKeys = useSettingsStore(state => state.showPressedKeys); const setShowPressedKeys = useSettingsStore(state => state.setShowPressedKeys); const [keysOptions, setKeysOptions] = useState(["show-pressed-keys"]); useEffect(() => { if (showPressedKeys) { setKeysOptions((prevItems: string[]) => [...prevItems, "show-pressed-keys"]); } else { setKeysOptions((prevItems) => prevItems.filter(item => item !== "show-pressed-keys")); } }, [showPressedKeys]); const handleShowPressedChange = (data: string[] | string) => { if (data.includes("show-pressed-keys")) { setShowPressedKeys(true); } else { setShowPressedKeys(false); } }; const ledSyncOptions: Option[] = [ { value: "auto", label: "Auto" }, { value: "browser", label: "Browser Only" }, { value: "host", label: "Host Only" }, ]; const keyboardLedSync = useSettingsStore(state => state.keyboardLedSync); const setKeyboardLedSync = useSettingsStore(state => state.setKeyboardLedSync); const [ledSync, setLedSync] = useState(keyboardLedSync); useEffect(() => { setLedSync(keyboardLedSync); }, [keyboardLedSync]); const handleLedChange = (data: string[] | string) => { setKeyboardLedSync(data as KeyboardLedSync); }; const DividerLine = ({isMobile = false}: {isMobile?: boolean}) => { if (isMobile) { return (
); } return
}; //custom-keyboard-layout if (showMore && isMobile) { return (
Keyboard Layout
); } if (isMobile && !showMore) { return (
{/* LED State Synchronization */}
{/* Keyboard Layout */}
} onSpecialOptionClick={() => setShowMore(true)} />
{/* Keys */}
); } return (
} onSpecialOptionClick={() => setShowMore(true)} />
setShowMore(false)} value={safeKeyboardLayout} onChange={onKeyboardLayoutChange} layoutOptions={layoutOptions} />
); }; export default KeyboardPanel;